no-std ahrs

A Rust port of Madgwick's AHRS algorithm

15 unstable releases

0.7.0 Sep 13, 2024
0.6.0 Jan 13, 2023
0.5.0 Feb 9, 2022
0.4.0 Oct 4, 2021
0.1.3 Nov 17, 2016
Download history 15/week @ 2024-06-10 6/week @ 2024-06-17 31/week @ 2024-06-24 25/week @ 2024-07-08 22/week @ 2024-07-15 33/week @ 2024-07-22 27/week @ 2024-07-29 1/week @ 2024-08-05 16/week @ 2024-08-12 9/week @ 2024-08-19 25/week @ 2024-08-26 16/week @ 2024-09-02 171/week @ 2024-09-09 38/week @ 2024-09-16 76/week @ 2024-09-23

306 downloads per month
Used in 3 crates (2 directly)

MIT license

25KB
415 lines

ahrs-rs

crates.io Build Status

A Rust port of Sebastian Madgwick's AHRS algorithm.

Documentation

Usage

Add ahrs-rs to your Cargo.toml:

[dependencies]
ahrs = "0.7"

Here's a simple example that updates the filter state with arbitrary sensor data:

use ahrs::{Ahrs, Madgwick};
use nalgebra::Vector3;
use std::f64;

fn main() {
    // Initialize filter with default values
    let mut ahrs = Madgwick::default();

    // Obtain sensor values from a source
    let gyroscope = Vector3::new(60.1, 30.2, 20.3);
    let accelerometer = Vector3::new(0.1, 0.2, 0.3);
    let magnetometer = Vector3::new(0.5, 0.6, 0.7);

    // Run inputs through AHRS filter (gyroscope must be radians/s)
    let quat = ahrs
        .update(
            &(gyroscope * (f64::consts::PI / 180.0)),
            &accelerometer,
            &magnetometer,
        )
        .unwrap();
    let (roll, pitch, yaw) = quat.euler_angles();

    // Do something with the updated state quaternion
    println!("pitch={}, roll={}, yaw={}", pitch, roll, yaw);
}

Crate nalgebra is also needed as a dependency for its algebraic types Vector3 and Quaternion.

Feature flags

field_access

Gives [im]mutable access to inner algorithm struct fields that aren't normally exposed. The exposed API is considered unstable for the time-being, but is nevertheless a useful feature. For example:

use ahrs::Madgwick;

let mut ahrs = Madgwick::default();

#[cfg(feature = "field_access")]
{
    let sample_period: &mut f64 = ahrs.sample_period_mut();
    *sample_period = 0.5;
}

License

MIT

Dependencies

~3MB
~65K SLoC