control//state estimation//complementary filter
A complementary filter is a fixed-gain estimator that fuses two sensors whose errors live at different frequencies, passing one through a high-pass filter and the other through a low-pass filter whose sum is exactly one, and it estimates the attitude of a great many small drones, camera gimbals and balancing robots. The gyroscope, integrated, gives an angle that is precise over a short time and drifts; the accelerometer gives the angle from gravity, \(\phi^{acc}=\operatorname{atan2}(a_y,a_z)\), without drift but polluted by the motors' vibration and by every manoeuvre's acceleration (IMU). Their defects sit in different bands, so the filter keeps each sensor where it is good:
A complementary filter is a fixed-gain estimator that fuses two sensors whose errors live at different frequencies, passing one through a high-pass filter and the other through a low-pass filter whose sum is exactly one, and it estimates the attitude of a great many small drones, camera gimbals and balancing robots. The gyroscope, integrated, gives an angle that is precise over a short time and drifts; the accelerometer gives the angle from gravity, ϕacc=atan2(ay,az)\phi^{acc}=\operatorname{atan2}(a_y,a_z)ϕacc=atan2(ay,az), without drift but polluted by the motors' vibration and by every manoeuvre's acceleration (IMU). Their defects sit in different bands, so the filter keeps each sensor where it is good:
ϕ^k=α (ϕ^k−1+ωk Δt)+(1−α) ϕkacc,α=ττ+Δt.\hat\phi_k=\alpha\,(\hat\phi_{k-1}+\omega_k\,\Delta t)+(1-\alpha)\,\phi^{acc}_k,\qquad \alpha=\frac{\tau}{\tau+\Delta t}.ϕ^k=α(ϕ^k−1+ωkΔt)+(1−α)ϕkacc,α=τ+Δtτ.
ω\omegaω is the gyroscope's rate, Δt\Delta tΔt the sampling period and τ\tauτ the time constant: on time scales shorter than τ\tauτ the gyroscope rules, on longer ones the accelerometer. At 1 kHz with τ=0.5\tau=0.5τ=0.5 s, α=0.998\alpha=0.998α=0.998. The name comes from the two filters adding up to one, so the true angle passes undistorted and only each sensor's defect is filtered out. Its limit is exact: a constant gyroscope bias bbb leaves a steady error bτb\taubτ, 0.25° with 0.5 °/s and τ=0.5\tau=0.5τ=0.5 s, and raising τ\tauτ to reject more vibration lets more bias through. The whole compromise lives in one number.
RMS, complementary0.62° RMS, Kalman0.32° RMS, accelerometer8.8° bias estimated0.57 °/s A maneuvering drone's tilt from a gyroscope with a 0.5 °/s bias and an accelerometer shaken at 1.5 m/s². Over the last 10 s the complementary filter with τ = 0.50 s is off by 0.62° RMS, the Kalman filter by 0.32°, the accelerometer alone by 8.8°. The bias alone shifts the complementary filter by b τ = 0.25°, while the Kalman filter estimates the bias at 0.57 °/s and subtracts it. The best τ for these sensors is about 0.75 s, at 0.59°, which does not reach the Kalman filter.
With no bias, find the τ\tauτ that minimizes the complementary filter's error; then raise the gyroscope bias and watch it settle about bτb\taubτ off, an offset no τ\tauτ removes, while the Kalman filter estimates the bias and subtracts it.
Four lines, a dozen operations per step and one parameter that anyone understands.
If the attitude is only needed to stabilize and the sensors are decent, it is the right tool. The Kalman filter or the extended Kalman filter pays off when sensors at different rates must be fused, biases and position estimated, or the uncertainty used to decide (rejecting readings, detecting faults): it does not improve the attitude by magic, it improves the bookkeeping (flyswatter rule).
The usual upgrade adds an integral term that estimates the gyroscope bias, which is the Mahony filter, the kind small flight-controller firmwares such as Betaflight run. The Madgwick filter reaches orientation by gradient descent on the accelerometer's and magnetometer's error, and ArduPilot keeps a direction-cosine-matrix estimator of the same family (DCM) as a backup to its EKF.
A sustained manoeuvre fools it. In a coordinated turn the accelerometer's down is tilted for seconds, and with a short τ\tauτ the estimate follows it; the time constant must be longer than the manoeuvres it is meant to ride through.
It is a special case of the Kalman filter: for one particular noise model it is the steady-state Kalman filter, with the gain set by hand through τ\tauτ instead of computed from the noise.