|
1 | | -from magicbot import StateMachine, timed_state |
| 1 | +import math |
2 | 2 |
|
3 | | -from components.coral_placer import CoralPlacerComponent |
| 3 | +from magicbot import StateMachine, state, tunable |
| 4 | + |
| 5 | +from components.chassis import ChassisComponent |
| 6 | +from components.wrist import WristComponent |
4 | 7 |
|
5 | 8 |
|
6 | 9 | class CoralPlacer(StateMachine): |
7 | | - coral_placer_component: CoralPlacerComponent |
| 10 | + wrist: WristComponent |
| 11 | + chassis: ChassisComponent |
| 12 | + |
| 13 | + # In degrees for tuning, converted to radians in tilt-to call |
| 14 | + CORAL_PLACE_ANGLE = tunable(0.0) |
| 15 | + CORAL_LOWER_ANGLE = tunable(-20.0) |
| 16 | + |
| 17 | + RETREAT_DISTANCE = tunable(0.2) |
8 | 18 |
|
9 | 19 | def __init__(self): |
10 | | - pass |
| 20 | + self.coral_scored = False |
11 | 21 |
|
12 | | - def place(self): |
| 22 | + def place(self) -> None: |
| 23 | + self.engage("lowering", force=True) |
| 24 | + |
| 25 | + def lift(self) -> None: |
13 | 26 | self.engage() |
14 | 27 |
|
15 | | - @timed_state(duration=2.0, first=True, must_finish=True) |
16 | | - def placing(self): |
17 | | - self.coral_placer_component.place() |
| 28 | + def coral_is_scored(self) -> bool: |
| 29 | + return self.coral_scored |
| 30 | + |
| 31 | + @state(first=True, must_finish=True) |
| 32 | + def raising(self, initial_call: bool) -> None: |
| 33 | + if initial_call: |
| 34 | + self.coral_scored = False |
| 35 | + self.wrist.tilt_to(math.radians(self.CORAL_PLACE_ANGLE)) |
| 36 | + |
| 37 | + @state(must_finish=True) |
| 38 | + def lowering(self, initial_call: bool) -> None: |
| 39 | + if initial_call: |
| 40 | + self.wrist.tilt_to(math.radians(self.CORAL_LOWER_ANGLE)) |
| 41 | + if self.wrist.at_setpoint(): |
| 42 | + self.coral_scored = True |
| 43 | + self.next_state("safing") |
| 44 | + |
| 45 | + @state(must_finish=True) |
| 46 | + def safing(self, initial_call: bool) -> None: |
| 47 | + if initial_call: |
| 48 | + self.score_pos = self.chassis.get_pose() |
| 49 | + |
| 50 | + current_pose = self.chassis.get_pose() |
| 51 | + |
| 52 | + distance = self.score_pos.translation().distance(current_pose.translation()) |
| 53 | + |
| 54 | + if distance >= self.RETREAT_DISTANCE: |
| 55 | + self.wrist.go_to_neutral() |
| 56 | + self.done() |
| 57 | + return |
0 commit comments