pub struct AltitudeController { /* private fields */ }Expand description
Altitude PID loop with gravity feedforward: turns an altitude target into a linear thrust command.
It holds only state (the integral); gains and limits come from the
AltitudeControllerConfig passed to each control call, so they can
change between ticks without resetting the state.
Implementations§
Source§impl AltitudeController
impl AltitudeController
Sourcepub fn control(
&mut self,
config: &AltitudeControllerConfig,
dt: f32,
target: AltitudeTarget,
estimate: AltitudeEstimate,
) -> f32
pub fn control( &mut self, config: &AltitudeControllerConfig, dt: f32, target: AltitudeTarget, estimate: AltitudeEstimate, ) -> f32
Controls the altitude of the vehicle by computing a normalized thrust command using a PID controller.
The PID gives a vertical acceleration a (m/s², up); the thrust is
hover_thrust · (g + a) / (g · cos tilt), saturated to [0, 1]. Zero error is hover,
and a tilted vehicle gets more thrust so its vertical component still balances gravity.
The boost stops growing past 60° (2×) and fades to zero thrust between about 84° and
90°, so an inverted vehicle isn’t driven toward the ground.
§Arguments
config- Gains, limits, and hover thrust; assumed valid (AltitudeControllerConfig::validate). An invalid config does not panic: a negative or NaNmax_integralacts as 0.dt- Time step since the last update in seconds (must be positive and finite).target- Desired altitude and vertical velocity.estimate- Current altitude, vertical velocity, and attitude.
§Returns
A normalized thrust command (0.0 to 1.0).
§Panics
If dt is not positive and finite.
§Examples
use stable_flight::controllers::{
AltitudeController, AltitudeControllerConfig, AltitudeEstimate, AltitudeTarget,
DEFAULT_HOVER_THRUST,
};
use stable_flight::nalgebra::UnitQuaternion;
let config = AltitudeControllerConfig::default();
let mut controller = AltitudeController::default();
let dt = 0.004;
let target = AltitudeTarget { altitude_m: 10.0, velocity_mps: 0.0 };
let level = UnitQuaternion::identity();
// On target, still, and level: the feedforward hover thrust.
let on_target = AltitudeEstimate { altitude_m: 10.0, velocity_mps: 0.0, attitude: level };
let thrust = controller.control(&config, dt, target, on_target);
assert!((thrust - DEFAULT_HOVER_THRUST).abs() < 1e-6);
// Below target: more than hover.
let below = AltitudeEstimate { altitude_m: 9.5, velocity_mps: -0.1, attitude: level };
let thrust = controller.control(&config, dt, target, below);
assert!(thrust > DEFAULT_HOVER_THRUST && thrust <= 1.0);Trait Implementations§
Source§impl Clone for AltitudeController
impl Clone for AltitudeController
Source§impl Debug for AltitudeController
impl Debug for AltitudeController
Source§impl Default for AltitudeController
impl Default for AltitudeController
Source§impl PartialEq for AltitudeController
impl PartialEq for AltitudeController
impl StructuralPartialEq for AltitudeController
Auto Trait Implementations§
impl Freeze for AltitudeController
impl RefUnwindSafe for AltitudeController
impl Send for AltitudeController
impl Sync for AltitudeController
impl Unpin for AltitudeController
impl UnsafeUnpin for AltitudeController
impl UnwindSafe for AltitudeController
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> Scalar for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.