-
Notifications
You must be signed in to change notification settings - Fork 4
/
Joystick.py
97 lines (85 loc) · 3.18 KB
/
Joystick.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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import pygame
import platform
import os
import logging
from numpy import interp
pygame.init()
clock = pygame.time.Clock()
class Joystick(object):
def __init__(self):
# get pygame ready
os.putenv('DISPLAY', ':0.0')
pygame.display.set_mode((1, 1))
js = pygame.joystick.Joystick(0)
js.init()
self.name = js.get_name()
self.is_forward = 1
self.is_manual_mode = True
self.is_cruise_mode = False
self.throttle = 0.0
self.direction = 90
self.should_exit = False
self.event = {}
self.key_mapping = self.get_key_mapping()
def get_key_mapping(self):
if platform.system() == "Darwin":
# mac with wired controller
if self.name == "Wireless Controller":
# ps4 controller via wire
return {
"SWITCH_MODE_BTN": 12,
"STOP_BTN": 2,
"STEER_AXIS": 0,
"THROTTLE_AXIS": 5,
"GEAR_CHANGE_AXIS": 3,
"CRUISE_MODE": 1,
}
elif self.name == "Xbox One Wired Controller":
# xbox one controller via wire
return {
"SWITCH_MODE_BTN": 10,
"STOP_BTN": 12,
"STEER_AXIS": 0,
"THROTTLE_AXIS": 5,
"GEAR_CHANGE_AXIS": 3,
"CRUISE_MODE": 11,
}
elif platform.system() == "Linux":
# raspberry pi
return {
"SWITCH_MODE_BTN": 8,
"STOP_BTN": 1,
"STEER_AXIS": 0,
"THROTTLE_AXIS": 5,
"GEAR_CHANGE_AXIS": 4,
"CRUISE_MODE": 2
}
def get_event(self):
clock.tick(30)
js = self
js.event = {}
for ev in pygame.event.get(pygame.JOYAXISMOTION):
if ev.axis == js.key_mapping["STEER_AXIS"]:
js.direction = interp(ev.value, [-1, 1], [0, 180])
js.event["direction"] = round(js.direction, 2)
if ev.axis == js.key_mapping["GEAR_CHANGE_AXIS"]:
if ev.value < (-0.7):
js.is_forward = 1
elif ev.value > (0.7):
js.is_forward = -1
if ev.axis == js.key_mapping["THROTTLE_AXIS"]:
js.throttle = (
interp(ev.value, [-1, 1], [0, 1])) * js.is_forward
js.event["throttle"] = round(js.throttle, 2)
for ev in pygame.event.get(pygame.JOYBUTTONDOWN):
logging.debug("button pressed: %d", ev.button)
if ev.button == js.key_mapping["STOP_BTN"]:
js.should_exit = True
js.event["should_exit"] = js.should_exit
if ev.button == js.key_mapping["SWITCH_MODE_BTN"]:
js.is_manual_mode = not js.is_manual_mode
js.event["is_manual_mode"] = js.is_manual_mode
if ev.button == js.key_mapping["CRUISE_MODE"]:
js.is_cruise_mode = not js.is_cruise_mode
js.event["is_cruise_mode"] = js.is_cruise_mode
return self.event