Skip to content

Software

Fusion (AHRS)

From github.com/xioTechnologies/Fusion (opens in a new tab)

  • Perception
  • Library
  • MIT

A sensor fusion library that combines gyroscope, accelerometer and magnetometer data from an IMU into an orientation estimate, written in C with a Python package.

Facts

GitHub stars
1,779read on 28 Nov 2026
Last update
18 Sept 2026Newest commit on main
Maintenance
Activea commit in the last 90 days
Difficulty
Intermediate
Hardware it needs, as the project states it
It states no other specific hardware requirements.

Checked on GitHub on 28 Nov 2026.

Licenses

Code
MIT (opens in a new tab)

Read by Robotheca from LICENSE.md (opens in a new tab) (MIT) at commit a8d7224 (opens in a new tab), on 16 Oct 2026.

Commercial use
Yes

At a glance

What it is
  • Computes orientation relative to the earth by combining gyroscope data with accelerometer and magnetometer corrections in a complementary filter.
  • Supports systems with only a gyroscope and accelerometer, or those using an external heading source such as GPS.
  • Includes acceleration and magnetic rejection to reduce errors from linear motion and magnetic distortion.
  • Provides a bias algorithm that estimates gyroscope offset at run time during stationary periods.
  • Offers sensor models for applying calibration parameters to gyroscope, accelerometer and magnetometer readings.
  • Optimised for embedded systems, with options for inlining and a fast inverse square root.

Robotheca's summary of the project's documents, not the creator's words.

Robots on Robotheca
No robot on Robotheca uses it yet.
Licence
MIT. Commercial use: Yes. Where it was read
Install
In the creator's words: The project's README (opens in a new tab)
Maintenance
Active, last commit on 18 Sept 2026.

Use it

How to install it, in the creator's words: The project's README (opens in a new tab)

Robotheca doesn't review code. Read it before running it.

Files and versions

Facts read at commit a8d7224

Robotheca never hosts the code: each link goes to the creator's own page.

The README

The creator's own README, copied as written: README.md by @xioTechnologies, at commit a8d7224, under MIT (opens in a new tab). The original on GitHub (opens in a new tab).

Read the README here

(opens in a new tab) (opens in a new tab) (opens in a new tab) (opens in a new tab) (opens in a new tab)

Fusion

Fusion is a sensor fusion library for Inertial Measurement Units (IMUs), optimised for embedded systems. Fusion is a C library but is also available as the Python package, imufusion (opens in a new tab). Two example Python scripts, simple_example.py (opens in a new tab) and advanced_example.py (opens in a new tab) are provided with example sensor data to demonstrate use of the package.

AHRS algorithm

The Attitude And Heading Reference System (AHRS) algorithm combines gyroscope, accelerometer, and magnetometer data into a single measurement of orientation relative to the earth. The algorithm also supports systems that use only a gyroscope and accelerometer, and systems that use a gyroscope and accelerometer combined with an external source of heading measurement such as GPS.

The algorithm is based on the revised AHRS algorithm presented in chapter 7 of Madgwick's PhD thesis (opens in a new tab). This is a different algorithm to the better-known initial AHRS algorithm presented in chapter 3, commonly referred to as the Madgwick algorithm.

The algorithm calculates the orientation as the integration of the gyroscope summed with a feedback term. The feedback term is equal to the error in the current measurement of orientation as determined by the other sensors, multiplied by a gain. The algorithm therefore functions as a complementary filter that combines high-pass filtered gyroscope measurements with low-pass filtered measurements from other sensors with a corner frequency determined by the gain. A low gain will 'trust' the gyroscope more and so be more susceptible to drift. A high gain will increase the influence of other sensors and the errors that result from accelerations and magnetic distortions. A gain of zero will ignore the other sensors so that the measurement of orientation is determined by only the gyroscope.

Startup

Startup occurs when the algorithm starts for the first time and during overrange recovery. During startup, the acceleration and magnetic rejection features are disabled and the gain is ramped down from 10 to the final value over a 3 second period. This allows the measurement of orientation to rapidly converge from an arbitrary initial value to the value indicated by the sensors.

Overrange recovery

