symetrie-hexapod 0.17.3__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,416 @@
1
+ import socket
2
+ import threading
3
+ import time
4
+ from telnetlib import Telnet
5
+ from time import sleep
6
+ from typing import List
7
+
8
+ import paramiko
9
+
10
+ from egse.device import DeviceTransport
11
+ from egse.hexapod.symetrie import logger
12
+ from egse.system import wait_until
13
+
14
+
15
+ RETURN_CODES = {
16
+ 0: "Success.",
17
+ -1: "Undefined error.",
18
+ -10: "Wrong value for parameter at index 0.",
19
+ -11: "Wrong value for parameter at index 1.",
20
+ -12: "Wrong value for parameter at index 2.",
21
+ -13: "Wrong value for parameter at index 3.",
22
+ -14: "Wrong value for parameter at index 4.",
23
+ -15: "Wrong value for parameter at index 5.",
24
+ -16: "Wrong value for parameter at index 6.",
25
+ -17: "Wrong value for parameter at index 7.",
26
+ -18: "Wrong value for parameter at index 8.",
27
+ -19: "Wrong value for parameter at index 9.",
28
+ -20: "Wrong value for parameter at index 10.",
29
+ -21: "Wrong value for parameter at index 11.",
30
+ -22: "Wrong value for parameter at index 12.",
31
+ -23: "Wrong value for parameter at index 13.",
32
+ -24: "Wrong value for parameter at index 14.",
33
+ -25: "Wrong value for parameter at index 15.",
34
+ -26: "Wrong value for parameter at index 16.",
35
+ -27: "Wrong value for parameter at index 17.",
36
+ -28: "Wrong value for parameter at index 18.",
37
+ -29: "Wrong value for parameter at index 19.",
38
+ -30: "Unknown command number.",
39
+ -31: "This configuration command is a 'get' only type.",
40
+ -32: "This configuration command is a 'set' only type.",
41
+ -33: "The axis number do not correspond to an axis defined on the controller.",
42
+ -34: "A stop task is running.",
43
+ -35: "All motors need to be control on.",
44
+ -36: "All motors need to be control off.",
45
+ -37: "Emergency stop is pressed.",
46
+ -38: "A motion task is running.",
47
+ -39: "A home task is running.",
48
+ -40: "Requested move is not feasible.",
49
+ -41: "Power supply of limit switches is off.",
50
+ -42: "Power supply of encoders is off.",
51
+ -43: "A fatal error is present. This type of error needs a controller restart to be removed.",
52
+ -44: "An error is present, error reset is required.",
53
+ -45: "Home is not completed.",
54
+ -46: "Software option not available (can be linked to hardware configuration).",
55
+ -47: "Virtual home: file was created on another controller (different MAC address).",
56
+ -48: "Virtual home: some positions read in file are out of software limits.",
57
+ -49: "Virtual home: file data were stored while hexapod was moving.",
58
+ -50: "Virtual home: no data available.",
59
+ -51: "Command has been rejected because another action is running.",
60
+ -52: "Timeout waiting for home complete status.",
61
+ -53: "Timeout waiting for control on status.",
62
+ -54: "Timeout on motion program start.",
63
+ -55: "Timeout on home task start.",
64
+ -56: "Timeout on virtual home write file task.",
65
+ -57: "Timeout on virtual home delete file task.",
66
+ -58: "Timeout on virtual home read file task.",
67
+ -59: "Timeout on disk access verification task.",
68
+ -60: "Configuration file: save process failed.",
69
+ -61: "Configuration file: loaded file is empty.",
70
+ -62: "Configuration file: loaded data are corrupted.",
71
+ -63: "No access to the memory disk.",
72
+ -64: "File does not exist.",
73
+ -65: "Folder access failed.",
74
+ -66: "Creation of folder tree on the memory disk failed.",
75
+ -67: "Generation or write of the checksum failed.",
76
+ -68: "File read: no data or wrong data size.",
77
+ -69: "File read: no checksum.",
78
+ -70: "File read: incorrect checksum.",
79
+ -71: "File write: failed.",
80
+ -72: "File open: failed.",
81
+ -73: "File delete: failed.",
82
+ -74: "Get MAC address failed.",
83
+ -75: "NaN (Not a Number) or infinite value found.",
84
+ -76: "The coordinate system transformations are not initialized.",
85
+ -77: "A kinematic error is present.",
86
+ -78: "The motor phase process failed (phase search or phase set from position offset).",
87
+ -79: "The motor phase is not found.",
88
+ -80: "Timeout waiting for control off status.",
89
+ -81: "The requested kinematic mode (number) is not defined for the machine.",
90
+ -82: "Timeout waiting for phase found status.",
91
+ -1000: "Internal error: 'RET_Dev_CfS_NaNReturned'.",
92
+ -1001: "Internal error: 'RET_Dev_CfS_FctNotAvailableInKernel'.",
93
+ -1002: "Internal error: 'RET_Dev_CfS_UndefinedCfSType'.",
94
+ -1003: "Internal error: 'RET_Dev_CfS_FIO_UndefinedFioType'.",
95
+ -1004: "Internal error: 'RET_Dev_CfS_FIO_HomeFile_UndefinedAction'.",
96
+ -1005: "Internal error: 'RET_Dev_UndefinedEnumValue'.",
97
+ -1006: "Internal error: 'RET_Dev_LdataCmdStatusIsNegative'.",
98
+ -1007: "Internal error: 'RET_Dev_NumMotorsInCoord_Sup_DEF_aGrQ_SIZE'.",
99
+ -1008: "Internal error: 'RET_Dev_NumMotorsInCoord_WrongNumber'.",
100
+ -1009: "Internal error: 'RET_String_StrCat_DestSizeReached'.",
101
+ -1010: "Internal error: 'RET_String_LengthOverStringSize'.",
102
+ -1011: "Internal error: 'RET_String_AllCharShouldIntBetween_0_255'.",
103
+ -1012: "Internal error: 'RET_String_StrCpy_DestSizeReached'.",
104
+ -1013: "Internal error: 'RET_ErrAction_HomeReset'.",
105
+ -1014: "Internal error: 'RET_Home_StopReceivedWhileRunning'.",
106
+ -1015: "Internal error: 'RET_UndefinedKinAssembly'.",
107
+ -1016: "Internal error: 'RET_WrongPmcConfig'.",
108
+ }
109
+
110
+
111
+ class ZondaError(Exception):
112
+ pass
113
+
114
+
115
+ class ZondaSSHInterface:
116
+ def __init__(self, hostname: str):
117
+ self.client = None
118
+ self.gpascii_client = None
119
+ self.connected = False
120
+ self.ssh_output = None
121
+ self.ssh_error = None
122
+ self.verbose = False
123
+ self.hostname = hostname
124
+
125
+ self.semaphore = threading.Semaphore()
126
+
127
+ self.CommandReturns = RETURN_CODES
128
+
129
+ # Etablish the SSH connection with the controller and open gpascii
130
+ def connect(self, ip):
131
+ try:
132
+ # Paramiko.SSHClient can be used to make connections to the remote server and
133
+ # transfer files
134
+ logger.info("Establishing ssh connection with {}...".format(ip))
135
+ self.client = paramiko.SSHClient()
136
+ # Parsing an instance of the AutoAddPolicy to set_missing_host_key_policy() changes
137
+ # it to allow any host.
138
+ self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
139
+ self.client.connect(
140
+ hostname=ip,
141
+ port=22,
142
+ username="root",
143
+ password="deltatau",
144
+ timeout=5,
145
+ allow_agent=False,
146
+ look_for_keys=False,
147
+ )
148
+ logger.warning("Connected to the server: {}".format(ip))
149
+ self.connected = True
150
+ except paramiko.AuthenticationException:
151
+ logger.warning("Authentication failed, please verify your credentials.")
152
+ return False
153
+ except paramiko.SSHException as sshException:
154
+ logger.warning("Could not establish SSH connection: %s" % sshException)
155
+ return False
156
+ except socket.timeout as e:
157
+ logger.warning("Connection timed out.")
158
+ return False
159
+ except Exception as e:
160
+ logger.warning("Exception in connecting to the server.")
161
+ logger.warning("Python says:", e)
162
+ self.client.close()
163
+ return False
164
+
165
+ self.gpascii_client = self.client.invoke_shell(term="vt100")
166
+ self.sendcommand("gpascii -2", 0.5)
167
+ self.sendcommand("echo7")
168
+
169
+ def is_connected(self):
170
+ return self.connected
171
+
172
+ # send a command through gpascii
173
+ def sendcommand(self, command, wait=0):
174
+ self.gpascii_client.send(command + "\r\n")
175
+ if self.verbose == True:
176
+ pass # print("Command send: {}".format(command))
177
+
178
+ if wait > 0:
179
+ sleep(wait)
180
+
181
+ # wait for response from gpascii
182
+ step = 0.1
183
+ time = 0
184
+
185
+ while True:
186
+ if self.gpascii_client.recv_ready():
187
+ break
188
+ if time > 2.0:
189
+ logger.warning("Command response timed out!")
190
+ return ""
191
+ sleep(step)
192
+ time += step
193
+
194
+ # get and clean response
195
+ response = self.gpascii_client.recv(2048)
196
+ response = response.decode()
197
+ response = response.replace(command, "")
198
+ response = response.replace("\r", "")
199
+ # response = response.replace("\n"," ")
200
+ response = response.replace("\x06", "")
201
+
202
+ # remove empty elements
203
+ cleaned = ""
204
+ lines = response.split("\n")
205
+ for line in lines:
206
+ if line == "" or line == " ":
207
+ lines.remove(line)
208
+ else:
209
+ cleaned += line + "\n"
210
+
211
+ # if self.verbose is True:
212
+ # print("Command rcv: {}".format(cleaned))
213
+ return cleaned
214
+
215
+ def cmd_decode(self, name, arguments=list()):
216
+ command = ""
217
+
218
+ if "JOG" in name:
219
+ command += "c_ax={} ".format(arguments[0])
220
+ arguments.pop(0)
221
+
222
+ # set cfg part
223
+ cfg = 1
224
+ if "?" in name:
225
+ cfg = 0
226
+ name = name.replace("?", "")
227
+ if "CFG_" in name:
228
+ command += "c_cfg={} ".format(cfg)
229
+
230
+ # create the parameters part of a command using the given arguments
231
+ for index in range(0, len(arguments)):
232
+ command += "c_par({})={} ".format(index, arguments[index])
233
+
234
+ # finish command line
235
+ command += "c_cmd=C_{}".format(name)
236
+
237
+ # logger.debug("command sent: ", command)
238
+ return command
239
+
240
+ def cmd_check(self):
241
+ # sends "c_cmd" and returns the state of the command that has been previously sent.
242
+ Timer = 0
243
+ last = time.process_time()
244
+
245
+ # For a maximum of 5 sec
246
+ while Timer < 5:
247
+ # wait and count
248
+ time.sleep(0.1)
249
+ Timer += time.process_time() - last
250
+ last = time.process_time()
251
+
252
+ # ask for command state
253
+ response = self.sendcommand("c_cmd")
254
+ response.replace("\n", "")
255
+
256
+ if "=" in response:
257
+ # get value
258
+ elements = response.split("=", 1)
259
+ element = elements[1]
260
+
261
+ # exit if command is completed
262
+ if int(element) <= 0:
263
+ response = element
264
+
265
+ code = int(response)
266
+
267
+ if code < 0:
268
+ message = self.CommandReturns[code]
269
+ logger.warning("Command Error return: {} : {}".format(code, message))
270
+ elif code == 0:
271
+ message = "Command successful: 0, execution time {} sec".format(Timer)
272
+ # logger.info(message)
273
+ return [code, message]
274
+ logger.warning("Error: Order timed out !")
275
+ return [-1, "Error: Order timed out !"]
276
+
277
+ def p_check(self, number=20):
278
+ # ask the value of the command parameters
279
+ command = "c_par(0)"
280
+ if number > 1:
281
+ command += ",{},1".format(number)
282
+
283
+ answer = self.sendcommand(command)
284
+ return answer
285
+
286
+ def disconnect(self):
287
+ try:
288
+ logger.info(f"Closing ssh connection with {self.hostname}...")
289
+ self.client.close()
290
+ self.gpascii_client.close()
291
+ self.connected = False
292
+ logger.warning("...disconnected from ssh")
293
+ except Exception as e_exc:
294
+ raise ZondaError(f"Could not close socket to {self.hostname}") from e_exc
295
+
296
+
297
+ class ZondaTelnetInterface(DeviceTransport):
298
+ """
299
+ The ZONDA Hexapod device interface based on the telnet protocol.
300
+ """
301
+
302
+ TELNET_TIMEOUT = 0.5
303
+
304
+ def __init__(self, hostname: str, port: int):
305
+ self.telnet = Telnet()
306
+ self.hostname = hostname
307
+ self.port = port
308
+ self._is_connected = False
309
+
310
+ def connect(self):
311
+ self.telnet.open(self.hostname, self.port)
312
+ self.telnet.read_until(b"login: ", timeout=self.TELNET_TIMEOUT)
313
+ self.telnet.write(b"root\r\n")
314
+ self.telnet.read_until(b"Password: ", timeout=self.TELNET_TIMEOUT)
315
+ self.telnet.write(b"deltatau\r\n")
316
+ self.telnet.read_until(b"ppmac# ", timeout=self.TELNET_TIMEOUT)
317
+ self.telnet.write(b"gpascii -2\r\n")
318
+ self.telnet.write(b"echo7\r\n")
319
+ self.telnet.read_until(b"\x06", timeout=self.TELNET_TIMEOUT)
320
+ self.telnet.read_very_eager()
321
+ self._is_connected = True
322
+
323
+ def is_connected(self):
324
+ return self._is_connected
325
+
326
+ def disconnect(self):
327
+ self.telnet.read_very_eager()
328
+ self.telnet.close()
329
+ self._is_connected = False
330
+
331
+ def check_command_status(self):
332
+ if wait_until(lambda: self.trans("c_cmd")[0] == "0", interval=0.01):
333
+ rc = int(self.trans("c_cmd")[0])
334
+ logger.warning(f"Command check: {RETURN_CODES[rc]} [{rc}]")
335
+ else:
336
+ rc = 0
337
+
338
+ return rc, RETURN_CODES[rc]
339
+
340
+ def get_pars(self, count: int = 20):
341
+ # ask the value of the command parameters
342
+ command = "c_par(0)"
343
+ if count > 1:
344
+ command += f",{count},1"
345
+
346
+ return self.trans(command)
347
+
348
+ def trans(self, cmd: str) -> List:
349
+ self.write(cmd)
350
+ response = self.read()
351
+ if response and response[0] == cmd:
352
+ return response[1:]
353
+ else:
354
+ return response
355
+
356
+ def read(self) -> List:
357
+ response = self.telnet.read_until(b"\x06\r\n", timeout=self.TELNET_TIMEOUT)
358
+ response = response.decode()
359
+ parts = response.split("\r\n")
360
+ if len(parts) == 1:
361
+ return []
362
+ if parts[-2] == "\x06":
363
+ return parts[:-2]
364
+ logger.warning("Expected ACK at the end of the response")
365
+ return parts
366
+
367
+ def write(self, cmd: str):
368
+ self.telnet.write(cmd.encode() + b"\r\n")
369
+
370
+
371
+ def decode_command(cmd_name: str, *args):
372
+ args = list(args)
373
+ cmd_name = cmd_name.upper()
374
+
375
+ full_command = ""
376
+
377
+ if "JOG" in cmd_name:
378
+ arg = args.pop(0)
379
+ full_command += f"c_ax={arg} "
380
+
381
+ # set cfg part
382
+
383
+ if "?" in cmd_name:
384
+ cmd_name = cmd_name.replace("?", "")
385
+ cfg = 0
386
+ else:
387
+ cfg = 1
388
+
389
+ if "CFG_" in cmd_name:
390
+ full_command += f"c_cfg={cfg} "
391
+
392
+ # create the parameters part of a full_command using the given arguments
393
+
394
+ for index, arg in enumerate(args):
395
+ full_command += f"c_par({index})={arg} "
396
+
397
+ # finish full_command line
398
+
399
+ full_command += f"c_cmd=C_{cmd_name}"
400
+
401
+ return full_command
402
+
403
+
404
+ if __name__ == "__main__":
405
+ import rich
406
+
407
+ zonda = ZondaTelnetInterface("192.168.56.10", 23)
408
+ zonda.connect()
409
+ rich.print(zonda.trans("s_hexa,50,1"))
410
+ rich.print(zonda.trans("s_ax_1,6,1"))
411
+ rich.print(zonda.trans("s_pos_ax_1,6,1"))
412
+ rich.print(zonda.read())
413
+ zonda.disconnect()
414
+
415
+ print(decode_command("CFG_LIMIT?", 0))
416
+ print(decode_command("CFG_LIMIT", 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12))
@@ -0,0 +1,118 @@
1
+ from pathlib import Path
2
+
3
+ from egse.command import ClientServerCommand
4
+ from egse.control import ControlServer
5
+ from egse.hexapod.symetrie import ControllerFactory
6
+ from egse.hexapod.symetrie import get_hexapod_controller_pars
7
+ from egse.hexapod.symetrie import logger
8
+ from egse.hexapod.symetrie.zonda import ZondaInterface
9
+ from egse.hexapod.symetrie.zonda import ZondaSimulator
10
+ from egse.hk import convert_hk_names
11
+ from egse.metrics import define_metrics
12
+ from egse.protocol import CommandProtocol
13
+ from egse.settings import Settings
14
+ from egse.system import format_datetime
15
+ from egse.zmq_ser import bind_address
16
+
17
+ _HERE = Path(__file__).parent
18
+
19
+ zonda_settings = Settings.load(filename="zonda.yaml", location=_HERE)
20
+
21
+
22
+ class ZondaCommand(ClientServerCommand):
23
+ pass
24
+
25
+
26
+ class ZondaProtocol(CommandProtocol):
27
+ def __init__(self, control_server: ControlServer, device_id: str, simulator: bool = False):
28
+ super().__init__(control_server)
29
+ self.simulator = simulator
30
+ self.device_id = device_id
31
+
32
+ # FIXME: HK needs to be working
33
+ # self.hk_conversion_table = read_conversion_dict(self.control_server.get_storage_mnemonic(), use_site=True)
34
+
35
+ if self.simulator:
36
+ self.hexapod = ZondaSimulator()
37
+ else:
38
+ *_, device_type, controller_type = get_hexapod_controller_pars(device_id)
39
+
40
+ factory = ControllerFactory()
41
+ self.hexapod = factory.create(device_type, device_id=device_id)
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 ZONDA Hexapod, check the log messages.")
48
+
49
+ self.load_commands(zonda_settings.Commands, ZondaCommand, ZondaInterface)
50
+ self.build_device_method_lookup_table(self.hexapod)
51
+
52
+ # self.metrics = define_metrics("ZONDA")
53
+
54
+ def get_bind_address(self):
55
+ return bind_address(
56
+ self.control_server.get_communication_protocol(),
57
+ self.control_server.get_commanding_port(),
58
+ )
59
+
60
+ def get_status(self):
61
+ status = super().get_status()
62
+
63
+ mach_positions = self.hexapod.get_machine_positions()
64
+ user_positions = self.hexapod.get_user_positions()
65
+ actuator_length = self.hexapod.get_actuator_length()
66
+
67
+ status.update({"mach": mach_positions, "user": user_positions, "alength": actuator_length})
68
+
69
+ return status
70
+
71
+ def get_housekeeping(self) -> dict:
72
+ result = dict()
73
+ result["timestamp"] = format_datetime()
74
+
75
+ mach_positions = self.hexapod.get_machine_positions()
76
+ user_positions = self.hexapod.get_user_positions()
77
+ actuator_length = self.hexapod.get_actuator_length()
78
+ actuator_temperature = self.hexapod.get_temperature()
79
+
80
+ # TODO If you change these names, please, also change them in egse.fov.fov_hk!
81
+ for idx, key in enumerate(["user_t_x", "user_t_y", "user_t_z", "user_r_x", "user_r_y", "user_r_z"]):
82
+ result[key] = user_positions[idx]
83
+
84
+ for idx, key in enumerate(["mach_t_x", "mach_t_y", "mach_t_z", "mach_r_x", "mach_r_y", "mach_r_z"]):
85
+ result[key] = mach_positions[idx]
86
+
87
+ for idx, key in enumerate(["alen_t_x", "alen_t_y", "alen_t_z", "alen_r_x", "alen_r_y", "alen_r_z"]):
88
+ result[key] = actuator_length[idx]
89
+
90
+ for idx, key in enumerate(["atemp_1", "atemp_2", "atemp_3", "atemp_4", "atemp_5", "atemp_6"]):
91
+ result[key] = actuator_temperature[idx]
92
+
93
+ # # TODO:
94
+ # # the get_general_state() method should be refactored as to return a dict instead of a
95
+ # # list. Also, we might want to rethink the usefulness of returning the tuple,
96
+ # # it the first return value ever used?
97
+ #
98
+ # _, _ = self.hexapod.get_general_state()
99
+ #
100
+ result["Homing done"] = self.hexapod.is_homing_done()
101
+ result["In position"] = self.hexapod.is_in_position()
102
+
103
+ # hk_dict = convert_hk_names(result, self.hk_conversion_table)
104
+
105
+ # for key, value in hk_dict.items():
106
+ # if key != "timestamp":
107
+ # self.metrics[key].set(value)
108
+
109
+ return result # hk_dict
110
+
111
+ def is_connected(self):
112
+ # FIXME(rik): There must be another way to check if the socket is still alive...
113
+ # This will send way too many VERSION requests to the controllers.
114
+ # According to SO [https://stackoverflow.com/a/15175067] the best way
115
+ # to check for a connection drop / close is to handle the exceptions
116
+ # properly.... so, no polling for connections by sending it a simple
117
+ # command.
118
+ return self.hexapod.is_connected()