Skip to content

Commit 26e5660

Browse files
committed
Extract _should_stop decision
1 parent 1591bcc commit 26e5660

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

pod-operation/src/state_machine.rs

+19-13
Original file line numberDiff line numberDiff line change
@@ -218,20 +218,10 @@ impl StateMachine {
218218
fn _running_periodic(&mut self) -> State {
219219
info!("Rolling Running state");
220220

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();
222223

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) {
235225
return State::Stopped;
236226
}
237227

@@ -256,6 +246,22 @@ impl StateMachine {
256246
State::Running
257247
}
258248

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+
259265
// To avoid conflicts with the state-transition model,
260266
// each of these event handlers must wait for an ongoing transition to complete
261267
// by awaiting the mutex lock to be acquired.

0 commit comments

Comments
 (0)