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

Robot Foundations · Module 3: Making a Robot Move

Feedback: Measure, Compare, Correct

Measure where you are, compare with where you want to be, and correct by an amount that matches the gap. Then do it again.

  • Lesson 4 of 4
  • Beginner
  • No code
  • About 10 minutes

The last lesson ended with Pip well off course after a straight drive, and no fixed set of commands could prevent it. The cure is almost embarrassingly simple, and you use it every time you fill a glass of water: you watch the level, and you slow the pour as it nears the top. You never calculate in advance how long to pour. This lesson turns that habit into three lines of arithmetic, which are the lines you will write in the first lab.

What you will be able to do

  • Compute an error as the difference between a goal and a measurement.
  • Compute a proportional command: gain × error.
  • Explain why feedback still works when the robot does not behave as the formulas say.
  • Describe what happens when the gain is too small or too large.

The idea

Feedback means using the result of your actions to choose the next action. It closes the loop that the last lesson left open.

The robot loopA loop of four blocks. Sense sends readings to Think. Think sends commands to Act. Act causes motion in the World. The World, now changed, is sensed again. SenseMeasureThinkCompare and correctActDo it WorldWhat really happened measured correction motion what changed
Closed loop: every pass measures what the last command really did.

Each pass of the loop does three things.

  1. Measure. Find out where the robot is now.
  2. Compare. Subtract the measurement from the goal. The result is the error: how much is still wrong, and in which direction.
  3. Correct. Send a command that reduces the error.

The error is what you want minus what you have.

error = goal − measurement

An error of zero means you have arrived. Its sign tells you which way to go.

How big should the correction be?

The simplest sensible answer: in proportion to the error. Far from the goal, act strongly. Close to it, act gently. At the goal, do nothing.

The command is the error multiplied by a number you choose, called the gain.

command = gain × error

The gain is often written K. This rule is called proportional control, and the controller a P controller.

Take the simplest case. Pip is on a line, 2 m short of a target, and can measure the distance still to go. That distance is the error. Use the rule speed = gain × error with a gain of 1:

Pip driving to a target with speed = 1 × error and a time step of 0.5 s
PassTime (s)Error (m)Speed command (m/s)Moved this step (m)
00221
10.5110.5
210.50.50.25
31.50.250.250.125
420.1250.1250.063
52.50.063

The error halves on every pass. Pip rushes when far away and creeps in at the end, and the rule that produced this never mentions how far the target was. Nobody planned the slowdown. It falls out of "command = gain × error".

Why it survives a bad robot

Now make the wheels slip so badly that Pip covers only 70% of the distance it should on every step. An open-loop plan would stop 0.6 m short and never know. The feedback rule does not care: after a step that fell short, the measured error is larger than expected, so the next command is larger too.

Error against time for three casesA plot of the distance still to go against time. With a gain of 1 the error falls smoothly from 2 metres toward zero. With a gain of 1 and wheels that slip by 30 percent it falls a little more slowly but still reaches zero. With a gain of 3.4 the error jumps past zero to the other side and back again several times, shrinking only slowly.00.511.522.533.54-1.5-1-0.500.511.52time (s)error: distance still to go (m) gain 1, wheels slipping 30%gain 1gain 3.4: overshoots back and forth
Blue: gain 1. Green: gain 1 with wheels that lose 30% of every move. It is slower and still arrives. Orange: a gain that is too large.

Feedback does not need the robot to behave as predicted. It needs only that a correction in the right direction makes the error smaller.

Choosing the gain

The gain sets how hard the robot reacts.

  • Too small: the robot is sluggish and takes a long time to arrive.
  • About right: it arrives quickly and settles.
  • Too large: each correction is bigger than the error it was meant to remove. The robot shoots past the goal, turns around, shoots past again. This is overshoot and oscillation, the orange line in the figure. Larger still and each swing is bigger than the last.

There is no universal right value. It depends on the robot, the time step and how quickly the motors respond, which is why tuning gains is a skill of its own. Start small and increase until the response is quick but does not overshoot.

Two errors, two corrections

To drive to a point on a floor, Pip has two things to get right and two commands to do it with:

The two errors of a robot driving to a point and the command that corrects each
What is wrongErrorCommand that fixes it
It is not at the targetdistance to the targetforward speed: v = gain × distance
It is not pointing at the targetheading error: the direction to the target minus its own headingturning speed: ω = gain × heading error

One more practical line is needed. The motors have a top speed, so the command is cut off there: far away the rule would ask for 2 m/s and Pip can do 0.5. Limiting a command like this is called saturation, and every real controller includes it.

Those are the lines you will write in the lab: compute the distance, compute the heading error, set v and ω from them, limit v. Around ten lines of Python make a robot drive to any point from any starting pose.

Words to know

