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 * 1Implementations§
Source§impl Complementary
impl Complementary
Sourcepub fn update(&mut self, rate: f32, absolute: f32, dt: f32) -> f32
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.
Trait Implementations§
Source§impl Clone for Complementary
impl Clone for Complementary
Source§fn clone(&self) -> Complementary
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)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreimpl Copy for Complementary
Auto Trait Implementations§
impl Freeze for Complementary
impl RefUnwindSafe for Complementary
impl Send for Complementary
impl Sync for Complementary
impl Unpin for Complementary
impl UnsafeUnpin for Complementary
impl UnwindSafe for Complementary
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
Mutably borrows from an owned value. Read more