-
Notifications
You must be signed in to change notification settings - Fork 0
/
joysticks.py
69 lines (54 loc) · 2.19 KB
/
joysticks.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
from pygame import joystick, time
import threading
import math
import options
def clamp(input: float) -> int:
return int(input) if abs(input) < 1000 else int(math.copysign(1000, input))
def deadband(input: float) -> float:
if input < options.JOY_DEADBAND and input > -options.JOY_DEADBAND:
return 0
else:
return (input - math.copysign(options.JOY_DEADBAND, input)) / (1 - options.JOY_DEADBAND)
def active_steering(turn, speed):
return turn / (0.1 * abs(speed) + 1)
class Controller(threading.Thread):
def __init__(self, id, *args, **kwargs):
super(Controller, self).__init__(*args, **kwargs)
self.joy = joystick.Joystick(id)
self.values = [0] * self.joy.get_numaxes()
self.hat = [0, 0]
def run(self):
clock = time.Clock()
while 1:
self.joy.init()
self.values = [self.joy.get_axis(i) for i in range(self.joy.get_numaxes())]
self.values = [deadband(i) for i in self.values]
self.values[0] = active_steering(self.values[0], self.values[1]) # axis 0, 1 are turn, speed respectively
self.values = [clamp(i * 1000 * options.SPEEDRATIO) for i in self.values]
self.values[1] = -self.values[1]
self.hat[:] = self.joy.get_hat(0)
self.hat[0] = -self.hat[0]
self.joy.quit()
clock.tick(60)
def bytes(self):
return (i.to_bytes(2, 'little', signed=True) for i in self)
def __len__(self):
return len(self.values) + len(self.hat)
def __getitem__(self, idx):
return self.values[idx] if idx < 4 else self.hat[idx - 4]
def get_controller():
joy_id = 0
joystick.init()
if joystick.get_count() > 1:
for i in range(joystick.get_count()):
print(i, joystick.Joystick(i).get_name())
joy_id = int(input('There are multiple joysticks which would you like to use? '))
controller = Controller(joy_id, daemon=True)
controller.start()
return controller
if __name__ == '__main__':
controller = get_controller()
clock = time.Clock()
while(1):
print(', '.join(f'{i}'.rjust(6) for i in controller), end='\r')
clock.tick(120)