Skip to content

Commit 9f4862a

Browse files
committed
Merge branch 'main' of github.com:elephantrobotics/pymycobot into mycobot_280_refactor
2 parents 61cba7c + 0e8ba89 commit 9f4862a

File tree

1 file changed

+58
-12
lines changed

1 file changed

+58
-12
lines changed

pymycobot/mycobotpro630.py

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -591,18 +591,38 @@ def _send_can(self, data, can_id=0x007, timeout=0.5):
591591
except can.CanError:
592592
print("Error: Cannot send can message")
593593

594-
def _receive_can(self, msg_data=None, timeout=0.5, num_tries=1000):
594+
def _receive_can(
595+
self,
596+
msg_data=None,
597+
timeout=0.5,
598+
num_tries=1000,
599+
destroy_can=True,
600+
messages_num=1,
601+
):
595602
"""Receives next message from CAN bus.
596603
597604
Args:
598-
timeout (float, optional): How long time to receive message. Defaults to 0.5.
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.
599617
600618
Returns:
601-
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).
602620
"""
603621
msg_data = msg_data or []
604622
msg = None
623+
msg_list = []
605624
self._init_can()
625+
606626
for _ in range(num_tries):
607627
msg = self.bus.recv(timeout)
608628
msg_found = True
@@ -615,10 +635,23 @@ def _receive_can(self, msg_data=None, timeout=0.5, num_tries=1000):
615635
msg_found = False
616636
if msg_found:
617637
break
618-
self._destroy_can()
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+
645+
if destroy_can:
646+
self._destroy_can()
647+
619648
if not msg_found:
620649
msg = None
621-
return msg
650+
651+
if messages_num == 1:
652+
return msg
653+
else:
654+
return msg_list
622655

623656
def _init_robot(self):
624657
"""Initializes robot parameters."""
@@ -2809,7 +2842,10 @@ def tool_gripper_pro_set_angle(self, angle):
28092842
command.extend([(crc16_value >> 8), (crc16_value & 0xFF)])
28102843
self.tool_serial_write_data(command)
28112844
ret = self.tool_serial_read_data(11)
2812-
return bool(ret[8])
2845+
if len(ret) == 11:
2846+
return bool(ret[8])
2847+
2848+
return False
28132849

28142850
def tool_gripper_pro_get_angle(self):
28152851
"""Returns current angle of Pro Gripper.
@@ -2822,7 +2858,10 @@ def tool_gripper_pro_get_angle(self):
28222858
command.extend([(crc16_value >> 8), (crc16_value & 0xFF)])
28232859
self.tool_serial_write_data(command)
28242860
ret = self.tool_serial_read_data(11)
2825-
return int((ret[7] << 8) | (ret[8]))
2861+
if len(ret) == 11:
2862+
return int((ret[7] << 8) | (ret[8]))
2863+
2864+
return -1
28262865

28272866
def tool_gripper_pro_open(self):
28282867
"""Fully opens Pro Gripper.
@@ -2918,11 +2957,18 @@ def tool_serial_read_data(self, n):
29182957
"""
29192958
self._send_can([0x02, 0xB4, n])
29202959
data = []
2921-
msg = self._receive_can()
2922-
data.extend(msg.data[3:])
2923-
while msg is not None and len(data) < n:
2924-
msg = self._receive_can()
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:
29252968
data.extend(msg.data[3:])
2969+
else:
2970+
for i in range(len(msg)):
2971+
data.extend(msg[i].data[3:])
29262972
return data
29272973

29282974
def tool_serial_write_data(self, bytes):
@@ -2944,7 +2990,7 @@ def tool_serial_write_data(self, bytes):
29442990
msg_bytes.extend(list(chunk))
29452991
# print("msg_bytes = " + str(list(msg_bytes)))
29462992
self._send_can(msg_bytes)
2947-
msg = self._receive_can()
2993+
msg = self._receive_can(destroy_can=False)
29482994
return msg.data[2]
29492995

29502996
def tool_serial_flush(self):

0 commit comments

Comments
 (0)