sensor-fusion Rust Crate

This crate contains sensor fusion algorithms to combine output from a gyroscope, accelerometer, and optionally a magnetometer to give output that has less uncertainty than the output of the individual sensors.
Three sensor fusion implementations are available:
- Complementary Filter
- Mahony Filter
- Madgwick Filter
The Madgwick filter has been refactored to be more computationally efficient (and so faster) than the standard version used in many implementations, see Optimization below.
Simple example
Here's a simple example that calculates the orientation by fusing accelerometer and gyro values:
use ;
use Vector3df32;
The Madgwick filter also supports three-way fusing of accelerometer, gyroscope, and magnetometer readings:
# use ;
# use crate;
# let mut madgwick = default;
# let dt: f32 = 0.001;
# let acc = default;
# let gyro_rps = default;
# let mag = default;
let orientation = madgwick.fuse_acc_gyro_mag;
Mahony filter
The Mahony filter has the same interface as the Madgwick filter:
# use ;
use crate;
let mut mahony = default;
# let dt: f32 = 0.001;
# let acc = default;
# let gyro_rps = default;
let orientation = mahony.fuse_acc_gyro;
The Mahony filter does not support 3-way fusion using a magnetometer.
Method call interface
The FuseAccGyro and FuseAccGyroMag traits allow method-call syntax to be used:
# use ;
use crate;
let mut madgwick = default;
# let dt: f32 = 0.001;
# let acc = default;
# let gyro_rps = default;
# let mag = default;
let orientation = .fuse_acc_gyro_using;
// or
let orientation = .fuse_acc_gyro_mag_using;
SIMD support
SIMD support (for the f32 variants) can be enabled with the simd feature.
It is currently experimental, so if you used SIMD make sure you benchmark to show that you are indeed getting a performance improvement over the non-SIMD version.
This uses portable simd, which requires the nightly compiler, since it is still unstable in rust.
This can be invoked using rustup, eg:
Optimization {#opt}
Classically, the calculation of the Madgwick gradient descent corrective step involves multiplication of a vector by a matrix, this involves a total of 54 arithmetic operations for the acc/gyro case.
However, because both the matrix and vector contain zero elements, and because there is some symmetry in the matrix, the calculation can be refactored to use fewer arithmetic operations.
Indeed it can be reduced to a total of 31 arithmetic operations. By using SIMD this can be further reduced to 16 operations (11 scalar and 5 SIMD operations).
See below:
// Classic version
//
// total:
// 54 arithmetic operations (35 multiplications, 19 additions/subtractions)
//
fn madgwick_step(q: Quaternionf32, a: Vector3df32) -> Quaternionf32 {
let M = Matrix4x4f32::new( // 10 multiplications
-2.0*q.x, 2.0*q.w, 0.0, 0.0,
2.0*q.y, 2.0*q.z, -4.0*q.w, 0.0,
-2.0*q.z, 2.0*q.y, -4.0*q.x, 0.0,
2.0*q.w, 2.0*q.x, 0.0, 0.0
);
let v = Vector4df32::new( // 9 multiplications, 7 additions/subtractions
2.0*( q.w*q.y - q.z*q.x) - a.x,
2.0*( q.z*q.w + q.x*q.y) - a.y,
2.0*(0.5 - q.w*q.w - q.x*q.x) - a.z,
0.0
);
M * v // 16 multiplications, 12 additions
}
// Refactored version
//
// total:
// 31 arithmetic operations (19 multiplications, 12 additions/subtractions)
// when converted to SIMD this becomes:
// 16 operations (7 multiplications, 4 additions, 3 vector multiplications, 2 vector additions)
//
# use ;
Original implementation
I originally implemented this crate as a C++ library: Library-SensorFusion.
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.