use crate::VehicleState;
#[cfg(test)]
mod tests;
const PITCH_MAX_RAD: f32 = 0.3;
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub(super) struct BackDeceleration {
pitch_rad: f32,
}
impl BackDeceleration {
pub(super) fn reset(&mut self) {
self.pitch_rad = 0.0;
}
pub(super) fn pitch_rad(
&mut self,
dt: f32,
state: &VehicleState,
target_mps2: f32,
gain: f32,
) -> f32 {
let deceleration_mps2 = ground_deceleration_mps2(state);
if deceleration_mps2.is_finite() {
let shortfall_mps2 = target_mps2 - deceleration_mps2;
self.pitch_rad =
(self.pitch_rad + gain * shortfall_mps2 * dt).clamp(0.0, PITCH_MAX_RAD);
}
self.pitch_rad
}
}
fn ground_deceleration_mps2(state: &VehicleState) -> f32 {
let velocity_mps = state.velocity_mps.xy();
let speed_mps = velocity_mps.norm();
if speed_mps == 0.0 {
return 0.0;
}
-state.acceleration_mps2.xy().dot(&velocity_mps) / speed_mps
}