Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 38 ยท Projects and Practice

Project: Tracking a Moving Target with a Kalman Filter

Build a 2D target tracker: simulate a moving object with noisy, missing and outlier measurements, then track it with a Kalman filter, gating and tuning.

A security robot follows a person; a drone tracks a vehicle; a warehouse robot predicts where a forklift will be. All of them face the same problem: a sensor that is noisy, sometimes silent, and occasionally wrong. In this project you build a tracker from the Kalman filter, measure how much it helps, make it robust to dropouts and outliers, and tune it.

You will work with a simulated target moving in 2D (meters, seconds), tracked at 10 Hz with a position sensor (like a camera or radar). The target moves mostly straight but turns now and then, so it is not perfectly constant-velocity: the filter has to tolerate that mismatch.

What the tracker must handle

Noise Every position reading is slightly off.
Dropouts Sometimes no reading arrives.
Outliers Sometimes a reading is very wrong.
Maneuvers The target changes direction.
1 / 2

Step 1: simulate and filter

State: [x, y, vx, vy]. Model: constant velocity: position advances by velocity times dt. Measurement: noisy (x, y) only. We reuse the matrix form: F (transition), H (measurement), Q (process noise), R (measurement noise).

For Q we use the white-noise acceleration model: the target can accelerate randomly with standard deviation sigma_a, which gives a covariance block [[dt^4/4, dt^3/2], [dt^3/2, dt^2]] * sigma_a^2 for each axis.

Python Step 1: a Kalman filter tracker versus raw measurements

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 filtered position error is clearly smaller than the raw sensor error, and the filter also reports a velocity that the sensor never measured. Around the turn the velocity estimate lags for more than a second (about 15 steps): the model believes velocity is constant, so it needs many noisy measurements to be convinced the target changed direction.

Step 2: dropouts, outliers and gating

Real sensors skip frames and sometimes report garbage (a camera locks onto the wrong person, a radar sees a reflection). Two simple defenses:

  • Dropouts: skip the update step and rely on the prediction. The covariance P grows, honestly reporting rising uncertainty.
  • Outliers: compute the innovation y = z - H x and its covariance S. The Mahalanobis distance d2 = y^T S^-1 y tells you how many "standard deviations" away the measurement is. For two measurement dimensions, a gate of d2 < 9.21 accepts about 99 percent of good readings (chi-squared, 2 degrees of freedom). Readings beyond the gate are rejected.
Python Step 2: coasting through dropouts and rejecting outliers

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.

Without gating, each wild reading drags the track away; with gating they are rejected and the position error drops. During the blackout the error stays small because the filter keeps predicting and the target happens to keep moving straight. If it had turned during the blackout, the error would have grown quickly (the covariance P grows to warn you), and the track would snap back only when readings return. Gating has a risk: if the target really makes a sudden maneuver, its readings look like outliers and could be rejected for too long. A common safeguard is to reset or re-acquire the track after several consecutive rejections.

Step 3: tuning Q

sigma_a says how much we believe the target can accelerate. It is the main tuning knob.

Python Step 3: scan sigma_a and read the trade-off

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.

Look for the sweet spot. A very small sigma_a makes the filter overconfident in the constant-velocity model: it smooths nicely on straight segments but lags behind turns. A very large value makes it trust the raw measurements, so the output gets as noisy as the sensor. Good tuning chooses a value that matches how the target really maneuvers. Better still, use two models (constant velocity and a turning or higher-noise model) and blend them: that is the idea of an Interacting Multiple Model (IMM) filter.

Ideas to extend it

  1. Add a second sensor (a slower, more accurate radar at 2 Hz) and fuse both with different R values.
  2. Use measurements in polar coordinates (range and bearing) and move to an Extended Kalman Filter.
  3. Track several targets at once: you need data association (which measurement belongs to which track) before any filter update.
  4. Make the robot follow the track: command a speed that keeps a 2 m distance behind the estimated position, using the predicted position a half second ahead.
  5. Log the innovation and check that it looks like zero-mean noise with the size S predicts. This is the standard test that your Q and R are consistent.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. During a sensor blackout, what should the tracker do?

    Prediction continues from the motion model, and the covariance grows to reflect rising uncertainty. When readings return, the update corrects the estimate.
  2. 2. What does the Mahalanobis gate test?

    d2 = y^T S^-1 y compares the innovation to its predicted covariance, so surprising readings are rejected.
  3. 3. With a very small sigma_a, how does the tracker behave when the target turns?

    A small process noise says the model is nearly perfect, so new measurements move the velocity estimate only slowly.
  4. 4. What is a risk of gating?

    If the model is wrong about a real maneuver, correct measurements fall outside the gate. Track re-acquisition after several rejections avoids getting stuck.

Summary

  • A constant-velocity Kalman filter estimates position and unmeasured velocity from noisy position readings, beating the raw sensor error.
  • Dropouts are handled by predicting without updating; outliers by gating on the Mahalanobis distance.
  • sigma_a (process noise) trades lag against noise; tune it on logged data or use multiple models (IMM).
  • Check innovations for consistency, and add re-acquisition logic so gating cannot lock out real maneuvers.

Next: Robotics Practice Problems