Madgwick Filter
- Introduced by
- Sebastian Madgwick
- Year
- 2010
- Type
- Filter algorithm
- Family
- Gradient Descent Filtering
What does it do?
The Madgwick Filter is a computationally lightweight attitude estimation algorithm that fuses inertial measurements (accelerometer, gyroscope) with magnetic measurements (magnetometer) to compute a quaternion orientation.
Introduced by Sebastian Madgwick in 2010, the algorithm uses gradient descent optimization to minimize the error between measured and expected sensor outputs, yielding accurate, drift-free attitude estimates on embedded systems with minimal computational cost. The Madgwick Filter is now ubiquitous in consumer electronics, robotics, and aerospace systems.
The intuition behind the method
The idea is to find the quaternion (orientation) that best explains the observed sensor measurements. Accelerometer and compass tell us where gravity and magnetic north are; gyroscope tells us how fast we are rotating. The algorithm computes a direction (gradient) of attitude adjustment that brings the estimated and measured vectors closer together, then steps slightly in that direction. Repeated many times per second, this produces a smooth, real-time attitude estimate that does not drift.
History
Sebastian Madgwick developed the algorithm around 2010 while researching motion capture and human movement analysis. The algorithm was published in the IEEE International Conference on Rehabilitation Robotics in 2011 and released as open-source code. It gained rapid adoption in the DIY robotics and maker communities due to its simplicity and effectiveness. Major consumer electronics companies (smartphones, VR headsets, fitness trackers) adopted variants. The Madgwick Filter remains popular in embedded systems and education, often serving as the introduction to sensor fusion for students.
When to use it
Use Madgwick Filter when you need lightweight, real-time attitude estimation on resource-constrained devices (microcontrollers, mobile phones, wearables). Ideal for applications where Kalman Filter complexity is overkill. Effective indoors and outdoors, in vehicles, drones, and wearables. Deploy when you have a 9-DOF IMU and want to avoid tuning multiple Kalman Filter parameters. Use for consumer electronics, hobby robotics, and real-time control systems.
Limitations and considerations
- Heuristic, not optimal; does not minimize any formal cost function (unlike Kalman Filter).
- Performance depends critically on beta (step size); no automatic adaptation if sensor quality or motion changes.
- Magnetometer disturbance can corrupt heading; requires manual calibration; does not automatically detect or reject bad measurements.
- Large accelerations (in MARG variant) cause temporary errors as accelerometer misaligns with gravity; filter recovery is heuristic, not principled.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Minimal computational cost; implemented in < 100 lines of code; runs on microcontrollers and mobile processors.
- No matrices or complex linear algebra required; only quaternion algebra and dot products.
- Provides IMU-only (6-DOF) and MARG (9-DOF) variants; adaptable to available sensors.
- Single tuning parameter (beta) makes it easy to customize; no covariance matrices or matrix inversions.
Applications
- Smartphone and wearable motion tracking: enables motion gesture recognition and augmented reality without heavy computation.
- Quadcopter and drone stabilization: provides roll, pitch, yaw estimates for flight control on resource-limited autopilots.
- Virtual reality (VR) headset motion tracking: real-time head orientation for immersive displays.
- Motion capture and human animation: tracks body segment orientation for gait analysis and animation.
- Robotics and hobby drones: lightweight alternative to Kalman Filters for attitude estimation on Arduino, Raspberry Pi.
How it works
How it works
Collect measurements from a 9-DOF IMU: 3-axis accelerometer, 3-axis gyroscope, and 3-axis magnetometer at 50+ Hz.
Normalize accelerometer and magnetometer vectors to unit length; store gyroscope raw rate vector; apply any known sensor calibration (bias, scale factor).
Compute gradient of the measurement error with respect to quaternion; step the quaternion in the direction that minimizes error (gradient descent); integrate gyroscope rate to predict quaternion evolution.
Formula
\nabla f = \frac{\partial f}{\partial \vec{q}}Blend the gradient-descent correction with gyroscope integration via a single parameter (beta) that controls the correction strength; output quaternion at each update.
Convert quaternion to Euler angles (roll, pitch, yaw) or use quaternion directly; report attitude; assess confidence based on sensor reliability and motion state.
Common pitfalls
- Choosing beta too high causes rapid oscillation and divergence; too low causes sluggish response; typical range is 0.01–0.1, tuned empirically.
- Ignoring magnetometer calibration; soft iron and hard iron distortion can cause heading errors; calibration is critical for accurate yaw.
- Assuming accelerometer is always valid; during intense acceleration, accelerometer cannot be used for roll/pitch estimation; some algorithms detect and ignore accelerometer during maneuvers.
- Deploying without initialization; the algorithm converges within seconds, but it is best initialized with the vehicle at rest (static initialization).
Frequently asked questions
How do I choose the beta parameter?
Beta controls the rate of gradient descent correction. Start with beta = 0.041 (the value Madgwick used). If the filter is sluggish (slow to respond to motion), increase beta (up to 0.1–0.5). If the filter oscillates or drifts, lower beta (down to 0.01). Tune empirically by watching attitude response to known motions.
What is the difference between IMU (6-DOF) and MARG (9-DOF) variants?
IMU variant uses accelerometer and gyroscope only; it provides roll and pitch but yaw drifts without a reference. MARG variant adds magnetometer for absolute yaw; it provides stable 3-DOF attitude but is sensitive to magnetic distortion. Use IMU for short-duration flights; use MARG for stationary or slow navigation.
Can I run Madgwick Filter on a 32-bit microcontroller?
Yes. The algorithm is very efficient; ARM Cortex-M4 can run it at 200+ Hz. Even 8-bit microcontrollers (with floating-point libraries) can manage 50+ Hz. Performance depends on clock rate and floating-point support. With fixed-point arithmetic, it scales to much slower processors.
How do I detect magnetic disturbance?
Monitor magnetometer magnitude; Earth's field is ~50 micro-Tesla; larger deviation indicates distortion. Compare compass heading (from magnetometer) with heading from gyro integration; large disagreement signals disturbance. Mitigation: temporarily ignore magnetometer updates during disturbance, or reduce its weight (lower beta_m in some variants).
Related methods
Sources
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 referenceCopied.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 referenceCopied.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 referenceCopied.