pub struct BodyRateController { /* private fields */ }Expand description
A PID-based controller for managing angular rates (roll, pitch, yaw) of a vehicle.
This controller uses a PID algorithm to compute corrective moments based on desired and estimated angular rates. It is designed as the innermost control loop, directly affecting motor commands.
It holds only state (integral and derivative history); gains and limits come from the
BodyRateControllerConfig passed to each control call, so they can
change between ticks without resetting the state.
§Units
- Time (
dt) is in seconds. - Desired and estimated rates are expected in rad/s.
- Output moments are
P·e + ∫I·e − D·ω̇, saturated to[-1, 1]per axis; the mixer scales them to motor commands.
§The I term
The integral accumulates I·e·dt, the moment itself, as ArduPilot’s AC_PID and PX4’s
RateControl do, and is clamped to the config’s limit in moment. A change of I gain
changes how fast it grows, not what it holds, so a retune doesn’t bump the output; setting
I to 0 drops it.
§D on the measured rate
D damps the measured rate’s change (ω̇, the angular acceleration), not the error’s, so a
setpoint step adds no derivative kick: on the error, a step of A rad/s would kick the
moment by D·A/dt for one tick (35·A with a designed d of 0.139 at 250 Hz), clipping it
and showing as overshoot. Inside the loop the two are the same, so stability and the
response to a disturbance don’t change; only a setpoint change is followed by P and I alone.
PX4’s RateControl takes D on the measured rate too; ArduPilot’s AC_PID takes it on the
error and low-passes it (ATC_RAT_RLL_FLTD).
Implementations§
Source§impl BodyRateController
impl BodyRateController
Sourcepub fn control(
&mut self,
config: &BodyRateControllerConfig,
dt: f32,
desired_rates_radps: Vec3,
estimated_rates_radps: Vec3,
) -> Vec3
pub fn control( &mut self, config: &BodyRateControllerConfig, dt: f32, desired_rates_radps: Vec3, estimated_rates_radps: Vec3, ) -> Vec3
Controls the angular velocity (body rates) of the vehicle using a PID controller.
Computes the error between desired and estimated rates (both rad/s) and applies PID
control to it, per axis with config.roll_gains, pitch_gains, and yaw_gains.
§Arguments
config- Gains and limits; assumed valid (BodyRateControllerConfig::validate). An invalid limit does not panic: a negative or NaN integral limit acts as 0.dt- Time step since the last update in seconds (must be positive and finite).desired_rates_radps- Desired angular rates (roll, pitch, yaw) in rad/s.estimated_rates_radps- Current measured angular rates (roll, pitch, yaw) in rad/s.
§Returns
Moments (roll, pitch, yaw), each saturated to [-1, 1].
§Panics
If dt is not positive and finite.
A non-finite component in desired_rates_radps or estimated_rates_radps gives a zero
moment on that axis alone and leaves that axis’s state untouched, so a bad gyro axis
doesn’t take damping off the others (the input policy in architecture.md).
§Examples
use stable_flight::controllers::{BodyRateController, BodyRateControllerConfig};
use stable_flight::Vec3;
let config = BodyRateControllerConfig::default();
let mut controller = BodyRateController::default();
let dt = 0.004; // seconds; 250 Hz control loop
let desired_rates_radps = Vec3::new(0.1, 0.0, 0.05); // roll, pitch, yaw rates (rad/s)
let estimated_rates_radps = Vec3::zeros();
let moments = controller.control(&config, dt, desired_rates_radps, estimated_rates_radps);
assert!(moments.x > 0.0 && moments.z > 0.0);
assert!(moments.iter().all(|m| (-1.0..=1.0).contains(m)));Trait Implementations§
Source§impl Clone for BodyRateController
impl Clone for BodyRateController
Source§impl Debug for BodyRateController
impl Debug for BodyRateController
Source§impl Default for BodyRateController
impl Default for BodyRateController
Source§impl PartialEq for BodyRateController
impl PartialEq for BodyRateController
impl StructuralPartialEq for BodyRateController
Auto Trait Implementations§
impl Forget for BodyRateController
impl Freeze for BodyRateController
impl Move for BodyRateController
impl RefUnwindSafe for BodyRateController
impl Send for BodyRateController
impl Sync for BodyRateController
impl Unpin for BodyRateController
impl UnsafeUnpin for BodyRateController
impl UnwindSafe for BodyRateController
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.