Skip to content

Commit 61cba7c

Browse files
committed
优化320 力控夹爪接口-与三指区分
1 parent b1cadc6 commit 61cba7c

File tree

2 files changed

+49
-91
lines changed

2 files changed

+49
-91
lines changed

pymycobot/error.py

Lines changed: 36 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,6 @@ def calibration_parameters(**kwargs):
761761
check_0_or_1(parameter, value, [1, 2, 3], value_type, MyCobot320DataException, int)
762762
elif parameter == "gripper_angle":
763763
gripper_id, gripper_angle = value
764-
print(type(gripper_id), type(gripper_angle))
765764
if not isinstance(gripper_id, int) or not isinstance(gripper_angle, int):
766765
raise MyCobot320DataException(
767766
"Both 'gripper_id' and 'gripper_angle' in {} must be integers".format(parameter))
@@ -806,76 +805,54 @@ def calibration_parameters(**kwargs):
806805
raise MyCobot320DataException(
807806
"The range of 'speed' in {} is 1 ~ 100, but the received value is {}".format(parameter,
808807
speed))
809-
elif parameter == "joint_id":
810-
check_value_type(parameter, value_type, MyCobot320DataException, int)
811-
if not 0 <= value <= 6:
812-
raise MyCobot320DataException(
813-
"speed value not right, should be 1 ~ 100, the received speed is %s"
814-
% value
815-
)
808+
816809
elif parameter == "set_gripper_args":
810+
if len(value) != 3:
811+
raise ValueError(f"Expected 3 arguments, but got {len(value)}")
817812
gripper_id, address, data = value
818-
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):
819814
raise MyCobot320DataException(
820815
"All arguments in {} must be integers".format(parameter))
821816
if gripper_id < 1 or gripper_id > 254:
822817
raise MyCobot320DataException(
823818
"The range of 'gripper_id' in {} is 1 ~ 254, but the received value is {}".format(parameter,
824819
gripper_id))
825-
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,
826-
36,
827-
37, 38, 39, 40, 42, 44]
828-
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:
829823
raise MyCobot320DataException(
830-
"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,
831825
address))
832826
if address in invalid_addresses:
833827
raise MyCobot320DataException(
834828
"'address' in {} cannot be one of the following values: {}, but the received value is {}".format(
835829
parameter, invalid_addresses, address))
836-
print('data:', data)
837-
if isinstance(data, list):
838-
# 处理 data 为列表的情况
839-
if len(data) != 6:
830+
# 根据 address 来处理 value
831+
if address in [3, 43]:
832+
if data < 1 or data > 254:
840833
raise MyCobot320DataException(
841-
"data must be a list of length 6。"
842-
)
843-
for index, value in enumerate(data):
844-
if not (0 <= value <= 100):
845-
raise MyCobot320DataException(
846-
"Each value in the data list must be between 0 and 100. Error index {}: The received value is {}".format(index, value)
847-
)
848-
else:
849-
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]:
850838
raise MyCobot320DataException(
851-
"The parameter {} must be an integer".format(data)
852-
)
853-
# 根据 address 来处理 value
854-
if address in [3, 43]:
855-
if data < 1 or data > 254:
856-
raise MyCobot320DataException(
857-
"Error in parameter '{}': The range of 'value' for address={} is 1 ~ 254, but the received value is {}".format(
858-
parameter, address, data))
859-
elif address == 10:
860-
if data not in [0, 1]:
861-
raise MyCobot320DataException(
862-
"Error in parameter '{}': Value for address={} must be 0 or 1, but the received value is {}".format(
863-
parameter, address, data))
864-
elif address in [25]:
865-
if data < 0 or data > 254:
866-
raise MyCobot320DataException(
867-
"Error in parameter '{}': The range of 'value' for address={} is 0 ~ 254, but the received value is {}".format(
868-
parameter, address, data))
869-
elif address in [21, 23]:
870-
if data < 0 or data > 16:
871-
raise MyCobot320DataException(
872-
"Error in parameter '{}': The range of 'value' for address={} is 0 ~ 16, but the received value is {}".format(
873-
parameter, address, data))
874-
elif address == 41:
875-
if data < 0 or data > 100:
876-
raise MyCobot320DataException(
877-
"Error in parameter '{}': The range of 'value' for address={} is 0 ~ 100, but the received value is {}".format(
878-
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))
879856

880857
elif parameter == "get_gripper_args":
881858
gripper_id, address = value
@@ -886,7 +863,8 @@ def calibration_parameters(**kwargs):
886863
raise MyCobot320DataException(
887864
"The range of 'gripper_id' in {} is 1 ~ 254, but the received value is {}".format(parameter,
888865
gripper_id))
889-
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]
890868
if address < 1 or address > 44:
891869
raise MyCobot320DataException(
892870
"The range of 'address' in {} is 1 ~ 44, but the received value is {}".format(parameter,
@@ -1281,7 +1259,8 @@ def calibration_parameters(**kwargs):
12811259
elif parameter in ['servo_id_pdi', 'encode_id']:
12821260
check_value_type(parameter, value_type, MyPalletizer260DataException, int)
12831261
if value < 1 or (value > 4 and value != 7):
1284-
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))
12851264
elif parameter == "torque":
12861265
torque_min = 150
12871266
torque_max = 980

