Part 4 · How robots think

30Dodging things on the fly

No map this time, and no plan. A box slides along the rover's line to the beacon, and the rover gets there by one rule run fifty times a second: everything near pushes the steering away, the beacon pulls it back.

This assumes: Measuring distance: sonar and lidar, Wheels: how rovers turn with no steering wheel, Planning a path around obstacles. About 9 minutes of reading.

Walk across a busy station concourse. You do not plan a route round each person before you set off. You look, a stranger stepping into your way gets a small sidestep, and you keep going toward the platform without thinking about any of it. The last module was the other thing: a map, a goal, and a whole route worked out before a wheel turned. This module is what you did on the concourse. The rover has no map at all, the thing in its way is moving, and it gets to the beacon by looking, fifty times a second, and doing one small thing about what it sees.

The same rover, no map

The rover, the pen and the beacon are the ones from the first module, and the five rangefinders on its nose are the ones the distance sensors module drew as beams. What is new is a brown box that slides back and forth along the line between the rover and the beacon, at fifteen centimeters a second, turning round at each end without a pause; and a brain that has to get past it. The last module's planner could not do this: its map was a snapshot, and a box that moves is on no snapshot.

Reactive control is steering by a rule that turns this instant's sensor readings straight into a command for the motors, and does it every trip round the sense-think-act loop, with no map, no plan and nothing remembered from the trip before. Whatever the beams report right now decides what the wheels are told right now, and a moment later the loop goes round again and asks afresh.

Pushes and pulls

Here is the rover's rule in full: two parts, added up.

The beacon pulls. The rover is told which way the beacon lies, as it was in the first module, and the further off its nose the beacon is, the harder the steering is turned toward it.

Everything near pushes. Each of the three forward beams, the one straight ahead and the two thirty degrees to either side, watches for anything within eighty centimeters. When it sees something, it pushes the steering away from its own side, and the nearer the thing, the harder the push: nothing at eighty centimeters, a hard turn at a hand's width. The ahead beam has no side of its own, so it pushes toward whichever side the other beams say has more room. The two wide beams, seventy-five degrees out, push only from very close, and mostly do something different: they are a brake on one side of the steering. When the sum of everything else says turn toward a side, and the wide beam on that side has something within thirty-five centimeters, the turn that way is cut down, to nothing at a hand's width. A thing on the flank is not in the way; the brake keeps the rover from swinging into it.

Add the pull and the pushes together and the sum is one number, how hard to turn and which way, which becomes two wheel speeds as in the wheels module. Two smaller rules ride along. The nearest thing in front sets the speed, full speed with nothing within seventy centimeters, down to a third of it with something at the nose. And the nearer the nearest thing, the less the beacon's pull counts, so that getting clear comes first and getting there second.

Two more things complete the rule. The beacon's post is itself a thing the beams see, and a rule that pushed away from everything near would flinch from its own goal and never arrive; so a reading that lines up with the beacon, at the beacon's distance, is taken to be the post and ignored. And when the beacon is within thirty centimeters the rover stops: not a push or a pull, but the end of the job.

Engineers call this arrangement a potential field: a reactive rule written as pushes and pulls added up. Picture the floor of the pen as a landscape. The beacon is the bottom of a valley, every wall and box is a hill, and the rover is a ball rolling downhill. The slope under the ball is the sum, pointing the way the pushes and the pull add up to; the ball rolls that way, and the rover turns that way. A box that moves is a hill that moves, and the ball does not need to know that: it only ever feels the slope where it is.

Why it works on a thing that moves

Nothing in the rule mentions the box. It knows only that a beam reports thirty centimeters on the right this instant; next instant, whatever the box has done, the beam reports again and the rule runs again. A box that has moved since the last look is no different, to this brain, from a box that was always there. That is the whole reason a rule like this is on nearly every robot: the world changes faster than any map can be redrawn, and the sensors are always up to date.

The same fact is its weakness, and you will see it. The brain remembers nothing, so the moment no beam sees the box, the box is gone: the pushes stop, the pull wins, and the rover turns back toward the beacon, which is toward the box. The wide beam on that side finds the box again, pushes, and the rover swings off again. Dodging on the fly is a series of glances, not a decision made once.

The box

The box below is the rover in its pen, seen from the side so that the line runs left to right: the rover at the left, facing right, the beacon at the right, and the brown box between them, on the line, forty-four centimeters from the rover's nose. The five beams are drawn as in the distance sensors module, with their five numbers under the box, and the rover's path is drawn on the floor in gray.

Two switches sit under the picture. "Turn away from what is near" is the pushes: untick it and the rule is the beacon's pull alone, with the speed rule still slowing the rover for anything ahead. "The box moves" is the box: untick it and the box stands still wherever it was when you did. Both start on. The line under the switches says what the rule is doing this instant: what is nearest and where, which way the pushes and the pull are turning the steering, the speed, and what the wheels are told. It opens paused; press Play to set it going, Reset to put everything back.

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.

What you saw

