Skip to content
Back to search results

Mahony Filter

Introduced by
Robert Mahony
Year
2008
Type
Observer algorithm
Family
Complementary Filtering
Font size

What does it do?

The Mahony Filter is a complementary observer-based attitude filter that fuses gyroscope, accelerometer, and magnetometer measurements to estimate quaternion orientation.

Developed by Robert Mahony and colleagues in 2008, the filter combines gyroscope rate integration with corrective feedback from vector measurements (accelerometer, compass) using proportional-integral control principles. The Mahony Filter provides similar performance to Kalman Filters but with simpler implementation and lower computational cost, making it ideal for resource-constrained systems and real-time control.

The intuition behind the method

Think of the Mahony Filter as a closed-loop controller where gyroscope provides the 'open-loop' estimate (fast but drifting) and accelerometer/compass provide error signals (slow but stable). A proportional-integral (PI) controller computes corrections and feeds them back to the gyroscope integration loop. High gyroscope gains give fast response; high accelerometer gains provide stability. The two are balanced to achieve both responsiveness and drift-free attitude.

History

Robert Mahony and colleagues at Australian National University developed the complementary observer approach around 2008 while working on quadrotor control. The method was formalized in several influential papers on multirotor attitude estimation. Mahony's formulation unified classical complementary filtering (used in aerospace for decades) with modern quaternion algebra and control theory. The filter became standard in the open-source autopilot community (ArduPilot, PX4) and is widely used in research and commercial systems.

When to use it

Use Mahony Filter for attitude estimation on embedded systems when Kalman Filter is too complex or costly. Ideal for quadrotors, multirotors, and aircraft autopilots. Deploy when you want a physically-motivated, easily-tuned alternative to Madgwick (which uses gradient descent). Suitable for real-time control loops requiring low latency and stable estimates. Use when you understand proportional-integral control and can tune K_p, K_i.

Limitations and considerations

  • Tuning K_p and K_i requires understanding of control theory and empirical experimentation.
  • Performance sensitive to tuning; poor choice of gains can cause oscillation or sluggish response.
  • Integral term can cause wind-up; large, sustained errors accumulate and cause overshoot; mitigation needed.
  • Like Madgwick, magnetometer disturbance affects heading accuracy; requires calibration and possibly disturbance detection.

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

Strengths

  • Physically intuitive; based on control theory (PI feedback); easier to understand and debug than gradient descent.
  • Explicit gyroscope bias estimation; the integral term learns and corrects sensor bias over time.
  • Two tuning parameters (K_p, K_i) have clear physical meaning (proportional and integral gain) unlike beta in Madgwick.
  • Similar computational cost to Madgwick; lightweight and suitable for embedded systems.

Applications

  • Quadcopter and multirotor flight control: primary attitude estimator in most open-source autopilots (ArduPilot, PX4).
  • Fixed-wing aircraft autopilot: attitude reference in low-cost UAV flight controllers.
  • Unmanned aerial vehicle (UAV) research: standard filter in academic motion control studies.
  • Marine vessel heading and stabilization: attitude reference for ship autopilot and roll stabilization.
  • Mobile robot orientation tracking: orientation estimation for wheeled and legged robots.

How it works

How it works

1

Collect measurements from a 9-DOF IMU: 3-axis accelerometer, 3-axis gyroscope, and 3-axis magnetometer at 50+ Hz.

2

Normalize accelerometer and magnetometer vectors; obtain gyroscope rate; apply calibration if available.

3

Compute expected direction of gravity (down) and magnetic north (from current attitude estimate); compare with measured directions; compute error as cross product (geometric error).

Formula
\dot{\vec{q}} = \frac{1}{2}\vec{q} \otimes \vec{\omega} + k_p \vec{e} + k_i \int \vec{e} dt
4

Apply proportional correction (K_p * error) and integral correction (K_i * integral(error)) to estimate gyroscope bias; update quaternion using corrected angular velocity.

5

Output quaternion; convert to Euler angles if needed; assess confidence based on error magnitude and sensor agreement.

Common pitfalls

  • Choosing K_p and K_i too high causes oscillation and instability; typical values are K_p ~ 1.0, K_i ~ 0.1, tuned empirically.
  • Forgetting to bound the integral term; unchecked integration can accumulate errors; introduce integral wind-up limits.
  • Assuming the accelerometer error model is Gaussian; non-Gaussian disturbances (vibration, impact) can corrupt the filter; robust estimators may be needed.
  • Deploying without testing under intended motion profiles; filter tuning depends on expected maneuvering rate and sensor quality.

Frequently asked questions

How do I tune K_p and K_i?

K_p is the proportional gain (typically 0.5–2.0); higher K_p means faster correction but risk of oscillation. K_i is the integral gain (typically 0.01–0.1); it controls bias correction rate. Start with K_p=1.0, K_i=0.1. Increase K_p if the filter lags behind maneuvers; decrease if it oscillates. Increase K_i if bias drift is significant; decrease if integral wind-up occurs.

What is integral wind-up and how do I prevent it?

Integral wind-up occurs when the integral term grows unboundedly during sustained error (e.g., magnetometer disturbance). When the error finally clears, the filter overshoots. Prevent by saturating the integral term (bound it between limits) or by conditional integration (only integrate when error is small).

What is the difference between Mahony and Madgwick filters?

Mahony uses PI feedback with explicit bias estimation; Madgwick uses gradient descent. Mahony is based on control theory; Madgwick on optimization. Both have similar computational cost. Mahony typically has faster bias convergence; Madgwick is simpler to implement. For attitude-only (no bias), they perform similarly.

Can Mahony handle large accelerations?

During large accelerations, accelerometer measures inertial acceleration + gravity, causing pitch/roll error. The Mahony Filter assumes accelerometer points downward; large accelerations violate this. Some implementations use acceleration magnitude thresholding to reduce accelerometer weight during maneuvers, improving robustness.

Related methods

Sources

  1. 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 reference
    Copied.
  2. Mahony, 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 reference
    Copied.
  3. Valenti, 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
    Copied.