Angular rates that exceed the gyroscope measurement range cannot be tracked and will trigger an overrange recovery. Overrange recovery is activated when the angular rate exceeds 98% of the gyroscope measurement range and will trigger a restart of the algorithm.

Acceleration rejection

The acceleration rejection feature reduces the errors that result from the accelerations of linear and rotational motion. Acceleration rejection works by calculating an error as the angular difference between the instantaneous measurement of inclination indicated by the accelerometer, and the current measurement of inclination provided by the algorithm output. If the error is greater than a threshold then the accelerometer will be ignored for that algorithm update. This is equivalent to a dynamic gain that decreases as accelerations increase.

Prolonged accelerations risk an overdependency on the gyroscope and will trigger an acceleration recovery. Acceleration recovery activates when the error exceeds the threshold for more than 90% of algorithm updates over a period of t / (0.1p - 9), where t is the recovery trigger timeout and p is the percentage of algorithm updates where the error exceeds the threshold. The recovery will remain active until the error exceeds the threshold for less than 90% of algorithm updates over the period -t / (0.1p - 9). The accelerometer will be used by every algorithm update during recovery.

Magnetic rejection

The magnetic rejection feature reduces the errors that result from temporary magnetic distortions. Magnetic rejection works using the same principle as acceleration rejection, operating on the magnetometer instead of the accelerometer and by comparing the measurements of heading instead of inclination.

Algorithm outputs

The algorithm provides four outputs: quaternion, gravity, linear acceleration, and earth acceleration. The quaternion describes the orientation of the sensor relative to the earth. This can be converted to a rotation matrix using the FusionQuaternionToMatrix function or to Euler angles using the FusionQuaternionToEuler function. Gravity is a direction of gravity in the sensor coordinate frame. Linear acceleration is the accelerometer measurement with gravity removed. Earth acceleration is the accelerometer measurement in the earth coordinate frame with gravity removed. The algorithm supports North-West-Up (NWU), East-North-Up (ENU), and North-East-Down (NED) axes conventions.

Algorithm settings

The AHRS algorithm settings are defined by the FusionAhrsSettings structure and set using the FusionAhrsSetSettings function.

SettingDescription
sampleRateSample rate (in Hz).
conventionEarth axes convention (NWU, ENU, or NED).
gainDetermines the influence of the gyroscope relative to other sensors. A value of zero will disable startup and the acceleration and magnetic rejection features. A value of 0.5 is appropriate for most applications.
gyroscopeRangeGyroscope range (in degrees per second). Overrange recovery will activate if the gyroscope measurement exceeds 98% of this value. A value of zero will disable this feature. The value should be set to the range specified in the gyroscope datasheet.
accelerationRejectionThreshold (in degrees) used by the acceleration rejection feature. A value of zero will disable this feature. A value of 10 degrees is appropriate for most applications.
magneticRejectionThreshold (in degrees) used by the magnetic rejection feature. A value of zero will disable the feature. A value of 10 degrees is appropriate for most applications.
rejectionTimeoutAcceleration and magnetic recovery trigger timeout (in seconds). A value of zero will disable the acceleration and magnetic rejection features. A period of 5 seconds is appropriate for most applications.
Algorithm internal states

The AHRS algorithm internal states are defined by the FusionAhrsInternalStates structure and obtained using the FusionAhrsGetInternalStates function.

StateDescription
accelerationErrorAngular error (in degrees) of the algorithm output relative to the instantaneous measurement of inclination indicated by the accelerometer. The acceleration rejection feature will ignore the accelerometer if this value exceeds the accelerationRejection threshold set in the algorithm settings.
accelerometerIgnoredtrue if the accelerometer was ignored by the previous algorithm update.
accelerationRecoveryTriggerAcceleration recovery trigger value between 0.0 and 1.0. Acceleration recovery will activate when this value reaches 1.0 and then deactivate when the value reaches 0.0.
magneticErrorAngular error (in degrees) of the algorithm output relative to the instantaneous measurement of heading indicated by the magnetometer. The magnetic rejection feature will ignore the magnetometer if this value exceeds the magneticRejection threshold set in the algorithm settings.
magnetometerIgnoredtrue if the magnetometer was ignored by the previous algorithm update.
magneticRecoveryTriggerMagnetic recovery trigger value between 0.0 and 1.0. Magnetic recovery will activate when this value reaches 1.0 and then deactivate when the value reaches 0.0.
Algorithm flags

