symetrie-hexapod 2023.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,432 @@
1
+ """
2
+ A Graphical User Interface for monitoring and commanding the Symétrie Hexapod.
3
+
4
+ Start the GUI from your terminal as follows:
5
+
6
+ puna_ui [--type proxy|direct|simulator]
7
+
8
+ This GUI is based on the SYM_positioning application from Symétrie. The intent
9
+ is to provide operators a user interface which is platform independent, but
10
+ familiar.
11
+
12
+ The application is completely written in Python/Qt5 and can therefore run on any
13
+ platform that supports Python and Qt5.
14
+
15
+ """
16
+ import argparse
17
+ import logging
18
+ import multiprocessing
19
+ import sys
20
+ import threading
21
+ from enum import IntEnum
22
+ from pathlib import Path
23
+
24
+ from PyQt5.QtCore import QLockFile
25
+
26
+ from egse.hexapod.symetrie import ControllerFactory
27
+ from egse.hexapod.symetrie import ProxyFactory
28
+ from egse.hexapod.symetrie import get_hexapod_controller_pars
29
+ from egse.hexapod.symetrie.punaplus import PunaPlusController
30
+ from egse.hexapod.symetrie.punaplus import PunaPlusProxy
31
+
32
+ multiprocessing.current_process().name = "puna_ui"
33
+
34
+ from PyQt5.QtGui import QIcon
35
+ from PyQt5.QtWidgets import QApplication, QMessageBox
36
+ from PyQt5.QtWidgets import QFrame
37
+ from PyQt5.QtWidgets import QHBoxLayout
38
+ from PyQt5.QtWidgets import QVBoxLayout
39
+ from prometheus_client import start_http_server
40
+
41
+ from egse.gui import show_warning_message
42
+ from egse.gui.led import Indic
43
+ from egse.gui.states import States
44
+ from egse.hexapod.symetrie.hexapod_ui import ActuatorStates
45
+ from egse.hexapod.symetrie.hexapod_ui import HexapodUIController
46
+ from egse.hexapod.symetrie.hexapod_ui import HexapodUIModel
47
+ from egse.hexapod.symetrie.hexapod_ui import HexapodUIView
48
+ from egse.hexapod.symetrie.puna import PunaSimulator
49
+ from egse.process import ProcessStatus
50
+ from egse.resource import get_resource
51
+ from egse.settings import Settings
52
+ from egse.system import do_every
53
+
54
+ MODULE_LOGGER = logging.getLogger(__name__)
55
+
56
+
57
+ class DeviceControllerType(IntEnum):
58
+ ALPHA = 0
59
+ ALPHA_PLUS = 1
60
+
61
+
62
+ DCT = DeviceControllerType
63
+
64
+
65
+ # Status LEDs define the number of status leds (length of the list), the description and the
66
+ # default color when the LED is on.
67
+
68
+ STATUS_LEDS_ALPHA = [
69
+ ["Error", Indic.RED], # bit 0
70
+ ["System Initialized", Indic.GREEN], # bit 1
71
+ ["In position", Indic.GREEN], # bit 2
72
+ ["Amplifier enabled", Indic.GREEN], # bit 3
73
+ ["Homing done", Indic.GREEN], # bit 4
74
+ ["Brake on", Indic.GREEN], # bit 5
75
+ ["Emergency stop", Indic.ORANGE], # bit 6
76
+ ["Warning FE", Indic.ORANGE], # bit 7
77
+ ["Fatal FE", Indic.RED], # bit 8
78
+ ["Actuator Limit Error", Indic.RED], # bit 9
79
+ ["Amplifier Error", Indic.RED], # bit 10
80
+ ["Encoder error", Indic.RED], # bit 11
81
+ ["Phasing error", Indic.RED], # bit 12
82
+ ["Homing error", Indic.RED], # bit 13
83
+ ["Kinematic error", Indic.RED], # bit 14
84
+ ["Abort input error", Indic.RED], # bit 15
85
+ ["R/W memory error", Indic.RED], # bit 16
86
+ ["Temperature error", Indic.RED], # bit 17
87
+ ["Homing done (virtual)", Indic.ORANGE], # bit 18
88
+ ["Encoders power off", Indic.ORANGE], # bit 19
89
+ ["Limit switches power off", Indic.ORANGE], # bit 20
90
+ ["Reserved", Indic.BLACK], # bit 21
91
+ ["Reserved", Indic.BLACK], # bit 22
92
+ ["Reserved", Indic.BLACK], # bit 23
93
+ ]
94
+
95
+ STATUS_LEDS_ALPHA_PLUS = [
96
+ ["Error", Indic.RED], # bit 0
97
+ ["System Initialized", Indic.GREEN], # bit 1
98
+ ["Control On", Indic.GREEN], # bit 2
99
+ ["In Position", Indic.GREEN], # bit 3
100
+ ["Motion Task Running", Indic.GREEN], # bit 4
101
+ ["Home Task Running", Indic.GREEN], # bit 5
102
+ ["Home Complete", Indic.GREEN], # bit 6
103
+ ["Home Virtual", Indic.GREEN], # bit 7
104
+ ["Phase Found", Indic.GREEN], # bit 8
105
+ ["Brake on", Indic.GREEN], # bit 9
106
+ ["Motion Restricted", Indic.RED], # bit 10
107
+ ["Power on Encoders", Indic.GREEN], # bit 11
108
+ ["Power on Limit switches", Indic.GREEN], # bit 12
109
+ ["Power on Drives", Indic.GREEN], # bit 13
110
+ ["Emergency Stop", Indic.RED], # bit 14
111
+ ]
112
+
113
+ STATUS_LEDS = {
114
+ DCT.ALPHA: STATUS_LEDS_ALPHA,
115
+ DCT.ALPHA_PLUS: STATUS_LEDS_ALPHA_PLUS
116
+ }
117
+
118
+ # The index of the Control LED
119
+
120
+ CONTROL_ONOFF = {
121
+ DCT.ALPHA: 3,
122
+ DCT.ALPHA_PLUS: 2
123
+ }
124
+
125
+ ACTUATOR_STATE_LABELS_ALPHA = [
126
+ "In position",
127
+ "Control loop on servo motors active",
128
+ "Homing done",
129
+ "Input “Home switch”",
130
+ "Input “Positive limit switch”",
131
+ "Input “Negative limit switch”",
132
+ "Brake control output",
133
+ "Following error (warning)",
134
+ "Following error",
135
+ "Actuator out of bounds error",
136
+ "Amplifier error",
137
+ "Encoder error",
138
+ "Phasing error (brushless engine only)",
139
+ ]
140
+
141
+ ACTUATOR_STATE_LABELS_ALPHA_PLUS = [
142
+ "Error: ",
143
+ "Control On: ",
144
+ "In Position: ",
145
+ "Motion Task Running: ",
146
+ "Home task running: ",
147
+ "Home complete: ",
148
+ "Phase found: ",
149
+ "Brake on: ",
150
+ "Home HW input: ",
151
+ "Negative HW limit switch: ",
152
+ "Positive HW limit switch: ",
153
+ "SW limit reached: ",
154
+ "Following Error: ",
155
+ "Drive fault: ",
156
+ "Encoder error: ",
157
+ ]
158
+
159
+ ACTUATOR_STATE_LABELS = {
160
+ DCT.ALPHA: ACTUATOR_STATE_LABELS_ALPHA,
161
+ DCT.ALPHA_PLUS: ACTUATOR_STATE_LABELS_ALPHA_PLUS
162
+ }
163
+
164
+ GUI_SETTINGS = Settings.load("PUNA GUI")
165
+
166
+
167
+ class PunaUIView(HexapodUIView):
168
+ def __init__(self, device_controller_type: DCT, device_id: str):
169
+ super().__init__()
170
+
171
+ self.dct = device_controller_type
172
+
173
+ if self.dct == DCT.ALPHA:
174
+ title = f"Hexapod PUNA Controller (Alpha) – {device_id}"
175
+ else:
176
+ title = f"Hexapod PUNA Controller (Alpha+) – {device_id}"
177
+
178
+ self.setWindowTitle(title)
179
+ self.actuator_states = ActuatorStates(labels=ACTUATOR_STATE_LABELS[self.dct])
180
+
181
+ self.init_gui()
182
+
183
+ def init_gui(self):
184
+
185
+ # The main frame in which all the other frames are located, the outer Application frame
186
+
187
+ app_frame = QFrame()
188
+ app_frame.setObjectName("AppFrame")
189
+
190
+ # The left part which shows the states and positions
191
+
192
+ status_frame = QFrame()
193
+ status_frame.setObjectName("StatusFrame")
194
+
195
+ # The right part which has tabs that allow settings, movements, maintenance etc.
196
+
197
+ tabs_frame = QFrame()
198
+ tabs_frame.setObjectName("TabsFrame")
199
+
200
+ # The states of the Hexapod (contains all the leds)
201
+
202
+ states_frame = QFrame()
203
+ states_frame.setObjectName("StatesFrame")
204
+
205
+ # The user, machine positions and actuator lengths
206
+
207
+ positions_frame = QFrame()
208
+ positions_frame.setObjectName("PositionsFrame")
209
+
210
+ hbox = QHBoxLayout()
211
+ vbox_left = QVBoxLayout()
212
+ vbox_right = QVBoxLayout()
213
+
214
+ self.createToolbar()
215
+ self.createStatusBar()
216
+
217
+ self.states = States(STATUS_LEDS[self.dct])
218
+
219
+ user_positions_widget = self.createUserPositionWidget()
220
+ mach_positions_widget = self.createMachinePositionWidget()
221
+ actuator_length_widget = self.createActuatorLengthWidget()
222
+
223
+ vbox_right.addWidget(user_positions_widget)
224
+ vbox_right.addWidget(mach_positions_widget)
225
+ vbox_right.addWidget(actuator_length_widget)
226
+
227
+ positions_frame.setLayout(vbox_right)
228
+
229
+ vbox_left.addWidget(self.states)
230
+
231
+ states_frame.setLayout(vbox_left)
232
+
233
+ hbox.addWidget(states_frame)
234
+ hbox.addWidget(positions_frame)
235
+
236
+ status_frame.setLayout(hbox)
237
+
238
+ tabbed_widget = self.create_tabbed_widget()
239
+
240
+ hbox = QHBoxLayout()
241
+ hbox.addWidget(tabbed_widget)
242
+ tabs_frame.setLayout(hbox)
243
+
244
+ hbox = QHBoxLayout()
245
+ hbox.addWidget(status_frame)
246
+ hbox.addWidget(tabs_frame)
247
+
248
+ app_frame.setLayout(hbox)
249
+
250
+ self.setCentralWidget(app_frame)
251
+
252
+ def update_status_bar(self, message=None, mode=None, timeout=2000):
253
+ if message:
254
+ self.statusBar().showMessage(message, msecs=timeout)
255
+ if mode:
256
+ self.mode_label.setStyleSheet(
257
+ f"border: 0; " f"color: {'red' if 'Simulator' in mode else 'black'};"
258
+ )
259
+ self.mode_label.setText(f"mode: {mode}")
260
+ self.statusBar().repaint()
261
+
262
+ def updatePositions(self, userPositions, machinePositions, actuatorLengths):
263
+
264
+ if userPositions is None:
265
+ MODULE_LOGGER.warning("no userPositions passed into updatePositions(), returning.")
266
+ return
267
+
268
+ for upos in range(len(self.user_positions)):
269
+ try:
270
+ self.user_positions[upos][1].setText(f"{userPositions[upos]:10.4f}")
271
+ except IndexError:
272
+ MODULE_LOGGER.error(f"IndexError in user_positions, upos = {upos}")
273
+
274
+ if machinePositions is None:
275
+ MODULE_LOGGER.warning("no machinePositions passed into updatePositions(), returning.")
276
+ return
277
+
278
+ for mpos in range(len(self.mach_positions)):
279
+ self.mach_positions[mpos][1].setText(f"{machinePositions[mpos]:10.4f}")
280
+
281
+ if actuatorLengths is None:
282
+ MODULE_LOGGER.warning("no actuatorLengths passed into updatePositions(), returning.")
283
+ return
284
+
285
+ for idx, alen in enumerate(self.actuator_lengths):
286
+ alen[1].setText(f"{actuatorLengths[idx]:10.4f}")
287
+
288
+ def updateStates(self, states):
289
+
290
+ if states is None:
291
+ return
292
+
293
+ self.updateControlButton(states[CONTROL_ONOFF[self.dct]])
294
+ self.states.set_states(states)
295
+
296
+ def updateControlButton(self, flag):
297
+
298
+ self.control.set_selected(on=flag)
299
+
300
+
301
+ class PunaUIModel(HexapodUIModel):
302
+ def __init__(self, connection_type):
303
+
304
+ hostname, port, dev_id, dev_name, _ = get_hexapod_controller_pars()
305
+ if connection_type == "proxy":
306
+ device = ProxyFactory().create(dev_name, device_id=dev_id)
307
+ elif connection_type == "direct":
308
+ device = ControllerFactory().create(dev_name, device_id=dev_id)
309
+ device.connect()
310
+ elif connection_type == "simulator":
311
+ device = PunaSimulator()
312
+ else:
313
+ raise ValueError(
314
+ f"Unknown type of Hexapod implementation passed into the model: {connection_type}"
315
+ )
316
+
317
+ super().__init__(connection_type, device)
318
+
319
+ self._device_id = dev_id
320
+
321
+ if device is not None:
322
+ MODULE_LOGGER.debug(f"Hexapod initialized as {device.__class__.__name__}")
323
+
324
+ @property
325
+ def device_id(self):
326
+ return self._device_id
327
+
328
+ def get_device_controller_type(self) -> DCT:
329
+ return DCT.ALPHA_PLUS if isinstance(self.device, (PunaPlusProxy, PunaPlusController)) else DCT.ALPHA
330
+
331
+ def get_speed(self):
332
+ vt, vr, vt_min, vr_min, vt_max, vr_max = self.device.get_speed()
333
+ return vt, vr
334
+
335
+
336
+ class PunaUIController(HexapodUIController):
337
+ def __init__(self, model: PunaUIModel, view: PunaUIView):
338
+ super().__init__(model, view)
339
+
340
+ def update_values(self):
341
+
342
+ super().update_values()
343
+
344
+ # Add here any updates to PUNA specific widgets
345
+
346
+
347
+ def parse_arguments():
348
+ """
349
+ Prepare the arguments that are specific for this application.
350
+ """
351
+ parser = argparse.ArgumentParser()
352
+ parser.add_argument(
353
+ "--type",
354
+ dest="type",
355
+ action="store",
356
+ choices={"proxy", "simulator", "direct"},
357
+ help="Specify Hexapod implementation you want to connect to.",
358
+ default="proxy",
359
+ )
360
+ parser.add_argument(
361
+ "--profile",
362
+ default=False,
363
+ action="store_true",
364
+ help="Enable info logging messages with method profile information.",
365
+ )
366
+ return parser.parse_args()
367
+
368
+
369
+ def main():
370
+ lock_file = QLockFile(str(Path("~/puna_ui.app.lock").expanduser()))
371
+
372
+ styles_location = get_resource(":/styles/default.qss")
373
+ app_logo = get_resource(":/icons/logo-puna.svg")
374
+
375
+ args = list(sys.argv)
376
+ args[1:1] = ["-stylesheet", str(styles_location)]
377
+ app = QApplication(args)
378
+ app.setWindowIcon(QIcon(str(app_logo)))
379
+
380
+ if lock_file.tryLock(100):
381
+
382
+ process_status = ProcessStatus()
383
+
384
+ timer_thread = threading.Thread(target=do_every, args=(10, process_status.update))
385
+ timer_thread.daemon = True
386
+ timer_thread.start()
387
+
388
+ start_http_server(GUI_SETTINGS.METRICS_PORT)
389
+
390
+ args = parse_arguments()
391
+
392
+ if args.profile:
393
+ Settings.set_profiling(True)
394
+
395
+ if args.type == "proxy":
396
+ *_, device_id, device_name, _ = get_hexapod_controller_pars()
397
+ factory = ProxyFactory()
398
+ proxy = factory.create(device_name, device_id=device_id)
399
+ if not proxy.ping():
400
+ description = "Could not connect to Hexapod Control Server"
401
+ info_text = (
402
+ "The GUI will start, but the connection button will show a disconnected state. "
403
+ "Please check if the Control Server is running and start the server if needed. "
404
+ "Otherwise, check if the correct HOSTNAME for the control server is set in the "
405
+ "Settings.yaml "
406
+ "configuration file."
407
+ )
408
+
409
+ show_warning_message(description, info_text)
410
+
411
+ model = PunaUIModel(args.type)
412
+ view = PunaUIView(model.get_device_controller_type(), model.device_id)
413
+ PunaUIController(model, view)
414
+
415
+ view.show()
416
+
417
+ return app.exec_()
418
+ else:
419
+ error_message = QMessageBox()
420
+ error_message.setIcon(QMessageBox.Warning)
421
+ error_message.setWindowTitle("Error")
422
+ error_message.setText("The Puna GUI application is already running!")
423
+ error_message.setStandardButtons(QMessageBox.Ok)
424
+
425
+ return error_message.exec()
426
+
427
+
428
+ if __name__ == "__main__":
429
+
430
+ logging.basicConfig(level=logging.DEBUG, format=Settings.LOG_FORMAT_FULL)
431
+
432
+ sys.exit(main())
@@ -0,0 +1,107 @@
1
+ from egse.hexapod.symetrie import ControllerFactory
2
+ from egse.hexapod.symetrie import ProxyFactory
3
+ from egse.hexapod.symetrie import get_hexapod_controller_pars
4
+ from egse.hexapod.symetrie.dynalpha import AlphaPlusControllerInterface
5
+ from egse.hexapod.symetrie.dynalpha import AlphaPlusTelnetInterface
6
+ from egse.mixin import DynamicCommandMixin
7
+ from egse.proxy import DynamicProxy
8
+ from egse.settings import Settings
9
+ from egse.zmq_ser import connect_address
10
+
11
+ CTRL_SETTINGS = Settings.load("Hexapod PUNA Control Server")
12
+
13
+
14
+ class PunaPlusInterface(AlphaPlusControllerInterface):
15
+ """
16
+ Interface definition for the PunaPlusController and the PunaPlusProxy.
17
+ """
18
+
19
+
20
+ class PunaPlusController(PunaPlusInterface, DynamicCommandMixin):
21
+ def __init__(self, hostname: str = "127.0.0.1", port: int = 23):
22
+ self.transport = self.device = AlphaPlusTelnetInterface(hostname, port)
23
+ self.hostname = hostname
24
+ self.port = port
25
+
26
+ super().__init__()
27
+
28
+ def get_controller_type(self):
29
+ return "ALPHA+"
30
+
31
+ def is_simulator(self):
32
+ return False
33
+
34
+ def is_connected(self):
35
+ return self.device.is_connected()
36
+
37
+ def connect(self):
38
+ self.device.connect()
39
+
40
+ def disconnect(self):
41
+ self.device.disconnect()
42
+
43
+ def reconnect(self):
44
+ if self.is_connected():
45
+ self.disconnect()
46
+ self.connect()
47
+
48
+
49
+ class PunaPlusProxy(DynamicProxy, PunaPlusInterface):
50
+ """
51
+ The PunaPlusProxy class is used to connect to the control server and send commands to the
52
+ Hexapod PUNA remotely. The devce controller for that PUNA hexapod is a Alpha+ controller.
53
+ """
54
+
55
+ def __init__(
56
+ self,
57
+ protocol=CTRL_SETTINGS.PROTOCOL,
58
+ hostname=CTRL_SETTINGS.HOSTNAME,
59
+ port=CTRL_SETTINGS.COMMANDING_PORT,
60
+ ):
61
+ """
62
+ Args:
63
+ protocol: the transport protocol [default is taken from settings file]
64
+ hostname: location of the control server (IP address) [default is taken from settings
65
+ file]
66
+ port: TCP port on which the control server is listening for commands [default is
67
+ taken from settings file]
68
+ """
69
+ super().__init__(connect_address(protocol, hostname, port))
70
+
71
+
72
+ if __name__ == "__main__":
73
+
74
+ from egse.hexapod.symetrie.puna import PunaProxy
75
+
76
+ # The following imports are needed for the isinstance() to work
77
+ from egse.hexapod.symetrie.punaplus import PunaPlusProxy
78
+ from egse.hexapod.symetrie.punaplus import PunaPlusController
79
+
80
+ print()
81
+
82
+ *_, device_id, device_name, _ = get_hexapod_controller_pars()
83
+ print(f"{device_name = }, {device_id = }")
84
+
85
+ factory = ProxyFactory()
86
+ proxy = factory.create(device_name, device_id="1A")
87
+ assert isinstance(proxy, PunaProxy)
88
+
89
+ proxy = factory.create(device_name, device_id="2B")
90
+ assert isinstance(proxy, PunaPlusProxy)
91
+
92
+ print(proxy.info())
93
+
94
+ factory = ControllerFactory()
95
+
96
+ device = factory.create("PUNA", device_id="H_2B")
97
+ device.connect()
98
+ assert isinstance(device, PunaPlusController)
99
+
100
+ print(device.info())
101
+
102
+ device = factory.create("ZONDA")
103
+ device.connect()
104
+
105
+ print(device.info())
106
+
107
+ device.disconnect()