Skip to content
Tactiverge

School/✦ Research Engineer/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

1.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.

  • ◇ Write the discrete complementary filter and explain its two paths.
  • ◇ Relate α to a time constant τ = αΔt/(1 − α).
  • ◇ Compare complementary and Kalman filtering for a biased relative sensor.

2.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?

Explorer 03 — Sensor fusion

Two imperfect sensors, one better estimate

A camera gives absolute position but loses sight of the target; odometry never drops out but drifts. Combining them gives an estimate better than either alone.

POSITION (cm)NO VIEWNO VIEWNO VIEW-40-200204002468101214161820TIME (s)
  • True position
  • Camera reading
  • Odometry dead-reckoning
  • Fused (Kalman)
  • ±2σ uncertainty
  • Camera dropout

Camera · absolute, drops out

3.0 cm
25 %

Long-run share of time with no view, in bursts of about 1.2 s (occlusions).

Odometry · relative, drifts

4.0 cm/s
1.5 cm/s

A constant error in measured speed: its position estimate drifts by bias × time.

Fusion method

The Kalman filter weights each sensor by its stated noise, tracks its own uncertainty, and estimates the odometry bias while the camera can see.

Show

Root-mean-square error vs truth · lower is better

  • Camera aloneholds last reading in dropouts7.9 cm
  • Odometry alonedead reckoning from start15.1 cm
  • Kalman filterfused estimate0.9 cm
  • Fused, in dropoutsonly samples with no camera1.1 cm

Camera coverage 60 %. Camera error only over frames it saw: 2.9 cm (≈ its σ).

State

Seed 7. The camera saw the target 60 % of the time in 4 dropouts. Error (RMSE): camera alone 7.9 cm holding its last reading, odometry alone 15.1 cm, kalman filter 0.9 cm (1.1 cm during dropouts). Fusion is 88 % better than the better single sensor. Estimated odometry bias 1.53 cm/s (true 1.50).

Seed 7. The camera saw the target 60 % of the time in 4 dropouts. Error (RMSE): camera alone 7.9 cm holding its last reading, odometry alone 15.1 cm, kalman filter 0.9 cm (1.1 cm during dropouts). Fusion is 88 % better than the better single sensor. Estimated odometry bias 1.53 cm/s (true 1.50).

How to read this

Time runs left to right over 20 seconds. The thin solid line is where the object really is. Crosses are camera readings; hatched columns are dropouts where the camera sees nothing. The dashed line is odometry integrated from the start: smooth, but it wanders away as its bias accumulates. The thick line is the fused estimate, with a shaded ±2σ band showing how uncertain the Kalman filter believes it is. The band widens during each dropout and snaps back when the camera returns.

This mirrors Tactiverge's research question: when vision drops out, can local sensing such as force and joint feedback carry the estimate until it returns?

Assumptions & limitations
  • One dimension, a smooth known trajectory and a single object: real tracking is 3-D, with orientation, multiple objects and data association.
  • Noise is white Gaussian with the σ you set, and the Kalman filter is told the true σ values. Real filters must be tuned against noise they can only estimate.
  • Odometry error is a constant bias plus white noise; real drift also comes from slip, scale-factor error and temperature.
  • Dropouts are random bursts, independent of where the object is. Real occlusions often happen exactly when the task gets hard, for example when a gripper covers the object.
  • The camera has no latency and runs at the same 20 Hz as odometry; real cameras are slower and delayed, which the filter would have to compensate for.
  • Figures are from one seeded run of 401 samples; change the seed to see how much the numbers vary by chance.
Can't use the interactive? Read a description instead

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.

3.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1 · a fresh version every time

A complementary filter estimates tilt: θ = α(θ_prev + ω·Δt) + (1 − α)·θ_acc. With θ_prev = 5°, gyro rate ω = -4°/s, Δt = 0.05 s, accelerometer tilt θ_acc = 6.5° and α = 0.98, what is the new θ, in degrees?

°

4.Predict

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

Guess first. Being wrong here is how the surprise works.

5.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

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

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

  • The complementary filter averages the two sensors.
    ✓ It splits them by frequency: gyro for fast changes, accelerometer for slow ones.
  • A bigger α is always better because the gyro is smoother.
    ✓ Bigger α means a longer τ and more bias error. The best α balances drift against jitter.

6.Watch

→ 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.

11 min 12 s · captions on YouTube · source

Nothing loads from YouTube until you press play.

The video is optional: everything you need is also in the lesson text and the interactive.

7.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 2

A complementary filter runs at Δt = 0.005 s with α = 0.99. What is its time constant τ, in seconds?

s

Practice 3

What happens if α = 1?

Practice 4

With τ = 2 s and an uncorrected gyro bias of 0.3 °/s, what is the approximate steady-state tilt error, in degrees?

°

8.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.

9.Reflect

Concepts: Sensor fusion · Noise and filtering