17Measuring distance: sonar and lidar
Five beams on the rover's nose, each reporting one number, the distance to the first thing in its way. Watch a box walk from beam to beam as the rover turns past it, and find the thing twenty centimeters in front of the nose that no beam can see.
This assumes: What makes something a robot?. About 9 minutes of reading.
Stand at the edge of a quarry and shout. A moment later the far wall shouts back. Count the gap: a whole second, say. Sound crosses about a third of a kilometer in a second, so your shout went a third of a kilometer there and back, and the wall is about a hundred and seventy meters away: a distance measured with nothing but a noise and a clock. Nearly every robot that moves about a room measures distance the same way, many times a second, and this module is about what that trick reports, and what it misses.
The echo and the clock
A rangefinder is a sensor that sends something out, times how long it takes to come back, and reports one number: the distance along its line to the first thing that sent the signal back. The wait is the time of flight, the time the click or flash takes to fly to the object and home again. The clock stops at the first echo to arrive, which comes from the nearest thing on the line, so anything behind it is never heard. The signal went there and back, so the object is half of that far off; a chip on the sensor does the halving and hands over meters.
The cheapest kind sends a click. Sonar is a rangefinder that sends a short click of sound too high to hear and listens for the echo. Sound travels about 340 meters a second, so an echo from a wall a meter away is home in six thousandths of a second, and that chip counts in millionths, so timing it is easy. The whole sensor is two small round cans on a board the size of a stamp, one to click and one to listen, for about a pound.
Lidar does the same with a flash of laser light instead of a click. Light is nearly a million times faster than sound: a flash to a wall a meter away is home in seven billionths of a second, too short for an ordinary chip to count, so a lidar's electronics are built with some care to catch a gap that small. What the trouble buys is the beam. A wave spreads out when it leaves an opening not much bigger than the wave itself. A sonar click is made of sound waves about a centimeter long leaving a can about a centimeter across, so it fans out into a cone; a laser's light waves are a thousandth of a millimeter long leaving a lens thousands of times wider, so the flash barely spreads at all and stays a pencil-thin line, which is why a laser pointer makes a dot on the far wall. So a lidar knows exactly which direction its number came from. Bolt one to a motor and spin it, and it measures a few hundred directions on every turn: those distances, drawn as dots round the sensor, are a floor plan of the room. That is the turret on top of a robot vacuum. The rover below carries five of its tiny cousins: laser rangefinders on a chip the size of a grain of rice, with no motor and a reach of a meter or two.
What the number is, and what it is not
Whichever way the signal is made, what reaches the robot's computer is one number, and it is worth being exact about what it says: the distance along one line, from the sensor to the first thing that sent the signal back. It does not say what the thing was: a wall, a box, a chair leg and a person's shin all come back as 0.73. It says nothing about anything beside the line: the world to either side of a beam a few millimeters wide does not exist, as far as that sensor is concerned. And it has a reach. The sensor listens for a fixed time, and if nothing has come back by then it reports that it saw nothing. Sonar gives up at about four meters, the little laser chips at a meter or two, a spinning lidar at tens of meters. "Nothing came back" is a reading in its own right: the way along that line is clear for at least that far.
The box
The box below holds the rover from the first module, in the same pen, with the same beacon, the brown box in its way, and the same brain: head for the beacon, and turn away from anything close ahead. What is new is that you can see what the brain sees. Five rangefinders sit on the rover's nose, one looking straight ahead, one thirty degrees to each side, and one seventy-five degrees to each side, and each has a reach of one and a half meters. Each is drawn as a blue line from the nose to the first thing in its way, with an orange dot where the signal came back. A sensor that found nothing within its reach is drawn as a gray line the full meter and a half, with no dot. Under the picture is one bar for each sensor, with its number in meters or the words "nothing in range"; a full bar is the sensor's whole reach. Under those, a chart of the ahead sensor's number over the last six seconds, and a slider marked Noise, to be left at none until asked.
The view is from the side of the pen: the rover starts at the bottom left, facing right, and the beacon stands at the top right. The rover opens paused; Play sets it going, Reset puts it back at the start, and Slow runs it at a fraction of its speed.
This is easier on a larger screen, but it works here too.
What you saw
Five numbers and no picture. Before Play, you could see a box, a post and four walls; the sensors reported 0.82 on the left, 1.08 and 0.53 on the right, and nothing straight ahead, where the far wall stood in plain view twenty centimeters beyond the sensor's reach. The box was a number. The post was not there. That is what a rangefinder gives a robot.
The box walked from beam to beam because the beams do not move. Each sensor looks along its own line, fixed to the nose, and reports the box only while the box is on that line. As the rover turned right, the box slid off the ahead line, onto the left line, onto the far left line, and off the end. Nothing about the box changed; the rover turned under its own sensors. A robot that wants to know where a thing is has to pair each number with which way its line was pointing at the time; a spinning lidar is that idea done properly, one beam, a few hundred directions a turn, the direction remembered with every distance.
The beacon was invisible for the same reason. The post is three centimeters wide, and it stood in the gap between two beams: thirty degrees apart, those two lines are already ten centimeters apart twenty centimeters from the nose, and a post narrower than the gap fits between them without touching either. A spinning lidar closes the gaps to about a degree, but never to nothing: at ten meters a gap of one degree is seventeen centimeters wide, and a table leg hides in it easily. The rover found the beacon only because this simulation tells it where the beacon is, as the first module said.
You may also have caught two or three bars going empty at once during the sharpest turns, around five seconds in. The rover is a tall box on two driven wheels and a caster, a small free-swiveling third wheel, and its weight sits high; in a pivot one wheel is thrown into reverse while the other drives on, and that sudden braking on one side throws the body forward the way a stopping bus throws a standing passenger, by ten degrees and more. A beam that tips up ten degrees clears the twenty-centimeter wall a meter away and finds nothing. Real robots do this too: a rangefinder on a body that pitches over a bump looks at the ceiling for a moment.
The wobble
Then the noise. The rover and the wall stood still and the numbers did not: a few centimeters either side of the truth at ± 5 cm, a hand's width at ± 20. A real rangefinder does this on its own; the slider chooses how much. A sonar's echo does not arrive as a clean click but as a smear a few millionths of a second long, and where in the smear the chip decides the echo "arrived" varies from click to click; the speed of sound drifts with the temperature of the air, so the same wall reads a little differently on a cold morning and a warm afternoon. A lidar's return is faint, barely brighter than the dark around it, and the instant the chip picks as its arrival shifts a little from flash to flash. A good lidar wobbles by a centimeter, a cheap sonar by several, and on the same wall the number is never quite the same twice. The 0.00 at ± 20 cm is the wobble clipped at zero. What a robot does about that is the last module of this part; here, the truth and the report are two different things, and the sensor hands over the second.
Clicks against flashes
Sonar and lidar answer the same question and fail in opposite ways. A sonar's cone notices a table leg that a laser line would miss, but cannot say where in the cone the echo came from, only that something in it is at that distance. Its click is swallowed by soft things, so a curtain can come back as nothing, and a smooth wall met at a slant bounces the click away instead of back and reads as nothing too. It is slow, because each click has to come home before the next goes out. In its favor, it is untroubled by dust and fog, which scatter a flash of light, and it sees glass.
A lidar's line is exact, its flash comes back from cloth as happily as from brick, it reads thousands of times a second, and the number is good to a centimeter. But glass lets its flash straight through and a mirror sends it elsewhere, so a patio door reads as the garden beyond it; matt black surfaces send back too little light to time; and bright sunlight can drown the return. Robots that have to be sure carry both.
What the simulation leaves out
The five beams here are perfect lines with no width, they report instantly, and they are exact until you add the wobble yourself; a real sonar's cone would have caught the post. Every surface here sends the signal back whatever the angle it is met at, so a wall can never vanish by being slanted. And the pen has walls but no ceiling, so a beam that tips up in a turn sees nothing, where in a real room it would meet the ceiling and report a distance that was true and useless. The rest is real: one line, one number, and no idea what it hit.
Watch
How To Use Ultrasonic Sensors with Arduino! + Project Idea! — Robonyx (3:24 of it, starting at 0:14)
- Why this video
- A real ultrasonic sensor, the cheap four-pin kind found on the nose of most hobby rovers, in someone's hands: the transmitter that clicks and the receiver that listens, the echo timed and halved, and then the sensor bolted to a small servo that swings it back and forth so that one beam sweeps a room and draws it as a radar picture. That sweep is the whole idea of a spinning laser scanner, done with a sensor that costs a pound.
- What to watch for
- At 0:17, the two round cans on the sensor: one sends the click, the other hears the echo. At 0:36, the timing pulse, the only thing the sensor actually hands over, and at 0:59 the distance halved because the sound went there and back. From 1:07 to 2:21 he wires it to an Arduino, which you can skip. At 2:39 the sonar scanner: the sensor on a servo sweeping back and forth, and at 3:30 the radar picture it draws, with each sweep painting one dot per direction, which is exactly what a spinning rangefinder does a few hundred times a turn.
Words introduced
- rangefinder
- A sensor that sends something out, times how long it takes to come back, and reports one number, the distance along its line to the first thing that sent the signal back, or that nothing came back within its reach.
- time of flight
- How long a rangefinder's click or flash takes to fly to the object and back. Halve it, multiply by the speed of sound or light, and that is the distance.
- sonar
- A rangefinder that sends a click of sound too high to hear and times the echo. Cheap and slow, and its click spreads into a cone, so it sees a wide patch but cannot say where in the patch the echo came from.
- lidar
- A rangefinder that sends a flash of laser light and times its return. The flash is a pencil-thin line, so the number belongs to one exact direction; spun round on a motor, one lidar draws a floor plan of a room as dots.
Where next
Suggested next: 18. Cameras: what a robot actually sees
