Skip to content

Commit 89e2b00

Browse files
committed
chk, forgot stuff from testing setup
1 parent ef7fea6 commit 89e2b00

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

pod-operation/src/components/wheel_encoder.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::utils::GpioPins;
77
const WHEEL_DIAMETER: f32 = 0.0762; // meters
88
const ENCODER_RESOLUTION: f32 = 16.0; // pulses per revolution
99
const DISTANCE_PER_COUNT: f32 = WHEEL_DIAMETER * std::f32::consts::PI / ENCODER_RESOLUTION; // feet
10+
const BRAKING_DECELERATION: f32 = -15.14; // m/s^2
1011

1112
#[derive(Clone, Copy, num_enum::FromPrimitive, num_enum::IntoPrimitive)]
1213
#[repr(i8)]
@@ -122,6 +123,10 @@ impl WheelEncoder {
122123
self.velocity
123124
}
124125

126+
pub fn get_braking_distance(&self) -> f32 {
127+
-self.velocity.powi(2) / (2.0 * BRAKING_DECELERATION)
128+
}
129+
125130
fn read_state(&self) -> EncoderState {
126131
encode_state(self.pin_a.read(), self.pin_b.read())
127132
}

pod-operation/src/state_machine.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,21 @@ impl StateMachine {
218218
info!("Rolling Running state");
219219

220220
let encoder_value = self.wheel_encoder.measure().expect("wheel encoder faulted"); // Read the encoder value
221+
222+
let current_braking_distance = self.wheel_encoder.get_braking_distance();
223+
224+
// Predict next tick's braking distance
221225
let current_velocity = self.wheel_encoder.get_velocity();
226+
let predicted_velocity =
227+
current_velocity + BRAKING_DECELERATION * TICK_INTERVAL.as_secs_f32();
228+
let predicted_braking_distance = -predicted_velocity.powi(2) / (2.0 * BRAKING_DECELERATION);
229+
222230
// Check if the predicted braking distance requires stopping
223231
if encoder_value + current_velocity * TICK_INTERVAL.as_secs_f32() >= STOP_THRESHOLD {
224232
return State::Stopped;
225233
}
226234

227-
if encoder_value <= STOP_THRESHOLD {
235+
if predicted_braking_distance <= BRAKING_THRESHOLD {
228236
return State::Stopped;
229237
}
230238

0 commit comments

Comments
 (0)