The AHRS algorithm flags are defined by the FusionAhrsFlags structure and obtained using the FusionAhrsGetFlags function.

FlagDescription
startuptrue during algorithm startup.
overrangeRecoverytrue during overrange recovery.
accelerationRecoverytrue during acceleration recovery.
magneticRecoverytrue during magnetic recovery.
Sample clock errors

The FusionAhrsSetSamplePeriod function may be called before each algorithm update with the measured sample period to compensate for gyroscope sample clock errors. The measured sample period must approximately match sampleRate.

Bias algorithm

The bias algorithm provides run-time estimation of the gyroscope offset to compensate for variations in temperature and fine-tune existing offset calibration that may already be in place. This algorithm may be used to improve gyroscope measurements before providing them to the AHRS algorithm.

The algorithm estimates the gyroscope offset by identifying the stationary periods that occur naturally in many applications. Stationary periods are detected as the gyroscope measurement remaining below a threshold for a period of time. The gyroscope offset is then updated using a high-pass filter with a very low cutoff frequency.

Settings

The bias algorithm settings are defined by the FusionBiasSettings structure and are configured using the FusionBiasSetSettings function.

SettingDescription
sampleRateSample rate in Hz. The default value is 100 Hz.
stationaryThresholdStationary detection threshold in degrees per second. The default value is 3 degrees per second.
stationaryPeriodStationary detection period in seconds. The default value is 3 seconds.
Non-volatile memory

The run-time estimate of the gyroscope offset may be saved to and restored from non-volatile memory to avoid re-estimation after each power cycle. This is supported by the FusionBiasGetOffset and FusionBiasSetOffset functions.

Sensor models

Accurate algorithm outputs require calibrated sensor measurements. The library provides sensor models for applying gyroscope, accelerometer, and magnetometer calibration parameters. The library does not provide a method to determine these parameters.

Inertial model

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

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

where:

  • $\mathbf{i}_c$ = Calibrated gyroscope or accelerometer (function return value)
  • $\mathbf{i}_u$ = Uncalibrated gyroscope or accelerometer (uncalibrated function parameter)
  • $\mathbf{M}$ = Misalignment matrix (misalignment function parameter)
  • $\mathbf{s}$ = Sensitivity diagonal matrix (sensitivity function parameter)
  • $\mathbf{b}$ = Offset vector (offset function parameter)
Magnetic model

The FusionModelMagnetic function applies magnetometer calibration parameters using the model:

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

where:

  • $\mathbf{m}_c$ = Calibrated magnetometer (function return value)
  • $\mathbf{m}_u$ = Uncalibrated magnetometer (uncalibrated function parameter)
  • $\mathbf{S}$ = Soft-iron matrix (softIronMatrix function parameter)
  • $\mathbf{h}$ = Hard-iron offset vector (hardIronOffset function parameter)
Remap sensor axes

Sensors are often mounted such that their axes are not aligned with the body axes. The library provides the FusionRemap function to remap sensor axes to the body frame using one of the 24 possible orthogonal axis permutations defined by FusionRemapAlignment.

Optimisations
Inline functions

The FUSION_INLINE macro implements either __forceinline or inline __attribute__((always_inline)) to inline functions and optimise for speed at the cost of program size. This can be disabled by uncommenting the FUSION_NO_INLINE in FusionConfig.h (opens in a new tab) or adding this as a preprocessor definition.

Fast inverse square root

Vector and quaternion normalisation operations use Pizer's implementation (opens in a new tab) of the fast inverse square root (opens in a new tab) algorithm. This increases speed for devices without an FPU, at a negligible cost to accuracy. Uncommenting FUSION_USE_NORMAL_SQRT in FusionConfig.h (opens in a new tab) or adding this as a preprocessor definition will use normal square root for all operations.

Robotheca lists software as a reference: nothing is hosted or run here, and every fact links to where it was read.