Skip to the lesson
FixTheRobot Explore Robotics / Robot Foundations / Module 4 / Lesson 3

Robot Foundations · Module 4: Sensing the World

Sensors That Look Inward: Encoders and Inertial Sensors

Some sensors look at the world. Others feel the robot: how far its wheels turned, how fast it is turning, how hard it is pushed.

  • Lesson 3 of 4
  • Beginner
  • No code
  • About 9 minutes

Close your eyes and walk ten steps forward, then turn to your left. You know roughly where you are, and you know it without a single look at the world: you counted steps and felt the turn. Robots do the same with sensors that measure themselves. They are less glamorous than a laser scanner and every mobile robot depends on them, because they work in the dark, in fog and in a corridor with blank walls.

What you will be able to do

  • Explain how a wheel encoder counts ticks and how ticks become distance.
  • Work out how far a robot moved and how much it turned from two encoder counts.
  • Say what a gyroscope and an accelerometer measure.
  • Explain why these sensors drift, and why a small error grows.

The idea

The sensors of the last lesson are exteroceptive: they measure the outside world. The ones in this lesson are proprioceptive (from the Latin for "one's own"): they measure the robot itself. They have this advantage: they need nothing outside the robot, so nothing outside can block them.

Wheel encoders: counting turns

A wheel encoder is a small disc attached to the wheel, with evenly spaced slots around it. A light gate on one side and a detector on the other send a signal every time a slot goes by. The electronics count these signals. One signal is a tick.

