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,193 @@
1
+ BaseClass:
2
+ egse.hexapod.symmetrie.PunaInterface
3
+
4
+ ProxyClass:
5
+ egse.hexapod.symetrie.PunaProxy
6
+
7
+ ControlServerClass:
8
+ egse.hexapod.symetrie.PunaControlServer
9
+
10
+ ControlServer:
11
+ egse.hexapod.symetrie.puna_cs
12
+
13
+ UserInterface:
14
+ egse.hexapod.symetrie.puna_ui
15
+
16
+ Commands:
17
+
18
+ # Each of these groups is parsed and used on both the server and the client side.
19
+ #
20
+ # The group name (e.g. is_simulator) will be monkey patched in the Proxy class for the device
21
+ # or service.
22
+ #
23
+ # The other field are:
24
+ # description: Used by the doc_string method to generate a help string
25
+ # cmd: Command string that will eventually be send to the hardware controller for
26
+ # the device. This cmd string is also used at the client side to parse and
27
+ # validate the arguments.
28
+ # device_method: The name of the method to be called on the device class.
29
+ # These should all be defined by the interface class for the device, i.e.
30
+ # PunaInterface in this case.
31
+ # When the device_method is the same as the group name, it can be omitted.
32
+ # response: The name of the method to be called from the device protocol.
33
+ # This method should exist in the subclass of the CommandProtocol base class,
34
+ # i.e. in this case it will be the PunaProtocol class.
35
+ # The default (when no response is given) is 'handle_device_method'.
36
+
37
+ # Definition of the the DeviceInterface
38
+
39
+ is_simulator:
40
+ description: Ask if the connected class is a simulator instead of the real device Controller class.
41
+ returns: bool | True if the far end is a simulator instead of the real hardware
42
+
43
+ is_connected:
44
+ description: Check if the Hexapod hardware controller is connected.
45
+
46
+ connect:
47
+ description: Connect the Hexapod hardware controller
48
+
49
+ reconnect:
50
+ description: Reconnect the Hexapod hardware controller.
51
+
52
+ This command will force a disconnect and then try to re-connect to the controller.
53
+
54
+ disconnect:
55
+ description: Disconnect from the hexapod controller.
56
+
57
+ This command will be send to the Hexapod Control Server which will then
58
+ disconnect from the hardware controller.
59
+
60
+ This command does not affect the ZeroMQ connection of the Proxy to the
61
+ control server. Use the service command `disconnect_cs()` to disconnect
62
+ from the control server.
63
+
64
+
65
+ # Definition of the device commands
66
+
67
+ is_in_position:
68
+ description: Returns True when the actuators are in position.
69
+
70
+ info:
71
+ description: Retrieve basic information about the Hexapod and the Controller.
72
+
73
+ reset:
74
+ description: Completely resets the Hexapod controller with the standard boot cycle.
75
+ cmd: "$$$"
76
+
77
+ stop:
78
+ description: Stop the current motion.
79
+ cmd: "&2 Q20=2"
80
+
81
+ homing:
82
+ description: Start the homing cycle for the Hexapod.
83
+ cmd: "&2 Q20=1"
84
+
85
+ is_homing_done:
86
+ description: Check if Homing is done.
87
+
88
+ set_virtual_homing:
89
+ description: Starts the virtual homing cycle on the hexapod.
90
+
91
+ This command uses the position given in parameters to initialize the hexapod position.
92
+ No movements of the hexapod are performed during this homing cycle. Please note that the
93
+ position specified in parameters must match the absolute position of the Object coordinate
94
+ system in the User coordinate system (see description in the manual chapter 2 on coordinates
95
+ systems). This position correspond to the answer of the command `get_user_positions()`.
96
+ During this operation, it is important to have the same hexapod position as those defined
97
+ during the record of the position. Otherwise, the system initialization will be incorrect.
98
+
99
+ cmd: "&2 Q71={tx} Q72={ty} Q73={tz} Q74={rx} Q75={ry} Q76={rz} Q20=42"
100
+
101
+ clear_error:
102
+ description: Clear all errors in the controller software.
103
+ cmd: "&2 Q20=15"
104
+
105
+ activate_control_loop:
106
+ description: Activates the control loop on motors.
107
+ cmd: "&2 Q20=3"
108
+
109
+ deactivate_control_loop:
110
+ description: Disables the control loop on the servo motors.
111
+ cmd: "&2 Q20=4"
112
+
113
+ configure_coordinates_systems:
114
+ description: Change the definition of the User Coordinate System and the Object Coordinate System.
115
+ cmd: "&2 Q80={tx_u} Q81={ty_u} Q82={tz_u} Q83={rx_u} Q84={ry_u} Q85={rz_u} Q86={tx_o} Q87={ty_o} Q88={tz_o} Q89={rx_o} Q90={ry_o} Q91={rz_o} Q20=21"
116
+
117
+ get_coordinates_systems:
118
+ description: Retrieve the definition of the User Coordinate System and the Object Coordinate System.
119
+ cmd: "&2 Q20=31"
120
+ query: "&2 Q20 Q80,12,1"
121
+
122
+ get_general_state:
123
+ description: Retreive general state information of the hexapod.
124
+
125
+ get_user_positions:
126
+ description: Retreive the position of the Object Coordinate System in the User Coordinate System.
127
+ cmd: "&2 Q53,6,1"
128
+
129
+ get_machine_positions:
130
+ description: Retreive the position of the Platform Coordinate System in the Machine Coordinate System.
131
+ cmd: "&2 Q47,6,1"
132
+
133
+ get_actuator_length:
134
+ description: Retreive the current length of the hexapod actuators.
135
+ cmd: "&2 Q41,6,1"
136
+
137
+ get_actuator_state:
138
+ description: Returns the general state of the actuators.
139
+
140
+ move_absolute:
141
+ description: Move/define the Object Coordinate System position and orientation expressed in the invariant user coordinate system.
142
+
143
+ The rotation centre coincides with the Object Coordinates System origin and
144
+ the movements are controlled with translation components at first (Tx, Ty, tZ)
145
+ and then the rotation components (Rx, Ry, Rz).
146
+ cmd: "&2 Q70=0 Q71={tx:.6f} Q72={ty:.6f} Q73={tz:.6f} Q74={rx:.6f} Q75={ry:.6f} Q76={rz:.6f} Q20=11"
147
+
148
+ move_relative_object:
149
+ description: Move the object relative to its current object position and orientation.
150
+ cmd: "&2 Q70=1 Q71={tx:.6f} Q72={ty:.6f} Q73={tz:.6f} Q74={rx:.6f} Q75={ry:.6f} Q76={rz:.6f} Q20=11"
151
+
152
+ move_relative_user:
153
+ description: Move the object relative to its current object position and orientation.
154
+ cmd: "&2 Q70=2 Q71={tx:.6f} Q72={ty:.6f} Q73={tz:.6f} Q74={rx:.6f} Q75={ry:.6f} Q76={rz:.6f} Q20=11"
155
+
156
+ check_absolute_movement:
157
+ description: Check if the requested object movement is valid.
158
+ cmd: "&2 Q70=0 Q71={tx} Q72={ty} Q73={tz} Q74={rx} Q75={ry} Q76={rz} Q20=10"
159
+
160
+ check_relative_object_movement:
161
+ description: Check if the requested object movement is valid.
162
+ cmd: "&2 Q70=1 Q71={tx} Q72={ty} Q73={tz} Q74={rx} Q75={ry} Q76={rz} Q20=10"
163
+
164
+ check_relative_user_movement:
165
+ description: Check if the requested object movement is valid.
166
+ cmd: "&2 Q70=2i Q71={tx} Q72={ty} Q73={tz} Q74={rx} Q75={ry} Q76={rz} Q20=10"
167
+
168
+ goto_zero_position:
169
+ cmd: "&2 Q80=1 Q20=13"
170
+
171
+ goto_retracted_position:
172
+ cmd: "&2 Q80=2 Q20=13"
173
+
174
+ goto_specific_position:
175
+ cmd: "&2 Q80={pos} Q20=13"
176
+
177
+ perform_maintenance:
178
+ description: Ask the controller to perform the maintenance cycle which consists to
179
+ travel the full range on one axis. Full range corresponds to the Hexapod
180
+ machine limts (defined by the manufacturer), and the movement is
181
+ performed in Machine coordinate system.
182
+ cmd: "{axis}"
183
+
184
+ get_speed:
185
+ description: Returns the movement speed. Translation speed is expressed in mm per
186
+ second, the angular speed is expressed in degrees per second.
187
+
188
+ set_speed:
189
+ description: Sets the speed of movements.
190
+ cmd: "&2 Q80={vt} Q81={vr} Q20=25"
191
+
192
+ get_debug_info:
193
+ description: Returns debugging status information.
@@ -0,0 +1,197 @@
1
+ """
2
+ The Control Server that connects to the Hexapod PUNA Hardware Controller.
3
+
4
+ Start the control server from the terminal as follows:
5
+
6
+ $ puna_cs start-bg
7
+
8
+ or when you don't have the device available, start the control server in simulator mode. That
9
+ will make the control server connect to a device software simulator:
10
+
11
+ $ puna_cs start --sim
12
+
13
+ Please note that software simulators are intended for simple test purposes and will not simulate
14
+ all device behavior correctly, e.g. timing, error conditions, etc.
15
+
16
+ """
17
+ import logging
18
+
19
+ from egse.hexapod.symetrie import ProxyFactory
20
+ from egse.hexapod.symetrie import get_hexapod_controller_pars
21
+ from egse.process import SubProcess
22
+
23
+ if __name__ != "__main__":
24
+ import multiprocessing
25
+ multiprocessing.current_process().name = "puna_cs"
26
+
27
+ import sys
28
+
29
+ import click
30
+ import rich
31
+ import zmq
32
+
33
+ from egse.control import is_control_server_active
34
+ from egse.zmq_ser import connect_address
35
+
36
+ from prometheus_client import start_http_server
37
+
38
+ from egse.control import ControlServer
39
+ from egse.hexapod.symetrie.puna import PunaProxy
40
+ from egse.hexapod.symetrie.puna_protocol import PunaProtocol
41
+ from egse.settings import Settings
42
+
43
+ logger = logging.getLogger(__name__)
44
+
45
+ CTRL_SETTINGS = Settings.load("Hexapod PUNA Control Server")
46
+
47
+
48
+ class PunaControlServer(ControlServer):
49
+ """PunaControlServer - Command and monitor the Hexapod PUNA hardware.
50
+
51
+ This class works as a command and monitoring server to control the Symétrie Hexapod PUNA.
52
+ This control server shall be used as the single point access for controlling the hardware
53
+ device. Monitoring access should be done preferably through this control server also,
54
+ but can be done with a direct connection through the PunaController if needed.
55
+
56
+ The sever binds to the following ZeroMQ sockets:
57
+
58
+ * a REQ-REP socket that can be used as a command server. Any client can connect and
59
+ send a command to the Hexapod.
60
+
61
+ * a PUB-SUP socket that serves as a monitoring server. It will send out Hexapod status
62
+ information to all the connected clients every five seconds.
63
+
64
+ """
65
+
66
+ def __init__(self):
67
+ super().__init__()
68
+
69
+ self.device_protocol = PunaProtocol(self)
70
+
71
+ self.logger.info(f"Binding ZeroMQ socket to {self.device_protocol.get_bind_address()}")
72
+
73
+ self.device_protocol.bind(self.dev_ctrl_cmd_sock)
74
+
75
+ self.poller.register(self.dev_ctrl_cmd_sock, zmq.POLLIN)
76
+
77
+ def get_communication_protocol(self):
78
+ return CTRL_SETTINGS.PROTOCOL
79
+
80
+ def get_commanding_port(self):
81
+ return CTRL_SETTINGS.COMMANDING_PORT
82
+
83
+ def get_service_port(self):
84
+ return CTRL_SETTINGS.SERVICE_PORT
85
+
86
+ def get_monitoring_port(self):
87
+ return CTRL_SETTINGS.MONITORING_PORT
88
+
89
+ def get_storage_mnemonic(self):
90
+ try:
91
+ return CTRL_SETTINGS.STORAGE_MNEMONIC
92
+ except AttributeError:
93
+ return "PUNA"
94
+
95
+ def before_serve(self):
96
+ start_http_server(CTRL_SETTINGS.METRICS_PORT)
97
+
98
+
99
+ @click.group()
100
+ def cli():
101
+ pass
102
+
103
+
104
+ @cli.command()
105
+ @click.option("--simulator", "--sim", is_flag=True,
106
+ help="Start the Hexapod Puna Simulator as the backend.")
107
+ def start(simulator):
108
+ """Start the Hexapod Puna Control Server."""
109
+
110
+ if simulator:
111
+
112
+ Settings.set_simulation_mode(True)
113
+
114
+ try:
115
+
116
+ controller = PunaControlServer()
117
+ controller.serve()
118
+
119
+ except KeyboardInterrupt:
120
+
121
+ print("Shutdown requested...exiting")
122
+
123
+ except SystemExit as exit_code:
124
+
125
+ print("System Exit with code {}.".format(exit_code))
126
+ sys.exit(exit_code)
127
+
128
+ except Exception:
129
+
130
+ logger.exception("Cannot start the Hexapod Puna Control Server")
131
+
132
+ # The above line does exactly the same as the traceback, but on the logger
133
+ # import traceback
134
+ # traceback.print_exc(file=sys.stdout)
135
+
136
+ return 0
137
+
138
+
139
+ @cli.command()
140
+ @click.option("--simulator", "--sim", is_flag=True,
141
+ help="Start the Hexapod Puna Simulator as the backend.")
142
+ def start_bg(simulator):
143
+ """Start the PUNA Control Server in the background."""
144
+ sim = "--simulator" if simulator else ""
145
+ proc = SubProcess("puna_cs", ["puna_cs", "start", sim])
146
+ proc.execute()
147
+
148
+
149
+ @cli.command()
150
+ def stop():
151
+ """Send a 'quit_server' command to the Hexapod Puna Control Server."""
152
+
153
+ *_, device_id, device_name, controller_type = get_hexapod_controller_pars()
154
+
155
+ factory = ProxyFactory()
156
+
157
+ try:
158
+ with factory.create(device_name, device_id=device_id) as proxy:
159
+ sp = proxy.get_service_proxy()
160
+ sp.quit_server()
161
+ except ConnectionError:
162
+ rich.print("[red]Couldn't connect to 'puna_cs', process probably not running. ")
163
+
164
+
165
+ @cli.command()
166
+ def status():
167
+ """Request status information from the Control Server."""
168
+
169
+ protocol = CTRL_SETTINGS.PROTOCOL
170
+ hostname_cs = CTRL_SETTINGS.HOSTNAME
171
+ port_cs = CTRL_SETTINGS.COMMANDING_PORT
172
+
173
+ endpoint = connect_address(protocol, hostname_cs, port_cs)
174
+
175
+ *_, device_id, device_name, controller_type = get_hexapod_controller_pars()
176
+
177
+ factory = ProxyFactory()
178
+
179
+ if is_control_server_active(endpoint):
180
+ rich.print("PUNA Hexapod: [green]active")
181
+ with factory.create(device_name, device_id=device_id) as puna:
182
+ sim = puna.is_simulator()
183
+ connected = puna.is_connected()
184
+ ip = puna.get_ip_address()
185
+ rich.print(f"type: {controller_type}")
186
+ rich.print(f"mode: {'simulator' if sim else 'device'}{'' if connected else ' not'} connected")
187
+ rich.print(f"hostname: {ip}")
188
+ rich.print(f"commanding port: {port_cs}")
189
+ else:
190
+ rich.print("PUNA Hexapod: [red]not active")
191
+
192
+
193
+ if __name__ == "__main__":
194
+
195
+ logging.basicConfig(level=logging.DEBUG, format=Settings.LOG_FORMAT_FULL)
196
+
197
+ sys.exit(cli())
@@ -0,0 +1,131 @@
1
+ import logging
2
+
3
+ from egse.command import ClientServerCommand
4
+ from egse.control import ControlServer
5
+ from egse.device import DeviceConnectionState
6
+ from egse.hexapod.symetrie import ControllerFactory
7
+ from egse.hexapod.symetrie.puna import PunaInterface
8
+ from egse.hexapod.symetrie.puna import PunaSimulator
9
+ from egse.hexapod.symetrie import get_hexapod_controller_pars
10
+ # from egse.hk import read_conversion_dict, convert_hk_names
11
+ from egse.protocol import CommandProtocol
12
+ from egse.settings import Settings
13
+ from egse.system import format_datetime
14
+ from egse.zmq_ser import bind_address
15
+
16
+ logger = logging.getLogger(__name__)
17
+
18
+ ctrl_settings = Settings.load("Hexapod PUNA Control Server")
19
+ PUNA_SETTINGS = Settings.load("PMAC Controller")
20
+ DEVICE_SETTINGS = Settings.load(filename="puna.yaml")
21
+
22
+
23
+ class PunaCommand(ClientServerCommand):
24
+ pass
25
+
26
+
27
+ class PunaProtocol(CommandProtocol):
28
+ def __init__(self, control_server: ControlServer):
29
+ super().__init__()
30
+ self.control_server = control_server
31
+
32
+ # self.hk_conversion_table = read_conversion_dict(self.control_server.get_storage_mnemonic(), use_site=True)
33
+
34
+ if Settings.simulation_mode():
35
+ self.hexapod = PunaSimulator()
36
+ else:
37
+ hostname, port, device_id, device_name, controller_type = get_hexapod_controller_pars()
38
+
39
+ factory = ControllerFactory()
40
+ self.hexapod = factory.create(device_name, device_id=device_id)
41
+ # self.hexapod = PunaController(hostname=hostname, port=port)
42
+ self.hexapod.add_observer(self)
43
+
44
+ try:
45
+ self.hexapod.connect()
46
+ except ConnectionError:
47
+ logger.warning("Couldn't establish a connection to the PUNA Hexapod, check the log messages.")
48
+
49
+ self.load_commands(DEVICE_SETTINGS.Commands, PunaCommand, PunaInterface)
50
+ self.build_device_method_lookup_table(self.hexapod)
51
+
52
+ def get_bind_address(self):
53
+ return bind_address(
54
+ self.control_server.get_communication_protocol(),
55
+ self.control_server.get_commanding_port(),
56
+ )
57
+
58
+ def get_device(self):
59
+ return self.hexapod
60
+
61
+ def get_status(self):
62
+
63
+ status = super().get_status()
64
+
65
+ if self.state == DeviceConnectionState.DEVICE_NOT_CONNECTED and not Settings.simulation_mode():
66
+ return status
67
+
68
+ mach_positions = self.hexapod.get_machine_positions()
69
+ user_positions = self.hexapod.get_user_positions()
70
+ actuator_length = self.hexapod.get_actuator_length()
71
+
72
+ status.update({"mach": mach_positions, "user": user_positions, "alength": actuator_length})
73
+
74
+ return status
75
+
76
+ def get_housekeeping(self) -> dict:
77
+
78
+ result = dict()
79
+ result["timestamp"] = format_datetime()
80
+
81
+ if self.state == DeviceConnectionState.DEVICE_NOT_CONNECTED and not Settings.simulation_mode():
82
+ return result
83
+
84
+ mach_positions = self.hexapod.get_machine_positions()
85
+ user_positions = self.hexapod.get_user_positions()
86
+ actuator_length = self.hexapod.get_actuator_length()
87
+
88
+ # The result of the previous calls might be None when e.g. the connection
89
+ # to the device gets lost.
90
+
91
+ if mach_positions is None or user_positions is None or actuator_length is None:
92
+ if not self.hexapod.is_connected():
93
+ logger.warning("Hexapod PUNA disconnected.")
94
+ self.update_connection_state(DeviceConnectionState.DEVICE_NOT_CONNECTED)
95
+ return result
96
+
97
+ for idx, key in enumerate(
98
+ ["user_t_x", "user_t_y", "user_t_z", "user_r_x", "user_r_y", "user_r_z"]
99
+ ):
100
+ result[key] = user_positions[idx]
101
+
102
+ for idx, key in enumerate(
103
+ ["mach_t_x", "mach_t_y", "mach_t_z", "mach_r_x", "mach_r_y", "mach_r_z"]
104
+ ):
105
+ result[key] = mach_positions[idx]
106
+
107
+ for idx, key in enumerate(
108
+ ["alen_t_x", "alen_t_y", "alen_t_z", "alen_r_x", "alen_r_y", "alen_r_z"]
109
+ ):
110
+ result[key] = actuator_length[idx]
111
+
112
+ # TODO:
113
+ # the get_general_state() method should be refactored as to return a dict instead of a
114
+ # list. Also, we might want to rethink the usefulness of returning the tuple,
115
+ # it the first return value ever used?
116
+
117
+ _, _ = self.hexapod.get_general_state()
118
+
119
+ result["Homing done"] = self.hexapod.is_homing_done()
120
+ result["In position"] = self.hexapod.is_in_position()
121
+
122
+ return result # convert_hk_names(result, self.hk_conversion_table)
123
+
124
+ def is_device_connected(self):
125
+ # FIXME(rik): There must be another way to check if the socket is still alive...
126
+ # This will send way too many VERSION requests to the controllers.
127
+ # According to SO [https://stackoverflow.com/a/15175067] the best way
128
+ # to check for a connection drop / close is to handle the exceptions
129
+ # properly.... so, no polling for connections by sending it a simple
130
+ # command.
131
+ return self.hexapod.is_connected()