With the pushes on, the rover never knew the box was coming and never needed to. The ahead beam reported it, the steering was pushed left, and the rover was clear before the box arrived. Then it forgot the box, turned back, was reminded by its wide beam, and held off until the box was behind it: four glances and four small answers, which from the outside looked like one smooth swerve. The status line was that loop in words.

With the pushes off, the same rover with the same sensors drove into the box, because the readings went nowhere; slowing down is not the same as getting out of the way. With the box standing still the rule did the same, in the same time, because from inside the loop the two were the same problem.

Where the rule fails

A rule with no memory and no map has three ways to go wrong. This pen shows only the third: it has no pocket, and the box only ever runs along the line.

It can be trapped. Put this rule in a pocket, three walls and an open side, with the beacon beyond the far wall. The far wall pushes it back, the side walls push it inward, the beacon pulls it forward, and the pushes and the pull can cancel. The rover turns one way, is pushed back the other, and rocks there, or noses into the far wall at its crawling speed, for ever. On the landscape, it is a ball in a dip that is not the valley: every way out means going away from the beacon first, and a rule that only feels the slope where it stands will never take it. The last module's planner exists for exactly this.

It can be herded. The rule reacts to where a thing is, not where it is going. A box crossing the rover's path from the side pushes the rover away from itself, which is toward where the box is heading; the box arrives there too, and pushes again. With the box in this pen set to cross the line instead of running along it, at the rover's own speed, it herded the rover into the far wall and pinned it there. A person on the concourse notices which way the other person is walking; a rule that does the same needs a little memory, enough to tell a thing's speed from two looks, and is then no longer quite reactive.

And it can miss. The beams are five thin lines with gaps between them, as the distance sensors module showed, and in the first try the box vanished from all five for a moment while it was still beside the rover. A spinning lidar has hundreds of beams and smaller gaps, but still gaps, and no memory of what was in them unless something is built to keep one.

What it is for

Nearly every robot that moves about carries a rule like this. A robot vacuum's bumper and cliff sensor run one: bump, turn; drop ahead, back off. A delivery robot on a sidewalk plans its route on a map and then, along the way, stops or steps aside for whoever is in front of it, because no map of a city has the pedestrians on it. Engineers speak of the planner and the rule as two layers of the robot's thinking, with different jobs. The planner sees the whole route and is slow and sometimes out of date; the reactive layer sees the next meter and is fast and always current. A well-built robot lets the planner choose where to go and the reactive layer decide, fifty times a second, how to get through the next meter, and plans again whenever the rule has carried it somewhere the plan did not expect.

What the simulation leaves out

The box slides on a rail at a steady speed and never does anything else; a person walking changes speed and direction, which is the hard part. The beams are exact and instant, and there are only five. The rover is told where the beacon is, as since the first module, which is what lets the rule tell the post from a box; a real robot must recognize its goal by other means. And the rule here keeps nothing at all between one trip round the loop and the next. Real reactive layers usually keep a few seconds of what the sensors reported, drawn onto a small map that travels with the robot, so that a thing which has slipped between the beams is not forgotten the instant it does.

Watch

Obstacle Avoiding Robot Car Using An Arduino — Michael Klements (3:34 of it, starting at 6:50)

Why this video
A real car, built on a kitchen table, running the rule on this page in its plainest form. It has one distance sensor on its nose, on a little swivel, and the person who built it tells you exactly what its brain does with the one number that sensor returns: nothing near, drive on; something near, stop, look left, look right, and turn toward whichever side has more room; no room either way, turn round. Then he puts it on the floor, and you watch a machine with no map and no plan find its way round a living room, one look at a time. The rover in the box does the same thing without stopping to look, with five beams instead of one on a swivel; this shows the idea in a form you could build yourself for the price of a few sensors.
What to watch for
From 6:50 he walks through the loop over the code: measure ahead, and if nothing is within the stopping distance, drive; at 7:15 the other case, stop and call a function that decides which way to turn. At 8:28 that decision: the sensor swivels left, then right, takes a distance each way, and the car turns toward the side with more space, or turns round if both sides are blocked. From 8:48 the car is on the floor. Watch it drive at a wall or a chair leg, stop short, swing its sensor both ways, and pick a side; at 9:34 he says what happens in a tight space, it turns round and comes back out the way it went in, which is the escape the rover in the box does not have. The captions hear "servo" as "surfer" and the car as "Cole".

Words introduced

reactive control
Steering a robot by a rule that turns this instant's sensor readings straight into a command for the motors, run every trip round the sense-think-act loop, with no map, no plan and nothing remembered from the trip before.
potential field
A way of writing a reactive rule as pushes and pulls added up. The goal pulls the robot toward it, everything the sensors see near pushes it away, harder the nearer, and the robot moves the way the sum points, as a ball rolls downhill on a landscape with the goal at the bottom and a hill on every obstacle.

Where next

Suggested next: 31. Planning for arms: moving without hitting yourself — An arm knows the angles that put its hand on a star, but a wall stands between, and turning straight to them swings the arm through it. The robot maps the wall in its own joint angles, searches the map, and lifts its hand over.