-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path3_Keyboard-Teleop.py
More file actions
74 lines (62 loc) · 2.33 KB
/
3_Keyboard-Teleop.py
File metadata and controls
74 lines (62 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from pal.products.qcar import QCar
from pynput import keyboard
# --- Control state ---
throttle = 0.0
steering = 0.0
THROTTLE_STEP = 0.3 # Increment per keypress (-2.0 to 2.0 range)
STEERING_STEP = 0.2 # Increment per keypress (-0.5 to 0.5 range)
THROTTLE_MAX = 2.0
THROTTLE_MIN = -2.0
STEERING_MAX = 0.5
STEERING_MIN = -0.5
def on_press(key):
global throttle, steering
try:
if key == keyboard.Key.up:
throttle = min(throttle + THROTTLE_STEP, THROTTLE_MAX)
elif key == keyboard.Key.down:
throttle = max(throttle - THROTTLE_STEP, THROTTLE_MIN)
elif key == keyboard.Key.right:
steering = min(steering + STEERING_STEP, STEERING_MAX)
elif key == keyboard.Key.left:
steering = max(steering - STEERING_STEP, STEERING_MIN)
elif key == keyboard.Key.space:
# Emergency stop
throttle = 0.0
steering = 0.0
print("\n[SPACE] Emergency stop triggered.")
except AttributeError:
pass # Ignore non-special keys
def on_release(key):
global throttle, steering
# Gradually reset to neutral on key release
if key in (keyboard.Key.up, keyboard.Key.down):
throttle = 0.0
elif key in (keyboard.Key.left, keyboard.Key.right):
steering = 0.0
# --- Start keyboard listener (non-blocking, runs in background thread) ---
listener = keyboard.Listener(on_press=on_press, on_release=on_release)
listener.start()
myCar = QCar(readMode=1, frequency=10)
print("Arrow Key Control Active:")
print(" ↑ / ↓ : Throttle forward / backward")
print(" ← / → : Steer left / right")
print(" SPACE : Emergency stop")
print(" CTRL+C : Quit\n")
try:
while True:
myCar.write(throttle, steering)
myCar.read()
print(
f"Throttle: {throttle:+.2f} | Steering: {steering:+.2f} | "
f"Battery: {myCar.batteryVoltage:.2f} V | "
f"Motor Current: {myCar.motorCurrent:.2f} A | "
f"Tach: {myCar.motorTach:.2f}",
end="\r" # Overwrite the same line for clean output
)
except KeyboardInterrupt:
print("\n\nProgram stopped by user (CTRL+C).")
finally:
listener.stop()
myCar.write(0.0, 0.0)
print("Motors stopped. Safe to disconnect.")