Part 2 · How robots sense

20Noise: why every sensor lies a little, and how averaging helps

Two rovers with the same shaky sensors drive at the same box. One believes every reading and flinches at a box that is not there yet; the other believes the average of the last few, and drives smoothly. Then make it average too much, and watch it turn too late.

This assumes: Measuring distance: sonar and lidar. About 9 minutes of reading.

Step on a bathroom scale three times in a row and it will likely show three slightly different numbers. You did not change between steps; the reading did. Every sensor does this, and the rover in the last module was no exception: with Noise turned up, its rangefinders' dots jumped about on the wall while nothing moved. This module is about what that does to a robot that has to act on the numbers, and about the oldest trick for calming them down, which comes with a price.

A wobble with no pattern

Noise is the random wobble in a sensor's readings: a little high one time, a little low the next, and no pattern to which. The last module gave the reasons for a rangefinder: the echo arrives as a smear rather than a click, and a faint flash of light is hard to time exactly, so where the chip decides the signal "arrived" shifts a little every time. No amount of care in building the sensor removes that entirely. It is what measuring is like: a better-made sensor wobbles less, never not at all.

A person reading a noisy scale shrugs and takes the middle. A robot cannot shrug. Its brain is a set of rules, and the rover's are the ones from the first module: head for the beacon, and if anything is nearer than 40 centimeters straight ahead, turn away. A rule like that takes each number at its word. A reading that comes in twenty centimeters short is, to the rule, a box twenty centimeters nearer than it really is, and the robot turns away from a box that is not there yet. It has no "really" column to check against. The number is all it has.

Two rovers

The box below holds two copies of the last module's rover, driving at a brown box with the beacon behind it. In the earlier modules this simulation quietly let the rover's brain use the true distances, whatever the Noise slider said. Here each brain gets only what its sensors report, as a real one would. The beacon's direction is still handed to both brains exactly, as it always has been; the noise is in the five distances.

Both rovers have the same sensors with the same noise, set very high: about 20 centimeters either way, several times worse than a cheap sonar, so that you can see it. The only difference is what the brain does with the numbers. The left rover steers by every reading as it arrives. The right one steers by the average of the last eight readings, and it has a second slider, Averaging, that sets how many. Each side has its own Noise slider, and both start at the same setting. Its bars and chart carry a green average beside the orange readings.

Play, Pause, Reset and Slow work both rovers at once; each slider moves its own side only. The rovers open paused, and Reset puts the sliders back where they started, so set a slider first and then press Play. Each rover's path is drawn on the floor behind it. The noise is random, so no two runs are quite the same.

Steering by every reading

Loading the simulation. The robot runs in your browser, so the first one takes a few seconds to arrive.

Steering by the average

Loading the simulation. The robot runs in your browser, so the first one takes a few seconds to arrive.

This is easier on a larger screen, but it works here too.

Why the average is steadier

An average works because noise has no favorite direction. Take eight readings of a box 50 centimeters away, and some will say 60, some 42, one 71, one 35; the too-high ones and the too-low ones pull against each other, and their average comes out much nearer 50 than most of the readings do on their own. The canceling is never complete, though, so each extra reading helps a little less than the one before. Coins show why. Toss four and you will rarely get two heads and two tails exactly, and rarely four of a kind either; typically one side wins by about two. Toss a hundred and one side typically wins by about ten, not by fifty: the leftover grows, but far more slowly than the number of tosses. Errors that go up or down at random behave the same way. Add up four readings and the total is typically off by about twice what one reading is off by, not four times. Divide that total by four to get the average, and the average is off by half as much as one reading. Sixteen readings leave a total off by about four times one reading's error, and dividing by sixteen leaves a quarter. At 20 centimeters of noise, the right rover's eight readings leave about 7 centimeters of wobble in the green line.

The rover's sensors report twenty times a second, and the brain cannot wait around collecting a pile of readings before every decision. So it keeps a moving average: the average of the last eight readings, worked out afresh every time a new one arrives, the newest coming in and the oldest dropping out. The window of eight slides along with time, and there is always an answer ready. A moving average is the simplest filter: anything that cleans up a sensor's readings before the robot acts on them.

What averaging costs

The average has a weakness hidden in the word "last". The eight readings it is made of were taken over the last four tenths of a second, while the box came steadily closer. The early ones said farther, the late ones nearer, and together they average out to the reading taken halfway through: the box as it was about a fifth of a second ago. The rover drives at about 30 centimeters a second, so in that fifth of a second it came about 5 centimeters closer. That delay is the filter's lag: how far behind the world a filtered reading is. At eight readings it hardly matters. Averaging more readings buys a steadier number and pays for it in lag, and the bill is paid in distance.

What you saw

