11import pygame
22import time
3+ import math
34from pymycobot import MyCobot
45from threading import Thread
56from enum import Enum
67import typing as T
78
8- mc = MyCobot ("/dev/ttyAMA0" )
9+ mc = MyCobot ("/dev/ttyAMA0" , "1000000" )
10+ mc .set_fresh_mode (1 )
11+
912context = {"running" : True }
1013arm_speed = 50
1114sampling_rate = 10
15+ arm_angle_table = {"init" : [0 , 0 , - 90 , 0 , 0 , 0 ]}
16+ global_states = {
17+ "enable" : True ,
18+ "initialized" : True ,
19+ "origin" : None ,
20+ "gripper_val" : 0 ,
21+ "pump" : False ,
22+ }
1223
1324
1425class JoyStickKey (Enum ):
@@ -94,8 +105,11 @@ def get_joystick():
94105
95106
96107def dispatch_key_action (key : T .Union [JoyStickKey , JoyStickContinous ], value : float ):
97- global mc , key_hold_timestamp
98- print (f"key { key } " )
108+ global mc , arm_angle_table , global_states
109+ print (f"key : { key } value : { value } " )
110+
111+ not_zero = lambda x : x < - 0.1 or x > 0.1
112+ is_zero = lambda x : - 0.1 < x < 0.1
99113
100114 if key == JoyStickKey .StartKey :
101115 if mc .is_all_servo_enable () != 1 :
@@ -122,32 +136,87 @@ def dispatch_key_action(key: T.Union[JoyStickKey, JoyStickContinous], value: flo
122136 mc .set_color (0 , 0 , 0 )
123137 time .sleep (0.5 )
124138 mc .set_color (0 , 255 , 0 )
125-
126- check_valid_key_hold = (
127- lambda key : key_hold_timestamp [key ] > 0
128- and time .time () - key_hold_timestamp [key ] > 2
129- )
130- check_valid_continous_hold = (
131- lambda key : key_hold_timestamp [key ] > 0
132- and value > 0.5
133- and time .time () - key_hold_timestamp [key ] > 2
134- )
135-
136- if key == JoyStickKey .L1 :
137- if check_valid_key_hold (key ):
138- mc .send_angles ([0 , 0 , 0 , 0 , 0 , 0 ], arm_speed )
139+ global_states ["initialized" ] = True
139140 elif key == JoyStickKey .R1 :
140- if check_valid_key_hold (key ):
141- # TODO : 移动到初始点位
142- pass
143- elif key == JoyStickContinous .L2 :
144- if check_valid_continous_hold (key ):
145- mc .release_all_servos ()
146- elif key == JoyStickContinous .R2 :
147- if check_valid_continous_hold (key ):
148- mc .power_on ()
149- else :
150- key_hold_timestamp = get_init_key_hold_timestamp ()
141+ mc .send_angles (arm_angle_table ["init" ], arm_speed )
142+ global_states ["enable" ] = True
143+ time .sleep (3 )
144+
145+ if global_states ["enable" ] and global_states ["initialized" ]:
146+ # 坐标移动
147+ if key in [
148+ JoyStickContinous .LeftXAxis ,
149+ JoyStickContinous .LeftYAxis ,
150+ JoyStickContinous .RightYAxis ,
151+ JoyStickKey .ArrowUp ,
152+ JoyStickKey .ArrowDown ,
153+ JoyStickKey .ArrowLeft ,
154+ JoyStickKey .ArrowRight ,
155+ JoyStickContinous .RightXAxis ,
156+ ]:
157+ if global_states ["origin" ] is None :
158+ coords = []
159+ for _ in range (5 ):
160+ coords = mc .get_coords ()
161+ if len (coords ) != 0 :
162+ break
163+
164+ if len (coords ) != 0 :
165+ global_states ["origin" ] = coords
166+ else :
167+ print ("Can't get coords." )
168+ return
169+
170+ if is_zero (value ):
171+ global_states ["origin" ] = None
172+ return
173+
174+ x , y , z , rx , ry , rz = global_states ["origin" ]
175+
176+ # Y coord
177+ if key == JoyStickContinous .LeftXAxis :
178+ mc .send_coord (2 , y - value * 10 , 10 )
179+ global_states ["origin" ][1 ] += value
180+ time .sleep (0.05 )
181+ # X coord
182+ elif key == JoyStickContinous .LeftYAxis :
183+ mc .send_coord (1 , x - value * 10 , 10 )
184+ global_states ["origin" ][0 ] += value
185+ time .sleep (0.05 )
186+ # Z coord
187+ elif key == JoyStickContinous .RightYAxis :
188+ mc .send_coord (3 , z - value * 10 , 10 )
189+ global_states ["origin" ][2 ] += value
190+ time .sleep (0.05 )
191+ elif key == JoyStickContinous .RightXAxis :
192+ mc .send_coord (6 , rz - value * 10 , 10 )
193+ global_states ["origin" ][5 ] += value
194+ time .sleep (0.05 )
195+ elif key == JoyStickKey .ArrowUp :
196+ mc .send_coord (4 , rx + 10 , 10 )
197+ global_states ["origin" ][3 ] += 10
198+ time .sleep (0.05 )
199+ elif key == JoyStickKey .ArrowDown :
200+ mc .send_coord (4 , rx - 10 , 10 )
201+ global_states ["origin" ][3 ] -= 10
202+ time .sleep (0.05 )
203+ elif key == JoyStickKey .ArrowLeft :
204+ mc .send_coord (5 , ry - 10 , 10 )
205+ global_states ["origin" ][4 ] -= 10
206+ time .sleep (0.05 )
207+ elif key == JoyStickKey .ArrowRight :
208+ mc .send_coord (5 , ry + 10 , 10 )
209+ global_states ["origin" ][4 ] += 10
210+ time .sleep (0.05 )
211+
212+ # 功能性
213+ if key == JoyStickContinous .L2 and not_zero (value ):
214+ print (123 )
215+ mc .release_all_servos ()
216+ time .sleep (0.03 )
217+ elif key == JoyStickContinous .R2 and not_zero (value ):
218+ mc .power_on ()
219+ time .sleep (0.03 )
151220
152221
153222def dispatch_continous_key_action (key : JoyStickContinous , value : float ):
@@ -158,7 +227,7 @@ def retreive_joystick_input(joystick, context):
158227 while context ["running" ]:
159228 for event in pygame .event .get ():
160229 if event .type == pygame .QUIT :
161- running = False
230+ context [ " running" ] = False
162231 elif event .type == pygame .JOYBUTTONDOWN or event .type == pygame .JOYBUTTONUP :
163232 n = joystick .get_numbuttons ()
164233 for key_id in range (n ):
@@ -171,35 +240,22 @@ def retreive_joystick_input(joystick, context):
171240 axes = joystick .get_numaxes ()
172241 for key_id in range (axes ):
173242 axis = joystick .get_axis (key_id )
174- dispatch_continous_key_action (joystick_continous_map [key_id ], axis )
243+
244+ if (
245+ joystick_continous_map [key_id ] == JoyStickContinous .L2
246+ or joystick_continous_map [key_id ] == JoyStickContinous .R2
247+ ):
248+ axis = math .ceil (axis )
249+ if int (axis ) == - 1 :
250+ continue
251+
252+ dispatch_key_action (joystick_continous_map [key_id ], axis )
175253
176254 elif event .type == pygame .JOYHATMOTION :
177255 hat = joystick .get_hat (0 )
178256 dispatch_key_action (joystick_key_map [hat ], 1.0 )
179257
180258
181- def get_input (joystick , context ):
182- while context ["running" ]:
183- n = joystick .get_numbuttons ()
184- for key_id in range (n ):
185- button_status = joystick .get_button (key_id )
186- if not button_status :
187- continue
188- dispatch_key_action (joystick_key_map [key_id ], 1.0 )
189-
190- axes = joystick .get_numaxes ()
191- for key_id in range (axes ):
192- axis = joystick .get_axis (key_id )
193- if axis > 0.01 :
194- dispatch_key_action (joystick_continous_map [key_id ], axis )
195-
196- hat = joystick .get_hat (0 )
197- # print(hat)
198- # dispatch_key_action(joystick_key_map[hat], 1.0)
199-
200- # time.sleep(1 / sampling_rate)
201-
202-
203259if __name__ == "__main__" :
204260 joystick = get_joystick ()
205261 Thread (target = retreive_joystick_input , args = (joystick , context )).start ()
0 commit comments