Skip to main content

Complementary

Struct Complementary 

Source
pub struct Complementary { /* private fields */ }
Expand description

Blends a drifting rate measurement with a noisy absolute one into a steady estimate.

A gyroscope gives a smooth rate of turn but drifts over time; an accelerometer gives an absolute tilt that is right on average but noisy. A complementary filter trusts the rate over the short term and the absolute reading over the long term, so the result is both smooth and drift-free. Each step it integrates the rate onto its estimate, then nudges that toward the absolute reading by an amount set by alpha. The same filter fuses any fast-rate-plus-slow-absolute pair, not only an IMU.

§Examples

use pamoja_kit::Complementary;

// Heavily trust the integrated rate, lightly correct toward the absolute reading.
let mut tilt = Complementary::new(0.98, 0.0);
// The rate reads +10 per second for 0.1 s while the absolute reads about 1.
let angle = tilt.update(10.0, 1.0, 0.1);
assert!((angle - 1.0).abs() < 0.05); // about 0.98 * 1 + 0.02 * 1

Implementations§

Source§

impl Complementary

Source

pub fn new(alpha: f32, initial: f32) -> Self

Creates a filter.

§Arguments
  • alpha - the weight on the integrated rate, in [0.0, 1.0]; near 1.0 trusts the rate and corrects slowly, near 0.0 follows the absolute reading. Clamped to the unit interval.
  • initial - the starting estimate.
§Returns

A filter seeded with initial.

Source

pub fn update(&mut self, rate: f32, absolute: f32, dt: f32) -> f32

Fuses a rate and an absolute reading over a time step and returns the new estimate.

§Arguments
  • rate - the rate of change, such as degrees per second from a gyroscope.
  • absolute - the absolute reading, such as a tilt from an accelerometer.
  • dt - the time since the previous update.
§Returns

The fused estimate, alpha * (estimate + rate * dt) + (1 - alpha) * absolute.

Source

pub fn estimate(&self) -> f32

Returns the current fused estimate.

Trait Implementations§

Source§

impl Clone for Complementary

Source§

fn clone(&self) -> Complementary

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 Copy for Complementary

Source§

impl Debug for Complementary

Source§

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

Formats the value using the given formatter. Read more

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.