Skip to content

Commit 37a0078

Browse files
committed
add myarmm and myarmc class api
1 parent 73dc169 commit 37a0078

File tree

6 files changed

+676
-59
lines changed

6 files changed

+676
-59
lines changed

pymycobot/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
from pymycobot.mercurychassis import MercuryChassis
2727
from pymycobot.mercurysocket import MercurySocket
2828
from pymycobot.mycobotpro630 import Phoenix
29-
29+
from pymycobot.myarmm import MyArmM
30+
from pymycobot.myarmc import MyArmC
3031
__all__ = [
3132
"MyCobot",
3233
"CommandGenerator",
@@ -50,7 +51,9 @@
5051
"MyArmSocket",
5152
"MercuryChassis",
5253
"MercurySocket",
53-
"Phoenix"
54+
"Phoenix",
55+
"MyArmM",
56+
"MyArmC"
5457
]
5558

5659

pymycobot/common.py

Lines changed: 85 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,31 @@ class ProtocolCode(object):
216216
MERCURY_DRAG_TECH_PAUSE = 0x72
217217
MERCURY_DRAG_TEACH_CLEAN = 0x73
218218

219+
GET_ROBOT_MODIFIED_VERSION = 1
220+
GET_ROBOT_FIRMWARE_VERSION = 2
221+
GET_ROBOT_AUXILIARY_FIRMWARE_VERSION = 3
222+
GET_ROBOT_ATOM_MODIFIED_VERSION = 4
223+
GET_ROBOT_TOOL_FIRMWARE_VERSION = 9
224+
GET_ROBOT_SERIAL_NUMBER = 5
225+
SET_ROBOT_ERROR_CHECK_STATE = 6
226+
GET_ROBOT_ERROR_CHECK_STATE = 7
227+
GET_ROBOT_ERROR_STATUS = 0x15
228+
GET_ATOM_PRESS_STATUS = 0x6b
229+
GET_ATOM_LED_COLOR = 0x6a
230+
SET_ATOM_PIN_STATUS = 0x61
231+
GET_ATOM_PIN_STATUS = 0x62
232+
SET_MASTER_PIN_STATUS = 0x65
233+
GET_MASTER_PIN_STATUS = 0x66
234+
SET_AUXILIARY_PIN_STATUS = 0xa0
235+
GET_AUXILIARY_PIN_STATUS = 0xa1
236+
SET_SERVO_MOTOR_CLOCKWISE = 0x73
237+
GET_SERVO_MOTOR_CLOCKWISE = 0Xea
238+
SET_SERVO_MOTOR_COUNTER_CLOCKWISE = 0x74
239+
GET_SERVO_MOTOR_COUNTER_CLOCKWISE = 0xeb
240+
SET_SERVO_MOTOR_CONFIG = 0x52
241+
GET_SERVO_MOTOR_CONFIG = 0x53
242+
CLEAR_RECV_QUEUE = 0x19
243+
GET_RECV_QUEUE_LENGTH = 0x08
219244
GET_BASE_COORDS = 0xF0
220245
BASE_TO_SINGLE_COORDS = 0xF1
221246
COLLISION = 0xF2
@@ -226,6 +251,9 @@ class ProtocolCode(object):
226251
JOG_INC_COORD = 0xF7
227252
COLLISION_SWITCH = 0xF8
228253
IS_COLLISION_ON = 0xF9
254+
CLEAR_ROBOT_ERROR = 0x16
255+
GET_RECV_QUEUE_SIZE = 0x17
256+
SET_RECV_QUEUE_SIZE = 0x18
229257

230258
# IIC
231259
# SET_IIC_STATE = 0xA4
@@ -265,6 +293,41 @@ class ProtocolCode(object):
265293

266294

