Engineering · Robot Autonomy · Perception

Sensor Fusion: Two Liars, One Truth

How does a robot know if it is tipping over? It has a tiny chip that feels turning and feels which way is down. Each sense makes mistakes, so the robot listens to both and mixes them together. You do the same thing every time you stand on one foot!

A gyroscope is smooth and quick, but it slowly drifts off. An accelerometer never drifts, but it jitters and gets fooled by every bump. Neither can be trusted alone. Sensor fusion blends them so each covers the other's weakness. It's how a phone knows which way is up, a drone stays level, and a self-balancing robot stays upright.

Ages 6–18 · 3 levels45–60 minutesLive IMU simFilters
← Back to Engineering
Three traces over time: a drifting orange gyro line, a jagged blue accelerometer line, and a smooth green fused line that follows the true angle gyro drifts · accel jitters · fused follows

Explorer · Story time

Two helpers who both make mistakes

I’m Bolt, and I balance on one wheel. To stay up, I need to know how much I’m leaning. I have two helpers inside me.

Spinny (the gyroscope) feels every turn. Spinny is smooth and fast, but slowly forgets where “straight up” is.

Droppy (the accelerometer) always knows which way is down, because it feels gravity. But Droppy is jumpy. Every bump makes Droppy shout the wrong answer.

So I listen to Spinny for quick moves, and to Droppy a tiny bit, to remember which way is down. Mixing helpers like this is called sensor fusion.

  • Sensor · a part that feels or measures
  • Tilt · how much something leans
  • Gravity · the pull toward the ground
  • Mix / fuse · put together

Try it with your body

  1. Flamingo test. Stand on one foot with your eyes open. Count to 10. Now close your eyes and try again. Which was harder? Your eyes and your inner ear are two sensors, and your brain fuses them.
  2. Spin and stop. Spin slowly five times, then stop. The room seems to keep moving! Your inner ear “drifted,” just like Spinny. Look at something still to fix it.
  3. Balance a ruler. Balance a ruler or broom upright on your hand. You watch the top and move your hand. That is exactly what a balancing robot does, many times a second.

Meet the sensors

Every sensor lies a little

An IMU (inertial measurement unit) is a tiny chip holding a gyroscope and an accelerometer — the same chip as in the MPU-6050 module in your sensor kit. To find tilt, you can use either one. Both are wrong, in opposite ways.

Gyroscope

Measures how fast you're rotating. Add up (integrate) the rate over time to get the angle.

Good: smooth, instant, ignores bumps. Bad: a tiny constant error (bias) gets added every step, so the angle drifts away forever.

Accelerometer

Measures which way gravity pulls. Gravity points down, so its direction gives the tilt directly.

Good: never drifts. Bad: noisy, and any shove, bump, or turn looks like tilt.

Fusion

Trust the gyro for fast changes. Trust the accelerometer for the long-run average. Blend the two continuously.

Robots fuse many more: wheel encoders, GPS, cameras, LiDAR, and compass.

Sim · Tilt estimator

Fuse a gyro and an accelerometer

The robot wobbles on its wheel. Each colored line is a guess of how much it leans. The white line is the real answer. Turn lines on and off with the checkboxes. Which guess stays closest to white?

The blue robot body wobbles. Its IMU reads 100 times a second. Each line is a different way to estimate the tilt. The white line is the truth, which a real robot never gets to see. Watch the scoreboard below the chart.

Show
Motion
Sensor flaws
Complementary filter

1 · Spinny forgets

Choose Hold still. Wait 10 seconds. The orange gyro line slowly floats away even though the robot is still!

2 · Droppy jumps

Turn on Bumpy floor. Watch the blue line jump with every bump.

3 · Better together

Look at the green line. It is smooth like Spinny but does not float away. Press Describe the chart to check which guess is best.

1 · Drift

Set movement to Hold still and wait. What happens to the gyro-only line? Set the bias to 0. Does the drift stop?

2 · Tune α

Drag α to 0.5, then 0.999. Which extreme looks like the accelerometer, and which like the gyro? Find the α with the lowest error.

3 · Bumps

Turn on the bumpy floor. Which estimate gets fooled most? Turn on Kalman and compare it to your best α.

The math, one line each

Two classic filters

Complementary filter

angle = α·(angle + gyro·dt) + (1 − α)·accel

Each step: move the old angle by the gyro, then nudge it a tiny bit (1 − α) toward the accelerometer. The nudges are too small to pass noise through, but they add up enough to cancel drift. Five lines of Arduino code.

