TactivergeLite

Lite / Textbooks / Introduction to Intelligent Systems / Chapter 6

6. State estimation and sensor fusion

Version 1 · Technically checked (automated checks only) · 4 source lessons · Full interactive version

Introduction

No single sensor tells a robot where it is. Wheels drift, cameras drop frames, gyros wander. Fusion combines them so the estimate is better than any one. This chapter weights readings by their reliability, blends a fast drifting sensor with a slow absolute one in a complementary filter, and builds the Kalman filter's predict–update cycle, whose gain is exactly the weight that makes the combined variance smallest.

Figure 6.1. Two beliefs make a sharper one (described in words)

Two Gaussian beliefs about a position: 10 cm with σ = 1 cm and 11 cm with σ = 2 cm. Inverse-variance weights are 0.8 and 0.2, so the fused estimate is 10.2 cm with σ ≈ 0.894 cm, narrower than either sensor alone.

Explanation

Built from 4 School lessons. Each part is the lesson's own explanation.

6.1 Inverse-variance weighting

Two sensors measure the same distance. One is twice as precise. Should you trust it twice as much? Four times? All the way?

The recipe

x̂ = (z₁/σ₁² + z₂/σ₂²)/(1/σ₁² + 1/σ₂²) and σ_f = 1/√(1/σ₁² + 1/σ₂²). The fused value sits between the readings, closer to the more precise one; the fused σ is smaller than the smaller σ. Two equal sensors give σ/√2.

The fine print

It assumes both readings are unbiased, independent and have correctly known σ. A biased sensor drags the fused value with full confidence. Correlated errors (two cameras with the same calibration fault) give less improvement than the formula promises.

A consistency check

If the readings disagree by much more than √(σ₁² + σ₂²), something is wrong: a fault, a bias, a wrong σ. Real systems test the difference before fusing and reject outliers. In Kalman language this is innovation gating.

Derivation: Why inverse-variance weights are optimal
  1. Blend two independent unbiased readings: x̂ = w·z₁ + (1 − w)·z₂. It is unbiased for any w.

  2. Its variance is V(w) = w²σ₁² + (1 − w)²σ₂² (no cross term, by independence).

  3. Minimise: dV/dw = 2wσ₁² − 2(1 − w)σ₂² = 0 ⇒ w = σ₂²/(σ₁² + σ₂²) = (1/σ₁²)/(1/σ₁² + 1/σ₂²).

  4. d²V/dw² = 2(σ₁² + σ₂²) > 0, so this is a minimum.

  5. Substitute back: V_min = σ₁²σ₂²/(σ₁² + σ₂²), i.e. 1/σ_f² = 1/σ₁² + 1/σ₂². For n sensors, weights ∝ 1/σᵢ² and precisions add.

Common mix-ups

Open the Lite lesson: Inverse-variance weighting

6.2 The complementary filter

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

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.

Common mix-ups

Open the Lite lesson: The complementary filter

6.3 The predict–update cycle

Drive a metre: your uncertainty grows. See a landmark: it shrinks. Repeat forever. That rhythm is the whole idea of a filter.

Predict

Push the estimate through the model: x⁻ = x + u (here, the commanded move) and P⁻ = P + Q. Q is the variance the model adds each step: wheel slip, gusts, anything unmodelled. Prediction always increases uncertainty.

Update

When a measurement z with variance R arrives, compute the innovation z − x⁻, the gain K = P⁻/(P⁻ + R), then x = x⁻ + K(z − x⁻) and P = (1 − K)P⁻. This is the conjugate normal update from week 13 with the prediction as prior.

Checking a filter honestly

In simulation you know the truth, so measure RMSE of x − x_true. Also check consistency: about 95% of errors should fall within ±2√P. If the truth often leaves the band, the filter is overconfident (Q or R too small); if it never comes close to the edge, it is too cautious.

Common mix-ups

Open the Lite lesson: The predict–update cycle

6.4 The Kalman gain as the variance-minimising weight

Why P/(P + R)? Not a guess, not a rule of thumb: it is the only weight that makes the error as small as possible.

Reading the gain

K → 1 when R ≪ P⁻: trust the sensor, x ≈ z. K → 0 when R ≫ P⁻: trust the prediction, ignore the reading. K is not a tuning knob you set; it is computed from the variances you believe. What you tune are Q and R.

Steady state

With constant Q and R the variance stops changing. For the random-walk model x[k+1] = x[k] + u + w, the prior variance converges to P⁻∞ = (Q + √(Q² + 4QR))/2. With Q = 0.3, R = 2: P⁻∞ ≈ 0.939, K∞ ≈ 0.319. Many embedded filters precompute this constant gain.

In more dimensions

For vector states, K = P⁻Hᵀ(HP⁻Hᵀ + R)⁻¹ and P = (I − KH)P⁻, where H maps the state to what the sensor sees. The scalar case is H = 1. The same minimum-variance argument, done with matrices, gives the same structure.

Derivation: The scalar Kalman gain
  1. Update rule: x = x⁻ + K(z − x⁻) = (1 − K)x⁻ + Kz. Prediction error e⁻ has variance P⁻; measurement error v has variance R; they are independent.

  2. Posterior error e = (1 − K)e⁻ + K·v (the true x cancels because both terms are unbiased).

  3. Its variance: P(K) = (1 − K)²P⁻ + K²R.

  4. Minimise: dP/dK = −2(1 − K)P⁻ + 2KR = 0 ⇒ K = P⁻/(P⁻ + R).

  5. Substitute back: P = (1 − K)P⁻ = P⁻R/(P⁻ + R), i.e. 1/P = 1/P⁻ + 1/R: inverse-variance weighting, again.

