Fusion Sensor Fusion Library

repository·main·Indexed 23 days ago

https://github.com/xiotechnologies/fusion

A high-performance sensor fusion library optimized for embedded systems, providing AHRS (Attitude And Heading Reference System) capabilities. It combines gyroscope, accelerometer, and magnetometer data to estimate orientation, linear acceleration, and gravity vectors. Available as the imufusion Python package (v1.3.2), it includes features for gyroscope offset estimation via a Bias algorithm, sensor calibration using FusionModelInertial and FusionModelMagnetic, and axis remapping.

Tokens
1.7K
Snippets
0
Records
14
Agent score
32%

What's inside Fusion

  1. Use the Bias algorithm to estimate gyroscope offset

    main

    The Bias algorithm provides run-time estimation of the gyroscope offset to compensate for temperature variations and fine-tune existing calibrations. It works by detecting stationary periods—when gyroscope measurements remain below a specific threshold for a set duration—and updating the offset using a high-pass filter with a low cutoff frequency. This can be used to clean gyroscope data before passing it to the AHRS algorithm.

    To configure the algorithm, use the FusionBiasSettings structure with the FusionBiasSetSettings function.

  2. Understand AHRS algorithm outputs

    main

    The algorithm produces four primary outputs:

    • quaternion: Describes the orientation of the sensor relative to the Earth. This can be converted to a rotation matrix via FusionQuaternionToMatrix or to Euler angles via FusionQuaternionToEuler.
    • gravity: The direction of gravity in the sensor coordinate frame.
    • linear acceleration: The accelerometer measurement with gravity removed.
    • earth acceleration: The accelerometer measurement in the Earth coordinate frame with gravity removed.

    The algorithm supports three Earth axes conventions: NWU (North-West-Up), ENU (East-North-Up), and NED (North-East-Down).

  3. How the AHRS algorithm works

    main

    The Attitude And Heading Reference System (AHRS) algorithm combines gyroscope, accelerometer, and magnetometer data to measure orientation relative to the Earth.

    It is based on the revised AHRS algorithm from Madgwick's PhD thesis (Chapter 7), which differs from the standard Madgwick algorithm. The algorithm functions as a complementary filter: it integrates gyroscope measurements (high-pass) and applies a feedback term based on errors from other sensors (low-pass).

    Key behaviors:

    • Gain Control: The gain parameter determines the balance. A low gain trusts the gyroscope more (higher drift risk); a high gain increases the influence of other sensors (higher risk of errors from acceleration or magnetic distortion). A gain of zero ignores all sensors except the gyroscope.
    • Startup: During startup or angular rate recovery, the algorithm ramps the gain from 10 down to the final value over 3 seconds to allow rapid convergence. This period disables acceleration and magnetic rejection.
  4. Enable high-accuracy normalization via FUSION_USE_NORMAL_SQRT

    main

    By default, Fusion uses a fast inverse square root algorithm for vector and quaternion normalization. If you require higher accuracy at the cost of execution speed, you can enable standard square root operations by:

    1. Including the definition FUSION_USE_NORMAL_SQRT in FusionMath.h.
    2. Adding FUSION_USE_NORMAL_SQRT as a preprocessor definition in your build configuration.
  5. Configure the Bias algorithm settings

    main

    The Bias algorithm is configured using the FusionBiasSetSettings function and the FusionBiasSettings structure.

    SettingDescription
    sampleRateSample rate in Hz. Default: 100
    stationaryThresholdStationary detection threshold in degrees per second. Default: 3
    stationaryPeriodStationary detection period in seconds. Default: 3
  6. Configure AHRS algorithm settings

    main

    Algorithm settings are defined using the FusionAhrsSettings structure and applied via the FusionAhrsSetSettings function.

    SettingDescription
    sampleRateSample rate in Hz.
    conventionEarth axes convention (NWU, ENU, or NED).
    gainInfluence of gyroscope vs other sensors. 0.5 is typical. 0 disables startup and rejection features.
    gyroscopeRangeGyroscope range in deg/s. Angular rate recovery triggers at 98% of this value. 0 disables the feature.
    accelerationRejectionThreshold in degrees for acceleration rejection. 10 degrees is typical. 0 disables it.
    magneticRejectionThreshold in degrees for magnetic rejection. 10 degrees is typical. 0 disables it.
    rejectionTimeoutTimeout for acceleration/magnetic recovery in seconds. 5 seconds is typical. 0 disables the features.
  7. Apply sensor calibration using FusionModelInertial

    main

    The FusionModelInertial function applies calibration parameters to gyroscope or accelerometer measurements using the following model:

    $$\mathbf{i}_c = \mathbf{M} \mathbf{s} (\mathbf{i}_u - \mathbf{b})$$

    Where:

    • $\mathbf{i}_c$: Calibrated output
    • $\mathbf{i}_u$: Uncalibrated input
    • $\mathbf{M}$: Misalignment matrix
    • $\mathbf{s}$: Sensitivity diagonal matrix
    • $\mathbf{b}$: Offset vector
  8. Apply magnetometer calibration using FusionModelMagnetic

    main

    The FusionModelMagnetic function applies magnetometer calibration parameters using the following model:

    $$\mathbf{m}_c = \mathbf{S} (\mathbf{m}_u - \mathbf{h})$$

    Where:

    • $\mathbf{m}_c$: Calibrated output
    • $\mathbf{m}_u$: Uncalibrated input
    • $\mathbf{S}$: Soft-iron matrix
    • $\mathbf{h}$: Hard-iron offset vector
  9. Save and restore gyroscope offset from non-volatile memory

    main
    To avoid re-estimating the gyroscope offset every time the device powers on, you can save the current run-time estimate to non-volatile memory and restore it during startup. Use FusionBiasGetOffset to retrieve the current estimate and FusionBiasSetOffset to apply a previously saved value.
  10. Remap sensor axes using FusionRemap

    main
    If sensors are mounted such that their axes do not align with the body axes, use the FusionRemap function to transform them into the body frame. This function supports 24 possible orthogonal axis permutations defined by the FusionRemapAlignment enumeration.