1- from magicbot import StateMachine , feedback , state , timed_state
1+ from magicbot import StateMachine , feedback , state , tunable
22
33from components .injector import InjectorComponent
44from 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