Find a course

Module 4: Programming and ControlLesson 8 of 1012 min

Feedback Control

Learn the difference between open-loop and closed-loop control, compare bang-bang and proportional control, and tune a line follower with a gain.

Step 1 of 10Learn

Driving with your eyes closed

Try walking to a door ten steps away with your eyes closed. You'll probably end up close, but not exactly at the handle. Now walk with your eyes open: you constantly see small errors and correct them without thinking. That difference is the core idea of control engineering.

Open-loop control sends commands without checking the result. "Run both motors at 70% for 3 seconds" is open-loop. It works only if nothing unexpected happens: same battery voltage, same floor, identical motors. The square-driving robot that ended up in the hallway was open-loop.

Closed-loop control, also called feedback control, measures the result and uses the difference to correct the next command:

  1. Decide the setpoint: what you want (stay on the line, drive at 30 cm/s, hold the arm at 45 degrees).
  2. Measure what is actually happening with a sensor.
  3. Calculate the error: setpoint minus measurement.
  4. Adjust the actuators to shrink the error.
  5. Repeat, many times per second.

A home thermostat is a familiar closed-loop controller: it measures room temperature, compares it with the setting and switches the heat on or off. Cruise control in a car, a drone holding its height and a robot keeping its wheels at matching speeds all work the same way.

Step 2 of 10Practice · Diagram

The feedback loop

Which stage is missing in open-loop control, like a toaster on a timer?

Tap the shape that answers the question, then press Check.

The feedback loopCycle diagram: Setpoint: where you want to be → Compute the error → Controller picks a correction → Motors act → Sensor measures again.Closed-loop controlSetpoint: whereyou want to beCompute theerrorController picksa correctionMotors actSensormeasures again
Setpoint: where you want to be

For example, the edge of the line, or 90 degrees.

Compute the error

Error = setpoint minus measurement.

Controller picks a correction

Bang-bang, proportional, or more advanced.

Motors act

The robot moves a little.

Sensor measures again

This is the "eyes open" part that open-loop control lacks.

Step 3 of 10Practice · Sort into groups

Sort each system: open-loop or closed-loop control?

Tap an item, then tap its group (or drag it there).

Step 4 of 10Learn

Bang-bang control

The simplest feedback controller is bang-bang control, also called on-off control. It has only two outputs: full one way or full the other.

A line-following robot with bang-bang control uses one sensor at the edge of a black line:

  • If the sensor sees black (too far onto the line): turn right hard.
  • If it sees white (too far off the line): turn left hard.

It works, and it's easy to program. But watch it run and you'll see it zigzag constantly, wobbling back and forth across the edge. Every correction is a full-strength correction, so it always overshoots. On a curvy track or at higher speed, the zigzags can grow until the robot loses the line completely.

A thermostat is a bang-bang controller too: heat fully on or fully off. In a house, that's fine, because rooms warm and cool slowly. For robots that move quickly, smoother control usually works much better.

The fix is to make the correction proportional to the error: small error, gentle correction; big error, strong correction. That requires a sensor that tells you how far off the line you are, not just which side, such as an analog reflectance reading or an array of several sensors side by side.

Step 5 of 10Practice · True or false

Bang-bang control tends to make a line-following robot zigzag because every correction is full strength.

Step 6 of 10Learn

Proportional control

In proportional control (P control), the correction equals the error multiplied by a number called the gain, written Kp:

correction = Kp × error

Say a line sensor reading of 500 means "perfectly on the edge" (the setpoint). The sensor reads 620, so the error is 500 − 620 = −120. With Kp = 0.2, the correction is 0.2 × −120 = −24. The robot slows one wheel by 24 and speeds up the other by 24 (a negative correction steers one way, a positive one steers the other). A reading of 520 gives an error of only −20 and a gentle correction of −4.

Choosing Kp is called tuning:

  • Kp too small: the robot reacts lazily and drifts off on sharp curves.
  • Kp too large: it over-corrects and oscillates, behaving almost like bang-bang.
  • Kp just right: it follows smoothly.

Builders usually start small and double Kp until the robot starts to wobble, then back off a bit.

Proportional control has one weakness: when the error gets small, the correction gets small too, and friction or a steady push (like a slope) can leave a small leftover error. Engineers add more terms to fix this, giving the famous PID controller: proportional, integral (which adds up past error) and derivative (which reacts to how fast the error is changing). PID controllers run in drones, cars, factories and countless robots.

Step 7 of 10Practice · Chart

Bang-bang vs proportional on a line follower

Distance of the sensor from the line edge, in millimeters, over time after a bump knocks the robot sideways.

Tap the first moment when the bang-bang robot overshoots to the other side of the line.

Tap the point that answers the question, then press Check.

Bang-bang vs proportional on a line followerLine chart with 9 categories: 0: Bang-bang 10 mm, Proportional (gentle gain) 10 mm; 0.2: Bang-bang 4 mm, Proportional (gentle gain) 6 mm; 0.4: Bang-bang -4 mm, Proportional (gentle gain) 3.6 mm; 0.6: Bang-bang 4 mm, Proportional (gentle gain) 2.2 mm; 0.8: Bang-bang -4 mm, Proportional (gentle gain) 1.3 mm; 1.0: Bang-bang 4 mm, Proportional (gentle gain) 0.8 mm; 1.2: Bang-bang -4 mm, Proportional (gentle gain) 0.5 mm; 1.4: Bang-bang 4 mm, Proportional (gentle gain) 0.3 mm; 1.6: Bang-bang -4 mm, Proportional (gentle gain) 0.2 mm.-5 mm0 mm5 mm10 mmError00.20.40.60.81.01.21.41.6Time (s)
Illustrative curves.
Bang-bang vs proportional on a line follower
Time (s)Bang-bangProportional (gentle gain)
010 mm10 mm
0.24 mm6 mm
0.4-4 mm3.6 mm
0.64 mm2.2 mm
0.8-4 mm1.3 mm
1.04 mm0.8 mm
1.2-4 mm0.5 mm
1.44 mm0.3 mm
1.6-4 mm0.2 mm

Step 8 of 10Explore

Correction vs. error for proportional control

The output is Kp × error, limited to the motors' maximum of ±100. Raise Kp and watch the line get steeper: small errors get bigger corrections, and the output hits the limit sooner.

-100-50050100 -1000100
Correction for error 20
20
Error that reaches full power
100

Step 9 of 10Practice · Work it out

A robot arm should be at 90 degrees but its sensor reads 75 degrees. Using proportional control with Kp = 3, what is the correction?

Step 10 of 10Practice · What would you do?

What would you do?

Your proportional line follower wobbles side to side more and more as it speeds up, and sometimes loses the line. You're using Kp = 4. What should you try first?