-
Notifications
You must be signed in to change notification settings - Fork 0
/
input_providers.py
109 lines (84 loc) · 3.48 KB
/
input_providers.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
98
99
100
101
102
103
104
105
106
107
108
109
import pandas as pd
import pygame
from pid_controller import PidController
class InputProvider:
def get_input(self):
pass
class RecordedInputProvider(InputProvider):
def __init__(self, csv_path):
self.input_dataframe = pd.read_csv(csv_path, index_col=0)
self.index = self.input_dataframe.first_valid_index()
def get_input(self):
if self.index <= self.input_dataframe.last_valid_index():
throttle = self.input_dataframe.Throttle[self.index]
brakes = self.input_dataframe.Brakes[self.index]
steering = self.input_dataframe.Steering[self.index]
gear = self.input_dataframe.Gear[self.index]
self.index += 1
return throttle, gear, brakes, steering
class JoystickInputProvider(InputProvider):
def __init__(self):
self.gear = 0
self.gear_timer = 0
self.clock = pygame.time.Clock()
self.joysticks = []
for i in range(pygame.joystick.get_count()):
self.joysticks.append(pygame.joystick.Joystick(i))
self.joysticks[-1].init()
print("Detected pad:", self.joysticks[-1].get_name())
def get_input(self):
self.gear_timer += self.clock.tick()
throttle = - self.joysticks[-1].get_axis(3)
steering = - self.joysticks[-1].get_axis(4) * 30
if self.joysticks[-1].get_axis(2) < - 0.5:
brakes = - self.joysticks[-1].get_axis(2) * 5000
else:
brakes = 0
if - self.joysticks[-1].get_axis(1) > 0.9 and self.gear < 6 and self.gear_timer > 300:
self.gear += 1
self.gear_timer = 0
elif - self.joysticks[-1].get_axis(1) < -0.9 and self.gear > -1 and self.gear_timer > 300:
self.gear -= 1
self.gear_timer = 0
return throttle, self.gear, brakes, steering
class KeyboardInputProvider(InputProvider):
def __init__(self):
self.gear = 0
self.gear_timer = 0
self.clock = pygame.time.Clock()
def get_input(self):
steering = 0
self.gear_timer += self.clock.tick()
if pygame.key.get_pressed()[pygame.K_UP]:
throttle = 1
else:
throttle = 0
if pygame.key.get_pressed()[pygame.K_DOWN]:
brakes = 1
else:
brakes = 0
if pygame.key.get_pressed()[pygame.K_LEFT]:
steering = 20
if pygame.key.get_pressed()[pygame.K_RIGHT]:
steering = -20
if pygame.key.get_pressed()[pygame.K_q] and self.gear < 6 and self.gear_timer > 300:
self.gear += 1
self.gear_timer = 0
if pygame.key.get_pressed()[pygame.K_a] and self.gear > -1 and self.gear_timer > 300:
self.gear -= 1
self.gear_timer = 0
return throttle, self.gear, brakes, steering
class AutonomousDriver(InputProvider):
def __init__(self, input_file=None):
self.input = input_file
self.index = self.input.first_valid_index()
self.pid_controller = PidController(0, 0, 0)
self.line_error = 0
def get_input(self):
self.pid_controller.p_gain = self.input.P[self.index]
self.pid_controller.i_gain = self.input.I[self.index]
self.pid_controller.d_gain = self.input.D[self.index]
throttle = self.input.Throttle[self.index]
gear = self.input.Gear[self.index]
brakes = self.input.Brakes[self.index]
return throttle, gear, brakes, self.pid_controller.get_control(self.line_error)