Skip to content

Commit 0afb2d8

Browse files
authored
Merge pull request #99 from elephantrobotics/mycobot_280_refactor
Mycobot 280 refactor
2 parents 540c8a8 + b0359b5 commit 0afb2d8

File tree

8 files changed

+179
-107
lines changed

8 files changed

+179
-107
lines changed

pymycobot/common.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class ProGripper(object):
2222
SET_GRIPPER_PAUSE = 37
2323
SET_GRIPPER_RESUME = 38
2424
SET_GRIPPER_STOP = 39
25+
SET_GRIPPER_ANGLES = 45
2526

2627

2728
class ProtocolCode(object):
@@ -795,6 +796,10 @@ def read(self, genre, method=None, command=None, _class=None, timeout=None):
795796
elif _class in ["MyCobot", "MyCobotSocket", "MyCobot320", "MyCobot320Socket"]:
796797
if genre == ProtocolCode.GET_ROBOT_STATUS:
797798
wait_time = 75
799+
elif genre == ProtocolCode.GET_ROBOT_STATUS:
800+
wait_time = 90
801+
elif genre == ProtocolCode.SET_SSID_PWD or genre == ProtocolCode.GET_SSID_PWD:
802+
wait_time = 0.05
798803
data = b""
799804

800805
if method is not None:

pymycobot/error.py

Lines changed: 74 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -787,41 +787,63 @@ def calibration_parameters(**kwargs):
787787
"The range of 'torque_value' in {} is 100 ~ 300, but the received value is {}".format(parameter,
788788
torque_value))
789789

790-
elif parameter == "gripper_speed":
791-
gripper_id, speed = value
792-
if not isinstance(gripper_id, int) or not isinstance(speed, int):
793-
raise MyCobot320DataException(
794-
"Both 'gripper_id' and 'speed' in {} must be integers".format(parameter))
795-
if gripper_id < 1 or gripper_id > 254:
796-
raise MyCobot320DataException(
797-
"The range of 'gripper_id' in {} is 1 ~ 254, but the received value is {}".format(parameter,
798-
gripper_id))
799-
if speed < 1 or torque_value > 100:
800-
raise MyCobot320DataException(
801-
"The range of 'speed' in {} is 1 ~ 100, but the received value is {}".format(parameter,
802-
speed))
803-
elif parameter == "set_gripper_args":
804-
if len(value) != 3:
805-
raise ValueError(f"Expected 3 arguments, but got {len(value)}")
806-
gripper_id, address, data = value
807-
if not isinstance(gripper_id, int) or not isinstance(address, int) or not isinstance(data, int):
808-
raise MyCobot320DataException(
809-
"All arguments in {} must be integers".format(parameter))
810-
if gripper_id < 1 or gripper_id > 254:
811-
raise MyCobot320DataException(
812-
"The range of 'gripper_id' in {} is 1 ~ 254, but the received value is {}".format(parameter,
813-
gripper_id))
814-
invalid_addresses = [1, 2, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 22, 24, 26, 27, 28, 32, 33,
815-
36,
816-
37, 38, 39, 40, 42, 44]
817-
if address < 1 or address > 44:
790+
elif parameter == "gripper_speed":
791+
gripper_id, speed = value
792+
if not isinstance(gripper_id, int) or not isinstance(speed, int):
793+
raise MyCobot320DataException(
794+
"Both 'gripper_id' and 'speed' in {} must be integers".format(parameter))
795+
if gripper_id < 1 or gripper_id > 254:
796+
raise MyCobot320DataException(
797+
"The range of 'gripper_id' in {} is 1 ~ 254, but the received value is {}".format(parameter,
798+
gripper_id))
799+
if speed < 1 or speed > 100:
800+
raise MyCobot320DataException(
801+
"The range of 'speed' in {} is 1 ~ 100, but the received value is {}".format(parameter,
802+
speed))
803+
elif parameter == "joint_id":
804+
check_value_type(parameter, value_type, MyCobot320DataException, int)
805+
if not 0 <= value <= 6:
806+
raise MyCobot320DataException(
807+
"speed value not right, should be 1 ~ 100, the received speed is %s"
808+
% value
809+
)
810+
elif parameter == "set_gripper_args":
811+
gripper_id, address, data = value
812+
if not isinstance(gripper_id, int) or not isinstance(address, int):
813+
raise MyCobot320DataException(
814+
"All arguments in {} must be integers".format(parameter))
815+
if gripper_id < 1 or gripper_id > 254:
816+
raise MyCobot320DataException(
817+
"The range of 'gripper_id' in {} is 1 ~ 254, but the received value is {}".format(parameter,
818+
gripper_id))
819+
invalid_addresses = [1, 2, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 22, 24, 26, 27, 28, 32, 33,
820+
36,
821+
37, 38, 39, 40, 42, 44]
822+
if address < 1 or address > 48:
823+
raise MyCobot320DataException(
824+
"The range of 'address' in {} is 1 ~ 48, but the received value is {}".format(parameter,
825+
address))
826+
if address in invalid_addresses:
827+
raise MyCobot320DataException(
828+
"'address' in {} cannot be one of the following values: {}, but the received value is {}".format(
829+
parameter, invalid_addresses, address))
830+
print('data:', data)
831+
if isinstance(data, list):
832+
# 处理 data 为列表的情况
833+
if len(data) != 6:
818834
raise MyCobot320DataException(
819-
"The range of 'address' in {} is 1 ~ 44, but the received value is {}".format(parameter,
820-
address))
821-
if address in invalid_addresses:
835+
"data must be a list of length 6。"
836+
)
837+
for index, value in enumerate(data):
838+
if not (0 <= value <= 100):
839+
raise MyCobot320DataException(
840+
"Each value in the data list must be between 0 and 100. Error index {}: The received value is {}".format(index, value)
841+
)
842+
else:
843+
if not isinstance(data, int):
822844
raise MyCobot320DataException(
823-
"'address' in {} cannot be one of the following values: {}, but the received value is {}".format(
824-
parameter, invalid_addresses, address))
845+
"The parameter {} must be an integer".format(data)
846+
)
825847
# 根据 address 来处理 value
826848
if address in [3, 43]:
827849
if data < 1 or data > 254:
@@ -849,24 +871,24 @@ def calibration_parameters(**kwargs):
849871
"Error in parameter '{}': The range of 'value' for address={} is 0 ~ 100, but the received value is {}".format(
850872
parameter, address, data))
851873

