-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotter_status.py
More file actions
237 lines (202 loc) · 11 KB
/
plotter_status.py
File metadata and controls
237 lines (202 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import importlib.util
import os
class PlotterStatusService:
def __init__(self, ad, sem):
"""Store shared plotter dependencies and initialize cached status state."""
self.ad = ad
self.sem = sem
self.device_cache = {}
self.last_usb_id = None
self.last_known_status = {
"status": "off",
"machine": "none",
"device_info": "none",
"model_number": None,
"config": {},
}
def load_axidraw_config(self, config_path):
"""Load a model-specific AxiDraw config module into a serializable dictionary."""
if not config_path or not os.path.exists(config_path):
return {}
try:
spec = importlib.util.spec_from_file_location("axidraw_config", config_path)
config_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(config_module)
config_data = {}
if hasattr(config_module, 'speed_pendown'):
config_data['speed_pendown'] = config_module.speed_pendown
if hasattr(config_module, 'speed_penup'):
config_data['speed_penup'] = config_module.speed_penup
if hasattr(config_module, 'accel'):
config_data['accel'] = config_module.accel
if hasattr(config_module, 'pen_pos_up'):
config_data['pen_pos_up'] = config_module.pen_pos_up
if hasattr(config_module, 'pen_pos_down'):
config_data['pen_pos_down'] = config_module.pen_pos_down
if hasattr(config_module, 'pen_rate_raise'):
config_data['pen_rate_raise'] = config_module.pen_rate_raise
if hasattr(config_module, 'pen_rate_lower'):
config_data['pen_rate_lower'] = config_module.pen_rate_lower
if hasattr(config_module, 'model'):
config_data['model'] = config_module.model
if hasattr(config_module, 'const_speed'):
config_data['const_speed'] = config_module.const_speed
if hasattr(config_module, 'auto_rotate'):
config_data['auto_rotate'] = config_module.auto_rotate
if hasattr(config_module, 'reordering'):
config_data['reordering'] = config_module.reordering
if hasattr(config_module, 'pen_delay_down'):
config_data['pen_delay_down'] = config_module.pen_delay_down
if hasattr(config_module, 'pen_delay_up'):
config_data['pen_delay_up'] = config_module.pen_delay_up
if hasattr(config_module, 'resolution'):
config_data['resolution'] = config_module.resolution
travel_dimensions = {}
if hasattr(config_module, 'x_travel_default'):
travel_dimensions['x_travel_default'] = config_module.x_travel_default
if hasattr(config_module, 'y_travel_default'):
travel_dimensions['y_travel_default'] = config_module.y_travel_default
if hasattr(config_module, 'x_travel_V3A3'):
travel_dimensions['x_travel_V3A3'] = config_module.x_travel_V3A3
if hasattr(config_module, 'y_travel_V3A3'):
travel_dimensions['y_travel_V3A3'] = config_module.y_travel_V3A3
if hasattr(config_module, 'x_travel_V3XLX'):
travel_dimensions['x_travel_V3XLX'] = config_module.x_travel_V3XLX
if hasattr(config_module, 'y_travel_V3XLX'):
travel_dimensions['y_travel_V3XLX'] = config_module.y_travel_V3XLX
if hasattr(config_module, 'x_travel_MiniKit'):
travel_dimensions['x_travel_MiniKit'] = config_module.x_travel_MiniKit
if hasattr(config_module, 'y_travel_MiniKit'):
travel_dimensions['y_travel_MiniKit'] = config_module.y_travel_MiniKit
if hasattr(config_module, 'x_travel_SEA1'):
travel_dimensions['x_travel_SEA1'] = config_module.x_travel_SEA1
if hasattr(config_module, 'y_travel_SEA1'):
travel_dimensions['y_travel_SEA1'] = config_module.y_travel_SEA1
if hasattr(config_module, 'x_travel_SEA2'):
travel_dimensions['x_travel_SEA2'] = config_module.x_travel_SEA2
if hasattr(config_module, 'y_travel_SEA2'):
travel_dimensions['y_travel_SEA2'] = config_module.y_travel_SEA2
config_data['travel_dimensions'] = travel_dimensions
return config_data
except Exception as error:
print(f"Error loading config from {config_path}: {error}")
return {}
def get_plotter_status(self):
"""Inspect the connected AxiDraw and return the latest machine status snapshot."""
status_data = {
"status": "off",
"machine": "none",
"device_info": "none",
"model_number": None,
"config": {},
}
if not self.sem.acquire(blocking=False):
status_data = self.last_known_status.copy()
status_data["status"] = "busy"
return status_data
self.sem.release()
if self.last_usb_id and self.last_usb_id in self.device_cache:
cached = self.device_cache[self.last_usb_id]
status_data = cached.copy()
status_data["status"] = cached.get("status", "on")
return status_data
self.ad.plot_setup()
self.ad.options.mode = "manual"
self.ad.options.manual_cmd = "list_names"
self.ad.plot_run()
axidraw_list = self.ad.name_list
print(f"Debug - axidraw_list type: {type(axidraw_list)}")
print(f"Debug - axidraw_list: {axidraw_list}")
if axidraw_list is not None and len(axidraw_list) > 0:
device_identifier = axidraw_list[0]
print(f"Debug - device_identifier: '{device_identifier}'")
status_data["device_info"] = device_identifier
self.last_usb_id = device_identifier
machine_type = "Unknown"
machine_model = None
if "/dev/" in device_identifier or "COM" in device_identifier:
machine_type = "AxiDraw (No nickname assigned)"
machine_model = int(os.environ.get("AXIDRAW_MODEL", "4"))
print(f" Device uses port path: {device_identifier}")
print(f" Using default model: {machine_model}")
print(" To identify machine type, assign a nickname using:")
print(" axicli -m manual -M write_nameYourNicknameHere")
else:
nickname = device_identifier.lower()
if "mini" in nickname or "mk" in nickname:
machine_type = "MiniKit-v2"
machine_model = 4
elif "a3" in nickname or "se" in nickname or "large" in nickname:
machine_type = "AxiDraw-A3/SE"
machine_model = 2
elif "xlx" in nickname:
machine_type = "AxiDraw-XLX"
machine_model = 3
elif "v3" in nickname or "v2" in nickname:
machine_type = "AxiDraw-V2/V3"
machine_model = 1
elif "a1" in nickname:
machine_type = "AxiDraw-SE/A1"
machine_model = 5
elif "a2" in nickname:
machine_type = "AxiDraw-SE/A2"
machine_model = 6
else:
machine_type = f"AxiDraw ({device_identifier})"
machine_model = int(os.environ.get("AXIDRAW_MODEL", "4"))
print(f" Device nickname: {device_identifier}")
print(f" Detected machine type: {machine_type}")
print(f" Model number: {machine_model}")
status_data["machine"] = machine_type
status_data["model_number"] = machine_model
config_env_key = f"AXIDRAW_MODEL_{machine_model}_CONFIG"
config_path = os.environ.get(config_env_key)
if config_path:
print(f" Loading config from: {config_path}")
config_data = self.load_axidraw_config(config_path)
if machine_model == 1:
config_data['x_travel'] = config_data.get('travel_dimensions', {}).get('x_travel_default')
config_data['y_travel'] = config_data.get('travel_dimensions', {}).get('y_travel_default')
elif machine_model == 2:
config_data['x_travel'] = config_data.get('travel_dimensions', {}).get('x_travel_V3A3')
config_data['y_travel'] = config_data.get('travel_dimensions', {}).get('y_travel_V3A3')
elif machine_model == 3:
config_data['x_travel'] = config_data.get('travel_dimensions', {}).get('x_travel_V3XLX')
config_data['y_travel'] = config_data.get('travel_dimensions', {}).get('y_travel_V3XLX')
elif machine_model == 4:
config_data['x_travel'] = config_data.get('travel_dimensions', {}).get('x_travel_MiniKit')
config_data['y_travel'] = config_data.get('travel_dimensions', {}).get('y_travel_MiniKit')
elif machine_model == 5:
config_data['x_travel'] = config_data.get('travel_dimensions', {}).get('x_travel_SEA1')
config_data['y_travel'] = config_data.get('travel_dimensions', {}).get('y_travel_SEA1')
elif machine_model == 6:
config_data['x_travel'] = config_data.get('travel_dimensions', {}).get('x_travel_SEA2')
config_data['y_travel'] = config_data.get('travel_dimensions', {}).get('y_travel_SEA2')
if 'travel_dimensions' in config_data:
del config_data['travel_dimensions']
status_data["config"] = config_data
status_data["config"]["config_file"] = config_path
else:
print(f" No config file found for model {machine_model} (env var: {config_env_key})")
status_data["config"]["config_file"] = None
self.ad.interactive()
if self.ad.connect():
try:
raw_string = self.ad.usb_query('QC\r')
split_string = raw_string.split(",", 1)
voltage_value = int(split_string[1])
if voltage_value >= 250:
status_data["status"] = "on"
else:
status_data["status"] = "connected"
status_data["voltage"] = voltage_value
except (ValueError, IndexError):
status_data["status"] = "connected"
self.ad.options.mode = "manual"
self.ad.options.manual_cmd = "disable_xy"
self.ad.plot_run()
self.ad.disconnect()
self.last_known_status = status_data.copy()
if self.last_usb_id:
self.device_cache[self.last_usb_id] = status_data.copy()
return status_data