-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.py
211 lines (187 loc) · 5.22 KB
/
handler.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import json
import math
import threading
from inputs import get_gamepad
PATH = "C:\\tmp\\xRCsim\\"
X,Y,Z = 0,1,2
class BoneID:
INFO = 0
BODY = 1
LIFT = 3
SLIDE1 = 4
SLIDE2 = 5
class Time:
GAME = 2.5 * 60 * 1000
AUTO = 15 * 1000
TELEOP = (2.5 * 60 - 15) * 1000
class GameHandler():
def __init__(self):
self.buttons = {
"a": 0,
"b": 0,
"x": 0,
"y": 0,
"dpad_down": 0,
"dpad_up": 0,
"dpad_left": 0,
"dpad_right": 0,
"bumper_l": 0,
"bumper_r": 0,
"stop": 0,
"restart": 0,
"right_y": 0,
"right_x": 0,
"left_y": 0,
"left_x": 0,
"trigger_l": 0,
"trigger_r": 0,
}
self.time_left_ms = 0
self.robot = {}
self.game_elements = []
def Reset(self):
self.buttons = {
"a": 0,
"b": 0,
"x": 0,
"y": 0,
"dpad_down": 0,
"dpad_up": 0,
"dpad_left": 0,
"dpad_right": 0,
"bumper_l": 0,
"bumper_r": 0,
"stop": 0,
"restart": 0,
"right_y": 0,
"right_x": 0,
"left_y": 0,
"left_x": 0,
"trigger_l": 0,
"trigger_r": 0,
}
def Format(self):
return "\n".join(f"{key}={value}" for key,value in self.buttons.items())
def Write(self):
with open(PATH + "Controls.txt","w+") as file:
file.write(self.Format())
def ReadAll(self):
return self.ReadTime(), self.ReadRobot(), self.ReadElements()
def ReadTime(self):
try:
with open(PATH + "GAME_STATE.txt","r") as file:
self.time_left_ms = float(file.read().split("Time Left (ms) =")[-1].split("\n")[0])
except:
pass
return self.time_left_ms
def ReadRobot(self):
try:
with open(PATH + "myRobot.txt","r") as file:
self.robot = json.load(file)
except json.JSONDecodeError:
pass
return self.robot
def ReadElements(self):
try:
with open(PATH + "GameElements.txt","r") as file:
self.game_elements = json.load(file)
except json.JSONDecodeError:
pass
return self.game_elements
class XboxController(object):
MAX_TRIG_VAL = math.pow(2, 8)
MAX_JOY_VAL = 32768
def __init__(self):
self.LeftJoystickY = 0
self.LeftJoystickX = 0
self.RightJoystickY = 0
self.RightJoystickX = 0
self.LeftTrigger = 0
self.RightTrigger = 0
self.LeftBumper = 0
self.RightBumper = 0
self.A = 0
self.X = 0
self.Y = 0
self.B = 0
self.LeftThumb = 0
self.RightThumb = 0
self.Back = 0
self.Start = 0
self.LeftDPad = 0
self.RightDPad = 0
self.UpDPad = 0
self.DownDPad = 0
self._monitor_thread = threading.Thread(target=self._monitor_controller, args=())
self._monitor_thread.daemon = True
self._monitor_thread.start()
def read(self): # return the buttons/triggers that you care about in this methode
return {
"a": self.A,
"b": self.B,
"x": self.X,
"y": self.Y,
"dpad_down": 0,
"dpad_up": 0,
"dpad_left": 0,
"dpad_right": 0,
"bumper_l": 0,
"bumper_r": 0,
"stop": self.Back,
"restart": self.Start,
"right_y": self.RightJoystickY,
"right_x": self.RightJoystickX,
"left_y": -self.LeftJoystickY,
"left_x": self.LeftJoystickX,
"trigger_l": self.LeftTrigger,
"trigger_r": self.RightTrigger,
"auto_score": self.RightBumper,
"auto_aim": self.LeftBumper,
"scoring_up": self.UpDPad,
"scoring_down": self.DownDPad,
"scoring_left": self.LeftDPad,
"scoring_right": self.RightDPad,
}
def _monitor_controller(self):
while True:
events = get_gamepad()
for event in events:
if event.code == 'ABS_Y':
self.LeftJoystickY = event.state / XboxController.MAX_JOY_VAL # normalize between -1 and 1
elif event.code == 'ABS_X':
self.LeftJoystickX = event.state / XboxController.MAX_JOY_VAL # normalize between -1 and 1
elif event.code == 'ABS_RY':
self.RightJoystickY = event.state / XboxController.MAX_JOY_VAL # normalize between -1 and 1
elif event.code == 'ABS_RX':
self.RightJoystickX = event.state / XboxController.MAX_JOY_VAL # normalize between -1 and 1
elif event.code == 'ABS_Z':
self.LeftTrigger = event.state / XboxController.MAX_TRIG_VAL # normalize between 0 and 1
elif event.code == 'ABS_RZ':
self.RightTrigger = event.state / XboxController.MAX_TRIG_VAL # normalize between 0 and 1
elif event.code == 'BTN_TL':
self.LeftBumper = event.state
elif event.code == 'BTN_TR':
self.RightBumper = event.state
elif event.code == 'BTN_SOUTH':
self.A = event.state
elif event.code == 'BTN_NORTH':
self.Y = event.state #previously switched with X
elif event.code == 'BTN_WEST':
self.X = event.state #previously switched with Y
elif event.code == 'BTN_EAST':
self.B = event.state
elif event.code == 'BTN_THUMBL':
self.LeftThumb = event.state
elif event.code == 'BTN_THUMBR':
self.RightThumb = event.state
elif event.code == 'BTN_SELECT':
self.Back = event.state
elif event.code == 'BTN_START':
self.Start = event.state
elif event.code == 'ABS_HAT0X':
self.LeftDPad = int(event.state < 0)
self.RightDPad = int(event.state > 0)
elif event.code == 'ABS_HAT0Y':
self.UpDPad = int(event.state < 0)
self.DownDPad = int(event.state > 0)
# print(event.code, event.state)