267295
class DataProcessor(object):
296+
def _mesg(self, genre, *args, **kwargs):
297+
"""
298+
Args:
299+
genre: command type (Command)
300+
*args: other data.
301+
It is converted to octal by default.
302+
If the data needs to be encapsulated into hexadecimal,
303+
the array is used to include them. (Data cannot be nested)
304+
**kwargs: support `has_reply`
305+
has_reply: Whether there is a return value to accept.
306+
"""
307+
command_data = self._process_data_command(genre, self.__class__.__name__, args)
308+
if genre == 178:
309+
command_data = self._encode_int16(command_data)
310+
elif genre in [76, 77]:
311+
command_data = [command_data[0]] + self._encode_int16(command_data[1] * 10)
312+
elif genre == 115 and self.__class__.__name__ != "MyArmM":
313+
command_data = [command_data[1], command_data[3]]
314+
LEN = len(command_data) + 2
315+
command = [
316+
ProtocolCode.HEADER,
317+
ProtocolCode.HEADER,
318+
LEN,
319+
genre,
320+
]
321+
if command_data:
322+
command.extend(command_data)
323+
if self.__class__.__name__ in ["Mercury", "MercurySocket"]:
324+
command[2] += 1
325+
command.extend(self.crc_check(command))
326+
else:
327+
command.append(ProtocolCode.FOOTER)
328+
real_command = self._flatten(command)
329+
has_reply = kwargs.get("has_reply", False)
330+
return real_command, has_reply
268331
# Functional approach
269332
def _encode_int8(self, data):
270333
return struct.pack("b", data)
@@ -346,7 +409,7 @@ def _process_data_command(self, genre, _class, args):
346409
byte_value = data.to_bytes(4, byteorder='big', signed=True)
347410
res = []
348411
for i in range(len(byte_value)):
349-
res.append(byte_value[i])
412+
res.append(byte_value[i])
350413
processed_args.extend(res)
351414
else:
352415
processed_args.extend(self._encode_int16(args[index]))
@@ -358,7 +421,7 @@ def _process_data_command(self, genre, _class, args):
358421
byte_value = args[index].to_bytes(2, byteorder='big', signed=True)
359422
res = []
360423
for i in range(len(byte_value)):
361-
res.append(byte_value[i])
424+
res.append(byte_value[i])
362425
processed_args.extend(res)
363426
else:
364427
processed_args.append(args[index])
@@ -382,7 +445,7 @@ def _process_received(self, data, genre, arm=6):
382445
header_i, header_j = 0, 1
383446
while header_j < data_len - 4:
384447
if self._is_frame_header(data, header_i, header_j):
385-
if arm in [6, 7, 14]:
448+
if arm in [6, 7, 14, 8]:
386449
cmd_id = data[header_i + 3]
387450
elif arm == 12:
388451
cmd_id = data[header_i + 4]
@@ -393,7 +456,7 @@ def _process_received(self, data, genre, arm=6):
393456
header_j += 1
394457
else:
395458
return []
396-
if arm in [6, 7]:
459+
if arm in [6, 7, 8]:
397460
data_len = data[header_i + 2] - 2
398461
elif arm == 12:
399462
data_len = data[header_i + 3] - 2
@@ -410,7 +473,7 @@ def _process_received(self, data, genre, arm=6):
410473
data_pos = header_i + 5
411474
data_len -= 1
412475
else:
413-
if arm in [6, 7, 14]:
476+
if arm in [6, 7, 14, 8]:
414477
data_pos = header_i + 4
415478
elif arm == 12:
416479
data_pos = header_i + 5
@@ -423,7 +486,17 @@ def _process_received(self, data, genre, arm=6):
423486
# res.append(i)
424487
# return res
425488
if data_len in [6, 8, 12, 14, 16, 24, 26, 60]:
426-
if data_len == 8 and arm == 14 and cmd_id == ProtocolCode.IS_INIT_CALIBRATION:
489+
ignor_t = (
490+
ProtocolCode.GET_SERVO_CURRENTS,
491+
ProtocolCode.GET_SERVO_TEMPS,
492+
ProtocolCode.GET_SERVO_VOLTAGES,
493+
ProtocolCode.GET_SERVO_STATUS,
494+
ProtocolCode.IS_ALL_SERVO_ENABLE
495+
)
496+
if data_len == 8 and (
497+
(arm == 14 and cmd_id == ProtocolCode.IS_INIT_CALIBRATION) or
498+
(arm == 8 and cmd_id in ignor_t)
499+
):
427500
for v in valid_data:
428501
res.append(v)
429502
return res
@@ -572,21 +645,24 @@ def write(self, command, method=None):
572645
self._serial_port.flush()
573646

574647

575-
def read(self, genre, method=None, command=None, _class=None):
648+
def read(self, genre, method=None, command=None, _class=None, timeout=None):
576649
datas = b""
577650
data_len = -1
578651
k = 0
579652
pre = 0
580653
t = time.time()
581654
wait_time = 0.1
582655
if method is not None:
583-
wait_time = 0.3
656+
wait_time = 0.3
584657
if genre == ProtocolCode.GO_ZERO:
585658
wait_time = 120
659+
if timeout is not None:
660+
wait_time = timeout
586661
if _class in ["Mercury", "MercurySocket"]:
587662
if genre == ProtocolCode.POWER_ON:
588663
wait_time = 8
589-
elif genre in [ProtocolCode.POWER_OFF, ProtocolCode.RELEASE_ALL_SERVOS, ProtocolCode.FOCUS_ALL_SERVOS, ProtocolCode.RELEASE_SERVO, ProtocolCode.FOCUS_SERVO, ProtocolCode.STOP]:
664+
elif genre in [ProtocolCode.POWER_OFF, ProtocolCode.RELEASE_ALL_SERVOS, ProtocolCode.FOCUS_ALL_SERVOS,
665+
ProtocolCode.RELEASE_SERVO, ProtocolCode.FOCUS_SERVO, ProtocolCode.STOP]:
590666
wait_time = 3
591667
if method is not None:
592668
if genre == 177:

