-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrapid_react_67.py
567 lines (492 loc) · 20.7 KB
/
rapid_react_67.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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
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
from rapid_react import RapidReactGameElementState
THREE_CARGO_TIME_LIMIT: float = 1.625
class IntakeSide(Enum):
'''Represents the side of intake'''
LEFT = 0
RIGHT = 1
class IntakePosition(Enum):
'''Represents the position'''
UP = 0
UNKNOWN = 1
DOWN = 2
@staticmethod
def from_y(y: float) -> 'IntakePosition':
'''Returns the position from a y coordinate'''
if y > 0.45:
return IntakePosition.UP
if y < 0.4:
return IntakePosition.DOWN
return IntakePosition.UNKNOWN
def __invert__(self):
match(self):
case IntakePosition.UP: return IntakePosition.DOWN
case IntakePosition.DOWN: return IntakePosition.UP
case IntakePosition.UNKNOWN: return IntakePosition.UNKNOWN
@dataclass
class RR67RobotState(RobotState):
'''Represents the current state of a robot'''
body: Element
hood: Element
left_intake: Element
right_intake: Element
climber_arm_1: Element
climber_arm_2: Element
climber_hook_1: Element
climber_hook_2: Element
parts: list[Element]
@staticmethod
def read(file: TextIOWrapper) -> tuple['RR67RobotState', 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
hood = None
left_intake = None
right_intake = None
climber_arm_1 = None
climber_arm_2 = None
climber_hook_1 = None
climber_hook_2 = None
parts = []
for element in elements:
if element.name is None:
parts.append(element)
elif 'Body' in element.name:
body = element
elif 'Indicator' in element.name:
hood = element
elif 'IntakeFlap1' in element.name:
left_intake = element
elif 'IntakeFlap2' in element.name:
right_intake = element
elif 'arm1' in element.name:
climber_arm_1 = element
elif 'arm2' in element.name:
climber_arm_2 = element
elif 'Hook1' in element.name:
climber_hook_1 = element
elif 'Hook2' in element.name:
climber_hook_2 = element
else:
parts.append(element)
return RR67RobotState(
body, hood,
left_intake, right_intake,
climber_arm_1, climber_arm_2,
climber_hook_1, climber_hook_2,
parts
), robot_info
def intake_position(self, side: IntakeSide) -> IntakePosition:
'''Returns the position of the intake'''
if side is IntakeSide.LEFT:
return IntakePosition.from_y(self.left_intake.local_position.y)
return IntakePosition.from_y(self.right_intake.local_position.y)
def __str__(self) -> str:
return f"Robot @ {self.body.global_position}"
@dataclass
class RR67State(State):
'''Represents the current state of everything'''
robot: RR67RobotState
elements: RapidReactGameElementState
game: GameState
gamepad: GamepadState
robot_info: RobotInfo
__distance_to_hub: float = None
__angle_from_hub: float = None
__angle_to_hub: float = None
__alliance_cargo_in_robot: list[Element] = None
__nearest_cargo: Element = None
__nearest_cargo_info: tuple[float, float, IntakeSide] = None
@staticmethod
def read(game_file: TextIOWrapper, element_file: TextIOWrapper,
robot_file: TextIOWrapper, gamepad: Gamepad) -> 'RR67State':
'''Reads the current state from the files'''
try:
game_state = GameState.read(game_file)
element_state = RapidReactGameElementState.read(element_file)
robot_state, robot_info = RR67RobotState.read(robot_file)
gamepad_state = gamepad.read()
return RR67State(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 distance_to_hub(self) -> float:
'''Returns the distance to the hub'''
if self.__distance_to_hub is None:
self.__distance_to_hub = math.hypot(
self.robot.body.global_position.x,
self.robot.body.global_position.z
)
return self.__distance_to_hub
def angle_from_hub(self) -> float:
'''Returns the angle from the hub to the robot'''
if self.__angle_from_hub is None:
self.__angle_from_hub = math.degrees(math.atan2(self.robot.body.global_position.x,
self.robot.body.global_position.z))
self.__angle_from_hub = Util.fix_angle(self.__angle_from_hub)
return self.__angle_from_hub
def angle_to_hub(self) -> float:
'''Returns the angle to the hub from the robot'''
if self.__angle_to_hub is None:
self.__angle_to_hub = self.angle_from_hub() - self.robot.body.global_rotation.y + 90
self.__angle_to_hub = Util.fix_angle(self.__angle_to_hub)
return self.__angle_to_hub
def __alliance_cargo_search(self) -> None:
'''Find the angle & distance to nearest cargo, nearest intake, and # of cargo in robot'''
if self.robot_info.alliance == Alliance.BLUE:
alliance_cargo = self.elements.blue_cargo
else:
alliance_cargo = self.elements.red_cargo
nearest_distance = float('inf')
nearest = Util.nearest_element(
self.robot.body.global_position, alliance_cargo,
0.4, -0.5
)
cargo_in_bot = Util.elements_within(
self.robot.body.global_position,
self.elements.blue_cargo + self.elements.red_cargo,
0.4
)
nearest_vector = self.robot.body.global_position - nearest.global_position
angle = math.degrees(math.atan2(nearest_vector.x, nearest_vector.z))
angle = angle - self.robot.body.global_rotation.y
angle = Util.fix_angle(angle)
# Wrap angle for dual intakes
if angle > 90:
angle -= 180
intake = IntakeSide.LEFT
elif angle < -90:
angle += 180
intake = IntakeSide.LEFT
else:
intake = IntakeSide.RIGHT
self.__nearest_cargo = nearest
self.__nearest_cargo_info = (angle, nearest_distance, intake)
self.__alliance_cargo_in_robot = cargo_in_bot
def cargo_in_robot(self) -> list[Element]:
'''Returns the cargo in the robot'''
if self.__alliance_cargo_in_robot is None:
self.__alliance_cargo_search()
return self.__alliance_cargo_in_robot
def nearest_cargo(self) -> Element:
'''Returns the nearest cargo'''
if self.__nearest_cargo is None:
self.__alliance_cargo_search()
return self.__nearest_cargo
def nearest_cargo_info(self) -> tuple[float, float, IntakeSide]:
'''Returns the angle to, distance to, and intake closest to the nearest cargo'''
if self.__nearest_cargo_info is None:
self.__alliance_cargo_search()
return self.__nearest_cargo_info
@dataclass
class RR67Controls(Controls):
'''Represents the current controls for a robot'''
reverse_intake: bool
toggle_right_intake: bool
toggle_left_intake: bool
shoot: bool
aim_down: bool
aim_up: bool
climber_extend: bool
climber_retract: bool
precision_left: bool
precision_right: bool
stop: bool
restart: bool
right_y: float
rotate: float
forward_reverse: float
strafe: float
climber_reverse: float
climber_forward: float
precision: float = 0.3
@staticmethod
def from_gamepad_state(gamepad: GamepadState) -> 'RR67Controls':
'''Returns the default controls'''
return RR67Controls(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.toggle_right_intake, self.toggle_left_intake,
self.shoot,
self.aim_down, self.aim_up,
self.climber_retract, self.climber_extend,
self.precision_left, self.precision_right,
self.stop, self.restart,
self.right_y, self.rotate,
self.forward_reverse, self.strafe,
self.climber_reverse, self.climber_forward,
self.precision
).write()
class RR67Command(Command):
'''Represents a command to modify the controls for the robot'''
def __init__(self):
pass
def __call__(self, state: RR67State, controls: RR67Controls) -> RR67Controls:
'''Executes the command'''
return controls
class TranslationCommand(RR67Command):
'''Automated control of rotation'''
def __call__(self, state: RR67State, controls: RR67Controls) -> RR67Controls:
'''Execute'''
if not state.gamepad.bumper_left or abs(state.gamepad.left_x) > 0.1:
return controls
(angle_to_nearest_cargo, distance_to_nearest_cargo,
nearest_intake) = state.nearest_cargo_info()
if abs(angle_to_nearest_cargo) > 30 or distance_to_nearest_cargo < 0.625:
return controls
if nearest_intake == IntakeSide.LEFT:
controls.strafe = -1.0
elif nearest_intake == IntakeSide.RIGHT:
controls.strafe = 1.0
return controls
class RotationCommand(RR67Command):
'''Automated control of rotation'''
def __init__(self):
super().__init__()
self.__pid = PID(-0.022, -0.000, -0.010, setpoint=0, dt=0.1, output_limits=(-1, 1))
def __call__(self, state: RR67State, controls: RR67Controls) -> RR67Controls:
'''Execute'''
# Gather data
angle_to_hub = state.angle_to_hub()
angle_to_nearest_cargo, _, _ = state.nearest_cargo_info()
# Determine controls
rotation = state.gamepad.right_x
if state.gamepad.bumper_right:
# Turn to hub
rotation = self.__pid(angle_to_hub)
# Logger.log(
# f"{datetime.isoformat(datetime.now())},"
# f"{state.robot.body.global_rotation.y},"
# f"{angle_to_hub},"
# f"{rotation},"
# f"{self.__pid._pid._proportional},"
# f"{self.__pid._pid._integral},"
# f"{self.__pid._pid._derivative}"
# )
elif state.gamepad.bumper_left:
# Turn to cargo
rotation = self.__pid(angle_to_nearest_cargo)
# Set controls
controls.rotate = rotation
return controls
class IntakeCommand(RR67Command):
'''Automated control of intake'''
class Mode(Enum):
'''Represents the mode'''
TWO_CARGO = 2
THREE_CARGO = 3
def __init__(self):
super().__init__()
self.__mode = IntakeCommand.Mode.THREE_CARGO
def __call__(self, state: RR67State, controls: RR67Controls) -> RR67Controls:
'''Execute'''
# Gather data
_, _, nearest_intake = state.nearest_cargo_info()
# Update mode
if state.gamepad.dpad_up and self.__mode != IntakeCommand.Mode.THREE_CARGO:
self.__mode = IntakeCommand.Mode.THREE_CARGO
print(f"Switching to {self.__mode.name}")
elif state.gamepad.dpad_down and self.__mode != IntakeCommand.Mode.TWO_CARGO:
self.__mode = IntakeCommand.Mode.TWO_CARGO
print(f"Switching to {self.__mode.name}")
# Determine controls
target_left_intake = IntakePosition.UNKNOWN
target_right_intake = IntakePosition.UNKNOWN
if self.__mode == IntakeCommand.Mode.TWO_CARGO:
# Keep both intakes up when aiming and only nearby intake down when intaking
if state.gamepad.bumper_right:
target_left_intake = IntakePosition.UP
target_right_intake = IntakePosition.UP
elif state.gamepad.bumper_left:
if nearest_intake == IntakeSide.LEFT:
target_left_intake = IntakePosition.DOWN
target_right_intake = IntakePosition.UP
elif nearest_intake == IntakeSide.RIGHT:
target_left_intake = IntakePosition.UP
target_right_intake = IntakePosition.DOWN
elif self.__mode == IntakeCommand.Mode.THREE_CARGO:
# Keep both intakes down in three cargo mode
target_left_intake = IntakePosition.DOWN
target_right_intake = IntakePosition.DOWN
body_position = state.robot.body.global_position
if state.game.phase in [GamePhase.READY, GamePhase.ENDGAME, GamePhase.FINISHED]:
# Keep both intakes up in the hangar in endgame
if (state.robot_info.alliance == Alliance.RED
and body_position.x < -0.875 and body_position.z < -4.5):
target_left_intake = IntakePosition.UP
target_right_intake = IntakePosition.UP
elif (state.robot_info.alliance == Alliance.BLUE
and body_position.x > 0.875 and body_position.z > 4.5):
target_left_intake = IntakePosition.UP
target_right_intake = IntakePosition.UP
# Allow manual override to opposite position while held
if target_left_intake != IntakePosition.UNKNOWN and state.gamepad.x:
target_left_intake = ~target_left_intake
if target_right_intake != IntakePosition.UNKNOWN and state.gamepad.b:
target_right_intake = ~target_right_intake
# Set controls
if target_left_intake != IntakePosition.UNKNOWN:
controls.toggle_left_intake = (
target_left_intake != state.robot.intake_position(IntakeSide.LEFT))
if target_right_intake != IntakePosition.UNKNOWN:
controls.toggle_right_intake = (
target_right_intake != state.robot.intake_position(IntakeSide.RIGHT))
return controls
class ShooterCommand(RR67Command):
'''Automated control of shooter'''
def __init__(self):
super().__init__()
self.__three_cargo_start = None
self.__bypass_enabled = False
def __call__(self, state: RR67State, controls: RR67Controls) -> RR67Controls:
'''Execute'''
# Gather data
cargo_in_robot = len(state.cargo_in_robot())
# Read controls
if state.gamepad.right_y < -0.9375 and not self.__bypass_enabled:
print('Bypassing cargo limit')
self.__bypass_enabled = True
elif state.gamepad.right_y > 0.25 and self.__bypass_enabled:
print('Disabling bypass')
self.__bypass_enabled = False
# Update cargo data
if self.__three_cargo_start is None:
if cargo_in_robot >= 3:
self.__three_cargo_start = time.time()
else:
if cargo_in_robot < 3:
self.__three_cargo_start = None
else:
time_left = THREE_CARGO_TIME_LIMIT - (time.time() - self.__three_cargo_start)
if time_left < 0.25 and not self.__bypass_enabled:
# Shoot cargo to avoid penalty
controls.shoot = True
return controls
class HoodCommand(RR67Command):
'''Automated control of the hood based on Eliot's angles'''
def __init__(self):
super().__init__()
self.__pid = PID(0.100, 0.001, 0.000, setpoint=0, output_limits=(-4, 4))
def __call__(self, state: RR67State, controls: RR67Controls) -> RR67Controls:
'''Execute'''
HOOD_ANGLES = [
165, 155, 147, 145, 140,
136, 127, 125, 120, 117,
110, 107, 102, 99, 94,
90, 86, 81, 77, 73,
69, 65, 60, 41, 40,
38, 35, 33, 31, 29,
27, 25, 22.5, 20, 0
]
distance_to_hub = state.distance_to_hub()
hood_index = int((distance_to_hub - 1.3) * 10)
hood_index = min(max(hood_index, 0), 34)
target_hood_angle = HOOD_ANGLES[hood_index]
hood_angle = state.robot.hood.local_rotation.x
if hood_angle >= 270:
hood_angle = (hood_angle - 450) * -1
elif hood_angle <= 90:
hood_angle = (hood_angle - 90) * -1
# Use PID control for the hood angle
angle_difference = target_hood_angle - hood_angle
angle_output = self.__pid(angle_difference)
controls.aim_up = angle_output < 0
controls.aim_down = angle_output > 0
controls.precision_left = True
controls.precision_right = True
controls.precision = abs(angle_output)
return controls
class ClimberCommand(RR67Command):
'''Automated control of the climber'''
def __init__(self):
super().__init__()
self.__pid = PID(-0.100, 0.000, 0.000, setpoint=0, output_limits=(-1, 1))
def __call__(self, state: RR67State, controls: RR67Controls) -> RR67Controls:
'''Execute'''
# Extend arms when in hangar during endgame
body_position = state.robot.body.global_position
if state.game.phase in [GamePhase.READY, GamePhase.ENDGAME, GamePhase.FINISHED]:
if (state.robot_info.alliance == Alliance.RED
and body_position.x < -0.875 and body_position.z < -4.5):
target_angle = 65
controls.climber_extend = True
elif (state.robot_info.alliance == Alliance.BLUE
and body_position.x > 0.875 and body_position.z > 4.5):
target_angle = 65
controls.climber_extend = True
else:
target_angle = 0
controls.climber_retract = True
else:
# Keep arms retracted
controls.climber_retract = True
target_angle = 0
# Control arm angle
if controls.climber_forward < 0.5 and controls.climber_reverse < 0.5:
if body_position.y > 0.25 and body_position.y < 0.75:
# Auto climb until up
controls.climber_forward = 1.0
elif body_position.y < 0.625:
# We use hook one but use similar logic to the dual intakes to move the nearest one
hook_angle = state.robot.climber_hook_1.local_rotation.z
if hook_angle > 180:
hook_angle -= 360
if hook_angle < -90:
hook_angle += 180
elif hook_angle > 90:
hook_angle -= 180
error = target_angle - hook_angle
control_output = self.__pid(error)
if control_output > 0:
controls.climber_forward = control_output
elif control_output < 0:
controls.climber_reverse = abs(control_output)
return controls
class RR67AutomationProvider(AutomationProvider):
'''Automated control of the Rapid React 67 robot'''
def __init__(self):
super().__init__()
self.__commands: tuple[RR67Command] = (
TranslationCommand(),
RotationCommand(),
IntakeCommand(),
ShooterCommand(),
HoodCommand(),
ClimberCommand()
)
def __call__(self, game_file: TextIOWrapper, element_file: TextIOWrapper,
robot_file: TextIOWrapper, gamepad: Gamepad) -> None:
'''Execute'''
state = RR67State.read(game_file, element_file, robot_file, gamepad)
if state is None:
return
control_outputs = RR67Controls.from_gamepad_state(state.gamepad)
for command in self.__commands:
control_outputs = command(state, control_outputs)
control_outputs.write()