Skip to content

Commit 726e950

Browse files
SLEEP IS FOR THE WEAK, make measure retraction based on velocity of the motors
1 parent 0a4aa8b commit 726e950

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

components/injector.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ def get_injector_positions(self) -> tuple[float, float]:
7878
self.injector_2_encoder.getPosition(),
7979
)
8080

81+
def get_injector_velocities(self) -> tuple[float, float]:
82+
return (
83+
self.injector_1_encoder.getVelocity(),
84+
self.injector_2_encoder.getVelocity(),
85+
)
86+
8187
def execute(self) -> None:
8288
if self.has_algae():
8389
self.has_seen_algae = True

controllers/algae_measurement.py

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from magicbot import StateMachine, feedback, state, timed_state
1+
import time
2+
3+
from magicbot import StateMachine, feedback, state, tunable
24

35
from components.injector import InjectorComponent
46
from components.shooter import ShooterComponent
@@ -9,11 +11,14 @@ class AlgaeMeasurement(StateMachine):
911
shooter_component: ShooterComponent
1012
injector_component: InjectorComponent
1113

14+
retraction_speed = tunable(-4.0)
15+
1216
def __init__(self) -> None:
1317
self.injector_starting_positions = (0.0, 0.0)
1418
self.flywheel_starting_positions = (0.0, 0.0)
1519
self.measured_sizes: list[float] = []
1620
self.measured_raw_sizes: list[float] = []
21+
self.recovery_start_time = 0
1722

1823
def measure(self) -> None:
1924
self.engage()
@@ -25,13 +30,19 @@ def initialising(self) -> None:
2530
if all(abs(v) <= 0.0001 for v in self.shooter_component.flywheel_speeds()):
2631
self.next_state("calculating")
2732

28-
@timed_state(
29-
duration=0.5,
30-
next_state="measuring",
31-
must_finish=True,
32-
)
33-
def pre_measure(self) -> None:
34-
self.injector_component.desired_injector_voltage = -2.0
33+
state(must_finish=True)
34+
35+
def pre_measure(self, initial_call) -> None:
36+
if initial_call:
37+
self.recovery_start_time = time.monotonic()
38+
self.injector_component.desired_injector_voltage = self.retraction_speed
39+
40+
if (
41+
(time.monotonic() - self.recovery_start_time) > 0.01
42+
and all(v < 0.01 for v in self.injector_component.get_injector_velocities())
43+
or (time.monotonic() - self.recovery_start_time) > 1.0
44+
):
45+
self.next_state("measuring")
3546

3647
@state(must_finish=True)
3748
def calculating(self) -> None:
@@ -77,9 +88,17 @@ def measuring(self, initial_call) -> None:
7788

7889
self.next_state("calculating")
7990

80-
@timed_state(duration=0.5, must_finish=True)
81-
def recovering(self) -> None:
82-
self.injector_component.desired_injector_voltage = -2.0
91+
@state(must_finish=True)
92+
def recovering(self, initial_call) -> None:
93+
if initial_call:
94+
self.recovery_start_time = time.monotonic()
95+
self.injector_component.desired_injector_voltage = self.retraction_speed
96+
if (
97+
(time.monotonic() - self.recovery_start_time) > 0.01
98+
and all(v < 0.01 for v in self.injector_component.get_injector_velocities())
99+
or (time.monotonic() - self.recovery_start_time) > 1.0
100+
):
101+
self.done()
83102

84103
@feedback
85104
def raw_ball_measurments(self) -> list[float]:

0 commit comments

Comments
 (0)