-
Notifications
You must be signed in to change notification settings - Fork 0
/
joystick-control.py
47 lines (38 loc) · 1.3 KB
/
joystick-control.py
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
from evdev import InputDevice, list_devices, ecodes
import robot
def store_x(value):
current_x = value;
def store_y(value):
current_y = value;
def limit_value(value):
if value > 100:
value = 100
elif value < -100:
value = -100
return value
def update_motor_powers():
power_l = limit_value((current_y / 32768 * 100) + (current_x / 32768 * 100))
power_r = limit_value((current_y / 32768 * 100) - (current_x / 32768 * 100))
robot.left (power_l)
robot.right(power_r)
devices = [InputDevice(device) for device in list_devices()]
keyboard = devices[0]
print(keyboard)
keypress_actions = {
ecodes.ABS_X: store_x,
ecodes.ABS_Y: store_y
}
current_x = 0
current_y = 0
power_l = 0
power_r = 0
try:
#with open("values.log", "w") as log:
for event in keyboard.read_loop():
if event.code in keypress_actions:
#print(categorized, categorized.event.code, categorized.event.sec, categorized.event.timestamp, categorized.event.type, categorized.event.usec, categorized.event.value, file=log)
keypress_actions[event.code](event.value)
update_motor_powers()
#print("x:", current_x, "y:", current_y, "power_l:", power_l, "power_r:", power_r, file=log)
except KeyboardInterrupt:
robot.stop()