Common mix-ups

Open the Lite lesson: The Kalman gain as the variance-minimising weight

Worked examples

Inverse-variance weighting: example 1

Readings 10 (σ = 3) and 13 (σ = 2). Fuse them.

Solution

Weights 1/9 : 1/4 = 4 : 9. x̂ = (4 × 10 + 9 × 13)/13 ≈ 12.08. σ_f = 6/√13 ≈ 1.66.

The complementary filter: example 1

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

Solution

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

The predict–update cycle: example 1

x = 0, P = 1 m². Drive u = 1 m with Q = 0.25 m². Fix z = 1.6 m with R = 0.5 m². Predict and update.

Solution

x⁻ = 1, P⁻ = 1.25. K ≈ 0.714. x ≈ 1.43 m, P ≈ 0.357 m² (σ ≈ 0.60 m).

The Kalman gain as the variance-minimising weight: example 1

Q = 0.3, R = 2. Find the steady-state prior variance, gain and posterior variance.

Solution

P⁻∞ = (0.3 + √(0.09 + 2.4))/2 ≈ 0.939. K∞ = 0.939/2.939 ≈ 0.319. P∞ = (1 − 0.319) × 0.939 ≈ 0.639.

Interactive figures, in words

Figure 6.2 from Inverse-variance weighting

Set σ₁ = 1, σ₂ = 2. Drag your weight w from 0 to 1 and watch the lower curve: the blend's σ dips to a minimum. Find it by hand, then press 'snap' to check. Repeat with equal σ's: where is the minimum now, and what is the fused σ?

The fusion diagram plots both sensor curves, your blend and the optimal fusion, plus a curve of blend σ against weight with its minimum marked. The readout gives the optimal weight, fused value and σ, and how far your chosen weight is from optimal.

Figure 6.3 from The predict–update cycle

Step predict → update ten times in clear skies and watch the band settle to a steady width. Reset, switch on dust, and step again: note how wide the band gets during the gaps. Check after each update that the green truth line stays inside the ±2σ band most of the time.

The Kalman stepper plots truth, landmark fixes and the estimate with a ±2σ band over 20 steps. Each button press updates a text readout with the estimate, σ, truth, error and gain.

The interactive versions are in the full chapter.

Laboratory

These run in a browser on the full site (they need a faster connection). Simulations are simplified educational models.

Practice

Each question links to its Lite lesson, where your answer is checked on our server and a wrong answer gets a hint. Answers are never sent to your device.

  1. Sensor σ's are 2 m and 4 m. What weight does the 2 m sensor get?
    Answer it in “Inverse-variance weighting”
  2. Sensor B has a hidden +5 m bias but claims σ = 0.5 m; sensor A is unbiased with σ = 2 m. The fused estimate will be…
    Answer it in “Inverse-variance weighting”
  3. A complementary filter runs at Δt = 0.005 s with α = 0.99. What is its time constant τ, in seconds?
    Answer it in “The complementary filter”
  4. What happens if α = 1?
    Answer it in “The complementary filter”
  5. A filter has P = 0.4 m² after an update. Q = 0.3 m² per step. With no fixes for 6 steps, what is P, in m²?
    Answer it in “The predict–update cycle”
  6. In simulation, the true position leaves the filter's ±2σ band on 40% of steps. The most likely problem is…
    Answer it in “The predict–update cycle”
  7. A filter is told R = 0 (a perfect sensor). What does it do at each update?
    Answer it in “The Kalman gain as the variance-minimising weight”
  8. P⁻ = 4 m², R = 1 m². What is the posterior variance P, in m²?
    Answer it in “The Kalman gain as the variance-minimising weight”

Engineering applications

Drones holding level

A drone fuses a gyro, which is smooth but drifts, with an accelerometer, which is noisy but knows where down is. A complementary filter gives a steady attitude estimate hundreds of times a second.

Phones finding themselves

A phone's location blends satellite fixes, Wi-Fi and motion sensors. When the satellite signal is lost indoors, the motion prediction carries the estimate until a fix returns.

Watch (optional)

Streaming video uses a lot of data. Everything you need is in the text.

Summary

Glossary

IMU (inertial measurement unit)
An IMU combines an accelerometer, which measures acceleration including gravity, and a gyroscope, which measures angular velocity; some add a magnetometer. Field Guide
Encoder counts and resolution
A rotary encoder reports rotation as a count of ticks. Its resolution is the number of counts per revolution (CPR), often four times the pulses per revolution with quadrature decoding. Field Guide
Odometry
Odometry estimates a robot's position and heading by adding up how far its wheels have turned since a known start. Field Guide
Random and systematic error
Random errors scatter readings unpredictably above and below the true value; systematic errors shift every reading the same way. Field Guide

Project: Where is the rover?

Using the rover or two-sensor simulation, estimate position with dead reckoning alone, then with fusion. Tune the filter's noise settings, state them, and report the error over a full route, including during sensor dropouts.

Deliverables

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

References

← 5. Decisions under uncertainty7. Learning from data →