Skip to content

Commit 73dc169

Browse files
committed
fix(mycobotpro630.py): Fix tool_get_digital_in()
1 parent 5b1c0d7 commit 73dc169

File tree

1 file changed

+72
-66
lines changed

1 file changed

+72
-66
lines changed

pymycobot/mycobotpro630.py

Lines changed: 72 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616
def is_debian_os():
1717
try:
1818
# 执行 lsb_release -a 命令,并捕获输出
19-
result = subprocess.run(['lsb_release', '-a'], capture_output=True, text=True, check=True)
19+
result = subprocess.run(
20+
["lsb_release", "-a"], capture_output=True, text=True, check=True
21+
)
2022

2123
# 解析输出,获取 Distributor ID 的信息
22-
lines = result.stdout.split('\n')
24+
lines = result.stdout.split("\n")
2325
for line in lines:
24-
if line.startswith('Distributor ID:'):
25-
distributor_id = line.split(':', 1)[1].strip()
26+
if line.startswith("Distributor ID:"):
27+
distributor_id = line.split(":", 1)[1].strip()
2628
if distributor_id != "Debian":
2729
return False
2830
return True
@@ -39,10 +41,11 @@ def is_debian_os():
3941
import hal
4042
import can
4143

42-
4344
class JogMode(Enum):
4445
JOG_JOINT = elerob.ANGULAR - 1
4546
JOG_TELEOP = elerob.LINEAR - 1
47+
48+
4649
from time import sleep
4750

4851
try:
@@ -52,6 +55,7 @@ class JogMode(Enum):
5255
def CPUTemperature():
5356
return 0
5457

58+
5559
COORDS_EPSILON = 0.50
5660
MAX_JOINTS = 6
5761
MAX_CARTE = 3
@@ -79,7 +83,6 @@ class RobotMoveState(Enum):
7983
RUN_PROGRAM_STATE = 5
8084

8185

