TactivergeLite

Lite / Independent Engineering Research / Week 16

The complementary filter

A gyro is smooth but drifts. An accelerometer never drifts but jitters. Could you keep the best half of each?

About 40 min · 3 goals · Checked by automated tests · Full interactive version

Why should I care?

The complementary filter is fusion in one line of code. It runs on hobby drones, phones and balance robots because it is cheap, intuitive and robust, and it shows the frequency-domain view of fusion that the Kalman filter hides.

Predict

In θ = 0.98(θ + ω·Δt) + 0.02·θ_acc, what does the 0.02 path mainly correct?

  1. The gyro's slow drift
  2. The accelerometer's jitter
  3. Both equally
  4. Neither: it is just rounding
Guess first, then reveal

The small, steady pull towards the accelerometer removes slow drift from integrating the gyro. The jitter of the accelerometer is mostly filtered out because it only gets 2% weight each step.

Discover

In the fusion lab choose the complementary filter. With a large odometry bias, sweep α from 0.5 to 0.99. Record the RMSE at each α. Where is the best α? Then switch to Kalman with the same settings and compare: which estimates (and removes) the bias?

What the interactive shows (text version)

The fusion lab's RMSE readout gives the numbers for each α; the expected trend, a best α in the middle with errors rising at both extremes, is described in the instruction.

You can also open the interactive on the full site when you have a faster connection.

Watch (optional)

Watch for: Watch for the picture of a low-pass filter on one sensor and a high-pass filter on the other, adding to 1.

Shows how blending a jittery accelerometer with a drifting gyro gives a drone a steady sense of its tilt.

“Drone Control and the Complementary Filter” by Brian Douglas (opens YouTube; streaming video uses a lot of data)

The video is optional. Everything you need is in this lesson's text.

Understand

Two paths that add to one

θ[k] = α(θ[k−1] + ω·Δt) + (1 − α)·θ_acc. The gyro path (integrate rate) is effectively high-passed: good for fast changes. The accelerometer path is low-passed: good for the slow, long-term level. Because the two filters add to exactly one, the true signal passes through unchanged.

Choosing α

The crossover time constant is τ = αΔt/(1 − α). With α = 0.98 and Δt = 0.01 s, τ = 0.49 s: changes faster than about half a second come from the gyro, slower ones from the accelerometer. Pick τ longer than the accelerometer's jitter but shorter than the time the gyro takes to drift noticeably.

Complementary versus Kalman

The complementary filter uses a fixed blend. A Kalman filter computes its gain from Q and R and can add the gyro bias as a state and estimate it, so the drift is removed rather than just pulled back. In steady state, a scalar Kalman filter with constant gain behaves much like a well-tuned complementary filter.

Explain it another way

Intuitive

Let the gyro steer moment to moment, while the accelerometer gently tugs the answer back towards 'down' over a second or so.

Technical

In the Laplace domain: θ̂ = [τs/(τs + 1)]·(ω/s) + [1/(τs + 1)]·θ_acc. The two transfer functions sum to 1. With a constant gyro bias b, the steady-state error is b·τ: smaller τ, less bias error but more accelerometer noise.

Mathematical

θ_prev = 5°, ω = 3 °/s, Δt = 0.02 s, θ_acc = 5.5°, α = 0.95: gyro step 5.06°, blend 0.95 × 5.06 + 0.05 × 5.5 = 5.082°.

Applied

Rover and drone attitude estimators often start as complementary filters, then grow into Kalman filters when bias estimation or extra sensors (magnetometer, sun sensor) are needed.

Worked example 1

α = 0.98, Δt = 0.01 s. Time constant? Steady-state tilt error from a 0.5 °/s gyro bias?

τ = 0.98 × 0.01/0.02 = 0.49 s. Error ≈ b·τ = 0.5 × 0.49 ≈ 0.25°.

Common mix-ups

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

°

s

Practice 3. What happens if α = 1?

°

Apply it

Using the fusion lab, find the α that minimises RMSE for two different odometry bias settings. Explain, with τ = αΔt/(1 − α), why the best α moves the way it does.

Reflect

When would you choose a complementary filter over a Kalman filter on a real robot?

Write your answer on paper or in your own notes. Lite never stores what you write.

← Inverse-variance weightingEscape speed and Hohmann transfers →