- Introduced by
- Robert Mahony
- Year
- 2008
- Type
- Observer algorithm
- Family
- Complementary Filtering
What does it do?
The Mahony filter is a complementary, observer-style attitude estimator with proportional-integral feedback.
It fuses gyroscope, accelerometer and magnetometer measurements into a quaternion orientation. The gyroscope rate is integrated to predict orientation, and measured gravity and magnetic directions provide an error signal that corrects the prediction. The approach is associated with Robert Mahony and is intended for real-time use on low-cost inertial measurement units.
The intuition behind the method
Think of the filter as a feedback loop around gyroscope integration. The gyroscope gives a fast, smooth estimate of rotation, but integrating it lets the estimate drift. The accelerometer and magnetometer give slower, noisy reference directions for down and north. The mismatch between where those measured directions point and where the current estimate predicts them to point becomes an error signal. A proportional term responds to the present error, and an integral term accumulates past error, which allows the filter to estimate a steady gyroscope bias. There is no separate gyroscope gain: the balance between trusting the gyroscope and trusting the reference vectors is set by K_p and K_i.
History
The complementary observer approach to attitude estimation is associated with Robert Mahony. The filter is formulated in quaternion form and is often compared with the gradient-descent Madgwick filter. A 2012 tutorial on multirotor modelling, estimation and control, with Mahony among its authors, covers the quadrotor setting in which filters of this kind are applied.
When to use it
Choose the Mahony filter for attitude estimation on embedded or real-time hardware where a full Kalman filter is considered too costly or too complex to tune, and where the platform carries a 9-DOF IMU. It suits multirotor and small-aircraft attitude loops that need a low-latency estimate. It is a reasonable choice for users comfortable with proportional-integral feedback who can tune the gains on logged data. Without a magnetometer (6-DOF operation) the filter can still correct roll and pitch from gravity, but heading is not corrected and drifts, so this configuration fits only applications that tolerate yaw drift. Avoid it, or adapt the accelerometer weighting, when the platform undergoes sustained linear acceleration, because that violates the gravity reference the filter assumes. If the application requires a statistically optimal estimate under an explicit noise model, a Kalman-type estimator is the more suitable choice.
Limitations and considerations
- The gains must be tuned empirically on logged data from the intended motion. The sensor specification alone does not determine them, and values tuned for one platform do not transfer directly to another.
- Poor gain choice produces either oscillation or slow tracking of rapid rotations.
- The integral term can wind up during sustained error, such as a long magnetic disturbance, and then overshoot when the error clears, so it must be bounded or gated.
- Heading depends on the magnetometer. A local magnetic disturbance biases yaw, and the filter has no independent yaw reference to resist it.
- Without a magnetometer, yaw is not corrected at all: roll and pitch remain referenced to gravity, but heading drifts with gyroscope bias.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- The error term is built from the mismatch between measured and predicted gravity and magnetic directions, so the filter's behaviour can be traced to its two gains and its vector references.
- The integral term estimates gyroscope bias explicitly, so a steady bias is learned and removed rather than only suppressed.
- K_p and K_i are separate, interpretable parameters: K_p governs how quickly the estimate responds to reference error, and K_i governs how fast the bias estimate adapts.
- The per-sample update consists of a few vector operations and a quaternion propagation, which keeps it practical for embedded control loops.
Applications
- Attitude estimation for quadrotor and multirotor vehicles, the setting addressed by the 2012 tutorial on multirotor estimation and control.
- Flight controllers for low-cost UAVs and small aircraft whose attitude reference is a 9-DOF IMU.
- Orientation tracking for mobile and legged robots that carry an inertial and magnetometer sensor package.
- Handheld or wearable motion tracking, where orientation is estimated from inertial and magnetic measurements.
How it works
How it works
Collect synchronised readings from a 9-DOF IMU (three-axis accelerometer, three-axis gyroscope and three-axis magnetometer) at a sampling rate matched to the platform's dynamics and the chosen gains.
Normalise the accelerometer and magnetometer vectors so that only their directions enter the error calculation. Obtain the gyroscope rate and apply any available calibration to the gyroscope and magnetometer.
Predict the expected gravity direction and the magnetic reference direction from the current attitude estimate, and compare them with the measured directions. The error is taken as the cross product between each measured direction and its predicted counterpart, which gives a three-component error vector.
Formula
\dot{\vec{q}} = \frac{1}{2}\vec{q} \otimes \vec{\omega} + k_p \vec{e} + k_i \int \vec{e} dtThe proportional term k_p·e and the integral term k_i∫e dt enter the attitude update shown in the mechanics formula, together with the gyroscope rate. The integral term accumulates error over time, and this accumulation is the mechanism by which the filter tracks a slowly varying gyroscope bias.
The output is the attitude quaternion, which can be converted to Euler angles if required. Read the error term as a health signal. Near a steady state it should stay small and settle; a large error that persists during motion usually indicates linear acceleration or a magnetic disturbance rather than a gain problem, and disagreement between the accelerometer-based and magnetometer-based predictions points the same way. Typical error magnitudes depend on the sensors and the platform, so compare the error against logged data from known-good runs rather than against a fixed value.
Common pitfalls
- Setting K_p or K_i too high causes oscillation. Start from small values and increase them only while the estimate remains stable on recorded data.
- Leaving the integral term unbounded lets error accumulate; apply saturation limits to the integral state.
- Treating the accelerometer as a pure gravity sensor. During linear acceleration, vibration or impact the accelerometer no longer points down, and the filter will correct toward a wrong attitude unless accelerometer weight is reduced during these periods.
- Validating only on static or slow motion. Gains that look stable at rest can fail during aggressive manoeuvres, so test on the motion profile the platform will actually fly or drive.
Frequently asked questions
How do I tune K_p and K_i?
Tune on logged data from the intended motion profile. Start with a small K_p and an even smaller K_i. Raise K_p if the estimate lags real rotations, and lower it if the estimate oscillates. Raise K_i only if a steady gyroscope bias persists once K_p is set, and lower it if the integral term causes overshoot or wind-up.
What is integral wind-up and how do I prevent it?
Integral wind-up occurs when the integral term grows without bound during sustained error, for example during a magnetometer disturbance. When the error finally clears, the filter overshoots. Prevent it by saturating the integral term between limits or by integrating only when the error is small.
What is the difference between Mahony and Madgwick filters?
The Mahony filter applies proportional-integral feedback to the reference-vector error and estimates gyroscope bias through its integral term. The Madgwick filter takes a gradient-descent step on an orientation objective. Both are low-cost quaternion filters, and the practical difference lies mainly in tuning structure and in how bias is handled.
Can Mahony handle large accelerations?
During large accelerations the accelerometer measures inertial acceleration plus gravity, which causes pitch and roll error. The Mahony filter assumes the accelerometer points down, and large accelerations violate this assumption. Some implementations reduce the accelerometer weight, or skip the accelerometer correction, while the measured acceleration magnitude departs noticeably from gravity. The departure threshold is not fixed by the method; choose it from logged data of the platform's manoeuvres, and keep the reduced weight active only for the duration of the disturbance.
Related methods
Sources
Mahony, R., Hamel, T., & Pflimlin, J. M. (2008). Multirotor aerial vehicles: Modeling, estimation, and control of quadrotors. IEEE Robotics and Automation Magazine, 19(3), 20–32.
Open publication referenceMahony, R., Hamel, T., & Pflimlin, J. M. (2012). Multirotor aerial vehicles: Modeling, estimation, and control of quadrotors. IEEE Robotics & Automation Magazine, 19(3), 20–32.
Open DOI referenceValenti, R. G., Dryanovski, I., & Xiao, J. (2016). Keeping a good attitude: A quaternion-based orientation filter for IMUs and MARGs. Sensors, 15(8), 19302–19330.
Open DOI reference