Madgwick Filter

Simulation
Introduced by
Sebastian Madgwick
Year
2010
Type
Filter algorithm
Family
Gradient Descent Filtering

What does it do?

The Madgwick Filter is an attitude estimation algorithm that combines gyroscope measurements with accelerometer and, optionally, magnetometer measurements to estimate a quaternion orientation.

It corrects gyroscope integration with a gradient-descent step that reduces the mismatch between measured gravity and magnetic field directions and the directions predicted from the current quaternion. Described by Sebastian Madgwick in a 2010 report and a 2011 conference paper, it is designed to be computationally light enough for embedded processors.

The intuition behind the method

Gyroscope integration tracks rotation quickly but accumulates error over time. The accelerometer indicates the direction of gravity and, in the MARG variant, the magnetometer indicates the direction of the local magnetic field; each gives an absolute reference for part of the orientation. The filter asks which small change in the quaternion would make the rotated reference directions match what the sensors measure, computes the gradient of that mismatch, and steps the quaternion a little in that direction. The step is scaled by beta, which balances trust in the gyroscope against the reference correction. Repeating this at each update gives a continuously corrected orientation estimate.

History

Sebastian Madgwick described the algorithm in a 2010 report from x-io Technologies, University of Bristol. It was presented in 2011 in a paper with A. J. L. Harrison and R. Vaidyanathan at the IEEE International Conference on Rehabilitation Robotics, which applies gradient descent to IMU and MARG orientation estimation. The 2011 paper is the conference reference for the method, and the 2010 report is the earlier technical description.

When to use it

Choose the Madgwick Filter when you need real-time orientation from an IMU on hardware with limited computing power, such as microcontrollers or wearable devices, and when a method with one main tuning parameter is preferable to one that needs a noise covariance model. It is a reasonable choice for roll and pitch estimation during slow or moderate motion. Use the MARG variant only when the magnetometer readings can be trusted; in environments with strong magnetic disturbance, or where accurate heading is needed over long periods, a method that models sensor noise explicitly, such as the quaternion-based extended Kalman filter studied by Sabatini, may be more appropriate. Do not choose it where a statistically optimal estimate with a known noise model is required.

Limitations and considerations

  • The correction is one gradient-descent step per update on a defined error function, not a statistically optimal estimator, so the output carries no covariance quantifying its uncertainty.
  • Beta is fixed. The filter does not change the correction strength when sensor noise or motion changes, so a gain tuned for one motion profile may be wrong for another.
  • The filter has no built-in test for disturbed readings. During sustained linear acceleration the accelerometer no longer measures gravity alone, so its correction introduces roll and pitch error in either variant.
  • In the IMU variant yaw has no absolute reference and follows gyroscope error. In the MARG variant yaw depends on the magnetometer, which is affected by nearby ferromagnetic material and local magnetic fields.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • The correction uses one gain (beta) and needs no covariance matrices or matrix inversion, so each update consists of a small number of quaternion operations.
  • The accelerometer gives a gravity reference for roll and pitch in the IMU variant; adding the magnetometer in the MARG variant also gives an absolute yaw reference.
  • Gyroscope prediction keeps the output responsive between correction steps. While the accelerometer and, in the MARG variant, the magnetometer readings are reliable, the reference correction counteracts gyroscope drift. During linear acceleration or magnetic disturbance this correction is itself biased (see limitations).
  • The same structure covers both the 6-axis IMU and 9-axis MARG configurations, so the sensor set can be matched to the available hardware.

Applications

  • Wearable and handheld motion tracking, where orientation must be computed on a small processor alongside other tasks.
  • Attitude estimation for small unmanned aircraft with limited processing resources.
  • Orientation tracking of body segments for human movement analysis, the field the original work addressed.
  • Head-orientation tracking for virtual reality displays.
  • Hobby and educational robotics on microcontroller boards, often as an introduction to sensor fusion.

How it works

How it works

1

Read the gyroscope angular rate and, where available, the accelerometer and magnetometer vectors from the IMU. The IMU variant uses the accelerometer and gyroscope only; the MARG variant uses all three sensor types, nine axes in total. Sample at a fixed update interval. The gyroscope integration step uses this interval, so the rate must be known and constant, and a beta value tuned at one rate should be rechecked if the rate changes.

2

