-
Notifications
You must be signed in to change notification settings - Fork 1
/
charged_up_254.py
472 lines (429 loc) · 17.9 KB
/
charged_up_254.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
from dataclasses import dataclass
from datetime import datetime
from enum import Enum
from io import TextIOWrapper
import json
import math
import time
from cached_pid import PID
from models import AutomationProvider, Command, Controls, Element, Alliance, GamePhase, GameState, GamepadState, Gamepad, ControlOutput, Logger, RobotInfo, RobotState, State, Util, Vector
from charged_up import ChargedUpGameElementState
@dataclass
class CU254RobotState(RobotState):
'''Represents the current state of a robot'''
body: Element
not_updated: Element
lift: Element
slide: Element
slide_2: Element
intake_1: Element
intake_2: Element
intake_3: Element
intake_4: Element
lift_buddy: Element
buddy_hinge: Element
parts: list[Element]
@staticmethod
def read(file: TextIOWrapper) -> tuple['CU254RobotState', RobotInfo]:
'''Returns the current state of the robot'''
raw = file.read()
raw = raw.strip()
raw = json.loads(raw)
elements: list[Element] = []
robot_info: RobotInfo
for raw_object in raw['myrobot']:
element = Element.from_json(raw_object)
if isinstance(element, RobotInfo):
robot_info = element
else:
elements.append(element)
body = None
not_updated = None
lift = None
slide = None
slide_2 = None
intake_1 = None
intake_2 = None
intake_3 = None
intake_4 = None
lift_buddy = None
buddy_hinge = None
parts = []
for element in elements:
if element.name is None:
parts.append(element)
elif 'Body' in element.name:
body = element
elif 'NotUpdated' in element.name:
not_updated = element
elif 'Slide2' in element.name:
slide_2 = element
elif 'Slide' in element.name:
slide = element
elif 'Intake1' in element.name:
intake_1 = element
elif 'Intake2' in element.name:
intake_2 = element
elif 'Intake3' in element.name:
intake_3 = element
elif 'Intake4' in element.name:
intake_4 = element
elif 'LiftBuddy' in element.name:
lift_buddy = element
elif 'BuddyHinge' in element.name:
buddy_hinge = element
elif 'Lift' in element.name:
lift = element
else:
parts.append(element)
return CU254RobotState(
body,
not_updated,
lift,
slide, slide_2,
intake_1, intake_2, intake_3, intake_4,
lift_buddy, buddy_hinge,
parts
), robot_info
def __str__(self) -> str:
return f"Robot @ {self.body.global_position}"
@dataclass
class CU254State(State):
'''Represents the current state of everything'''
robot: CU254RobotState
elements: ChargedUpGameElementState
game: GameState
gamepad: GamepadState
robot_info: RobotInfo
_elements_in_intake: list[Element] = None
@staticmethod
def read(game_file: TextIOWrapper, element_file: TextIOWrapper,
robot_file: TextIOWrapper, gamepad: Gamepad) -> 'CU254State':
'''Reads the current state from the files'''
try:
game_state = GameState.read(game_file)
element_state = ChargedUpGameElementState.read(element_file)
robot_state, robot_info = CU254RobotState.read(robot_file)
gamepad_state = gamepad.read()
return CU254State(robot_state, element_state, game_state, gamepad_state, robot_info)
except json.JSONDecodeError:
return None # Error reading file, try again
except ValueError:
return None # Error reading file, try again
def __element_search(self):
body_position = self.robot.body.global_position
body_rotation = self.robot.body.global_rotation
nearest = Util.nearest_element(
body_position,
self.elements.cones + self.elements.cubes,
0.0,
-1.0
)
nearest_position = nearest.global_position
delta = nearest_position - body_position
lift_position = self.robot.lift.local_position
slide_position = self.robot.slide.local_position
slide_2_position = self.robot.slide_2.local_position
intake_1_position = self.robot.intake_1.local_position
intake_2_position = self.robot.intake_2.local_position
intake_3_position = self.robot.intake_3.local_position
intake_4_position = self.robot.intake_4.local_position
intake_average_position = (intake_1_position + intake_2_position +
intake_3_position + intake_4_position) / 4
intake_rotated_position = intake_average_position.rotate(body_rotation.y)
lift_rotated_position = slide_position.rotate(body_rotation.y)
slide_rotated_position = slide_position.rotate(body_rotation.y)
slide_2_rotated_position = slide_2_position.rotate(body_rotation.y)
guess_position = intake_rotated_position + slide_rotated_position + slide_2_rotated_position + lift_rotated_position
Logger.log(
f"{body_position.x},{body_position.y},{body_position.z},"
f"{body_rotation.x},{body_rotation.y},{body_rotation.z},"
f"{nearest_position.x},{nearest_position.y},{nearest_position.z},"
f"{delta.x},{delta.y},{delta.z},{abs(delta)},"
f"{lift_position.x},{lift_position.y},{lift_position.z},{abs(lift_position)},"
f"{slide_position.x},{slide_position.y},{slide_position.z},{abs(slide_position)},"
f"{slide_2_position.x},{slide_2_position.y},{slide_2_position.z},{abs(slide_2_position)},"
f"{intake_1_position.x},{intake_1_position.y},{intake_1_position.z},{abs(intake_1_position)},"
f"{intake_2_position.x},{intake_2_position.y},{intake_2_position.z},{abs(intake_2_position)},"
f"{intake_3_position.x},{intake_3_position.y},{intake_3_position.z},{abs(intake_3_position)},"
f"{intake_4_position.x},{intake_4_position.y},{intake_4_position.z},{abs(intake_4_position)},"
f"{intake_average_position.x},{intake_average_position.y},{intake_average_position.z},{abs(intake_average_position)},"
f"{intake_rotated_position.x},{intake_rotated_position.y},{intake_rotated_position.z},{abs(intake_rotated_position)},"
f"{lift_rotated_position.x},{lift_rotated_position.y},{lift_rotated_position.z},{abs(lift_rotated_position)},"
f"{slide_rotated_position.x},{slide_rotated_position.y},{slide_rotated_position.z},{abs(slide_rotated_position)},"
f"{slide_2_rotated_position.x},{slide_2_rotated_position.y},{slide_2_rotated_position.z},{abs(slide_2_rotated_position)},"
f"{guess_position.x},{guess_position.y},{guess_position.z},{abs(guess_position)},"
)
self._elements_in_intake = Util.elements_within(
intake_average_position,
self.elements.cones + self.elements.cubes,
0.2
)
def elements_in_intake(self) -> list[Element]:
if self._elements_in_intake is None:
self.__element_search()
return self._elements_in_intake
@dataclass
class CU254Controls(Controls):
'''Represents the current controls for a robot'''
reverse_intake: bool
slide_out: bool
slide_in: bool
toggle_climb: bool
stow_arm: bool
station_arm: bool
high_arm: bool
mid_arm: bool
bumper_left: bool
bumper_right: bool
stop: bool
restart: bool
right_y: float
rotate: float
forward_reverse: float
strafe: float
elevator_down: float
elevator_up: float
precision: float = 0.3
@staticmethod
def from_gamepad_state(gamepad: GamepadState) -> 'CU254Controls':
'''Returns the default controls'''
return CU254Controls(gamepad.a, gamepad.b, gamepad.x, gamepad.y,
gamepad.dpad_down, gamepad.dpad_up, gamepad.dpad_right, gamepad.dpad_left,
gamepad.bumper_left, gamepad.bumper_right,
gamepad.start, gamepad.back,
gamepad.right_y, gamepad.right_x,
gamepad.left_y, gamepad.left_x,
gamepad.trigger_left, gamepad.trigger_right)
def write(self) -> None:
'''Default controls for the robot'''
return ControlOutput(
self.reverse_intake, self.slide_out, self.slide_in,
self.toggle_climb,
self.stow_arm, self.station_arm,
self.mid_arm, self.high_arm,
self.bumper_left, self.bumper_right,
self.stop, self.restart,
self.right_y, self.rotate,
self.forward_reverse, self.strafe,
self.elevator_down, self.elevator_up,
self.precision
).write()
class CU254Command(Command):
'''Represents a command to modify the controls for the robot'''
def __init__(self):
pass
def __call__(self, state: CU254State, controls: CU254Controls) -> CU254Controls:
'''Executes the command'''
return controls
class ArmCommand(CU254Command):
'''Automated control of the arm'''
class PlaceMode(Enum):
'''Represents the mode'''
HIGH = 0
MID = 1
LOW = 2
class PickupMode(Enum):
'''Represents the mode'''
DOUBLE_SUBSTATION = 0
GROUND = 1
def __init__(self):
super().__init__()
self.__pid = PID(-5.000, 0.000, 0.000, setpoint=0,
output_limits=(-1, 1))
self.__place_mode = ArmCommand.PlaceMode.LOW
self.__dpad_up = False
self.__pickup_mode = ArmCommand.PickupMode.DOUBLE_SUBSTATION
self.__bumper_left = False
self.__ground_override = False
self.__bumper_right = False
self.__cone = True
self.__dpad_left = False
def _in_loading_zone(self, alliance: Alliance, position: Vector) -> bool:
match (alliance):
case Alliance.RED:
if position.z > 2 and position.x > 3.15:
return True
if position.z > 5.3 and position.x > 1.7:
return True
return False
case Alliance.BLUE:
if position.z < -2 and position.x > 3.15:
return True
if position.z < -5.3 and position.x > 1.7:
return True
return False
case _:
return False
def _in_community(self, alliance: Alliance, position: Vector) -> bool:
match (alliance):
case Alliance.RED:
if position.z < -3.8 and position.x < -2.8:
return True
if position.z < -5.0 and position.x < 0.4:
return True
if position.z < -5.3 and position.x < 1.1 and position.x > 0.4:
return True
return False
case Alliance.BLUE:
if position.z > 3.8 and position.x < -2.8:
return True
if position.z > 5.0 and position.x < 0.4:
return True
if position.z > 5.3 and position.x < 1.1 and position.x > 0.4:
return True
return False
case _:
return False
def _ground_pickup(self) -> tuple[float, float]:
return 0.000, 0.332
def _stow(self) -> tuple[float, float]:
return 0.130, 0.287
def _double_substation(self) -> tuple[float, float]:
return 0.858, 0.343
def _high(self, cone: bool, cube: bool) -> tuple[float, float]:
if cone:
return 0.978, 0.424
elif cube:
return 0.811, 0.378
else:
return self._ground_pickup()
def _mid(self, cone: bool, cube: bool) -> tuple[float, float]:
if cone:
return 0.811, 0.378
elif cube:
return 0.590, 0.325
else:
return self._ground_pickup()
def _low(self, cone: bool, cube: bool) -> tuple[float, float]:
if cone:
return 0.140, 0.332
elif cube:
return 0.140, 0.332
else:
return self._ground_pickup()
def __call__(self, state: CU254State, controls: CU254Controls) -> CU254Controls:
'''Execute'''
# Update modes
if state.gamepad.dpad_up:
if not self.__dpad_up:
match self.__place_mode:
case ArmCommand.PlaceMode.HIGH:
self.__place_mode = ArmCommand.PlaceMode.LOW
case ArmCommand.PlaceMode.MID:
self.__place_mode = ArmCommand.PlaceMode.HIGH
case ArmCommand.PlaceMode.LOW:
self.__place_mode = ArmCommand.PlaceMode.MID
print(f"Switching to {self.__place_mode.name}")
self.__dpad_up = True
else:
if self.__dpad_up:
self.__dpad_up = False
if state.gamepad.bumper_left:
if not self.__bumper_left:
match self.__pickup_mode:
case ArmCommand.PickupMode.DOUBLE_SUBSTATION:
self.__pickup_mode = ArmCommand.PickupMode.GROUND
case ArmCommand.PickupMode.GROUND:
self.__pickup_mode = ArmCommand.PickupMode.DOUBLE_SUBSTATION
print(f"Switching to {self.__pickup_mode.name}")
self.__bumper_left = True
else:
if self.__bumper_left:
self.__bumper_left = False
if state.gamepad.bumper_right:
if not self.__bumper_right:
if self.__ground_override:
self.__ground_override = False
print('Disabling GROUND OVERRIDE')
else:
self.__ground_override = True
print('Enabling GROUND OVERRIDE')
self.__bumper_right = True
else:
if self.__bumper_right:
self.__bumper_right = False
if state.gamepad.dpad_left:
if not self.__dpad_left:
if self.__cone:
self.__cone = False
print('CUBE')
else:
self.__cone = True
print('CONE')
self.__dpad_left = True
else:
if self.__dpad_left:
self.__dpad_left = False
# See what element we have (TODO)
state.elements_in_intake()
cone = self.__cone
cube = not cone
body_position = state.robot.body.global_position
target_elevator = 0
target_slider = 0
if self.__ground_override:
# Override to intake from ground
target_elevator, target_slider = self._ground_pickup()
else:
if self._in_loading_zone(state.robot_info.alliance, body_position):
# Go to pickup position when in the loading zone
match self.__pickup_mode:
case ArmCommand.PickupMode.DOUBLE_SUBSTATION:
target_elevator, target_slider = self._double_substation()
case ArmCommand.PickupMode.GROUND:
target_elevator, target_slider = self._ground_pickup()
elif self._in_community(state.robot_info.alliance, body_position):
# Go to placement position when in community
match self.__place_mode:
case ArmCommand.PlaceMode.HIGH:
target_elevator, target_slider = self._high(cone, cube)
case ArmCommand.PlaceMode.MID:
target_elevator, target_slider = self._mid(cone, cube)
case ArmCommand.PlaceMode.LOW:
target_elevator, target_slider = self._low(cone, cube)
else:
# Stay stowed when outside
target_elevator, target_slider = self._stow()
# Control elevator position
elevator_height = state.robot.lift.local_position.y
if controls.elevator_up < 0.5 and controls.elevator_down < 0.5:
error = target_elevator - elevator_height
control_output = self.__pid(error)
if control_output > 0:
controls.elevator_up = control_output
elif control_output < 0:
controls.elevator_down = abs(control_output)
# Control slider position
slider_height = state.robot.slide_2.local_position.y - elevator_height
if not controls.slide_out and not controls.slide_in:
error = target_slider - slider_height
if error > 0.01:
controls.slide_out = True
controls.slide_in = False
elif error < -0.01:
controls.slide_out = False
controls.slide_in = True
controls.high_arm = False
controls.mid_arm = False
controls.stow_arm = False
controls.station_arm = False
# print(f"{elevator_height:.3f} {slider_height:.3f}")
return controls
class CU254AutomationProvider(AutomationProvider):
'''Automated control of the Rapid React 67 robot'''
def __init__(self):
super().__init__()
self.__commands: tuple[CU254Command] = (
ArmCommand(),
)
def __call__(self, game_file: TextIOWrapper, element_file: TextIOWrapper,
robot_file: TextIOWrapper, gamepad: Gamepad) -> None:
'''Execute'''
state = CU254State.read(game_file, element_file, robot_file, gamepad)
if state is None:
return
control_outputs = CU254Controls.from_gamepad_state(state.gamepad)
for command in self.__commands:
control_outputs = command(state, control_outputs)
control_outputs.write()