Kalman filter

Rudolf Kálmán (1960). It keeps a best guess and how uncertain it is. It predicts with the gyro (uncertainty grows), then corrects with the accelerometer. Its weight is set by comparing the two uncertainties, not by hand.

This version also estimates the gyro's bias and subtracts it.

Where it flew

A Kalman filter helped guide Apollo to the Moon. It fused the spacecraft's inertial sensors with radar and star sightings. Today it runs in every GPS receiver, phone, drone, and self-driving car.

Engineer deep dive

Filters as frequency and probabilityAges 15+

Why “complementary”

The filter is a high-pass on the integrated gyro plus a low-pass on the accelerometer. The two transfer functions add to exactly 1, so they complement each other:

Hgyro(s) = τs / (τs + 1)   Hacc(s) = 1 / (τs + 1)

The crossover time constant is τ = α·Δt / (1 − α). With α = 0.98 and Δt = 0.01 s, τ ≈ 0.49 s. Slower than that, trust the accelerometer. Faster, trust the gyro.

Kalman, in matrices

State x = [angle, bias]T. Predict, then update:

x̂⁻ = F x̂ + B u
P⁻ = F P Fᵀ + Q
K = P⁻Hᵀ (H P⁻Hᵀ + R)⁻¹
x̂ = x̂⁻ + K (z − H x̂⁻)
P = (I − K H) P⁻

Here F = [[1, −Δt], [0, 1]], u = gyro rate, H = [1, 0], z = accelerometer angle. The sim's kalmanStep() is these five lines written out by hand.

Tuning Q and R

R is how noisy you think the accelerometer is; Q is how much you think the true angle and bias can wander. Raise R and K shrinks, so the filter behaves like a high α. The Kalman filter is the optimal linear estimator only when the noise really is Gaussian with those variances.

Beyond linear

Real orientation is 3-D, and rotations don't add like vectors. Drones use quaternions with an Extended Kalman Filter (EKF) or the Madgwick/Mahony filters. Self-driving cars fuse LiDAR, GPS, wheel odometry, and IMU in one big EKF or factor graph.

Why it matters

Fusion feeds everything else

A balancing robot's PID controller can only be as good as the angle it's given. Feed it the gyro-only line and it slowly leans until it falls. Feed it the raw accelerometer and it shudders. Feed it the fused angle and it stands. The same goes for position: localization fuses wheel odometry with LiDAR scans.

Where you’ll see it

Fusion hardware, from Apollo to your pocket

Click any photo to enlarge it.

The Apollo inertial measurement unit, a metal sphere-shaped gyroscope assembly, in a museum case
Apollo’s IMU Spinning gyroscopes inside this sphere tracked the spacecraft’s orientation on the way to the Moon. Its drift was corrected with star sightings, and a Kalman filter did the blending.Photo: Daderot · CC0 · Wikimedia Commons
A small blue GY-521 circuit board with an MPU-6050 chip and labeled pins
The same job, on your bench The GY-521 module in your sensor kit holds an MPU-6050: a 3-axis gyroscope and a 3-axis accelerometer on one chip, for a few dollars.Photo: Nevit Dilmen · CC BY-SA 3.0 · Wikimedia Commons
A magnified die photo of the MPU-6050 chip showing its microscopic structures
Inside the chip This die photo shows the MPU-6050’s microscopic moving parts (MEMS). Tiny springs and masses shift as the chip tilts or turns, and the change is measured electrically.Photo: ZeptoBars · CC BY 3.0 · Wikimedia Commons
NASA’s Ingenuity helicopter standing on the red surface of Mars
Ingenuity on Mars NASA’s helicopter combined an IMU, a laser altimeter, and a downward camera to stay level and know where it was. Radio signals take too long to reach Earth, so no one could fly it by hand.Photo: NASA/JPL-Caltech/ASU/MSSS · Public domain · Wikimedia Commons

Robot autonomy series

Sense · Think · Act

Quick check

Lock it in

1. Which helper always knows which way is down?

2. Why does the robot listen to both helpers?

3. Why is it harder to stand on one foot with your eyes closed?

1. Why does a gyro-only angle drift even when the robot sits still?

2. Why do bumps fool the accelerometer?

3. In a complementary filter with α = 0.98, which sensor handles fast changes?

4. What does a Kalman filter do that a basic complementary filter doesn't?

5. A complementary filter runs at 100 Hz with α = 0.99. What is its crossover time constant?