Normalise the accelerometer and magnetometer vectors to unit length so that only their directions enter the correction. Keep the gyroscope rate as read, after applying any known bias or scale-factor calibration.

3

Form the error between the measured sensor directions and the directions predicted from the current quaternion, and compute the gradient of that error with respect to the quaternion. Step the quaternion in the direction that reduces the error. The gyroscope rate is integrated separately to predict the change in quaternion between updates.

Formula
\nabla f = \frac{\partial f}{\partial \vec{q}}
4

Combine the gyroscope-based prediction with the gradient-descent correction. The correction is weighted by the single gain beta, which sets how strongly the reference data pull the estimate. The result is the quaternion output at each update.

5

Use the quaternion directly, or convert it to roll, pitch and yaw Euler angles. Yaw has an absolute reference only in the MARG variant; in the IMU variant it is not corrected by any absolute reference. Judge confidence from sensor quality and motion state, because the filter does not itself flag unreliable measurements.

Common pitfalls

  • Beta set too high makes the estimate follow noisy accelerometer and magnetometer readings and can cause oscillation; set too low, the estimate responds slowly and keeps the gyroscope error. Tune beta by testing against motions of known orientation rather than adopting a fixed value.
  • Skipping magnetometer calibration: hard-iron and soft-iron distortion shifts the measured field direction and so corrupts the yaw estimate. Calibrate before relying on heading in the MARG variant.
  • Using the accelerometer as a gravity reference during intense manoeuvres: the basic filter does not detect this condition, so roll and pitch are biased for as long as the accelerometer is measuring more than gravity.
  • Starting from an arbitrary orientation: the initial quaternion sets the starting error the correction must remove. If the device is moving at start-up, the initial estimate can be far from the true attitude, so initialising while the device is at rest is a common precaution. The settling time after start-up should be measured on the target hardware.

Frequently asked questions

How do I choose the beta parameter?

Beta sets how strongly the gradient correction acts relative to the gyroscope prediction. If the estimate responds too slowly to real motion, increase beta; if it oscillates or follows sensor noise, decrease it. Change it in small steps and check the response against motions of known orientation. No universal value should be assumed, because the appropriate setting depends on the sensors, the sample rate and the motion.

What is the difference between the IMU (6-axis) and MARG (9-axis) variants?

The IMU variant uses the accelerometer and gyroscope. It gives roll and pitch referenced to gravity, but yaw has no absolute reference and follows gyroscope error. The MARG variant adds the magnetometer, which provides an absolute yaw reference, but that reference is only as good as the magnetic environment around the sensor. Choose IMU where the magnetic environment is unreliable and only roll and pitch are needed; choose MARG where heading matters and the magnetic environment has been checked.

Can I run the Madgwick Filter on a 32-bit microcontroller?

The filter consists of quaternion products and vector normalisation, so it is feasible on small processors. The achievable update rate depends on the processor, whether floating-point arithmetic is supported in hardware, and the implementation. Benchmark on the target hardware before relying on a specific rate.

How do I detect magnetic disturbance?

Compare the magnetometer magnitude with the expected local field strength for the site. The value of roughly 50 microtesla is a typical order of magnitude, but the field varies with location, so use a local reference model or a reading taken at a known-good site rather than a fixed constant; a large deviation suggests nearby distortion. Also compare the heading from the magnetometer with the heading from gyroscope integration over a short interval; a persistent disagreement indicates a disturbance. Mitigations include ignoring magnetometer updates while disturbance is suspected or reducing their influence. The formulation described on this page uses a single beta, so any separate magnetometer gain is an implementation choice rather than part of the method.

Related methods

Sources

  1. Madgwick, S. O. H., Harrison, A. J. L., & Vaidyanathan, R. (2011). Estimation of IMU and MARG orientation using a gradient descent algorithm. IEEE International Conference on Rehabilitation Robotics (ICORR), 1–7.

    Open publication reference
  2. Madgwick, S. O. H. (2010). An efficient orientation filter for inertial and inertial/magnetic sensor arrays. Report x-io Technologies, University of Bristol, UK.

    Open publication reference
  3. Sabatini, A. M. (2006). Quaternion-based extended Kalman filter for determining orientation by inertial and magnetic sensing. IEEE Transactions on Biomedical Engineering, 53(7), 1346–1356.

    Open DOI reference