Skip to content

Commit 221af5a

Browse files
authored
Merge pull request #240 from thedropbears/non-timed-measure
2 parents 0a4aa8b + 165a7a6 commit 221af5a

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
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 & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from magicbot import StateMachine, feedback, state, timed_state
1+
from magicbot import StateMachine, feedback, state, tunable
22

33
from components.injector import InjectorComponent
44
from components.shooter import ShooterComponent
@@ -9,11 +9,15 @@ class AlgaeMeasurement(StateMachine):
99
shooter_component: ShooterComponent
1010
injector_component: InjectorComponent
1111

12+
retraction_voltage = tunable(-4.0)
13+
number_of_iterations = tunable(2)
14+
1215
def __init__(self) -> None:
1316
self.injector_starting_positions = (0.0, 0.0)
1417
self.flywheel_starting_positions = (0.0, 0.0)
1518
self.measured_sizes: list[float] = []
1619
self.measured_raw_sizes: list[float] = []
20+
self._tm = 0.0
1721

1822
def measure(self) -> None:
1923
self.engage()
@@ -25,18 +29,15 @@ def initialising(self) -> None:
2529
if all(abs(v) <= 0.0001 for v in self.shooter_component.flywheel_speeds()):
2630
self.next_state("calculating")
2731

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
32+
@state(must_finish=True)
33+
def pre_measure(self, initial_call, state_tm) -> None:
34+
if self.retract(initial_call, state_tm):
35+
self.next_state("measuring")
3536

3637
@state(must_finish=True)
3738
def calculating(self) -> None:
38-
if len(self.measured_sizes) == 3:
39-
# Throw away the first one and average the last two
39+
if len(self.measured_sizes) == self.number_of_iterations:
40+
# Throw away the first one and average the rest
4041
self.shooter_component.algae_size = sum(self.measured_sizes[1:]) / (
4142
len(self.measured_sizes) - 1
4243
)
@@ -77,9 +78,25 @@ def measuring(self, initial_call) -> None:
7778

7879
self.next_state("calculating")
7980

80-
@timed_state(duration=0.5, must_finish=True)
81-
def recovering(self) -> None:
82-
self.injector_component.desired_injector_voltage = -2.0
81+
@state(must_finish=True)
82+
def recovering(self, initial_call, state_tm) -> None:
83+
if self.retract(initial_call, state_tm):
84+
self.done()
85+
86+
def retract(self, initial_call, state_tm) -> bool:
87+
self.injector_component.desired_injector_voltage = self.retraction_voltage
88+
if initial_call:
89+
self._tm = state_tm
90+
# We cannot be ready on the first iteration
91+
return False
92+
# TODO determine the correct speed threshold
93+
dt = state_tm - self._tm
94+
return (
95+
all(
96+
abs(v) < 0.01 for v in self.injector_component.get_injector_velocities()
97+
)
98+
or dt > 0.5
99+
)
83100

84101
@feedback
85102
def raw_ball_measurments(self) -> list[float]:

0 commit comments

Comments
 (0)