82-
8386
class JogDirection(Enum):
8487
POSITIVE = 1
8588
NEGATIVE = -1
@@ -1476,15 +1479,15 @@ def _jog_continuous(self, jog_mode, joint_or_axis, direction, speed, jog_id):
14761479
if self.s.task_state != elerob.STATE_ON:
14771480
return False
14781481
if (
1479-
(jog_mode == JogMode.JOG_JOINT)
1480-
and (self.s.motion_mode == elerob.TRAJ_MODE_TELEOP)
1482+
(jog_mode == JogMode.JOG_JOINT)
1483+
and (self.s.motion_mode == elerob.TRAJ_MODE_TELEOP)
14811484
) or (
1482-
(jog_mode == JogMode.JOG_TELEOP)
1483-
and (self.s.motion_mode != elerob.TRAJ_MODE_TELEOP)
1485+
(jog_mode == JogMode.JOG_TELEOP)
1486+
and (self.s.motion_mode != elerob.TRAJ_MODE_TELEOP)
14841487
):
14851488
return False
14861489
if (jog_mode == JogMode.JOG_JOINT) and (
1487-
joint_or_axis.value < 0 or joint_or_axis.value >= MAX_JOINTS
1490+
joint_or_axis.value < 0 or joint_or_axis.value >= MAX_JOINTS
14881491
):
14891492
return False
14901493
if (jog_mode == JogMode.JOG_TELEOP) and (joint_or_axis.value < 0):
@@ -1544,15 +1547,15 @@ def _jog_increment(self, jog_mode, joint_or_axis, incr, speed):
15441547
if self.s.task_state != elerob.STATE_ON:
15451548
return False
15461549
if (
1547-
(jog_mode == JogMode.JOG_JOINT)
1548-
and (self.s.motion_mode == elerob.TRAJ_MODE_TELEOP)
1550+
(jog_mode == JogMode.JOG_JOINT)
1551+
and (self.s.motion_mode == elerob.TRAJ_MODE_TELEOP)
15491552
) or (
1550-
(jog_mode == JogMode.JOG_TELEOP)
1551-
and (self.s.motion_mode != elerob.TRAJ_MODE_TELEOP)
1553+
(jog_mode == JogMode.JOG_TELEOP)
1554+
and (self.s.motion_mode != elerob.TRAJ_MODE_TELEOP)
15521555
):
15531556
return False
15541557
if (jog_mode == JogMode.JOG_JOINT) and (
1555-
joint_or_axis.value < 0 or joint_or_axis.value >= MAX_JOINTS
1558+
joint_or_axis.value < 0 or joint_or_axis.value >= MAX_JOINTS
15561559
):
15571560
return False
15581561
if (jog_mode == JogMode.JOG_TELEOP) and (joint_or_axis.value < 0):
@@ -1611,15 +1614,15 @@ def _jog_absolute(self, joint_or_axis, jog_mode, pos, speed):
16111614
if self.s.task_state != elerob.STATE_ON:
16121615
return False
16131616
if (
1614-
(jog_mode == JogMode.JOG_JOINT)
1615-
and (self.s.motion_mode == elerob.TRAJ_MODE_TELEOP)
1617+
(jog_mode == JogMode.JOG_JOINT)
1618+
and (self.s.motion_mode == elerob.TRAJ_MODE_TELEOP)
16161619
) or (
1617-
(jog_mode == JogMode.JOG_TELEOP)
1618-
and (self.s.motion_mode != elerob.TRAJ_MODE_TELEOP)
1620+
(jog_mode == JogMode.JOG_TELEOP)
1621+
and (self.s.motion_mode != elerob.TRAJ_MODE_TELEOP)
16191622
):
16201623
return False
16211624
if (jog_mode == JogMode.JOG_JOINT) and (
1622-
joint_or_axis.value < 0 or joint_or_axis.value >= MAX_JOINTS
1625+
joint_or_axis.value < 0 or joint_or_axis.value >= MAX_JOINTS
16231626
):
16241627
return False
16251628
if (jog_mode == JogMode.JOG_TELEOP) and (joint_or_axis.value < 0):
@@ -1646,7 +1649,7 @@ def jog_stop(self, jog_mode, joint_or_axis):
16461649
bool: True if jog stopped successfully, False otherwise
16471650
"""
16481651
if (jog_mode == JogMode.JOG_JOINT) and (
1649-
joint_or_axis.value < 0 or joint_or_axis.value >= MAX_JOINTS
1652+
joint_or_axis.value < 0 or joint_or_axis.value >= MAX_JOINTS
16501653
):
16511654
return False
16521655
if (jog_mode == JogMode.JOG_TELEOP) and (joint_or_axis.value < 0):
@@ -1853,8 +1856,8 @@ def is_program_running(self, do_poll=True):
18531856
if do_poll:
18541857
self.s.poll()
18551858
return (
1856-
self.s.task_mode == elerob.MODE_AUTO
1857-
and self.s.interp_state != elerob.INTERP_IDLE
1859+
self.s.task_mode == elerob.MODE_AUTO
1860+
and self.s.interp_state != elerob.INTERP_IDLE
18581861
)
18591862

18601863
def manual_ok(self, do_poll=True):
@@ -1932,8 +1935,8 @@ def get_current_line(self):
19321935
"""
19331936
self.s.poll()
19341937
if self.s.task_mode != elerob.MODE_AUTO or self.s.interp_state not in (
1935-
elerob.INTERP_READING,
1936-
elerob.INTERP_WAITING,
1938+
elerob.INTERP_READING,
1939+
elerob.INTERP_WAITING,
19371940
):
19381941
return -1
19391942
self.ensure_mode(TaskMode.AUTO)
@@ -1966,8 +1969,8 @@ def get_read_line(self):
19661969
"""
19671970
self.s.poll()
19681971
if self.s.task_mode != elerob.MODE_AUTO or self.s.interp_state not in (
1969-
elerob.INTERP_READING,
1970-
elerob.INTERP_WAITING,
1972+
elerob.INTERP_READING,
1973+
elerob.INTERP_WAITING,
19711974
):
19721975
return -1
19731976
self.ensure_mode(TaskMode.AUTO)
@@ -2013,8 +2016,8 @@ def program_pause(self):
20132016
"""Pause currently running program."""
20142017
self.s.poll()
20152018
if self.s.task_mode != elerob.MODE_AUTO or self.s.interp_state not in (
2016-
elerob.INTERP_READING,
2017-
elerob.INTERP_WAITING,
2019+
elerob.INTERP_READING,
2020+
elerob.INTERP_WAITING,
20182021
):
20192022
return
20202023
self.ensure_mode(TaskMode.AUTO)
@@ -2043,9 +2046,9 @@ def task_stop(self):
20432046
self.c.wait_complete()
20442047
self.s.poll()
20452048
print(
2046-
"Warning: Failed Task abort: task_mode={}, exec_status={}, interp_state={}".format(self.s.task_mode,
2047-
self.s.exec_state,
2048-
self.s.interp_state)
2049+
"Warning: Failed Task abort: task_mode={}, exec_status={}, interp_state={}".format(
2050+
self.s.task_mode, self.s.exec_state, self.s.interp_state
2051+
)
20492052
)
20502053

20512054
def task_stop_async(self):
@@ -2413,9 +2416,9 @@ def set_jog_mode(self, jog_mode):
24132416
"""
24142417
self.s.poll()
24152418
if (
2416-
self.s.estop
2417-
or not self.s.enabled
2418-
or (self.s.interp_state != elerob.INTERP_IDLE)
2419+
self.s.estop
2420+
or not self.s.enabled
2421+
or (self.s.interp_state != elerob.INTERP_IDLE)
24192422
):
24202423
return False
24212424
if self.s.task_mode != elerob.MODE_MANUAL:
@@ -2495,18 +2498,18 @@ def coords_to_gcode(self, coords):
24952498
str: gcode string to move to given coords
24962499
"""
24972500
return (
2498-
"X"
2499-
+ str(coords[0])
2500-
+ "Y"
2501-
+ str(coords[1])
2502-
+ "Z"
2503-
+ str(coords[2])
2504-
+ "A"
2505-
+ str(coords[3])
2506-
+ "B"
2507-
+ str(coords[4])
2508-
+ "C"
2509-
+ str(coords[5])
2501+
"X"
2502+
+ str(coords[0])
2503+
+ "Y"
2504+
+ str(coords[1])
2505+
+ "Z"
2506+
+ str(coords[2])
2507+
+ "A"
2508+
+ str(coords[3])
2509+
+ "B"
2510+
+ str(coords[4])
2511+
+ "C"
2512+
+ str(coords[5])
25102513
)
25112514

25122515
def angles_to_gcode(self, angles):
@@ -2545,12 +2548,12 @@ def coords_equal(self, coords_1, coords_2):
25452548
bool: True if coords are equal, False otherwise.
25462549
"""
25472550
return (
2548-
self.float_equal(coords_1[Axis.X.value], coords_2[Axis.X.value])
2549-
and self.float_equal(coords_1[Axis.Y.value], coords_2[Axis.Y.value])
2550-
and self.float_equal(coords_1[Axis.Z.value], coords_2[Axis.Z.value])
2551-
and self.float_equal(coords_1[Axis.RX.value], coords_2[Axis.RX.value])
2552-
and self.float_equal(coords_1[Axis.RY.value], coords_2[Axis.RY.value])
2553-
and self.float_equal(coords_1[Axis.RZ.value], coords_2[Axis.RZ.value])
2551+
self.float_equal(coords_1[Axis.X.value], coords_2[Axis.X.value])
2552+
and self.float_equal(coords_1[Axis.Y.value], coords_2[Axis.Y.value])
2553+
and self.float_equal(coords_1[Axis.Z.value], coords_2[Axis.Z.value])
2554+
and self.float_equal(coords_1[Axis.RX.value], coords_2[Axis.RX.value])
2555+
and self.float_equal(coords_1[Axis.RY.value], coords_2[Axis.RY.value])
2556+
and self.float_equal(coords_1[Axis.RZ.value], coords_2[Axis.RZ.value])
25542557
)
25552558

