|
1 | | -import enum |
| 1 | +import sys |
| 2 | +sys.path.append('.') |
| 3 | +import struct |
2 | 4 |
|
3 | 5 |
|
4 | | -class Angle(enum.Enum): |
| 6 | +class Command(): |
| 7 | + #BASIC |
| 8 | + HEADER = 0xfe |
| 9 | + FOOTER = 0xfa |
5 | 10 |
|
6 | | - J1 = 1 |
7 | | - J2 = 2 |
8 | | - J3 = 3 |
9 | | - J4 = 4 |
10 | | - J5 = 5 |
11 | | - J6 = 6 |
| 11 | + # Overall status |
| 12 | + POWER_ON = 0x10 |
| 13 | + POWER_OFF = 0x11 |
| 14 | + IS_POWER_ON = 0x12 |
| 15 | + SET_FREE_MODE = 0x13 |
| 16 | + |
| 17 | + # MDI MODE AND OPERATION |
| 18 | + GET_ANGLES = 0x20 |
| 19 | + SEND_ANGLE = 0x21 |
| 20 | + SEND_ANGLES = 0x22 |
| 21 | + GET_COORDS = 0x23 |
| 22 | + SEND_COORD = 0x24 |
| 23 | + SEND_COORDS = 0x25 |
| 24 | + PAUSE = 0x26 |
| 25 | + IS_PAUSED = 0x27 |
| 26 | + RESUME = 0x28 |
| 27 | + STOP = 0x29 |
| 28 | + IS_IN_POSITION = 0x2a |
| 29 | + IS_MOVING = 0x2b |
| 30 | + |
| 31 | + # JOG MODE AND OPERATION |
| 32 | + JOG_ANGLE = 0x30 |
| 33 | + JOG_COORD = 0x32 |
| 34 | + JOG_STOP = 0x34 |
| 35 | + |
| 36 | + # RUNNING STATUS AND SETTINGS |
| 37 | + GET_SPEED = 0x40 |
| 38 | + SET_SPEED = 0x41 |
| 39 | + GET_JOINT_MIN_ANGLE = 0x4a |
| 40 | + GET_JOINT_MAX_ANGLE = 0x4b |
| 41 | + |
| 42 | + # SERVO CONTROL |
| 43 | + IS_SERVO_ENABLE = 0x50 |
| 44 | + IS_ALL_SERVO_ENABLE = 0x51 |
| 45 | + |
| 46 | + # ATOM IO |
| 47 | + SET_COLOR = 0x6a |
| 48 | + SET_CLAW = 0x66 |
| 49 | + |
| 50 | + |
| 51 | +class DataProcesser(): |
| 52 | + # Functional approach |
| 53 | + def _encode_int8(self, data): |
| 54 | + return struct.pack('b', data) |
| 55 | + |
| 56 | + def _encode_int16(self, data): |
| 57 | + return list(struct.pack('>h', data)) |
| 58 | + |
| 59 | + def _decode_int8(self, data): |
| 60 | + return struct.unpack('b', data)[0] |
| 61 | + |
| 62 | + def _decode_int16(self, data): |
| 63 | + return struct.unpack('>h', data)[0] |
| 64 | + |
| 65 | + def _angle_to_int(self, angle): |
| 66 | + return int(angle * 100) |
| 67 | + |
| 68 | + def _coord_to_int(self, coord): |
| 69 | + return int(coord * 10) |
| 70 | + |
| 71 | + def _int_to_angle(self, _int): |
| 72 | + return round(_int / 100.0, 3) |
| 73 | + |
| 74 | + def _int_to_coord(self, _int): |
| 75 | + return round(_int / 10.0, 2) |
| 76 | + |
| 77 | + def _flatten(self, _list): |
| 78 | + return sum(([x] if not isinstance(x, list) else self._flatten(x) |
| 79 | + for x in _list), []) |
| 80 | + |
| 81 | + def _process_data_command(self, data, genre): |
| 82 | + if not data: |
| 83 | + return [] |
| 84 | + |
| 85 | + if genre in [ |
| 86 | + Command.SEND_ANGLES, Command.SEND_COORDS, |
| 87 | + Command.IS_IN_POSITION |
| 88 | + ]: |
| 89 | + _data_list = [] |
| 90 | + for value in data[:6]: |
| 91 | + _data_list.extend(self._encode_int16((value))) |
| 92 | + for value in data[6:]: |
| 93 | + _data_list.append((value)) |
| 94 | + return _data_list |
| 95 | + |
| 96 | + elif genre in [Command.SEND_ANGLE, Command.SEND_COORD]: |
| 97 | + return self._flatten( |
| 98 | + [data[0], |
| 99 | + self._flatten(self._encode_int16(data[1])), data[2]]) |
| 100 | + |
| 101 | + elif genre in [ |
| 102 | + Command.JOG_ANGLE, Command.JOG_COORD, Command.SET_COLOR |
| 103 | + ]: |
| 104 | + return data |
| 105 | + |
| 106 | + else: |
| 107 | + return [data[0]] |
| 108 | + |
| 109 | + def _is_frame_header(self, data, pos): |
| 110 | + return data[pos] == Command.HEADER and data[pos + 1] == Command.HEADER |
| 111 | + |
| 112 | + def _process_recived(self, data, genre): |
| 113 | + if not data: |
| 114 | + return [] |
| 115 | + |
| 116 | + data = bytearray(data) |
| 117 | + data_len = len(data) |
| 118 | + for idx in range(data_len - 1): |
| 119 | + if self._is_frame_header(data, idx): |
| 120 | + break |
| 121 | + else: |
| 122 | + return [] |
| 123 | + |
| 124 | + data_len = data[idx + 2] - 2 |
| 125 | + cmd_id = data[idx + 3] |
| 126 | + if cmd_id != genre: |
| 127 | + return [] |
| 128 | + data_pos = idx + 4 |
| 129 | + avild_data = (data[data_pos:data_pos + data_len]) |
| 130 | + |
| 131 | + res = [] |
| 132 | + if data_len == 12: |
| 133 | + for idx in range(0, len(avild_data), 2): |
| 134 | + a = avild_data[idx:idx + 2] |
| 135 | + res.append(self._decode_int16(a)) |
| 136 | + elif data_len == 2: |
| 137 | + res.append(self._decode_int16(avild_data)) |
| 138 | + else: |
| 139 | + res.append(self._decode_int8(avild_data)) |
| 140 | + return res |
| 141 | + |
| 142 | + def _process_bool(self, data): |
| 143 | + return data[0] if data else -1 |
12 | 144 |
|
13 | 145 |
|
14 | | -class Coord(enum.Enum): |
15 | 146 |
|
16 | | - X = 1 |
17 | | - Y = 2 |
18 | | - Z = 3 |
19 | | - Rx = 4 |
20 | | - Ry = 5 |
21 | | - Rz = 6 |
|
0 commit comments