Click & WhirrAll lessons

State Estimation · Lesson e2

The confident liar

This is the written walk-through of the lesson: the idea, stage by stage, plus the deeper asides. The interactive version, where you write real Python in the browser and drive a simulated robot, lives on the lesson page.

Your estimator from e1 is complete and holding. New problem: sometimes the fixes themselves lie. Multipath episodes corrupt the tower link, and both towers agree on a world 220 px away, so a filter that believes every fix sprints toward a phantom, three times per run, and cross-checking the towers catches nothing. You cannot fix the sensor. You can judge it: measure how far each tick’s fix disagrees with your belief (the innovation) and skip the wild ones. One if statement, known in the trade as validation gating.

A confident, wrong fix

Your e1 Estimator is running, complete, in the editor. Slip on, heading corrected, belief locked. Press Run and watch it hold beautifully for about six seconds.

Then the yank. Real range sensors have a failure mode called multipath: the signal bounces off something (here, the workshop wall) and arrives late, having traveled a path that is not the straight line you assume. The readings are not noisy garbage you could average away; they are clean, confident fixes that happen to describe a world about 220 px away from the real one. Worse, the whole tower link glitches together, so tower A and tower B agree with each other about the lie. Your filter treats every fix like gospel, pulls hard, and your belief sprints toward a phantom. Three bursts per run, on a schedule.

Judge the fix before you believe it

You cannot fix the sensor, but you can judge it. Your belief is not nothing: it is the accumulated verdict of every honest fix so far, and it is rarely more than a couple dozen pixels wrong. So before you correct, measure the innovation: the distance from where you believe you are to where this tick’s fix implies you are. An honest fix lands within the noise, a few tens of pixels. The phantom lands 220 px away. They are not subtle to tell apart, and the belief is the only referee left once the towers agree with each other.

Put the whole update under one condition: only correct when the jump is under self.gate. That single if is a famous move; real filters call it validation gating, and it is the difference between an estimator that averages its inputs and one that has standards.

Hold through all three bursts

The gate itself is a dial, and both ends of it fail. Set it huge and you have the starter again: every phantom walks straight through. Set it tiny and you starve: honest fixes scatter by the beacon noise, your gate rejects almost all of them, and with no corrections running at all the slip drift you beat in e1 comes roaring back. The steady checkpoint watches the whole run, bursts included, so it catches both failure modes.

Tune gate in the code and re-run until the belief holds through all three bursts. Passing updates the Estimator in your library: the capstone ahead will trust this class with the whole robot.

Go deeper: Gating without a magic number

Your gate is a fixed pixel count you tuned by eye. Real filters derive it: they carry a running estimate of their own uncertainty, so a wild jump means many times larger than my current doubt. When the filter is fresh and unsure, the gate opens wide and accepts big corrections; once it has settled, the gate tightens on its own. That self-sizing gate (usually phrased in standard deviations, a Mahalanobis distance) is another face of the same idea you met in e1’s aside: the Kalman filter turns your hand-tuned constants into quantities it computes.

The failure mode does not disappear, it just moves: if a filter ever becomes confidently wrong, a self-sized gate can reject the honest fixes that would rescue it. Robots that must survive that carry a watchdog that notices it has been rejecting everything for a while and reopens the gate. Standards, plus humility about them.

Ready to build it? The interactive lesson is where you write the code and watch the robot run.