25562559
def angles_equal(self, angles_1, angles_2):
@@ -2564,12 +2567,12 @@ def angles_equal(self, angles_1, angles_2):
25642567
bool: True if angles are equal, False otherwise.
25652568
"""
25662569
return (
2567-
self.float_equal(angles_1[Axis.X.value], angles_2[Axis.X.value])
2568-
and self.float_equal(angles_1[Axis.Y.value], angles_2[Axis.Y.value])
2569-
and self.float_equal(angles_1[Axis.Z.value], angles_2[Axis.Z.value])
2570-
and self.float_equal(angles_1[Axis.RX.value], angles_2[Axis.RX.value])
2571-
and self.float_equal(angles_1[Axis.RY.value], angles_2[Axis.RY.value])
2572-
and self.float_equal(angles_1[Axis.RZ.value], angles_2[Axis.RZ.value])
2570+
self.float_equal(angles_1[Axis.X.value], angles_2[Axis.X.value])
2571+
and self.float_equal(angles_1[Axis.Y.value], angles_2[Axis.Y.value])
2572+
and self.float_equal(angles_1[Axis.Z.value], angles_2[Axis.Z.value])
2573+
and self.float_equal(angles_1[Axis.RX.value], angles_2[Axis.RX.value])
2574+
and self.float_equal(angles_1[Axis.RY.value], angles_2[Axis.RY.value])
2575+
and self.float_equal(angles_1[Axis.RZ.value], angles_2[Axis.RZ.value])
25732576
)
25742577

25752578
def tool_get_firmware_version(self):
@@ -2600,11 +2603,15 @@ def tool_get_digital_in(self, pin_no):
26002603
pin_no (int): pin number
26012604
26022605
Returns:
2603-
int : pin value
2606+
int: pin value
26042607
"""
26052608
self.send_can(data=[0x02, 0x62, pin_no])
2606-
msg = self.receive_can()
2607-
pin_state = msg.data[2]
2609+
pin_state = -1
2610+
for _ in range(100):
2611+
msg = self.receive_can()
2612+
if msg.data[0] == 0x03 and msg.data[1] == 0x62 and msg.data[2] == pin_no:
2613+
pin_state = msg.data[3]
2614+
break
26082615
return pin_state
26092616