pymycobot/mycobot320.py

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -582,67 +582,46 @@ def get_end_type(self):
582582
"""
583583
return self._mesg(ProtocolCode.GET_END_TYPE, has_reply=True)
584584

585-
# Force Control Gripper and myHand Gripper Control
586-
def set_pro_gripper(self, gripper_id, address, joint_id=0, value=None, speed=None):
585+
# Force Control Gripper
586+
def set_pro_gripper(self, gripper_id, address, value):
587587
"""Setting the force-controlled gripper parameters
588588
589589
Args:
590590
gripper_id (int): 1 ~ 254
591591
address (int): Corresponding to the command sequence number in the force-controlled gripper protocol
592592
value : Parameters in the force-controlled gripper protocol
593593
"""
594-
if value is None:
595-
# self.calibration_parameters(class_name=self.__class__.__name__,
596-
# set_gripper_args=[gripper_id, address], joint_id=joint_id)
597-
return self._mesg(ProtocolCode.SET_TOQUE_GRIPPER, gripper_id, [address], [joint_id])
598-
elif speed:
599-
self.calibration_parameters(class_name=self.__class__.__name__,
600-
set_gripper_args=[gripper_id, address, value], joint_id=joint_id, speed=speed)
601-
return self._mesg(ProtocolCode.SET_TOQUE_GRIPPER, gripper_id, [address], [joint_id], [value], [speed])
602-
else:
603-
self.calibration_parameters(class_name=self.__class__.__name__,
604-
set_gripper_args=[gripper_id, address, value], joint_id=joint_id)
605-
return self._mesg(ProtocolCode.SET_TOQUE_GRIPPER, gripper_id, [address], [joint_id], [value])
594+
self.calibration_parameters(class_name=self.__class__.__name__, set_gripper_args=[gripper_id, address, value])
595+
return self._mesg(ProtocolCode.SET_TOQUE_GRIPPER, gripper_id, [address], [value])
606596

607-
def get_pro_gripper(self, gripper_id, address, joint_id=0):
597+
def get_pro_gripper(self, gripper_id, address):
608598
"""Get the force-controlled gripper parameters
609599
610600
Args:
611601
gripper_id (int): 1 ~ 254
612602
address (int): Corresponding to the command sequence number in the force-controlled gripper protocol
613603
"""
614-
self.calibration_parameters(class_name=self.__class__.__name__, get_gripper_args=[gripper_id, address], joint_id=joint_id)
615-
return self._mesg(ProtocolCode.GET_TOQUE_GRIPPER, gripper_id, [address], [joint_id])
604+
self.calibration_parameters(class_name=self.__class__.__name__, get_gripper_args=[gripper_id, address])
605+
return self._mesg(ProtocolCode.GET_TOQUE_GRIPPER, gripper_id, [address])
616606

617-
def set_pro_gripper_angle(self, gripper_id, joint_id=0, gripper_angle=None):
607+
def set_pro_gripper_angle(self, gripper_id, gripper_angle):
618608
""" Setting the angle of the force-controlled gripper
619609
620610
Args:
621611
gripper_id (int): 1 ~ 254
622612
gripper_angle (int): 0 ~ 100
623613
"""
624614
self.calibration_parameters(class_name=self.__class__.__name__, gripper_angle=[gripper_id, gripper_angle])
625-
return self.set_pro_gripper(gripper_id, ProGripper.SET_GRIPPER_ANGLE, joint_id=joint_id, value=gripper_angle)
626-
627-
def set_pro_gripper_angles(self, gripper_id, gripper_angles, speed):
628-
""" Setting the angle of the force-controlled gripper
629-
630-
Args:
631-
gripper_id (int): 1 ~ 254
632-
gripper_angles (list): 0 ~ 100
633-
speed (int): 0 ~ 100
634-
"""
635-
# self.calibration_parameters(class_name=self.__class__.__name__, gripper_angle=[gripper_id, gripper_angles])
636-
return self.set_pro_gripper(gripper_id, ProGripper.SET_GRIPPER_ANGLES, value=gripper_angles, speed=speed)
615+
return self.set_pro_gripper(gripper_id, ProGripper.SET_GRIPPER_ANGLE, gripper_angle)
637616

638-
def get_pro_gripper_angle(self, gripper_id, joint_id=0):
617+
def get_pro_gripper_angle(self, gripper_id):
639618
""" Setting the angle of the force-controlled gripper
640619
641620
Return:
642621
gripper_id (int): 1 ~ 254
643622
"""
644623
self.calibration_parameters(class_name=self.__class__.__name__, gripper_id=gripper_id)
645-
return self.get_pro_gripper(gripper_id, ProGripper.GET_GRIPPER_ANGLE, joint_id=joint_id)
624+
return self.get_pro_gripper(gripper_id, ProGripper.GET_GRIPPER_ANGLE)
646625

647626
def set_pro_gripper_open(self, gripper_id):
648627
""" Open force-controlled gripper
@@ -664,15 +643,15 @@ def set_pro_gripper_close(self, gripper_id):
664643
self.calibration_parameters(class_name=self.__class__.__name__, gripper_id=gripper_id)
665644
return self.set_pro_gripper_angle(gripper_id, 0)
666645

667-
def set_pro_gripper_calibration(self, gripper_id, joint_id=0):
646+
def set_pro_gripper_calibration(self, gripper_id):
668647
""" Setting the gripper jaw zero position
669648
670649
Args:
671650
gripper_id (int): 1 ~ 254
672651
673652
"""
674653
self.calibration_parameters(class_name=self.__class__.__name__, gripper_id=gripper_id)
675-
return self.set_pro_gripper(gripper_id, ProGripper.SET_GRIPPER_CALIBRATION, joint_id=joint_id)
654+
return self.set_pro_gripper(gripper_id, ProGripper.SET_GRIPPER_CALIBRATION, 0)
676655

677656
def get_pro_gripper_status(self, gripper_id):
678657
""" Get the clamping status of the gripper

0 commit comments

Comments
 (0)