11# coding=utf-8
22
33import time
4+ import threading
45
56from pymycobot .generate import CommandGenerator
67from pymycobot .common import ProtocolCode , write , read
@@ -28,6 +29,7 @@ def __init__(self, port, baudrate="115200", timeout=0.1, debug=False):
2829 self ._serial_port .timeout = timeout
2930 self ._serial_port .rts = False
3031 self ._serial_port .open ()
32+ self .lock = threading .Lock ()
3133
3234
3335 def _mesg (self , genre , * args , ** kwargs ):
@@ -43,94 +45,95 @@ def _mesg(self, genre, *args, **kwargs):
4345 has_reply: Whether there is a return value to accept.
4446 """
4547 real_command , has_reply = super (Mercury , self )._mesg (genre , * args , ** kwargs )
46- self ._write (self ._flatten (real_command ))
48+ with self .lock :
49+ self ._write (self ._flatten (real_command ))
4750
48- if has_reply :
49- data = self ._read (genre , _class = self .__class__ .__name__ )
50- if genre == ProtocolCode .SET_SSID_PWD :
51- return None
52- res = self ._process_received (data , genre , 14 )
53- if res == []:
54- return None
55- if genre in [
56- ProtocolCode .ROBOT_VERSION ,
57- ProtocolCode .GET_ROBOT_ID ,
58- ProtocolCode .IS_POWER_ON ,
59- ProtocolCode .IS_CONTROLLER_CONNECTED ,
60- ProtocolCode .IS_PAUSED , # TODO have bug: return b''
61- ProtocolCode .IS_IN_POSITION ,
62- ProtocolCode .IS_MOVING ,
63- ProtocolCode .IS_SERVO_ENABLE ,
64- ProtocolCode .IS_ALL_SERVO_ENABLE ,
65- ProtocolCode .GET_SERVO_DATA ,
66- ProtocolCode .GET_DIGITAL_INPUT ,
67- ProtocolCode .GET_GRIPPER_VALUE ,
68- ProtocolCode .IS_GRIPPER_MOVING ,
69- ProtocolCode .GET_SPEED ,
70- ProtocolCode .GET_ENCODER ,
71- ProtocolCode .GET_BASIC_INPUT ,
72- ProtocolCode .GET_TOF_DISTANCE ,
73- ProtocolCode .GET_END_TYPE ,
74- ProtocolCode .GET_MOVEMENT_TYPE ,
75- ProtocolCode .GET_REFERENCE_FRAME ,
76- ProtocolCode .GET_FRESH_MODE ,
77- ProtocolCode .GET_GRIPPER_MODE ,
78- ProtocolCode .SET_SSID_PWD ,
79- ProtocolCode .COBOTX_IS_GO_ZERO ,
80- ProtocolCode .GET_ERROR_DETECT_MODE
81- ]:
82- return self ._process_single (res )
83- elif genre in [ProtocolCode .GET_ANGLES ]:
84- return [self ._int2angle (angle ) for angle in res ]
85- elif genre in [
86- ProtocolCode .GET_COORDS ,
87- ProtocolCode .MERCURY_GET_BASE_COORDS ,
88- ProtocolCode .GET_TOOL_REFERENCE ,
89- ProtocolCode .GET_WORLD_REFERENCE ,
90- ]:
91- if res :
51+ if has_reply :
52+ data = self ._read (genre , _class = self .__class__ .__name__ )
53+ if genre == ProtocolCode .SET_SSID_PWD :
54+ return None
55+ res = self ._process_received (data , genre , 14 )
56+ if res == []:
57+ return None
58+ if genre in [
59+ ProtocolCode .ROBOT_VERSION ,
60+ ProtocolCode .GET_ROBOT_ID ,
61+ ProtocolCode .IS_POWER_ON ,
62+ ProtocolCode .IS_CONTROLLER_CONNECTED ,
63+ ProtocolCode .IS_PAUSED , # TODO have bug: return b''
64+ ProtocolCode .IS_IN_POSITION ,
65+ ProtocolCode .IS_MOVING ,
66+ ProtocolCode .IS_SERVO_ENABLE ,
67+ ProtocolCode .IS_ALL_SERVO_ENABLE ,
68+ ProtocolCode .GET_SERVO_DATA ,
69+ ProtocolCode .GET_DIGITAL_INPUT ,
70+ ProtocolCode .GET_GRIPPER_VALUE ,
71+ ProtocolCode .IS_GRIPPER_MOVING ,
72+ ProtocolCode .GET_SPEED ,
73+ ProtocolCode .GET_ENCODER ,
74+ ProtocolCode .GET_BASIC_INPUT ,
75+ ProtocolCode .GET_TOF_DISTANCE ,
76+ ProtocolCode .GET_END_TYPE ,
77+ ProtocolCode .GET_MOVEMENT_TYPE ,
78+ ProtocolCode .GET_REFERENCE_FRAME ,
79+ ProtocolCode .GET_FRESH_MODE ,
80+ ProtocolCode .GET_GRIPPER_MODE ,
81+ ProtocolCode .SET_SSID_PWD ,
82+ ProtocolCode .COBOTX_IS_GO_ZERO ,
83+ ProtocolCode .GET_ERROR_DETECT_MODE
84+ ]:
85+ return self ._process_single (res )
86+ elif genre in [ProtocolCode .GET_ANGLES ]:
87+ return [self ._int2angle (angle ) for angle in res ]
88+ elif genre in [
89+ ProtocolCode .GET_COORDS ,
90+ ProtocolCode .MERCURY_GET_BASE_COORDS ,
91+ ProtocolCode .GET_TOOL_REFERENCE ,
92+ ProtocolCode .GET_WORLD_REFERENCE ,
93+ ]:
94+ if res :
95+ r = []
96+ for idx in range (3 ):
97+ r .append (self ._int2coord (res [idx ]))
98+ for idx in range (3 , 6 ):
99+ r .append (self ._int2angle (res [idx ]))
100+ return r
101+ else :
102+ return res
103+ elif genre in [ProtocolCode .GET_SERVO_VOLTAGES ]:
104+ return [self ._int2coord (angle ) for angle in res ]
105+ elif genre in [ProtocolCode .GET_BASIC_VERSION , ProtocolCode .SOFTWARE_VERSION , ProtocolCode .GET_ATOM_VERSION ]:
106+ return self ._int2coord (self ._process_single (res ))
107+ elif genre in [
108+ ProtocolCode .GET_JOINT_MAX_ANGLE ,
109+ ProtocolCode .GET_JOINT_MIN_ANGLE ,
110+ ]:
111+ return self ._int2coord (res [0 ])
112+ elif genre == ProtocolCode .GET_ANGLES_COORDS :
113+ r = []
114+ for index in range (len (res )):
115+ if index < 7 :
116+ r .append (self ._int2angle (res [index ]))
117+ elif index < 10 :
118+ r .append (self ._int2coord (res [index ]))
119+ else :
120+ r .append (self ._int2angle (res [index ]))
121+ return r
122+ elif genre == ProtocolCode .GO_ZERO :
92123 r = []
93- for idx in range (3 ):
94- r .append (self ._int2coord (res [idx ]))
95- for idx in range (3 , 6 ):
96- r .append (self ._int2angle (res [idx ]))
124+ if res :
125+ if 1 not in res [1 :]:
126+ return res [0 ]
127+ else :
128+ for i in range (1 , len (res )):
129+ if res [i ] == 1 :
130+ r .append (i )
97131 return r
132+ elif genre in [ProtocolCode .COBOTX_GET_ANGLE , ProtocolCode .COBOTX_GET_SOLUTION_ANGLES ]:
133+ return self ._int2angle (res [0 ])
98134 else :
99135 return res
100- elif genre in [ProtocolCode .GET_SERVO_VOLTAGES ]:
101- return [self ._int2coord (angle ) for angle in res ]
102- elif genre in [ProtocolCode .GET_BASIC_VERSION , ProtocolCode .SOFTWARE_VERSION , ProtocolCode .GET_ATOM_VERSION ]:
103- return self ._int2coord (self ._process_single (res ))
104- elif genre in [
105- ProtocolCode .GET_JOINT_MAX_ANGLE ,
106- ProtocolCode .GET_JOINT_MIN_ANGLE ,
107- ]:
108- return self ._int2coord (res [0 ])
109- elif genre == ProtocolCode .GET_ANGLES_COORDS :
110- r = []
111- for index in range (len (res )):
112- if index < 7 :
113- r .append (self ._int2angle (res [index ]))
114- elif index < 10 :
115- r .append (self ._int2coord (res [index ]))
116- else :
117- r .append (self ._int2angle (res [index ]))
118- return r
119- elif genre == ProtocolCode .GO_ZERO :
120- r = []
121- if res :
122- if 1 not in res [1 :]:
123- return res [0 ]
124- else :
125- for i in range (1 , len (res )):
126- if res [i ] == 1 :
127- r .append (i )
128- return r
129- elif genre in [ProtocolCode .COBOTX_GET_ANGLE , ProtocolCode .COBOTX_GET_SOLUTION_ANGLES ]:
130- return self ._int2angle (res [0 ])
131- else :
132- return res
133- return None
136+ return None
134137
135138 def open (self ):
136139 self ._serial_port .open ()
0 commit comments