Feedback
Using a measurement of the result to decide the next action. On Pip: it checks its distance to the target on every pass of the loop.
Goal (setpoint)
The value you want a quantity to have. On Pip: distance to the target: 0 m.
Error
Goal minus measurement. On Pip: 2 m at the start.
Gain (K)
The number that turns an error into a command. Larger means a stronger reaction. On Pip: speed = 1 × error.
Proportional control
A command that is the gain times the error. On Pip: v = K × distance and ω = K × heading error.
Overshoot
Going past the goal before coming back. On Pip: with a gain of 3.4 it runs past the target.
Oscillation
Swinging back and forth around the goal. On Pip: the orange line in the figure.
Saturation
A command limited to what the actuator can deliver. On Pip: v is never more than 0.5 m/s.

A worked example: one pass of the loop on a floor

Pip is at the pose (1, 1, 0°). The target is at (3, 3). Use a gain of 1 for the forward speed and 2 for the turning speed.

  1. Measure and compare: distance. Differences: 2 m in x and 2 m in y. Distance = √(4 + 4) = 2.83 m.
  2. Measure and compare: heading. The target is equally far in x and in y, so the direction to it is 45° (programs compute this direction with a function called atan2). Pip faces 0°. Heading error = 45° − 0° = 45°, which is 0.785 rad.
  3. Correct: turning. ω = 2 × 0.785 = 1.57 rad/s. Positive: turn left, toward the target.
  4. Correct: forward. v = 1 × 2.83 = 2.83 m/s. That is above the limit, so v = 0.5 m/s.

Pip sets off at full speed, curving left. One time step later it measures again: the distance is a little smaller and the heading error is smaller, so both commands shrink. Nothing else is needed. If someone nudges Pip half way, the next measurement shows a new error and the same four steps deal with it.

Compare the capped and uncapped runs on the line: with the 0.5 m/s limit, the error after four passes is 1 m, against 0.125 m without it. The limit makes the start slower. It does not change where the robot ends up.

Try it yourself

A robot is on a line, 4 m from its target. Use speed = gain × error with a time step of 1 s, and no speed limit. (a) With a gain of 0.5, write the error after each of the first three steps. (b) Repeat with a gain of 1.5. What is different? (c) What would a gain of 2 do?

Answer

(a) Each step removes 0.5 × error: 4 → 2 → 1 → 0.5. It approaches smoothly. (b) Each step removes 1.5 × error, which is more than the whole error: 4 → −2 → 1 → −0.5. The robot overshoots to the far side each time, by half as much. It oscillates and still settles. (c) Each step removes twice the error: 4 → −4 → 4 → −4. It swings from side to side for ever and never arrives. Any gain above 2 makes each swing larger than the last.

Common mistakes

"error = measurement − goal."

Either sign can be made to work, but mixing them is a classic bug: the robot then drives away from the target, faster the further it gets. Use goal − measurement, and test that a positive error produces a command toward the goal.

"A bigger gain is always better, because it gets there faster."

Up to a point. Beyond it the robot overshoots, then oscillates, then becomes unstable. Real motors also lag, which lowers the gain at which this starts.

"Feedback makes the robot accurate."

Feedback makes the robot agree with its measurement. If the measurement is wrong, the robot goes precisely to the wrong place. How good the measurement is, and where it comes from, is the subject of module 4.

Remember this

  1. Feedback is a loop of measure, compare, correct.
  2. error = goal − measurement, and a proportional command is gain × error.
  3. Feedback tolerates slip, unequal wheels and nudges, because it reacts to what happened, not to what was expected.
  4. Too small a gain is sluggish. Too large a gain overshoots and oscillates.

Check yourself

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

Question 1

A robot should stop 0.5 m from a wall. Its sensor reads 2.0 m. With the rule speed = 0.8 × error, where error = measured distance − 0.5, what speed does it command? What happens to the speed as it approaches?

Explanation

Error = 2.0 − 0.5 = 1.5 m. Speed = 0.8 × 1.5 = 1.2 m/s. (Here the error is written so that a positive value means "too far away, drive forward".) As the robot approaches, the reading falls toward 0.5, the error falls toward 0 and so does the speed: it slows smoothly and stops at 0.5 m.

Question 2

Why does a proportional controller still reach the target when the wheels slip, while an open-loop plan does not?

Explanation

The open-loop plan computes its commands once, from the assumption that the wheels grip. When they slip it falls short and has no way of knowing. The controller measures the remaining error on every pass. A step that fell short leaves a larger error, which produces a larger command on the next pass, and this repeats until the measured error is zero.

Question 3

A robot drives toward a target, passes it, comes back, passes it again, and takes a long time to settle. What is the likely cause and the first thing to try?

Explanation

The gain is too large: each correction is bigger than the error it should remove, so the robot overshoots. Reduce the gain. If the response then becomes too slow, the usual next step is to add a term that reacts to how fast the error is changing, which is what the PID labs on this site teach.

What’s next

You have everything the first lab needs: pose, v and ω, error and a proportional correction. In the lab the robot is told its own pose exactly. Module 4 asks where that knowledge could come from on a real robot.

You are ready for the lab

Make Your First Robot Move

Drive a robot by hand, read its state, compute the error to a target and write the loop that drives it there. About 25–35 minutes.

Next lesson

What a Sensor Really Measures

Separate what is true from what a sensor reports, and describe a sensor by its range, resolution, rate and field of view.

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.