The left rover flinched. A reading that came in short looked, to its rule, exactly like a box that had come closer, and it turned early and swerved on the way. It never ran into anything: noise made it jumpy, not blind, and a jumpy robot is slow and wastes its battery on swerves. But a rule that fires on one reading will, sooner or later, fire on a bad one.

The right rover at eight readings was steadier and only a little late. At forty it was very steady and dangerously late, for the same reason as before with a window five times as long. Forty readings span two seconds, so the average describes the box as it was about a second ago; at 30 centimeters a second the rover came 30 centimeters closer in that second; so when the average finally read 40 centimeters, the box was really about 10 away. With the noise switched off, the average was still late by the same amount, because lag is not a side effect of noise. It is what an average of the past is.

So there is no free setting. Too little averaging and the robot believes every wobble; too much and it believes a world that has already moved on. Engineers choose the window for the job: a robot vacuum creeping toward a wall can afford a long average, a car braking at speed cannot. Faster sensors ease the choice, because the same number of readings then spans less time: a lidar reporting a thousand times a second gathers forty readings in four hundredths of a second, so their average is about two hundredths of a second old, and a rover at 30 centimeters a second moves less than a centimeter in that time.

Averaging also fixes only one kind of error. It cancels a wobble because a wobble goes both ways. A sensor that always reads 3 centimeters long averages to 3 centimeters long, however many readings you take: an error that points the same way every time has nothing to cancel against. The last module's sonar on a cold morning is one of these: the cold slows the sound by the same amount for every click, so every reading is off the same way until the air warms. That kind is found by checking the sensor against something known, not by averaging it.

Cleverer filters

A moving average treats the reading from two seconds ago as seriously as the one from just now, which is why it lags so badly. Better filters give the newest readings more say and the old ones less. The best ones also use what the robot knows about itself. Such a filter keeps its own running guess of the distance. A rover can tell how fast it is going from how fast its wheels are turning, and one that knows it is driving at 30 centimeters a second knows that a box ahead should come 1.5 centimeters closer between readings, so at every reading the filter first moves its guess 1.5 centimeters closer, and only then looks at the new reading and nudges the guess a little way toward it. The guess moves with the rover, so it does not live in the past; and one wild reading can only nudge it, so it does not flinch. The best known filter built this way is the Kalman filter, named after its inventor. It helped steer the Apollo spacecraft to the Moon, and some version of it runs in nearly every robot and phone today, blending everything they sense into one best guess.

What the simulation leaves out

The noise here is the tidiest kind there is: the same size at every distance, with each reading's error unconnected to the last one's, all drawn from the same smooth spread. Real noise is messier. A sonar's wobble grows with distance; a reading can be off by a meter because the echo came back from a different wall, and one reading that wild drags a short average a long way; and the errors of readings taken close together are often alike, so they cancel less than the arithmetic above promises. The sensors here also report exactly twenty times a second, all five at the same instant, which real ones rarely do. The rest is real: every sensor wobbles, a robot acts on the wobble unless something stops it, and anything that stops it is looking at the past.

Watch

How To Smooth Out Any Analog Signal? Motion Filtering & Signal Smoothing / Averaging With Arduino — Robotics EveryDay (4:09 of it, starting at 0:00)

Why this video
A real Arduino and a real knob, standing in for a sensor, with its readings drawn live on a plotter: a blue trace that shivers even when nobody touches anything, and a red trace that is the average of the last few readings. Then the window grows, step by step, from two readings to fifty, and you can watch both halves of this module happen on the one screen. The red line gets calmer, and it also slides further and further behind the blue one, rounding off every corner and arriving late at every change, which is lag you can see.
What to watch for
At 0:31 the raw readings "fluctuating a lot" with the knob untouched: that shiver is noise. At 0:55 the average of just two readings, red almost on top of blue. From 2:06 the window is ten readings, and at 2:23 twenty: each time the knob is turned, look at how far the red line trails to the right of the blue one. Just before 3:23 the knob is twisted quickly back and forth and the red line cannot keep up, and from 3:23 the window is fifty readings: dead calm when the knob is still, and far behind when it moves. Between the spoken lines it is music only, so keep your eyes on the two lines.

Words introduced

noise
The random wobble in a sensor's readings, a little high one time and a little low the next, with no pattern to it, so that the same distance never reads quite the same twice.
moving average
The average of a sensor's last few readings, worked out again every time a new one arrives, the newest reading coming in and the oldest dropping out.
filter
Anything that cleans up a sensor's readings before the robot acts on them. A moving average is the simplest filter there is.
lag
How far behind the world a filtered reading is. An average of the last two seconds of readings describes, roughly, where things were a second ago.

Where next

Suggested next: 21. Open loop, closed loop: the idea of feedback — Two rovers set off for the same beacon. One drives straight ahead for three seconds, because that is where the beacon was when someone measured. The other keeps looking at the beacon and steers toward it. Put oil under a wheel and only one of them still arrives.