Robotics
How a robot drives itself to a waypoint
Waypoint navigation gives a robot a target point, an x and a y, instead of a list of turns. On every pass of the loop it compares where it thinks it is, from dead reckoning, with where it's going, picks the direction that closes the gap, turns to face it if needed, and drives. A list of waypoints makes a mission. No route is stored anywhere: it falls out of the answers.
The rule
On a grid where the rover faces north, east, south or west:
- Arrived? If x and y both match the waypoint, move on to the next one, or stop at the last.
- Level up y first: want north if y is below the waypoint's, south if above.
- Then x: east if x is below, west if above.
- Face, then drive: if it isn't facing that way, pivot; otherwise drive, stepping x and y on every encoder tick.
From (0, 0) to (2, 5): five ticks north, a quarter turn right, two ticks east.
In code
if (x == wx[wp] && y == wy[wp]) {
if (wp < last) wp = wp + 1; // on to the next waypoint
else { stopWheels(); digitalWrite(13, HIGH); return; } // ARRIVED
}
int want;
if (y < wy[wp]) want = 0; // north
else if (y > wy[wp]) want = 2; // south
else if (x < wx[wp]) want = 1; // east
else want = 3; // west
Then, if dir isn't want, pivot right and count ticks: every 10 is a quarter turn, dir = (dir + 1) % 4. Otherwise drive, and on each tick add dx[dir] and dy[dir] to x and y.
Live experiment
Send the rover to a waypoint
The rover decides where to go on every encoder tick: north or south until level, then east or west, pivoting a quarter turn of 10 ticks when it needs to face a new way. Send it to the flag and the dock, then behind it, and try counting a quarter turn as 5 ticks.
“Go to a waypoint” is lesson 4 of 6 in Waypoint Wharf, Robotics World 3. Free with an account. Lessons unlock in order, so you’ll do the 17 before it first.
It only knows what it counts
The rover's position is a sum of ticks. Count a quarter turn as 5 ticks instead of 10 and each turn is only about 45°: the rover drives off at an angle, its sums still say it reached the waypoint, and it lights ARRIVED somewhere else. Wheel slip does the same, more slowly. Real robots correct dead reckoning with a gyro, beacons or GPS.
Beyond the grid
This rule is greedy: it always heads straight for the goal, so a wall in the way would stop it. Path planners such as A* search a map for the best way round obstacles, then hand the robot that route as a list of waypoints. Free-roaming robots steer toward the waypoint's bearing, atan2(dy, dx), instead of four grid directions. Drone flight plans, tractors crossing fields and pavement delivery robots all run on waypoint lists.
Free lesson
Now build it yourself
Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.
“Go to a waypoint” is lesson 4 of 6 in Waypoint Wharf, Robotics World 3. Free with an account. Lessons unlock in order, so you’ll do the 17 before it first.
Or start from the very first lesson, no account neededPractice questions
Three quick questions from the lessons. Pick an answer to check it.
Show the answer
Answer: North: y is below the waypoint's. It levels up north or south first. y is 0 and the waypoint's is 5, so north.
- Why not “West”? West makes x smaller, but x needs to grow from 0 to 2, and y comes first anyway.
- Why not “East: x is below the waypoint's”? x is below the waypoint's too, but the rule levels up y first. East comes once y matches.
- Why not “South”? South makes y smaller, and the waypoint's y, 5, is above the rover's 0.
Show the answer
Answer: West: y matches, and x is too big. y already matches. x is 2 and the waypoint's is −1, so it must go west, the way x gets smaller.
- Why not “Nowhere: it has arrived”? Arriving needs x and y both to match. y does, but x is 2, not −1.
- Why not “East”? East makes x bigger, and x has to fall from 2 to −1.
- Why not “North”? y already matches at 5, so there's nothing to gain going north.
Show the answer
Answer: y > wy, so want is south. The y tests come first. 0 is more than −3, so it wants south. West comes later, once y matches.
- Why not “None: it has arrived”? Arriving needs both to match, and the rover is 3 away in y and 2 in x.
- Why not “x > wx, so want is west”? x is more than −2, so west will be wanted, but only once y matches. The y tests come first.
- Why not “y < wy, so want is north”? 0 is more than −3, not less. y less than wy would mean the waypoint was above the rover.
“Go to a waypoint” is lesson 4 of 6 in Waypoint Wharf, Robotics World 3. Free with an account. Lessons unlock in order, so you’ll do the 17 before it first.
Questions people ask
What is a waypoint?
A point a robot or vehicle should pass through or stop at, given as coordinates: x and y on a floor, or latitude and longitude outdoors. A route is a list of waypoints, and the controller drives to each in turn.
How does a robot know it has reached a waypoint?
It compares its estimated position with the waypoint. On a grid that's an exact match; in the real world it's when the distance falls under a small radius, because the estimate is never perfect.
How do I make a robot navigate round obstacles?
Plan before you drive: a search such as A* on a map finds a path round the obstacles and turns it into waypoints. Add a distance sensor so the robot stops or detours if something new is in the way.
Related topics
- How a robot knows where it is with dead reckoningAdd up every step in the direction you face, and you know where you are, roughly.
- How to program a robot as a state machineOne state at a time, each with its own job and its own ways out.
- How to turn a robot exactly 90° with a gyroA gyro gives degrees per second. Add up rate × time and you know how far you've turned.
- How a two-wheeled robot steers and sets its speedHow a two-wheeled robot turns with no steering wheel, and how PWM sets each motor's speed.