Robotics
How a robot knows where it is with dead reckoning
Dead reckoning is working out where you are from where you started plus every movement since. A robot does it by counting wheel ticks for distance and tracking its heading, then adding each little step, in the direction it's facing, to a running x and y. It needs nothing outside the robot, but small errors add up, so the estimate drifts the further it goes.
Add up every step
Keep three numbers: x, y and the heading. On each step of distance d:
With the heading measured from east, facing east makes cos 1 and sin 0, so the step goes on x. Facing north, it goes on y. Turning changes only the heading.
A simple rover that only drives straight and turns in quarter turns can skip the trigonometry. Number the directions 0 to 3, north, east, south, west, and keep how far each one moves x and y:
int dx[] = {0, 1, 0, -1}; // north, east, south, west
int dy[] = {1, 0, -1, 0};
// each straight tick
x = x + dx[dir];
y = y + dy[dir];
// each quarter turn right
dir = (dir + 1) % 4;
The % 4 wraps west round to north, the way a compass does.
Live experiment
Where does the program think it is?
The rover drives a list of legs a tick at a time: each straight tick adds a step in the direction it faces, each pivot turns it a quarter right. The blue path is where it really goes; the dashed ring is where the program thinks it is. Try the bug that adds every tick to y.
“Where am I?” is lesson 3 of 6 in Waypoint Wharf, Robotics World 3. Free with an account. Lessons unlock in order, so you’ll do the 16 before it first.
How far from home?
Pythagoras gives the straight-line distance back to the start:
Drive 4 north, turn right, drive 3 east, and the robot is at (3, 4): 5 from home, though it drove 7.
Why it drifts
Dead reckoning only knows what it counts. A wheel that slips turns without moving the robot. A wheel slightly smaller than you measured makes every tick a bit too long. A turn that comes out at 88° instead of 90° sends every later step a little off line, and that error grows with every metre. Nothing in the sum ever corrects it.
So robots use dead reckoning for the short term and something outside for the long term. A gyro holds the heading better than wheel ticks. A line on the floor, a wall, a GPS fix, a camera or a beacon resets the estimate whenever one is in sight. Sailors navigated oceans the same way, with a compass, a log line and a clock, and checked their position against the stars when they could. Robot vacuums, warehouse robots and Mars rovers all do dead reckoning between their fixes.
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.
“Where am I?” is lesson 3 of 6 in Waypoint Wharf, Robotics World 3. Free with an account. Lessons unlock in order, so you’ll do the 16 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: x by +1. dx[1] is 1 and dy[1] is 0: facing east, each step moves the rover one along x.
- Why not “Nothing: only turns change position”? A straight tick is travel, so the position must change, along the way the rover faces.
- Why not “x by −1”? x goes down when the rover faces west. Facing east it goes up.
- Why not “y by +1”? Facing east, the step goes along x. y changes when the rover faces north or south.
Show the answer
Answer: The rover turns, it doesn't travel: only dir changes. We treat each turn as happening on the spot. A real pivot about one wheel shifts the centre a little too, and careful robots add that in.
- Why not “They're too small to matter”? Pivot ticks aren't small: each one turns the rover several degrees. They change the heading, not the position.
- Why not “The encoder switches off during a pivot”? The encoder still counts during a pivot; the program uses those ticks to change the heading instead.
- Why not “x and y are only worked out at the end”? x and y are updated on every straight tick as it happens, not saved up for the end.
Show the answer
Answer: Drifts further and further off. Adding up a small error every sample makes a big error eventually. It's why a gyro's angle drifts, which you'll meet in Dither Delta.
- Why not “Stays right”? A total keeps every small error it's ever added, so it can't stay right.
- Why not “Falls to zero”? Nothing pulls the total back toward zero; it keeps growing in the error's direction.
- Why not “Cancels the error”? A constant error points the same way every sample, so nothing cancels it.
“Where am I?” is lesson 3 of 6 in Waypoint Wharf, Robotics World 3. Free with an account. Lessons unlock in order, so you’ll do the 16 before it first.
Questions people ask
What is dead reckoning?
Working out where you are from where you started plus every movement since: how far you went and in which direction. Sailors did it with a compass, a log line and a clock; robots do it with wheel encoders and a gyro.
What is the difference between odometry and dead reckoning?
Odometry is measuring distance travelled from the wheels' rotation. Dead reckoning is the bigger idea of adding up distance and heading to track position. Wheel odometry is the usual way a ground robot does dead reckoning.
Why does dead reckoning drift?
Every small error, from wheel slip, a slightly wrong wheel size or a heading off by a degree, is added in and never taken out. The error grows the further the robot goes, so real robots correct it with landmarks, GPS or a camera.
Related topics
- How a wheel encoder measures distanceCount the notches as the wheel turns, and you know how far the robot went.
- How to make a two-wheeled robot drive straightEqual speeds don't mean straight. Measure each wheel and correct the difference.
- 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.
- How a complementary filter blends a gyro and accelerometerTrust the gyro for quick moves and the accelerometer for the long run.