852-
elif parameter == "get_gripper_args":
853-
gripper_id, address = value
854-
if not isinstance(gripper_id, int) or not isinstance(address, int):
855-
raise MyCobot320DataException(
856-
"All arguments in {} must be integers".format(parameter))
857-
if gripper_id < 1 or gripper_id > 254:
858-
raise MyCobot320DataException(
859-
"The range of 'gripper_id' in {} is 1 ~ 254, but the received value is {}".format(parameter,
860-
gripper_id))
861-
invalid_addresses = [5, 6, 10, 15, 16, 17, 18, 19, 20, 21, 23, 25, 29, 30, 31, 34, 35, 41, 43]
862-
if address < 1 or address > 44:
863-
raise MyCobot320DataException(
864-
"The range of 'address' in {} is 1 ~ 44, but the received value is {}".format(parameter,
865-
address))
866-
if address in invalid_addresses:
867-
raise MyCobot320DataException(
868-
"'address' in {} cannot be one of the following values: {}, but the received value is {}".format(
869-
parameter, invalid_addresses, address))
874+
elif parameter == "get_gripper_args":
875+
gripper_id, address = value
876+
if not isinstance(gripper_id, int) or not isinstance(address, int):
877+
raise MyCobot320DataException(
878+
"All arguments in {} must be integers".format(parameter))
879+
if gripper_id < 1 or gripper_id > 254:
880+
raise MyCobot320DataException(
881+
"The range of 'gripper_id' in {} is 1 ~ 254, but the received value is {}".format(parameter,
882+
gripper_id))
883+
invalid_addresses = [5, 6, 10, 15, 16, 17, 18, 19, 20, 21, 23, 25, 29, 30, 31, 34, 35, 41, 43]
884+
if address < 1 or address > 44:
885+
raise MyCobot320DataException(
886+
"The range of 'address' in {} is 1 ~ 44, but the received value is {}".format(parameter,
887+
address))
888+
if address in invalid_addresses:
889+
raise MyCobot320DataException(
890+
"'address' in {} cannot be one of the following values: {}, but the received value is {}".format(
891+
parameter, invalid_addresses, address))
870892
elif class_name in ["MechArm", "MechArmSocket"]:
871893
public_check(parameter_list, kwargs, robot_limit, class_name, MechArmDataException)
872894
elif class_name in ["MechArm270", "MechArmSocket"]:
@@ -1154,7 +1176,7 @@ def calibration_parameters(**kwargs):
11541176
elif parameter == 'gripper_type':
11551177
check_0_or_1(parameter, value, [1, 3, 4], value_type, MyPalletizer260DataException, int)
11561178
elif parameter == '_type_1':
1157-
check_0_or_1(parameter, value, [1, 2, 3, 4], value_type, MyPalletizer260DataException, int)
1179+
check_0_or_1(parameter, value, [1, 2, 3, 4, 5], value_type, MyPalletizer260DataException, int)
11581180
# if value not in [0, 1, 10]:
11591181
# raise exception_class("The data supported by parameter {} is 0 or 1 or 10, but the received value is {}".format(parameter, value))
11601182
elif parameter == 'gripper_value':