An encoder disc and its light gateA circular disc with 24 slots around its edge. A small light gate sits beside the rim and counts each slot as it passes. The disc is attached to a wheel and turns with it.light gate counts slotsthe disc turns with the wheel(24 slots drawn; Pip's counts 360 steps per turn)
An encoder. Each slot passing the gate makes one tick. More slots mean finer measurement.

The number of ticks for one full turn of the wheel is fixed when the encoder is made. Pip's is 360. Combined with the wheel size from the lesson Motors, Wheels and Other Actuators, every tick is a known distance:

One tick is one circumference divided by the number of ticks per turn.

distance per tick = 2 × π × r ÷ ticks per turn

Pip: 2 × π × 0.05 ÷ 360 = 0.87 mm per tick. That is very fine: less than a millimetre.

Distance rolled is the number of ticks times the distance per tick.

distance = ticks × distance per tick

This is the same formula as before, turned into a count. The robot does not need to know the wheel speed. It only needs to read the counter.

Two encoders give distance and turning

With one encoder on each wheel the robot has the two wheel distances. From the lesson How a Two-Wheeled Robot Drives, the average gives the forward distance and the difference gives the turn:

The distance moved is the average of the two wheel distances. The angle turned is their difference divided by the wheel base.

distance = (dR + dL) ÷ 2
angle turned = (dR − dL) ÷ L

The same two formulas as for speeds, applied to distances. The right wheel rolling further than the left means a left turn.

Add these up, one small step after another, and the robot knows how far it has gone and which way it faces, with no outside help. This is called odometry, and it is the subject of module 7.

Inertial sensors: feeling motion

An inertial measurement unit (IMU) is a small chip, found in every phone, with two kinds of sensor in it.

  • A gyroscope measures how fast the robot is turning: the turning speed ω of the earlier lessons. Add up its readings over time (new heading = heading + ω × dt) and you have a heading that does not depend on the wheels, so it is not fooled when a wheel slips.
  • An accelerometer measures how hard the robot is being pushed, including by gravity. At rest it reads gravity, which tells the robot which way is down. It is how a phone knows to rotate its screen.

Why these sensors drift

Both kinds work by adding up small measurements, and so both pass on every small error. Encoders miscount when a wheel slips, because the wheel turned and the robot did not go as far. A gyroscope has a tiny constant error, called a bias: even when standing still it reports a very slow turn. The sum of tiny errors grows without limit. This is drift.

Sensors that add up their own measurements are smooth and fast, and they drift. The longer the robot runs, the less far to trust them.

Drift is the reason outward-looking sensors exist. Encoders say how far the robot thinks it has gone. A wall, a landmark or a map says where it really is. Each one covers the other's weakness, which is the subject of the next lesson and of modules 6 and 7.

Words to know

Proprioceptive sensor
A sensor that measures the robot itself, such as its wheel turns or its own motion. On Pip: encoders and the inertial chip.
Encoder
A sensor on a wheel or motor that counts how far it has turned. On Pip: 360 ticks per wheel turn.
Tick
One count of an encoder: one slot passing the gate. On Pip: 0.87 mm of wheel travel.
Odometry
Working out how far a robot has moved and turned from its own wheel measurements, by adding up small steps. On Pip: its basic way of knowing where it is, built from the encoders.
Gyroscope
A sensor that measures turning speed. On Pip: reports ω in rad/s from the inertial chip.
Accelerometer
A sensor that measures how hard the robot is being pushed, including by gravity. On Pip: tells it which way is down.
Bias and drift
Bias: a small constant error in a sensor. Drift: the growing error that results from adding up readings that contain it. On Pip: a bias of 0.1° per second in the gyroscope drifts the heading by 6° in a minute.

A worked example: from ticks to a turn

Pip starts a short drive. When it stops, the left encoder has counted 1800 ticks and the right encoder 1900.

  1. Distance per tick. 0.3142 m (the circumference) ÷ 360 = 0.873 mm.
  2. Left wheel. 1800 × 0.873 mm = 1.571 m. That is 5 wheel turns.
  3. Right wheel. 1900 × 0.873 mm = 1.658 m, which is 5.28 turns.
  4. Distance moved. (1.658 + 1.571) ÷ 2 = 1.614 m.
  5. Angle turned. (1.658 − 1.571) ÷ 0.3 = 0.291 rad, which is 16.7°, to the left, because the right wheel rolled further.

Pip drove about 1.61 m and turned about 17° to the left, and it found that out without any outside information.

And the drift. The gyroscope has a bias of just 0.1° per second, which is invisible to the eye. After 60 s, adding up the readings gives a heading that is wrong by 0.1 × 60 = 6°. If Pip drove at 0.5 m/s for that minute, it travelled 30 m while the error grew from 0° to 6°. The average error was 3°, so it ends about 30 × sin 3° = 1.57 m to the side of where it believes it is. A bias too small to notice has become a miss of more than a metre.

Try it yourself

Pip's left encoder counts 720 ticks and its right encoder counts 720 ticks. (a) How many turns did each wheel make, and how far did the robot go? (b) What did it do? Now suppose the right encoder counted 540 instead. (c) What is different?

Answer

(a) 720 ÷ 360 = 2 turns each. Distance = 2 × 0.314 = 0.628 m. (b) Equal counts: it drove straight. (c) The right wheel rolled 540 × 0.00087 = 0.471 m and the left 0.628 m. The left rolled further, so the robot turned right: angle = (0.471 − 0.628) ÷ 0.3 = -0.52 rad, which is -30° (negative: a right turn).

Common mistakes

"Encoders measure how far the robot moved."

They measure how far the wheels turned. The two are equal only while the wheels grip. On a slippery floor the wheels turn and the robot hardly moves, and the encoders report a distance that never happened.

"A gyroscope measures heading."

It measures turning speed. Heading is obtained by adding the readings up over time, and any small error in them is added too. That is why a gyroscope's heading drifts.

"If the sensor is accurate enough, the drift goes away."

A better sensor drifts more slowly. It does not stop. Anything that adds up measurements has an error that grows for as long as it runs.

Remember this

  1. Proprioceptive sensors measure the robot itself, and work anywhere because they need nothing outside.
  2. An encoder counts ticks: distance = ticks × 2πr ÷ ticks per turn. Pip: 0.87 mm per tick.
  3. Two encoders give the distance moved (average) and the angle turned (difference ÷ wheel base).
  4. Gyroscopes and encoders drift, because they add up readings that carry small errors.

Check yourself

Three questions. Write an answer in your own words, then reveal the explanation. Nothing you type leaves your browser.

Question 1

A wheel of radius 0.05 m has an encoder with 360 ticks per turn. It counts 1000 ticks. How far did the wheel roll?

Explanation

One tick is 2 × π × 0.05 ÷ 360 = 0.000873 m. 1000 ticks is 0.873 m (about 2.78 turns).

Question 2

A robot's left encoder counts 2000 ticks and its right encoder counts 2000 ticks, yet a tape measure shows the robot moved only 1.5 m while the encoders suggest 1.75 m. What happened?

Explanation

The wheels slipped: they turned as many times as the encoders say, but gripped the floor poorly and the robot travelled less far. The encoders measure the wheels, not the robot. A second source (a scanner measuring distance to a wall, or an inertial sensor) would have shown the disagreement.

Question 3

Why can a gyroscope not simply be trusted for heading all day?

Explanation

The heading comes from adding up its turning-speed readings. Every reading has a tiny error (the bias), and adding them up adds up the errors too, so the heading error keeps growing for as long as the robot runs. After long enough it is large however small the bias was.

What’s next

Every sensor so far has been treated as if its readings were exact except for a constant slip. The last lesson of this module deals with the other kind of error, the random one, and with the fact that no single sensor is enough.

You are ready for the lab

Teach the Robot to Sense

Send a robot through a room with no sensor, then add a LiDAR, a camera and encoders, and choose the right sensor for each job. About 15–20 minutes.

Next lesson

Noise, and Why One Sensor Is Not Enough

Describe noise and bias, reduce noise by averaging, and say why robots combine different sensors.

Where this goes on the site

Nothing below is needed to continue this course. These pages take the topic of this module further, and most assume more mathematics or Python than this course does.