55from threading import Thread
66from enum import Enum
77import typing as T
8+ import platform
9+
10+ if "linux" in platform .platform ().lower ():
11+ import RPi .GPIO as GPIO
12+
13+ # 初始化
14+ GPIO .setmode (GPIO .BCM )
15+ # 引脚20/21分别控制电磁阀和泄气阀门
16+ GPIO .setup (20 , GPIO .OUT )
17+ GPIO .setup (21 , GPIO .OUT )
18+
19+
20+ # 开启吸泵
21+ def pump_on ():
22+ # 打开电磁阀
23+ GPIO .output (20 , 0 )
24+
25+
26+ # 停止吸泵
27+ def pump_off ():
28+ # 关闭电磁阀
29+ GPIO .output (20 , 1 )
30+ time .sleep (0.05 )
31+ # 打开泄气阀门
32+ GPIO .output (21 , 0 )
33+ time .sleep (1 )
34+ GPIO .output (21 , 1 )
35+ time .sleep (0.05 )
836
937
1038class JoyStickKey (Enum ):
@@ -116,7 +144,7 @@ def get_joystick():
116144
117145def dispatch_key_action (key : T .Union [JoyStickKey , JoyStickContinous ], value : float ):
118146 global mc , arm_angle_table , global_states
119- print (f"key : { key } value : { value } " )
147+ # print(f"key : {key} value : {value}")
120148
121149 not_zero = lambda x : x < - 0.1 or x > 0.1
122150 is_zero = lambda x : - 0.1 < x < 0.1
@@ -152,27 +180,71 @@ def dispatch_key_action(key: T.Union[JoyStickKey, JoyStickContinous], value: flo
152180 global_states ["enable" ] = True
153181 time .sleep (3 )
154182
155- if global_states ["enable" ] and global_states ["initialized" ]:
156- # 坐标移动
157- if key in [
158- JoyStickContinous .LeftXAxis ,
159- JoyStickContinous .LeftYAxis ,
160- JoyStickContinous .RightYAxis ,
161- JoyStickKey .ArrowUp ,
162- JoyStickKey .ArrowDown ,
163- JoyStickKey .ArrowLeft ,
164- JoyStickKey .ArrowRight ,
165- JoyStickContinous .RightXAxis ,
166- ]:
183+ if not global_states ["enable" ] or not global_states ["initialized" ]:
184+ return
185+
186+ # 坐标移动
187+ if key in [
188+ JoyStickContinous .LeftXAxis ,
189+ JoyStickContinous .LeftYAxis ,
190+ JoyStickContinous .RightYAxis ,
191+ JoyStickKey .ArrowUp ,
192+ JoyStickKey .ArrowDown ,
193+ JoyStickKey .ArrowLeft ,
194+ JoyStickKey .ArrowRight ,
195+ JoyStickContinous .RightXAxis ,
196+ JoyStickKey .ArrowReleased ,
197+ ]:
198+ if global_states ["origin" ] is None :
199+ coords = []
200+ for _ in range (5 ):
201+ coords = mc .get_coords ()
202+ if len (coords ) != 0 :
203+ break
204+
205+ if len (coords ) != 0 :
206+ global_states ["origin" ] = coords
207+ else :
208+ print ("Can't get coords." )
209+ return
210+
211+ if is_zero (value ):
212+ global_states ["move_key_states" ][key ] = 0
213+ else :
167214 global_states ["move_key_states" ][key ] = value
168215
216+ if key == JoyStickKey .ArrowReleased :
217+ global_states ["move_key_states" ][JoyStickKey .ArrowUp ] = 0
218+ global_states ["move_key_states" ][JoyStickKey .ArrowDown ] = 0
219+ global_states ["move_key_states" ][JoyStickKey .ArrowLeft ] = 0
220+ global_states ["move_key_states" ][JoyStickKey .ArrowRight ] = 0
221+
169222 # 功能性
170223 if key == JoyStickContinous .L2 and not_zero (value ):
171224 mc .release_all_servos ()
172225 time .sleep (0.03 )
173226 elif key == JoyStickContinous .R2 and not_zero (value ):
174227 mc .power_on ()
175228 time .sleep (0.03 )
229+ elif key == JoyStickKey .L1 and not_zero (value ):
230+ mc .send_angles ([0 , 0 , 0 , 0 , 0 , 0 ], 50 )
231+ time .sleep (3 )
232+
233+ # 工具
234+ if key == JoyStickKey .RLeftKey :
235+ global_states ["gripper_val" ] = min (100 , global_states ["gripper_val" ] + 5 )
236+ mc .set_gripper_value (global_states ["gripper_val" ], 50 )
237+ time .sleep (0.5 )
238+ elif key == JoyStickKey .RTopKey :
239+ global_states ["gripper_val" ] = max (0 , global_states ["gripper_val" ] - 5 )
240+ mc .set_gripper_value (global_states ["gripper_val" ], 50 )
241+ time .sleep (0.5 )
242+ elif key == JoyStickKey .RBottomKey :
243+ pump_on ()
244+ time .sleep (0.05 )
245+ elif key == JoyStickKey .RRightKey :
246+ pump_off ()
247+ time .sleep (0.05 )
176248
177249
178250def continous_move ():
@@ -183,7 +255,16 @@ def continous_move():
183255
184256 while True :
185257 if not global_states ["enable" ] or not global_states ["initialized" ]:
186- return
258+ time .sleep (0.05 )
259+ continue
260+
261+ origin = None
262+ if global_states ["origin" ] is None :
263+ time .sleep (0.05 )
264+ continue
265+ else :
266+ origin = global_states ["origin" ]
267+
187268 for key , value in global_states ["move_key_states" ].items ():
188269 if key in [
189270 JoyStickContinous .LeftXAxis ,
@@ -194,61 +275,43 @@ def continous_move():
194275 JoyStickKey .ArrowLeft ,
195276 JoyStickKey .ArrowRight ,
196277 JoyStickContinous .RightXAxis ,
197- ]:
198- if global_states ["origin" ] is None :
199- coords = []
200- for _ in range (5 ):
201- coords = mc .get_coords ()
202- if len (coords ) != 0 :
203- break
204-
205- if len (coords ) != 0 :
206- global_states ["origin" ] = coords
207- else :
208- print ("Can't get coords." )
209- return
210-
211- if is_zero (value ):
212- global_states ["origin" ] = None
213- return
214-
215- x , y , z , rx , ry , rz = global_states ["origin" ]
216-
278+ ] and not_zero (value ):
279+ x , y , z , rx , ry , rz = origin
217280 # Y coord
218281 if key == JoyStickContinous .LeftXAxis :
219282 mc .send_coord (2 , y - value * 10 , 10 )
220- global_states ["origin" ][ 1 ] += value
221- time . sleep ( 0.05 )
283+ if global_states ["origin" ] is not None :
284+ global_states [ "origin" ][ 1 ] -= value
222285 # X coord
223286 elif key == JoyStickContinous .LeftYAxis :
224287 mc .send_coord (1 , x - value * 10 , 10 )
225- global_states ["origin" ][ 0 ] += value
226- time . sleep ( 0.05 )
288+ if global_states ["origin" ] is not None :
289+ global_states [ "origin" ][ 0 ] -= value
227290 # Z coord
228291 elif key == JoyStickContinous .RightYAxis :
229292 mc .send_coord (3 , z - value * 10 , 10 )
230- global_states ["origin" ][ 2 ] += value
231- time . sleep ( 0.05 )
293+ if global_states ["origin" ] is not None :
294+ global_states [ "origin" ][ 2 ] -= value
232295 elif key == JoyStickContinous .RightXAxis :
233296 mc .send_coord (6 , rz - value * 10 , 10 )
234- global_states ["origin" ][ 5 ] += value
235- time . sleep ( 0.05 )
297+ if global_states ["origin" ] is not None :
298+ global_states [ "origin" ][ 5 ] -= value
236299 elif key == JoyStickKey .ArrowUp :
237- mc .send_coord (4 , rx + 10 , 10 )
238- global_states ["origin" ][ 3 ] += 10
239- time . sleep ( 0.05 )
300+ mc .send_coord (4 , rx + 1 , 10 )
301+ if global_states ["origin" ] is not None :
302+ global_states [ "origin" ][ 3 ] += 1
240303 elif key == JoyStickKey .ArrowDown :
241- mc .send_coord (4 , rx - 10 , 10 )
242- global_states ["origin" ][ 3 ] -= 10
243- time . sleep ( 0.05 )
304+ mc .send_coord (4 , rx - 1 , 10 )
305+ if global_states ["origin" ] is not None :
306+ global_states [ "origin" ][ 3 ] -= 1
244307 elif key == JoyStickKey .ArrowLeft :
245- mc .send_coord (5 , ry - 10 , 10 )
246- global_states ["origin" ][ 4 ] -= 10
247- time . sleep ( 0.05 )
308+ mc .send_coord (5 , ry - 1 , 10 )
309+ if global_states ["origin" ] is not None :
310+ global_states [ "origin" ][ 4 ] -= 1
248311 elif key == JoyStickKey .ArrowRight :
249- mc .send_coord (5 , ry + 10 , 10 )
250- global_states ["origin" ][ 4 ] += 10
251- time . sleep ( 0.05 )
312+ mc .send_coord (5 , ry + 1 , 10 )
313+ if global_states ["origin" ] is not None :
314+ global_states [ "origin" ][ 4 ] += 1
252315
253316 time .sleep (0.05 )
254317
@@ -293,3 +356,4 @@ def retreive_joystick_input(joystick, context):
293356if __name__ == "__main__" :
294357 joystick = get_joystick ()
295358 Thread (target = retreive_joystick_input , args = (joystick , context )).start ()
359+ Thread (target = continous_move ).start ()
0 commit comments