pymycobot/mecharm270.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,21 @@ def _mesg(self, genre, *args, **kwargs):
131131
return self._res(real_command, has_reply, genre)
132132

133133
def _res(self, real_command, has_reply, genre):
134-
try_count = 0
135-
while try_count < 3:
134+
if genre == ProtocolCode.SET_SSID_PWD or genre == ProtocolCode.GET_SSID_PWD:
136135
self._write(self._flatten(real_command))
137136
data = self._read(genre)
138-
if data is not None and data != b'':
139-
break
140-
try_count += 1
141137
else:
142-
return -1
138+
try_count = 0
139+
while try_count < 3:
140+
self._write(self._flatten(real_command))
141+
data = self._read(genre)
142+
if data is not None and data != b'':
143+
break
144+
try_count += 1
145+
else:
146+
return -1
143147
if genre == ProtocolCode.SET_SSID_PWD:
144-
return None
148+
return 1
145149
res = self._process_received(data, genre)
146150
if res is None:
147151
return None

pymycobot/mecharmsocket.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,21 @@ def _mesg(self, genre, *args, **kwargs):
124124
# [254,...,255]
125125
# data = self._write(self._flatten(real_command), "socket")
126126
with self.lock:
127-
try_count = 0
128-
while try_count < 3:
127+
if genre == ProtocolCode.SET_SSID_PWD or genre == ProtocolCode.GET_SSID_PWD:
129128
self._write(self._flatten(real_command), "socket")
130129
data = self._read(genre, method='socket')
131-
if data is not None and data != b'':
132-
break
133-
try_count += 1
134130
else:
135-
return -1
131+
try_count = 0
132+
while try_count < 3:
133+
self._write(self._flatten(real_command), "socket")
134+
data = self._read(genre, method='socket')
135+
if data is not None and data != b'':
136+
break
137+
try_count += 1
138+
else:
139+
return -1
136140
if genre == ProtocolCode.SET_SSID_PWD:
137-
return None
141+
return 1
138142
res = self._process_received(data, genre)
139143
if res is None:
140144
return None

pymycobot/mycobot280.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,21 @@ def _mesg(self, genre, *args, **kwargs):
135135
return self._res(real_command, has_reply, genre)
136136

137137
def _res(self, real_command, has_reply, genre):
138-
try_count = 0
139-
while try_count < 3:
138+
if genre == ProtocolCode.SET_SSID_PWD or genre == ProtocolCode.GET_SSID_PWD:
140139
self._write(self._flatten(real_command))
141140
data = self._read(genre)
142-
if data is not None and data != b'':
143-
break
144-
try_count += 1
145141
else:
146-
return -1
142+
try_count = 0
143+
while try_count < 3:
144+
self._write(self._flatten(real_command))
145+
data = self._read(genre)
146+
if data is not None and data != b'':
147+
break
148+
try_count += 1
149+
else:
150+
return -1
147151
if genre == ProtocolCode.SET_SSID_PWD:
148-
return None
152+
return 1
149153
res = self._process_received(data, genre)
150154
if res is None:
151155
return None

pymycobot/mycobot280socket.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,21 @@ def _mesg(self, genre, *args, **kwargs):
128128
# data = self._write(self._flatten(real_command), "socket")
129129
# # if has_reply:
130130
# data = self._read(genre, method='socket')
131-
try_count = 0
132-
while try_count < 3:
131+
if genre == ProtocolCode.SET_SSID_PWD or genre == ProtocolCode.GET_SSID_PWD:
133132
self._write(self._flatten(real_command), "socket")
134133
data = self._read(genre, method='socket')
135-
if data is not None and data != b'':
136-
break
137-
try_count += 1
138134
else:
139-
return -1
135+
try_count = 0
136+
while try_count < 3:
137+
self._write(self._flatten(real_command), "socket")
138+
data = self._read(genre, method='socket')
139+
if data is not None and data != b'':
140+
break
141+
try_count += 1
142+
else:
143+
return -1
140144
if genre == ProtocolCode.SET_SSID_PWD:
141-
return None
145+
return 1
142146
res = self._process_received(data, genre)
143147
if res is None:
144148
return None

0 commit comments

Comments
 (0)