Skip to main content

BodyRateController

Struct BodyRateController 

Source
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.

Implementations§

Source§

impl BodyRateController

Source

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: config.gains for roll and pitch, config.yaw_gains for yaw.

§Arguments
  • config - Gains and limits; assumed valid (BodyRateControllerConfig::validate). An invalid limit does not panic: a negative or NaN max_integral 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

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BodyRateController

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for BodyRateController

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl PartialEq for BodyRateController

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for BodyRateController

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> Scalar for T
where T: 'static + Clone + PartialEq + Debug,

Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.