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.
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.
Each pass of the loop does three things.
- Measure. Find out where the robot is now.
- Compare. Subtract the measurement from the goal. The result is the error: how much is still wrong, and in which direction.
- 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:
| Pass | Time (s) | Error (m) | Speed command (m/s) | Moved this step (m) |
|---|---|---|---|---|
| 0 | 0 | 2 | 2 | 1 |
| 1 | 0.5 | 1 | 1 | 0.5 |
| 2 | 1 | 0.5 | 0.5 | 0.25 |
| 3 | 1.5 | 0.25 | 0.25 | 0.125 |
| 4 | 2 | 0.125 | 0.125 | 0.063 |
| 5 | 2.5 | 0.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.
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:
| What is wrong | Error | Command that fixes it |
|---|---|---|
| It is not at the target | distance to the target | forward speed: v = gain × distance |
| It is not pointing at the target | heading error: the direction to the target minus its own heading | turning 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.
- Measure and compare: distance. Differences: 2 m in x and 2 m in y. Distance = √(4 + 4) = 2.83 m.
- 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.
- Correct: turning. ω = 2 × 0.785 = 1.57 rad/s. Positive: turn left, toward the target.
- 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
- Feedback is a loop of measure, compare, correct.
- error = goal − measurement, and a proportional command is gain × error.
- Feedback tolerates slip, unequal wheels and nudges, because it reacts to what happened, not to what was expected.
- 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.
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.
- LaterTune a PID Controller
The feedback of this module with two more terms.
- LaterFix the Oscillating Robot
What happens when the correction is too strong.
- LaterRobot Motion Models for Navigation
Other ways robots drive: car-like, omnidirectional.