pymycobot/generate.py

Lines changed: 10 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -94,47 +94,6 @@ def __init__(self, debug=False):
9494
self.log = logging.getLogger(__name__)
9595
self.calibration_parameters = calibration_parameters
9696

97-
def _mesg(self, genre, *args, **kwargs):
98-
"""
99-
Args:
100-
genre: command type (Command)
101-
*args: other data.
102-
It is converted to octal by default.
103-
If the data needs to be encapsulated into hexadecimal,
104-
the array is used to include them. (Data cannot be nested)
105-
**kwargs: support `has_reply`
106-
has_reply: Whether there is a return value to accept.
107-
"""
108-
command_data = self._process_data_command(genre, self.__class__.__name__, args)
109-
110-
if genre == 178:
111-
# 修改wifi端口
112-
command_data = self._encode_int16(command_data)
113-
114-
elif genre in [76, 77]:
115-
command_data = [command_data[0]] + self._encode_int16(command_data[1]*10)
116-
elif genre == 115:
117-
command_data = [command_data[1],command_data[3]]
118-
LEN = len(command_data) + 2
119-
120-
command = [
121-
ProtocolCode.HEADER,
122-
ProtocolCode.HEADER,
123-
LEN,
124-
genre,
125-
]
126-
if command_data:
127-
command.extend(command_data)
128-
if self.__class__.__name__ in ["Mercury", "MercurySocket"]:
129-
command[2] += 1
130-
command.extend(self.crc_check(command))
131-
else:
132-
command.append(ProtocolCode.FOOTER)
133-
134-
real_command = self._flatten(command)
135-
has_reply = kwargs.get("has_reply", False)
136-
return real_command, has_reply
137-
13897
# System status
13998
def get_robot_version(self): # TODO: test method <2021-03-11, yourname> #
14099
"""Get cobot version
@@ -144,15 +103,15 @@ def get_robot_version(self): # TODO: test method <2021-03-11, yourname> #
144103
mycobotPro: 101
145104
"""
146105
return self._mesg(ProtocolCode.ROBOT_VERSION, has_reply=True)
147-
106+
148107
def get_system_version(self):
149108
"""get system version"""
150-
return self._mesg(ProtocolCode.SOFTWARE_VERSION, has_reply = True)
109+
return self._mesg(ProtocolCode.SOFTWARE_VERSION, has_reply=True)
151110

152111
def get_robot_id(self):
153112
"""get robot id"""
154-
return self._mesg(ProtocolCode.GET_ROBOT_ID, has_reply = True)
155-
113+
return self._mesg(ProtocolCode.GET_ROBOT_ID, has_reply=True)
114+
156115
def set_robot_id(self, id):
157116
"""set robot id
158117
@@ -449,7 +408,8 @@ def set_encoder(self, joint_id, encoder, speed):
449408
encoder: The value of the set encoder.
450409
speed : 1 - 100
451410
"""
452-
self.calibration_parameters(class_name = self.__class__.__name__, encode_id = joint_id, encoder = encoder, speed=speed)
411+
self.calibration_parameters(class_name=self.__class__.__name__, encode_id=joint_id, encoder=encoder,
412+
speed=speed)
453413
return self._mesg(ProtocolCode.SET_ENCODER, joint_id, [encoder], speed)
454414

455415
def get_encoder(self, joint_id):
@@ -585,7 +545,8 @@ def set_servo_data(self, servo_id, data_id, value, mode=None):
585545
self.calibration_parameters(class_name = self.__class__.__name__, id=servo_id, address=data_id, value=value)
586546
return self._mesg(ProtocolCode.SET_SERVO_DATA, servo_id, data_id, value)
587547
else:
588-
self.calibration_parameters(class_name = self.__class__.__name__, id=servo_id, address=data_id, value=value, mode=mode)
548+
self.calibration_parameters(class_name=self.__class__.__name__, id=servo_id, address=data_id, value=value,
549+
mode=mode)
589550
return self._mesg(ProtocolCode.SET_SERVO_DATA, servo_id, data_id, [value], mode)
590551

591552
def get_servo_data(self, servo_id, data_id, mode=None):
@@ -770,7 +731,8 @@ def set_gripper_value(self, gripper_value, speed, gripper_type=None):
770731
4: Flexible gripper
771732
"""
772733
if gripper_type is not None:
773-
self.calibration_parameters(class_name = self.__class__.__name__, gripper_value=gripper_value, speed=speed, gripper_type=gripper_type)
734+
self.calibration_parameters(class_name=self.__class__.__name__, gripper_value=gripper_value, speed=speed,
735+
gripper_type=gripper_type)
774736
return self._mesg(ProtocolCode.SET_GRIPPER_VALUE, gripper_value, speed, gripper_type)
775737
else:
776738
return self._mesg(ProtocolCode.SET_GRIPPER_VALUE, gripper_value, speed)

0 commit comments

Comments
 (0)