STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
export_sensor_config.py
Go to the documentation of this file.
1try:
2 import tomli
3except ModuleNotFoundError:
4 import tomllib as tomli # stdlib since Python 3.11, same API
5import json
6import os
7import sys
8
9
10def main():
11 config_path = os.path.join(os.path.dirname(__file__), "..", "config", "config.toml")
12 try:
13 with open(config_path, "rb") as f:
14 config = tomli.load(f)
15 except Exception as e:
16 print(f"Error loading config: {e}", file=sys.stderr)
17 sys.exit(1)
18
19 sensor_roles = config.get("sensor_roles_pt_board", config.get("sensor_roles", {}))
20 boards = config.get("boards", {})
21
22 # Map packet_id to entity name
23 # packet_id is formatted as "HIGH_LOW" hex strings, e.g., "0x20_1"
24 mapping = {}
25
26 # PT defaults and board-specific roles (PT, TC, RTD, LC, etc)
27 # The config has `sensor_roles_pt_board`, `sensor_roles_pt2`, etc. Find anything starting with `sensor_roles`
28 for section_name, section_data in config.items():
29 if section_name.startswith("sensor_roles_"):
30 # Determine type from board id/name (PT is 0x20, TC=0x21, RTD=0x22, LC=0x23)
31 # Default to PT
32 prefix = "PT"
33 hi_byte = 0x20
34
35 if "tc" in section_name:
36 prefix = "TC"
37 hi_byte = 0x21
38 elif "rtd" in section_name:
39 prefix = "RTD"
40 hi_byte = 0x22
41 elif "lc" in section_name:
42 prefix = "LC"
43 hi_byte = 0x23
44
45 for role_name, channel_id in section_data.items():
46 if isinstance(channel_id, int):
47 entity_name = role_name.replace(" ", "_")
48 mapping[f"0x{hi_byte:02x}_{channel_id}"] = f"{prefix}.{entity_name}"
49 mapping[f"0x{hi_byte:02x}_{0x10 + channel_id}"] = (
50 f"{prefix}_Cal.{entity_name}"
51 )
52
53 # Actuators
54 actuator_roles = config.get("actuator_roles", {})
55 for role_name, act_info in actuator_roles.items():
56 if isinstance(act_info, list) and len(act_info) >= 2:
57 channel_id = act_info[1]
58 if isinstance(channel_id, int):
59 entity_name = role_name.replace(" ", "_")
60 mapping[f"0x30_{channel_id}"] = f"ACT.{entity_name}"
61
62 # Fill in remainder gaps with generic names if desired, but
63 # fsw_web_bridge can handle the fallback.
64
65 # Save mapping to /tmp/sensor_map.json
66 try:
67 with open("/tmp/sensor_map.json", "w") as f:
68 json.dump(mapping, f, indent=2)
69 except Exception as e:
70 print(f"Error writing mapping: {e}", file=sys.stderr)
71 sys.exit(1)
72
73
74if __name__ == "__main__":
75 main()