Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 38 ยท Sensors and Perception

Sensor Fusion

Combine noisy sensors into one good estimate: weighted averaging, the complementary filter for IMU tilt and a one-dimensional Kalman filter, with runnable Python.

Every sensor lies a little, but different sensors lie in different ways. A gyroscope is smooth but drifts; an accelerometer is noisy but anchored to gravity; wheel odometry is exact for a moment and then wanders; GPS is steady but jumpy. Sensor fusion combines them so that the result is better than any single one. This lesson builds the three core ideas: weighting by uncertainty, the complementary filter and the Kalman filter.

flowchart LR
    G["Gyroscope<br/>smooth, drifts"] --> F["Fusion"]
    A["Accelerometer<br/>noisy, no drift"] --> F
    F --> E["Estimate<br/>smooth and drift-free"]

Each sensor covers the other's weakness.

Weighting by uncertainty

Suppose two sensors measure the same distance: sensor 1 says 2.00 m with standard deviation 0.10 m, sensor 2 says 2.20 m with standard deviation 0.20 m. Which do you believe? Neither entirely: trust the precise one more. The best linear combination weights each by the inverse of its variance (ฯƒยฒ):

x = (x1/ฯƒ1ยฒ + x2/ฯƒ2ยฒ) / (1/ฯƒ1ยฒ + 1/ฯƒ2ยฒ)

and the fused variance is 1 / (1/ฯƒ1ยฒ + 1/ฯƒ2ยฒ), always smaller than either input. Fusing never loses information when the weights are right.

Python Inverse-variance fusion of two sensors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The complementary filter

Fusing a gyroscope and an accelerometer for tilt shows a different pattern: the errors live at different frequencies. The accelerometer is wrong at high frequency (noise, vibration) but right at low frequency (average gravity). The gyro is right at high frequency (quick motion) but wrong at low frequency (drift). So take the low frequencies from one and the high frequencies from the other:

angle = ฮฑ ยท (angle + gyro ยท dt) + (1 - ฮฑ) ยท accel_angle

with ฮฑ close to 1 (for example 0.98). At each step, advance the angle with the gyro and then nudge it slightly toward the accelerometer's answer. The nudge removes drift; the gyro supplies smoothness. This one line runs on tiny microcontrollers and balances many hobby robots. The time constant is ฯ„ = ฮฑ dt / (1 - ฮฑ): below it the gyro is trusted, above it the accelerometer.

Python Complementary filter for tilt

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The fused curve follows the truth closely, has far less noise than the accelerometer and no drift unlike the gyro. Try alpha values of 0.8 and 0.999 to see the trade-off.

The Kalman filter idea

The complementary filter uses a fixed weight. The Kalman filter computes the best weight at every step from the uncertainty. It keeps two things: the state estimate xฬ‚ and its variance P. Each cycle has two phases:

  1. Predict: use a model of how the state changes (for example "position += velocity ร— dt") to move the estimate forward. Uncertainty grows, because the model is imperfect: P โ† P + Q where Q is the process noise.
  2. Update: when a measurement z with noise variance R arrives, compute the Kalman gain K = P / (P + R). Move the estimate toward the measurement: xฬ‚ โ† xฬ‚ + K (z - xฬ‚), and reduce uncertainty P โ† (1 - K) P.

If the measurement is precise (R small), K approaches 1 and you trust it; if it is noisy, K is small and you trust the prediction. This is the same inverse-variance weighting as before, applied step by step through time. A later chapter extends this to several dimensions and to non-linear models (the extended Kalman filter) for localization.

Python A one-dimensional Kalman filter tracking a robot

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The gain starts large (the first estimate is a guess) and decreases as the filter becomes confident. If the motion model were wrong, say the robot really moved at 0.7 m/s, a small Q would make the filter lag behind; raising Q tells it to trust measurements more. Tuning Q and R is the day-to-day work with Kalman filters.

Practical advice

  • Fuse sensors with different error types; two identical sensors with the same flaw add little.
  • Timestamp every measurement; fusing data from different moments gives errors, especially during rotation.
  • Model the noise honestly: an overconfident filter (R too small) ignores corrections it should accept, and diverges.
  • Reject outliers before fusing (see the median filter in Sensors and Noise).
  • Start with the simplest filter that meets your needs: a complementary filter handles tilt on a microcontroller; use a Kalman filter when you have a good motion model and several sensors.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. When fusing two measurements of the same quantity, how should they be weighted?

    More precise sensors (smaller variance) get larger weights, and the fused variance is smaller than either.
  2. 2. Why does the complementary filter combine a gyroscope and an accelerometer?

    The gyro tracks quick motion but drifts slowly; the accelerometer is noisy quickly but correct on average.
  3. 3. In a Kalman filter, what happens when the measurement noise R becomes very large?

    K = P / (P + R); with large R the gain is small.
  4. 4. What are the two phases of each Kalman filter cycle?

    Predict moves the estimate with a model (uncertainty grows); update corrects it with a measurement (uncertainty shrinks).

Summary

  • Fusion combines sensors with complementary weaknesses so the estimate beats each one.
  • Inverse-variance weighting is the optimal way to merge two measurements.
  • The complementary filter blends gyro (high frequency) and accelerometer (low frequency) for tilt in one line.
  • A Kalman filter predicts with a model and updates with measurements, choosing the weight (gain) from uncertainty.

Next: Motors and Actuators