Skip to content

Commit 201b8ac

Browse files
committed
cp
1 parent e511530 commit 201b8ac

File tree

5 files changed

+208
-26
lines changed

5 files changed

+208
-26
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ __pycache__/port.cpython-39.pyc
2222
demo/__pycache__/port.cpython-39.pyc
2323
demo/__pycache__/port_setup.cpython-39.pyc
2424
.idea
25+
.vscode

.vscode/settings.json

Lines changed: 0 additions & 25 deletions
This file was deleted.

demo/handle_control/arm_profile/mycobot_280.json

Whitespace-only changes.

demo/handle_control/handle_control.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from pymycobot import MyCobot
55
import threading
66

7-
mc = MyCobot("com7")
7+
mc = MyCobot("/dev/ttyAMA0")
88

99
# The default initial point can be changed, if you want to change, you should change 'command' and 'zero' at the same time
1010
command = [144.8, -66.9, 185.3, 178.47, 0.87, -115.07]
@@ -371,6 +371,7 @@ def main():
371371

372372
pygame.quit()
373373

374+
374375
if __name__ == "__main__":
375376
# pass
376377
t = threading.Thread(target=control)

demo/handle_control/joy_control.py

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
import pygame
2+
import time
3+
from pymycobot import MyCobot
4+
from threading import Thread
5+
from enum import Enum
6+
import typing as T
7+
8+
mc = MyCobot("/dev/ttyAMA0")
9+
context = {"running": True}
10+
arm_speed = 50
11+
sampling_rate = 10
12+
13+
14+
class JoyStickKey(Enum):
15+
StartKey = 7
16+
SelectKey = 6
17+
ModeKey = 8
18+
RLeftKey = 2
19+
RRightKey = 1
20+
RTopKey = 3
21+
RBottomKey = 0
22+
R1 = 5
23+
L1 = 4
24+
LJoyStickKey = 9
25+
RJoyStickKey = 10
26+
ArrowUp = (0, 1)
27+
ArrowDown = (0, -1)
28+
ArrowLeft = (-1, 0)
29+
ArrowRight = (1, 0)
30+
ArrowReleased = (0, 0)
31+
32+
33+
class JoyStickContinous(Enum):
34+
LeftXAxis = 0
35+
LeftYAxis = 1
36+
L2 = 2
37+
RightXAxis = 3
38+
RightYAxis = 4
39+
R2 = 5
40+
41+
42+
joystick_key_map = {
43+
0: JoyStickKey.RBottomKey,
44+
1: JoyStickKey.RRightKey,
45+
2: JoyStickKey.RLeftKey,
46+
3: JoyStickKey.RTopKey,
47+
4: JoyStickKey.L1,
48+
5: JoyStickKey.R1,
49+
6: JoyStickKey.SelectKey,
50+
7: JoyStickKey.StartKey,
51+
8: JoyStickKey.ModeKey,
52+
9: JoyStickKey.LJoyStickKey,
53+
10: JoyStickKey.RJoyStickKey,
54+
(0, 1): JoyStickKey.ArrowUp,
55+
(0, -1): JoyStickKey.ArrowDown,
56+
(1, 0): JoyStickKey.ArrowRight,
57+
(-1, 0): JoyStickKey.ArrowLeft,
58+
(0, 0): JoyStickKey.ArrowReleased,
59+
}
60+
61+
joystick_continous_map = {
62+
0: JoyStickContinous.LeftXAxis,
63+
1: JoyStickContinous.LeftYAxis,
64+
2: JoyStickContinous.L2,
65+
3: JoyStickContinous.RightXAxis,
66+
4: JoyStickContinous.RightYAxis,
67+
5: JoyStickContinous.R2,
68+
}
69+
70+
71+
def get_init_key_hold_timestamp():
72+
return {
73+
JoyStickKey.L1: -1,
74+
JoyStickKey.R1: -1,
75+
JoyStickContinous.L2: -1,
76+
JoyStickContinous.R2: -1,
77+
}
78+
79+
80+
key_hold_timestamp = get_init_key_hold_timestamp()
81+
82+
83+
def get_joystick():
84+
pygame.init()
85+
pygame.joystick.init()
86+
87+
try:
88+
joystick = pygame.joystick.Joystick(0)
89+
except:
90+
print("Please connect the handle first.")
91+
exit(1)
92+
joystick.init()
93+
return joystick
94+
95+
96+
def dispatch_key_action(key: T.Union[JoyStickKey, JoyStickContinous], value: float):
97+
global mc, key_hold_timestamp
98+
print(f"key {key}")
99+
100+
if key == JoyStickKey.StartKey:
101+
if mc.is_all_servo_enable() != 1:
102+
mc.set_color(0, 0, 0)
103+
time.sleep(0.5)
104+
mc.set_color(255, 0, 0)
105+
time.sleep(0.5)
106+
mc.set_color(0, 0, 0)
107+
time.sleep(0.5)
108+
mc.set_color(255, 0, 0)
109+
time.sleep(0.5)
110+
mc.set_color(0, 0, 0)
111+
time.sleep(0.5)
112+
mc.set_color(255, 0, 0)
113+
else:
114+
mc.set_color(0, 0, 0)
115+
time.sleep(0.5)
116+
mc.set_color(0, 255, 0)
117+
time.sleep(0.5)
118+
mc.set_color(0, 0, 0)
119+
time.sleep(0.5)
120+
mc.set_color(0, 255, 0)
121+
time.sleep(0.5)
122+
mc.set_color(0, 0, 0)
123+
time.sleep(0.5)
124+
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+
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()
151+
152+
153+
def dispatch_continous_key_action(key: JoyStickContinous, value: float):
154+
print(f"{key}:{value}")
155+
156+
157+
def retreive_joystick_input(joystick, context):
158+
while context["running"]:
159+
for event in pygame.event.get():
160+
if event.type == pygame.QUIT:
161+
running = False
162+
elif event.type == pygame.JOYBUTTONDOWN or event.type == pygame.JOYBUTTONUP:
163+
n = joystick.get_numbuttons()
164+
for key_id in range(n):
165+
button_status = joystick.get_button(key_id)
166+
if not button_status:
167+
continue
168+
dispatch_key_action(joystick_key_map[key_id], 1.0)
169+
170+
elif event.type == pygame.JOYAXISMOTION:
171+
axes = joystick.get_numaxes()
172+
for key_id in range(axes):
173+
axis = joystick.get_axis(key_id)
174+
dispatch_continous_key_action(joystick_continous_map[key_id], axis)
175+
176+
elif event.type == pygame.JOYHATMOTION:
177+
hat = joystick.get_hat(0)
178+
dispatch_key_action(joystick_key_map[hat], 1.0)
179+
180+
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+
203+
if __name__ == "__main__":
204+
joystick = get_joystick()
205+
Thread(target=retreive_joystick_input, args=(joystick, context)).start()

0 commit comments

Comments
 (0)