Skip to content

Commit 2e9a18e

Browse files
authored
Merge pull request #100 from elephantrobotics/mycobot_280_refactor
Mycobot 280 refactor
2 parents 0e8ba89 + 9f4862a commit 2e9a18e

File tree

6 files changed

+285
-95
lines changed

6 files changed

+285
-95
lines changed

pymycobot/error.py

Lines changed: 52 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -550,9 +550,11 @@ def calibration_parameters(**kwargs):
550550
elif parameter == 'flag':
551551
check_0_or_1(parameter, value, [0, 1, 254], value_type, MyCobot280DataException, int)
552552
elif parameter == 'gripper_type':
553-
check_0_or_1(parameter, value, [1, 3, 4], value_type, MyCobot280DataException, int)
553+
if value is not None:
554+
check_0_or_1(parameter, value, [1, 3, 4, 5], value_type, MyCobot280DataException, int)
554555
elif parameter == '_type_1':
555-
check_0_or_1(parameter, value, [1, 2, 3, 4], value_type, MyCobot280DataException, int)
556+
if value is not None:
557+
check_0_or_1(parameter, value, [1, 2, 3, 4, 5], value_type, MyCobot280DataException, int)
556558
elif parameter == 'gripper_value':
557559
check_value_type(parameter, value_type, MyCobot280DataException, int)
558560
if value < 0 or value > 100:
@@ -636,6 +638,9 @@ def calibration_parameters(**kwargs):
636638
value))
637639
elif parameter == 'end_direction':
638640
check_0_or_1(parameter, value, [1, 2, 3], value_type, MyCobot280DataException, int)
641+
elif parameter == 'is_torque':
642+
if value is not None:
643+
check_0_or_1(parameter, value, [0, 1], value_type, MyCobot280DataException, int)
639644
elif class_name in ["MyCobot320", "MyCobot320Socket"]:
640645
for parameter in parameter_list[1:]:
641646
value = kwargs.get(parameter, None)
@@ -800,76 +805,54 @@ def calibration_parameters(**kwargs):
800805
raise MyCobot320DataException(
801806
"The range of 'speed' in {} is 1 ~ 100, but the received value is {}".format(parameter,
802807
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-
)
808+
810809
elif parameter == "set_gripper_args":
810+
if len(value) != 3:
811+
raise ValueError(f"Expected 3 arguments, but got {len(value)}")
811812
gripper_id, address, data = value
812-
if not isinstance(gripper_id, int) or not isinstance(address, int):
813+
if not isinstance(gripper_id, int) or not isinstance(address, int) or not isinstance(data, int):
813814
raise MyCobot320DataException(
814815
"All arguments in {} must be integers".format(parameter))
815816
if gripper_id < 1 or gripper_id > 254:
816817
raise MyCobot320DataException(
817818
"The range of 'gripper_id' in {} is 1 ~ 254, but the received value is {}".format(parameter,
818819
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:
820+
invalid_addresses = [1, 2, 4, 5, 6, 7, 8, 9, 12, 14, 15, 16, 17, 18, 19, 20, 22, 24, 26, 28, 33, 34, 35,
821+
40, 42, 44]
822+
if address < 1 or address > 44:
823823
raise MyCobot320DataException(
824-
"The range of 'address' in {} is 1 ~ 48, but the received value is {}".format(parameter,
824+
"The range of 'address' in {} is 1 ~ 44, but the received value is {}".format(parameter,
825825
address))
826826
if address in invalid_addresses:
827827
raise MyCobot320DataException(
828828
"'address' in {} cannot be one of the following values: {}, but the received value is {}".format(
829829
parameter, invalid_addresses, address))
830-
print('data:', data)
831-
if isinstance(data, list):
832-
# 处理 data 为列表的情况
833-
if len(data) != 6:
830+
# 根据 address 来处理 value
831+
if address in [3, 43]:
832+
if data < 1 or data > 254:
834833
raise MyCobot320DataException(
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):
834+
"Error in parameter '{}': The range of 'value' for address={} is 1 ~ 254, but the received value is {}".format(
835+
parameter, address, data))
836+
elif address == 10:
837+
if data not in [0, 1]:
844838
raise MyCobot320DataException(
845-
"The parameter {} must be an integer".format(data)
846-
)
847-
# 根据 address 来处理 value
848-
if address in [3, 43]:
849-
if data < 1 or data > 254:
850-
raise MyCobot320DataException(
851-
"Error in parameter '{}': The range of 'value' for address={} is 1 ~ 254, but the received value is {}".format(
852-
parameter, address, data))
853-
elif address == 10:
854-
if data not in [0, 1]:
855-
raise MyCobot320DataException(
856-
"Error in parameter '{}': Value for address={} must be 0 or 1, but the received value is {}".format(
857-
parameter, address, data))
858-
elif address in [25]:
859-
if data < 0 or data > 254:
860-
raise MyCobot320DataException(
861-
"Error in parameter '{}': The range of 'value' for address={} is 0 ~ 254, but the received value is {}".format(
862-
parameter, address, data))
863-
elif address in [21, 23]:
864-
if data < 0 or data > 16:
865-
raise MyCobot320DataException(
866-
"Error in parameter '{}': The range of 'value' for address={} is 0 ~ 16, but the received value is {}".format(
867-
parameter, address, data))
868-
elif address == 41:
869-
if data < 0 or data > 100:
870-
raise MyCobot320DataException(
871-
"Error in parameter '{}': The range of 'value' for address={} is 0 ~ 100, but the received value is {}".format(
872-
parameter, address, data))
839+
"Error in parameter '{}': Value for address={} must be 0 or 1, but the received value is {}".format(
840+
parameter, address, data))
841+
elif address in [25]:
842+
if data < 0 or data > 254:
843+
raise MyCobot320DataException(
844+
"Error in parameter '{}': The range of 'value' for address={} is 0 ~ 254, but the received value is {}".format(
845+
parameter, address, data))
846+
elif address in [21, 23]:
847+
if data < 0 or data > 16:
848+
raise MyCobot320DataException(
849+
"Error in parameter '{}': The range of 'value' for address={} is 0 ~ 16, but the received value is {}".format(
850+
parameter, address, data))
851+
elif address == 41:
852+
if data < 0 or data > 100:
853+
raise MyCobot320DataException(
854+
"Error in parameter '{}': The range of 'value' for address={} is 0 ~ 100, but the received value is {}".format(
855+
parameter, address, data))
873856

874857
elif parameter == "get_gripper_args":
875858
gripper_id, address = value
@@ -880,7 +863,8 @@ def calibration_parameters(**kwargs):
880863
raise MyCobot320DataException(
881864
"The range of 'gripper_id' in {} is 1 ~ 254, but the received value is {}".format(parameter,
882865
gripper_id))
883-
invalid_addresses = [5, 6, 10, 15, 16, 17, 18, 19, 20, 21, 23, 25, 29, 30, 31, 34, 35, 41, 43]
866+
invalid_addresses = [3, 5, 6, 10, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 30, 31, 32, 36, 37,
867+
38, 39, 41, 43]
884868
if address < 1 or address > 44:
885869
raise MyCobot320DataException(
886870
"The range of 'address' in {} is 1 ~ 44, but the received value is {}".format(parameter,
@@ -921,11 +905,14 @@ def calibration_parameters(**kwargs):
921905
% value
922906
)
923907
elif parameter == 'flag':
924-
check_0_or_1(parameter, value, [0, 1, 254], value_type, MechArmDataException, int)
908+
if value is not None:
909+
check_0_or_1(parameter, value, [0, 1, 254], value_type, MechArmDataException, int)
925910
elif parameter == 'gripper_type':
926-
check_0_or_1(parameter, value, [1, 3, 4], value_type, MechArmDataException, int)
911+
if value is not None:
912+
check_0_or_1(parameter, value, [1, 3, 4, 5], value_type, MechArmDataException, int)
927913
elif parameter == '_type_1':
928-
check_0_or_1(parameter, value, [1, 2, 3, 4], value_type, MechArmDataException, int)
914+
if value is not None:
915+
check_0_or_1(parameter, value, [1, 2, 3, 4, 5], value_type, MechArmDataException, int)
929916
# if value not in [0, 1, 10]:
930917
# raise exception_class("The data supported by parameter {} is 0 or 1 or 10, but the received value is {}".format(parameter, value))
931918
elif parameter == 'gripper_value':
@@ -1010,6 +997,9 @@ def calibration_parameters(**kwargs):
1010997
value))
1011998
elif parameter == 'end_direction':
1012999
check_0_or_1(parameter, value, [1, 2, 3], value_type, MechArmDataException, int)
1000+
elif parameter == 'is_torque':
1001+
if value is not None:
1002+
check_0_or_1(parameter, value, [0, 1], value_type, MyCobot280DataException, int)
10131003
elif class_name in ["MyArm", "MyArmSocket"]:
10141004
public_check(parameter_list, kwargs, robot_limit, class_name, MyArmDataException)
10151005
for parameter in parameter_list[1:]:
@@ -1269,7 +1259,8 @@ def calibration_parameters(**kwargs):
12691259
elif parameter in ['servo_id_pdi', 'encode_id']:
12701260
check_value_type(parameter, value_type, MyPalletizer260DataException, int)
12711261
if value < 1 or (value > 4 and value != 7):
1272-
raise MyPalletizer260DataException("The range of id is 1 ~ 4 or 7, but the received is {}".format(value))
1262+
raise MyPalletizer260DataException(
1263+
"The range of id is 1 ~ 4 or 7, but the received is {}".format(value))
12731264
elif parameter == "torque":
12741265
torque_min = 150
12751266
torque_max = 980

pymycobot/mecharm270.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,61 @@ def set_vision_mode(self, flag):
674674
self.calibration_parameters(class_name=self.__class__.__name__, flag=flag)
675675
return self._mesg(ProtocolCode.SET_VISION_MODE, flag)
676676

677+
def is_torque_gripper(self):
678+
"""Whether it is a force-controlled gripper
679+
680+
Return:
681+
1 - Force control
682+
0 - Non-force control
683+
"""
684+
return self.get_servo_data(7, 1)
685+
686+
def set_gripper_state(self, flag, speed, _type_1=None, is_torque=None):
687+
"""Set gripper switch state
688+
689+
Args:
690+
flag (int): 0 - open, 1 - close, 254 - release
691+
speed (int): 1 ~ 100
692+
_type_1 (int): default 1
693+
1 : Adaptive gripper. default to adaptive gripper
694+
2 : 5 finger dexterous hand
695+
3 : Parallel gripper, this parameter can be omitted
696+
4 : Flexible gripper
697+
is_torque (int): When there is no type parameter, this parameter can be omitted.
698+
1: Force control
699+
0: Non-force control
700+
"""
701+
self.calibration_parameters(class_name=self.__class__.__name__, flag=flag, speed=speed, _type_1=_type_1, is_torque=is_torque)
702+
args = [flag, speed]
703+
if _type_1 is not None:
704+
args.append(_type_1)
705+
if is_torque is not None:
706+
args.append(is_torque)
707+
return self._mesg(ProtocolCode.SET_GRIPPER_STATE, *args)
708+
709+
def set_gripper_value(self, gripper_value, speed, gripper_type=None, is_torque=None):
710+
"""Set gripper value
711+
712+
Args:
713+
gripper_value (int): 0 ~ 100
714+
speed (int): 1 ~ 100
715+
gripper_type (int): default 1
716+
1: Adaptive gripper
717+
3: Parallel gripper, this parameter can be omitted
718+
4: Flexible gripper
719+
is_torque (int): When there is no type parameter, this parameter can be omitted.
720+
1: Force control
721+
0: Non-force control
722+
"""
723+
self.calibration_parameters(class_name=self.__class__.__name__, gripper_value=gripper_value, speed=speed,
724+
gripper_type=gripper_type, is_torque=is_torque)
725+
args = [gripper_value, speed]
726+
if gripper_type is not None:
727+
args.append(gripper_type)
728+
if is_torque is not None:
729+
args.append(is_torque)
730+
return self._mesg(ProtocolCode.SET_GRIPPER_VALUE, *args, has_reply=True)
731+
677732
# Other
678733
def wait(self, t):
679734
time.sleep(t)

pymycobot/mecharmsocket.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,61 @@ def set_vision_mode(self, flag):
689689
self.calibration_parameters(class_name=self.__class__.__name__, flag=flag)
690690
return self._mesg(ProtocolCode.SET_VISION_MODE, flag)
691691

692+
def is_torque_gripper(self):
693+
"""Whether it is a force-controlled gripper
694+
695+
Return:
696+
1 - Force control
697+
0 - Non-force control
698+
"""
699+
return self.get_servo_data(7, 1)
700+
701+
def set_gripper_state(self, flag, speed, _type_1=None, is_torque=None):
702+
"""Set gripper switch state
703+
704+
Args:
705+
flag (int): 0 - open, 1 - close, 254 - release
706+
speed (int): 1 ~ 100
707+
_type_1 (int): default 1
708+
1 : Adaptive gripper. default to adaptive gripper
709+
2 : 5 finger dexterous hand
710+
3 : Parallel gripper, this parameter can be omitted
711+
4 : Flexible gripper
712+
is_torque (int): When there is no type parameter, this parameter can be omitted.
713+
1: Force control
714+
0: Non-force control
715+
"""
716+
self.calibration_parameters(class_name=self.__class__.__name__, flag=flag, speed=speed, _type_1=_type_1, is_torque=is_torque)
717+
args = [flag, speed]
718+
if _type_1 is not None:
719+
args.append(_type_1)
720+
if is_torque is not None:
721+
args.append(is_torque)
722+
return self._mesg(ProtocolCode.SET_GRIPPER_STATE, *args)
723+
724+
def set_gripper_value(self, gripper_value, speed, gripper_type=None, is_torque=None):
725+
"""Set gripper value
726+
727+
Args:
728+
gripper_value (int): 0 ~ 100
729+
speed (int): 1 ~ 100
730+
gripper_type (int): default 1
731+
1: Adaptive gripper
732+
3: Parallel gripper, this parameter can be omitted
733+
4: Flexible gripper
734+
is_torque (int): When there is no type parameter, this parameter can be omitted.
735+
1: Force control
736+
0: Non-force control
737+
"""
738+
self.calibration_parameters(class_name=self.__class__.__name__, gripper_value=gripper_value, speed=speed,
739+
gripper_type=gripper_type, is_torque=is_torque)
740+
args = [gripper_value, speed]
741+
if gripper_type is not None:
742+
args.append(gripper_type)
743+
if is_torque is not None:
744+
args.append(is_torque)
745+
return self._mesg(ProtocolCode.SET_GRIPPER_VALUE, *args, has_reply=True)
746+
692747
# Other
693748
def wait(self, t):
694749
time.sleep(t)

pymycobot/mycobot280.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,61 @@ def set_vision_mode(self, flag):
707707
self.calibration_parameters(class_name=self.__class__.__name__, flag=flag)
708708
return self._mesg(ProtocolCode.SET_VISION_MODE, flag)
709709

710+
def is_torque_gripper(self):
711+
"""Whether it is a force-controlled gripper
712+
713+
Return:
714+
40 - Force control
715+
9 - Non-force control
716+
"""
717+
return self.get_servo_data(7, 1)
718+
719+
def set_gripper_state(self, flag, speed, _type_1=None, is_torque=None):
720+
"""Set gripper switch state
721+
722+
Args:
723+
flag (int): 0 - open, 1 - close, 254 - release
724+
speed (int): 1 ~ 100
725+
_type_1 (int): default 1
726+
1 : Adaptive gripper. default to adaptive gripper
727+
2 : 5 finger dexterous hand
728+
3 : Parallel gripper, this parameter can be omitted
729+
4 : Flexible gripper
730+
is_torque (int): When there is no type parameter, this parameter can be omitted.
731+
1: Force control
732+
0: Non-force control
733+
"""
734+
self.calibration_parameters(class_name=self.__class__.__name__, flag=flag, speed=speed, _type_1=_type_1, is_torque=is_torque)
735+
args = [flag, speed]
736+
if _type_1 is not None:
737+
args.append(_type_1)
738+
if is_torque is not None:
739+
args.append(is_torque)
740+
return self._mesg(ProtocolCode.SET_GRIPPER_STATE, *args)
741+
742+
def set_gripper_value(self, gripper_value, speed, gripper_type=None, is_torque=None):
743+
"""Set gripper value
744+
745+
Args:
746+
gripper_value (int): 0 ~ 100
747+
speed (int): 1 ~ 100
748+
gripper_type (int): default 1
749+
1: Adaptive gripper
750+
3: Parallel gripper, this parameter can be omitted
751+
4: Flexible gripper
752+
is_torque (int): When there is no type parameter, this parameter can be omitted.
753+
1: Force control
754+
0: Non-force control
755+
"""
756+
self.calibration_parameters(class_name=self.__class__.__name__, gripper_value=gripper_value, speed=speed,
757+
gripper_type=gripper_type, is_torque=is_torque)
758+
args = [gripper_value, speed]
759+
if gripper_type is not None:
760+
args.append(gripper_type)
761+
if is_torque is not None:
762+
args.append(is_torque)
763+
return self._mesg(ProtocolCode.SET_GRIPPER_VALUE, *args, has_reply=True)
764+
710765
# Other
711766
def wait(self, t):
712767
time.sleep(t)

0 commit comments

Comments
 (0)