@@ -218,20 +218,10 @@ impl StateMachine {
218
218
fn _running_periodic ( & mut self ) -> State {
219
219
info ! ( "Rolling Running state" ) ;
220
220
221
- let encoder_value = self . wheel_encoder . measure ( ) . expect ( "wheel encoder faulted" ) ; // Read the encoder value
221
+ let distance = self . wheel_encoder . measure ( ) . expect ( "wheel encoder faulted" ) ; // Read the encoder value
222
+ let velocity = self . wheel_encoder . get_velocity ( ) ;
222
223
223
- // Predict next tick's braking distance
224
- let current_velocity = self . wheel_encoder . get_velocity ( ) ;
225
- let predicted_velocity =
226
- current_velocity + BRAKING_DECELERATION * TICK_INTERVAL . as_secs_f32 ( ) ;
227
- let predicted_braking_distance = -predicted_velocity. powi ( 2 ) / ( 2.0 * BRAKING_DECELERATION ) ;
228
-
229
- // Check if the predicted braking distance requires stopping
230
- if encoder_value + current_velocity * TICK_INTERVAL . as_secs_f32 ( ) >= STOP_THRESHOLD {
231
- return State :: Stopped ;
232
- }
233
-
234
- if predicted_braking_distance <= BRAKING_THRESHOLD {
224
+ if StateMachine :: _should_stop ( distance, velocity) {
235
225
return State :: Stopped ;
236
226
}
237
227
@@ -256,6 +246,22 @@ impl StateMachine {
256
246
State :: Running
257
247
}
258
248
249
+ /// Consider two stopping conditions based on the pod's distance and velocity
250
+ /// at the next tickwhich is when the stopping will actually initiate
251
+ fn _should_stop ( distance : f32 , velocity : f32 ) -> bool {
252
+ // Predict next tick's braking distance
253
+ let predicted_velocity =
254
+ velocity + BRAKING_DECELERATION * TICK_INTERVAL . as_secs_f32 ( ) ;
255
+ let predicted_braking_distance = -predicted_velocity. powi ( 2 ) / ( 2.0 * BRAKING_DECELERATION ) ;
256
+
257
+ // Check if the predicted braking distance requires stopping
258
+ if distance + velocity * TICK_INTERVAL . as_secs_f32 ( ) >= STOP_THRESHOLD {
259
+ return true ;
260
+ }
261
+
262
+ predicted_braking_distance <= BRAKING_THRESHOLD
263
+ }
264
+
259
265
// To avoid conflicts with the state-transition model,
260
266
// each of these event handlers must wait for an ongoing transition to complete
261
267
// by awaiting the mutex lock to be acquired.
0 commit comments