Skip to content

Commit 3b3ba93

Browse files
author
Leonid Fedorenchik
committed
mycobotpro630: Fix tool_serial_read_data()
1 parent a74f45e commit 3b3ba93

File tree

1 file changed

+54
-14
lines changed

1 file changed

+54
-14
lines changed

pymycobot/mycobotpro630.py

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -592,22 +592,37 @@ def _send_can(self, data, can_id=0x007, timeout=0.5):
592592
print("Error: Cannot send can message")
593593

594594
def _receive_can(
595-
self, msg_data=None, timeout=0.5, num_tries=1000, destroy_can=True
595+
self,
596+
msg_data=None,
597+
timeout=0.5,
598+
num_tries=1000,
599+
destroy_can=True,
600+
messages_num=1,
596601
):
597602
"""Receives next message from CAN bus.
598603
599604
Args:
600-
msg_data (None | List[int], optional): message to look for. Defaults to None (look for any message).
601-
timeout (float, optional): How long time to receive message. Defaults to 0.5.
602-
num_tries (int, optional): how many times to try to receive specified message. Defaults to 1000.
603-
destroy_can (bool, optional): if need to destroy CAN BUS instance after receiving message. Defaults to True.
605+
msg_data (None | List[int], optional): message to look for.
606+
Defaults to None (look for any message). If multiple messages
607+
are received, only the first one is compared to.
608+
timeout (float, optional): How long time to receive message.
609+
Defaults to 0.5.
610+
num_tries (int, optional): how many times to try to receive
611+
specified message. Defaults to 1000.
612+
destroy_can (bool, optional): if need to destroy CAN BUS instance
613+
after receiving message. Defaults to True.
614+
messages_num (int, optional): how many messages to read and return.
615+
If number of messages is more than 1, list of messages is
616+
returned. Defaults to 1.
604617
605618
Returns:
606-
Message | None: CAN message or None (if no message could be received).
619+
Message | List[Message] | None: CAN message or None (if no message could be received).
607620
"""
608621
msg_data = msg_data or []
609622
msg = None
623+
msg_list = []
610624
self._init_can()
625+
611626
for _ in range(num_tries):
612627
msg = self.bus.recv(timeout)
613628
msg_found = True
@@ -620,11 +635,23 @@ def _receive_can(
620635
msg_found = False
621636
if msg_found:
622637
break
638+
639+
if messages_num > 1 and msg_found:
640+
msg_list.append(msg)
641+
for i in range(messages_num - 1):
642+
msg = self.bus.recv(timeout)
643+
msg_list.append(msg)
644+
623645
if destroy_can:
624646
self._destroy_can()
647+
625648
if not msg_found:
626649
msg = None
627-
return msg
650+
651+
if messages_num == 1:
652+
return msg
653+
else:
654+
return msg_list
628655

629656
def _init_robot(self):
630657
"""Initializes robot parameters."""
@@ -2815,7 +2842,10 @@ def tool_gripper_pro_set_angle(self, angle):
28152842
command.extend([(crc16_value >> 8), (crc16_value & 0xFF)])
28162843
self.tool_serial_write_data(command)
28172844
ret = self.tool_serial_read_data(11)
2818-
return bool(ret[8])
2845+
if len(ret) == 11:
2846+
return bool(ret[8])
2847+
2848+
return False
28192849

28202850
def tool_gripper_pro_get_angle(self):
28212851
"""Returns current angle of Pro Gripper.
@@ -2828,7 +2858,10 @@ def tool_gripper_pro_get_angle(self):
28282858
command.extend([(crc16_value >> 8), (crc16_value & 0xFF)])
28292859
self.tool_serial_write_data(command)
28302860
ret = self.tool_serial_read_data(11)
2831-
return int((ret[7] << 8) | (ret[8]))
2861+
if len(ret) == 11:
2862+
return int((ret[7] << 8) | (ret[8]))
2863+
2864+
return -1
28322865

28332866
def tool_gripper_pro_open(self):
28342867
"""Fully opens Pro Gripper.
@@ -2924,11 +2957,18 @@ def tool_serial_read_data(self, n):
29242957
"""
29252958
self._send_can([0x02, 0xB4, n])
29262959
data = []
2927-
msg = self._receive_can(destroy_can=False)
2928-
data.extend(msg.data[3:])
2929-
while msg is not None and len(data) < n:
2930-
msg = self._receive_can(destroy_can=False)
2960+
messages_num = math.ceil(n / 5)
2961+
data_to_look_for = [2 + n, 0xB4]
2962+
if data_to_look_for[0] > 7:
2963+
data_to_look_for[0] = 7
2964+
msg = self._receive_can(
2965+
msg_data=data_to_look_for, destroy_can=False, messages_num=messages_num
2966+
)
2967+
if messages_num == 1:
29312968
data.extend(msg.data[3:])
2969+
else:
2970+
for i in range(len(msg)):
2971+
data.extend(msg[i].data[3:])
29322972
return data
29332973

29342974
def tool_serial_write_data(self, bytes):
@@ -2950,7 +2990,7 @@ def tool_serial_write_data(self, bytes):
29502990
msg_bytes.extend(list(chunk))
29512991
# print("msg_bytes = " + str(list(msg_bytes)))
29522992
self._send_can(msg_bytes)
2953-
msg = self._receive_can()
2993+
msg = self._receive_can(destroy_can=False)
29542994
return msg.data[2]
29552995

29562996
def tool_serial_flush(self):

0 commit comments

Comments
 (0)