Combining sensors
Why two imperfect sensors can beat one good one: complementary strengths, the complementary filter, and a first look at the idea behind the Kalman filter.
Lesson 3 of 6 · 35 min · Self-paced · Free
No sensor is good at everything
A balancing robot needs to know its tilt angle many times a second. Two cheap sensors can help, and each is flawed in a different way.
- A gyroscope measures rotation rate, in degrees per second. Add up rate × time and you get the change in angle. Over a fraction of a second this is smooth and accurate. But any small error in the rate, called a bias, is added up too. A bias of just 0.5 °/s becomes a 30° error after a minute. This slow wandering is called drift.
- An accelerometer senses the direction of gravity, so from it you can work out tilt directly. It does not drift: its answer is always about the present. But it is noisy, and every bump or acceleration of the robot looks like a change in gravity's direction.
The gyroscope is good over short times and bad over long ones. The accelerometer is the reverse. Combining them so each covers the other's weakness is called sensor fusion, and it appears everywhere in robotics: phones, drones, wheeled robots that combine wheel odometry with a camera, and grippers that combine vision with touch.
Watch
before you read on
The first video is the core one; the second is optional. Pause whenever you want, then carry on with the explanation below.
Shows how blending a jittery accelerometer with a drifting gyro gives a drone a steady sense of its tilt.
→ This is the complementary filter from this lesson: a jittery accelerometer blended with a drifting gyro.
11 min 12 s · captions on YouTube · source
Nothing loads from YouTube until you press play.
Builds intuition for estimating what you can't measure directly when your sensors are noisy.
→ Optional: the intuition behind the Kalman idea this lesson previews.
6 min 47 s · captions on YouTube · source
Nothing loads from YouTube until you press play.
Absolute and relative sensors
It helps to sort sensors into two kinds.
- Relative sensors measure change: gyroscopes, wheel encoders counting rotations, step counters. You get position or angle by adding up changes, so errors accumulate.
- Absolute sensors measure the quantity itself: an accelerometer's gravity direction, a camera seeing a marker, a GPS fix. Errors do not accumulate, but individual readings are noisy, slow, or sometimes missing altogether.
Fusion usually means: use the relative sensor to predict smoothly from moment to moment, and use the absolute sensor to correct the slow drift.
The complementary filter
The simplest fusion method fits in one line:
angle = α · (angle + gyro_rate · dt) + (1 − α) · accel_angleThe first part is a prediction: last estimate plus the gyroscope's change. The second part pulls the estimate gently towards the accelerometer's absolute answer. With α = 0.98, each update trusts the prediction 98 % and the accelerometer 2 %.
Why does this work? Over a short time, the 2 % pull is too weak to let the accelerometer's noise through, so the output is smooth like the gyroscope. Over a long time, that small pull adds up and steadily drags away any drift, so the output stays anchored like the accelerometer. In effect, the gyroscope signal is high-pass filtered, the accelerometer signal is low-pass filtered, and the two filters complement each other, which is where the name comes from.
The crossover time between "trust the gyro" and "trust the accelerometer" is:
τ = α · dt / (1 − α)At 100 Hz (dt = 0.01 s) with α = 0.98, τ = 0.98 × 0.01 / 0.02 = 0.49 s. Changes faster than about half a second come mostly from the gyroscope; slower ones mostly from the accelerometer.
Worked example
The numbers below are made up for illustration. The robot is held still at a true tilt of 10°. The gyroscope has a bias of 0.5 °/s; the accelerometer's reading has errors of up to 3°.
dt = 0.01 # 100 samples per second
alpha = 0.98
gyro_bias = 0.5 # deg/s error in the gyro
accel_noise = [2.0, -3.0, 1.0, -1.5, 1.5, -0.5, 0.5] # repeating error, deg
true_angle = 10.0
gyro_only = 10.0 # start both estimates at the right answer
fused = 10.0
for k in range(1, 1001):
gyro_rate = 0.0 + gyro_bias # true rate is zero
accel_angle = true_angle + accel_noise[k % 7]
gyro_only = gyro_only + gyro_rate * dt
fused = alpha * (fused + gyro_rate * dt) + (1 - alpha) * accel_angle
if k % 250 == 0:
print(f"t = {k * dt:5.2f} s gyro only {gyro_only:6.2f}"
f" accel {accel_angle:6.2f} fused {fused:6.2f}")Output:
t = 2.50 s gyro only 11.25 accel 9.50 fused 10.24
t = 5.00 s gyro only 12.50 accel 8.50 fused 10.22
t = 7.50 s gyro only 13.75 accel 7.00 fused 10.23
t = 10.00 s gyro only 15.00 accel 10.50 fused 10.25The gyroscope alone drifts steadily, 5° wrong after 10 seconds and getting worse. The accelerometer alone jumps around by up to 3°. The fused estimate stays within about a quarter of a degree, and stays there. That small leftover error is predictable: a complementary filter settles with an error of about bias × τ = 0.5 × 0.49 ≈ 0.25°. Raising α makes the output smoother but leaves more of the gyro bias in; lowering it removes bias faster but lets more accelerometer noise through.
A preview of the Kalman idea
The complementary filter uses a fixed α that you choose. A smarter question is: how much should I trust each sensor, given how noisy each one is right now?
Suppose two sensors measure the same distance, with typical errors (standard deviations) σ₁ = 3 cm and σ₂ = 4 cm. The best way to average them weights each by the inverse of its variance (σ²):
w₁ = σ₂² / (σ₁² + σ₂²) = 16 / 25 = 0.64
w₂ = σ₁² / (σ₁² + σ₂²) = 9 / 25 = 0.36
fused σ = √(σ₁² · σ₂² / (σ₁² + σ₂²)) = √(144 / 25) = 2.4 cmIf they read 52.0 cm and 47.0 cm, the fused estimate is 0.64 × 52.0 + 0.36 × 47.0 = 50.2 cm. Notice that the combined uncertainty, 2.4 cm, is smaller than either sensor alone. That is the core promise of fusion.
A Kalman filter applies this weighting at every time step. It keeps track of its own uncertainty: uncertainty grows during each prediction (because the relative sensor drifts) and shrinks at each correction (because the absolute sensor adds information). When the absolute sensor goes quiet, the filter knows it is becoming less certain and leans entirely on prediction; when a measurement arrives, it weights it according to how uncertain it has become. A Kalman filter can even estimate the bias itself, as an extra unknown, and subtract it. The full mathematics uses matrices and is beyond this lesson, but the intuition is just this: weight each source by how much you trust it, and keep track of how much you trust yourself.
Try it in the explorer
The explorer below fuses two sensors watching a point moving along a line. The camera is the absolute sensor: noisy, and sometimes blocked entirely for a second or so. Odometry is the relative sensor: always available but biased, so it drifts. The complementary option uses exactly the formula above, applied whenever a camera frame arrives: estimate = α · prediction + (1 − α) · camera.
Explorer 03 — Sensor fusion
Two imperfect sensors, one better estimate
- True position
- Camera reading
- Odometry dead-reckoning
- Fused (Kalman)
- ±2σ uncertainty
- Camera dropout
Camera · absolute, drops out
Long-run share of time with no view, in bursts of about 1.2 s (occlusions).
Odometry · relative, drifts
A constant error in measured speed: its position estimate drifts by bias × time.
More controls
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.
What to notice
When the camera disappears, every fused estimate falls back on odometry and starts to drift; the longer the dropout, the bigger the error. When the camera returns, the estimate is pulled back. The Kalman filter usually recovers more cleverly because it has also learned the odometry bias while the camera could see, so it drifts less during the gap.
The explorer is honest about its simplifications: its noise is perfectly random, the Kalman filter is told the true noise levels, and the camera has no delay. Real systems have to estimate noise levels and handle late measurements, which the lesson on timestamps returns to.
Knowledge check
Check your understanding
Go further
- Sensor fusion — Wikipedia ↗
An overview of fusion methods, including the inverse-variance weighting used here.
- Kalman filter — Wikipedia ↗
The full algorithm with its matrix mathematics. Demanding, but the opening sections explain the predict–update idea well.
Lesson materials · free to download
Sign in to track your progress. Reading is free without an account.