Learnsy lesson 2 ยท standalone TypeScript app

Kalman Filters

Track a delivery robot when GPS is noisy, wheel odometry drifts, and buildings block measurements. Compare Kalman filtering against simpler alternatives on the same route.

Interactive simulator

A delivery robot drives down a street. GPS dots are noisy and may disappear near buildings; wheel odometry is smooth but drifts. The Kalman estimate fuses both and reports uncertainty.

How to read it: raw GPS jumps, moving average lags, dead reckoning drifts, and Kalman balances the motion model with GPS corrections. Wider bands mean less certainty during GPS loss.

Foundation

A Kalman filter estimates a system state that you cannot observe perfectly. Here the hidden state is the robot's position and velocity. GPS reports noisy position; wheel odometry predicts motion between GPS fixes.

  • Prediction moves the previous estimate forward using wheel/IMU motion.
  • Uncertainty grows during prediction because the model is never exact.
  • Update pulls the prediction toward the measurement, weighted by expected sensor noise.

The mental model

The filter is not just a smoother. It carries a belief about where the robot is now, how fast it is moving, and how uncertain that belief is.

That is why it can beat a moving average: it can keep moving through GPS dropout using odometry, then let GPS pull the estimate back when measurements return.

Q: motion uncertainty

How much the robot might slip, accelerate, or be misread by odometry. Higher Q makes the filter more agile and less confident in dead reckoning.

R: GPS noise

How noisy GPS is expected to be. Higher R makes the filter trust each GPS dot less and lean more on the motion model.

P: covariance

The filter's current uncertainty. It grows while the robot coasts on odometry and shrinks when useful GPS measurements arrive.

Why not just use a simpler filter?

Kalman is worth the extra machinery when you have a useful motion model and noisy measurements that arrive over time. It is not automatically better for every signal.

MethodWhere it worksFailure mode in this lesson
Raw GPSSimple and immediate.Jumps around because every noisy dot is accepted as truth.
Moving averageCheap smoothing for steady signals.Lags during acceleration and freezes or goes stale during dropout.
Dead reckoningSmooth short-term tracking from wheel/IMU motion.Drifts because small odometry bias accumulates every second.
Kalman filterOnline sensor fusion with a model, measurements, and uncertainty.Needs reasonable Q/R tuning and a model close enough to reality.

FAQ

Why estimate velocity when GPS measures only position?

Position changes over time reveal velocity, and odometry directly predicts short-term motion. The filter connects those quantities, so GPS can correct both position and velocity.

What is the Kalman gain?

The gain is the adaptive weight between prediction and measurement. If the filter is uncertain and the sensor is trusted, gain rises. If the sensor is noisy or the filter is already confident, gain falls.

What happens during sensor dropout?

The update step is skipped. The estimate continues from the model, but uncertainty grows because there is no new evidence to correct drift.

Why can a low-Q filter drift after GPS returns?

Low process noise says the odometry model is very trustworthy. If wheels slip or speed changes unexpectedly, the filter resists GPS corrections and takes longer to recover.

TypeScript sketch

This is the core loop behind the simulator, written as compact TypeScript-style pseudocode.

function step(z: number | null, dt: number) {
  // Predict from constant velocity.
  x = F(dt).mul(x);
  P = F(dt).mul(P).mul(F(dt).transpose()).add(Q(dt));

  if (z === null) return x;

  // Correct with position measurement.
  const y = z - H.mul(x);
  const S = H.mul(P).mul(H.transpose()).add(R);
  const K = P.mul(H.transpose()).mul(S.inverse());
  x = x.add(K.mul(y));
  P = I.sub(K.mul(H)).mul(P);
  return x;
}

Practical warnings

  • Wrong Q or R values can look plausible while giving overconfident estimates.
  • Covariance is part of the output. Watch it, not only the estimate line.
  • The model matters. A constant-velocity filter can track acceleration only by treating it as process noise.
  • Real systems often need sensor validation before feeding measurements into the update step.