26102617
def tool_set_led_color(self, r, g, b):
@@ -2647,8 +2654,7 @@ def tool_set_gripper_value(self, value, speed):
26472654
self.send_can([0x03, 0x67, value, speed])
26482655

26492656
def tool_set_gripper_calibrate(self):
2650-
"""Set the current position of the gripper to zero
2651-
"""
2657+
"""Set the current position of the gripper to zero"""
26522658
self.send_can([0x01, 0x68])
26532659

26542660
def tool_set_gripper_enabled(self, enabled):
@@ -2724,7 +2730,7 @@ def tool_serial_write_data(self, bytes):
27242730
num_chunks = int(len(bytes) / CHUNK_SIZE + 1)
27252731
# print("num_chunks = " + str(num_chunks))
27262732
for i in range(0, len(bytes), CHUNK_SIZE):
2727-
chunk = bytes[i: i + CHUNK_SIZE]
2733+
chunk = bytes[i : i + CHUNK_SIZE]
27282734
# print("num_chunks = " + str(num_chunks) + ", i = " + str(i))
27292735
msg_bytes = [0x2 + len(chunk), 0xB5, num_chunks - int(i / CHUNK_SIZE)]
27302736
msg_bytes.extend(list(chunk))

0 commit comments

Comments
 (0)