Skip to content

Commit 82aac2e

Browse files
committed
cp
1 parent 9273aef commit 82aac2e

File tree

1 file changed

+104
-70
lines changed

1 file changed

+104
-70
lines changed

demo/handle_control/joy_control.py

Lines changed: 104 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,6 @@
66
from enum import Enum
77
import typing as T
88

9-
mc = MyCobot("/dev/ttyAMA0", "1000000")
10-
mc.set_fresh_mode(1)
11-
12-
context = {"running": True}
13-
arm_speed = 50
14-
sampling_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-
}
23-
249

2510
class JoyStickKey(Enum):
2611
StartKey = 7
@@ -78,6 +63,31 @@ class JoyStickContinous(Enum):
7863
5: JoyStickContinous.R2,
7964
}
8065

66+
mc = MyCobot("/dev/ttyAMA0", "1000000")
67+
mc.set_fresh_mode(1)
68+
69+
context = {"running": True}
70+
arm_speed = 50
71+
sampling_rate = 10
72+
arm_angle_table = {"init": [0, 0, -90, 0, 0, 0]}
73+
global_states = {
74+
"enable": True,
75+
"initialized": True,
76+
"origin": None,
77+
"gripper_val": 0,
78+
"pump": False,
79+
"move_key_states": {
80+
JoyStickContinous.LeftXAxis: 0,
81+
JoyStickContinous.LeftYAxis: 0,
82+
JoyStickContinous.RightYAxis: 0,
83+
JoyStickKey.ArrowUp: 0,
84+
JoyStickKey.ArrowDown: 0,
85+
JoyStickKey.ArrowLeft: 0,
86+
JoyStickKey.ArrowRight: 0,
87+
JoyStickContinous.RightXAxis: 0,
88+
},
89+
}
90+
8191

8292
def get_init_key_hold_timestamp():
8393
return {
@@ -154,71 +164,95 @@ def dispatch_key_action(key: T.Union[JoyStickKey, JoyStickContinous], value: flo
154164
JoyStickKey.ArrowRight,
155165
JoyStickContinous.RightXAxis,
156166
]:
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)
167+
global_states["move_key_states"][key] = value
211168

212169
# 功能性
213170
if key == JoyStickContinous.L2 and not_zero(value):
214-
print(123)
215171
mc.release_all_servos()
216172
time.sleep(0.03)
217173
elif key == JoyStickContinous.R2 and not_zero(value):
218174
mc.power_on()
219175
time.sleep(0.03)
220176

221177

178+
def continous_move():
179+
global global_states
180+
181+
not_zero = lambda x: x < -0.1 or x > 0.1
182+
is_zero = lambda x: -0.1 < x < 0.1
183+
184+
while True:
185+
if not global_states["enable"] or not global_states["initialized"]:
186+
return
187+
for key, value in global_states["move_key_states"].items():
188+
if key in [
189+
JoyStickContinous.LeftXAxis,
190+
JoyStickContinous.LeftYAxis,
191+
JoyStickContinous.RightYAxis,
192+
JoyStickKey.ArrowUp,
193+
JoyStickKey.ArrowDown,
194+
JoyStickKey.ArrowLeft,
195+
JoyStickKey.ArrowRight,
196+
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+
217+
# Y coord
218+
if key == JoyStickContinous.LeftXAxis:
219+
mc.send_coord(2, y - value * 10, 10)
220+
global_states["origin"][1] += value
221+
time.sleep(0.05)
222+
# X coord
223+
elif key == JoyStickContinous.LeftYAxis:
224+
mc.send_coord(1, x - value * 10, 10)
225+
global_states["origin"][0] += value
226+
time.sleep(0.05)
227+
# Z coord
228+
elif key == JoyStickContinous.RightYAxis:
229+
mc.send_coord(3, z - value * 10, 10)
230+
global_states["origin"][2] += value
231+
time.sleep(0.05)
232+
elif key == JoyStickContinous.RightXAxis:
233+
mc.send_coord(6, rz - value * 10, 10)
234+
global_states["origin"][5] += value
235+
time.sleep(0.05)
236+
elif key == JoyStickKey.ArrowUp:
237+
mc.send_coord(4, rx + 10, 10)
238+
global_states["origin"][3] += 10
239+
time.sleep(0.05)
240+
elif key == JoyStickKey.ArrowDown:
241+
mc.send_coord(4, rx - 10, 10)
242+
global_states["origin"][3] -= 10
243+
time.sleep(0.05)
244+
elif key == JoyStickKey.ArrowLeft:
245+
mc.send_coord(5, ry - 10, 10)
246+
global_states["origin"][4] -= 10
247+
time.sleep(0.05)
248+
elif key == JoyStickKey.ArrowRight:
249+
mc.send_coord(5, ry + 10, 10)
250+
global_states["origin"][4] += 10
251+
time.sleep(0.05)
252+
253+
time.sleep(0.05)
254+
255+
222256
def dispatch_continous_key_action(key: JoyStickContinous, value: float):
223257
print(f"{key}:{value}")
224258

0 commit comments

Comments
 (0)