Robotics Orientation · Lesson 07
Feedback & Control
Est. 45–60 min
Why
Why control is the heart of robotics
Telling a servo “go to 60°” is easy. Making it actually arrive at 60°, quickly, without shaking or overshooting, even when it’s carrying a load, is control. Every robot you will ever build has at least one control loop inside.
Concept
The big idea: measure, compare, correct, repeat
Open loop vs closed loop
- Open loop: send a command and hope. A ceiling-fan regulator on speed 3 doesn’t measure the fan speed or the room temperature. It just applies a setting.
- Closed loop: measure the result, compare with what you wanted, correct, repeat. An AC with a thermostat measures room temperature and switches the compressor to hold 24 °C.
The vocabulary of a control loop
| Term | Meaning | AC example | Servo example |
|---|---|---|---|
| Setpoint (target) | What you want | 24 °C | 90° |
| Measured value | What the sensor says now | 28 °C | 70° |
| Error | Setpoint − measured | −4 °C (too hot) | +20° |
| Controller | Decides the correction from the error | Thermostat logic | Chip inside the servo |
| Actuator | Applies the correction | Compressor | DC motor + gears |
| Plant | The thing being controlled | The room | The arm joint |
The simplest smart controller: proportional (P) control
correction = gain × error
- Big error → big correction; small error → small correction
- Gain (often written Kp) sets how aggressive the controller is
- Gain too low: slow and lazy; takes ages to reach the target.
- Gain just right: gets there quickly and settles.
- Gain too high: overshoots past the target, swings back, and may oscillate (the tap dance).
- Delay (slow sensor, slow communication) makes everything worse. You keep correcting based on old news.
How it works
How one control tick actually works
Read the sensor
measured = 70° (from the servo potentiometer or an encoder).
Compute the error
error = 90° − 70° = +20°. The sign tells the direction: positive means “go up”.
Compute the correction
With gain 0.5: correction = 0.5 × 20 = 10°.
Act
Command the motor to move 10° up. The joint is now at about 80°.
Repeat, fast
Next tick: error 10° → correction 5° → 85°… The error halves every tick and the joint glides in. Real servos repeat this hundreds of times per second.
Visual
Visualise it: gain, delay, overshoot
Move the target. Then raise the gain and add sensor delay, and watch the trace overshoot and oscillate. Use “Disturb plant” to push the joint and see the loop recover.
Interactive · Open-loop vs closed-loop
Current
20.0°
Error (seen)
40.0°
Correction
10.0
Overshoot
0.0°
Closed-loop: correction = gain × error, capped at ±12° per tick, applied every 120 ms. Open-loop holds the plant where it is — target changes do nothing without a model. Try gain 1.2 with 3 ticks of delay to see oscillation.
Worked example
Worked example: the same target, three different gains
Target 90°, start 0°, perfect sensor (no delay). Each tick: angle = angle + gain × (90 − angle).
| Tick | Gain 0.5 | Gain 1.0 | Gain 1.5 |
|---|---|---|---|
| 1 | 45.0 | 90.0 | 135.0 (overshoot!) |
| 2 | 67.5 | 90.0 | 67.5 |
| 3 | 78.8 | 90.0 | 101.2 |
| 4 | 84.4 | 90.0 | 84.4 |
| 5 | 87.2 | 90.0 | 92.8 |
| 6 | 88.6 | 90.0 | 88.6 |
- Gain 0.5: smooth, never overshoots, slows down as it gets close.
- Gain 1.0: one perfect jump. Only possible in a perfect simulation; a real motor can’t teleport.
- Gain 1.5: overshoots to 135° and zig-zags around the target before settling.
- Now add a 2-tick sensor delay to gain 0.5 (see the code lab). The same “safe” gain overshoots to 157.5° and keeps oscillating. Delay matters as much as gain.
Code lab
Code lab: simulate a P-controller in Python
Save as p_controller.py and run it. Each row prints 12 ticks of the joint angle.
# p_controller.py — proportional control of a joint angle, tick by tick.
def run(gain, delay=0, target=90.0, start=0.0, ticks=12):
angle = start
history = [angle]
out = []
for k in range(ticks):
measured = history[max(0, len(history) - 1 - delay)] # sensor may be late
error = target - measured
correction = gain * error
angle += correction
history.append(angle)
out.append(f"{angle:6.1f}")
return " ".join(out)
print("gain 0.5, no delay :", run(0.5))
print("gain 1.0, no delay :", run(1.0))
print("gain 1.5, no delay :", run(1.5))
print("gain 0.5, delay 2 :", run(0.5, delay=2))Expected output
gain 0.5, no delay : 45.0 67.5 78.8 84.4 87.2 88.6 89.3 89.6 89.8 89.9 90.0 90.0 gain 1.0, no delay : 90.0 90.0 90.0 90.0 90.0 90.0 90.0 90.0 90.0 90.0 90.0 90.0 gain 1.5, no delay : 135.0 67.5 101.2 84.4 92.8 88.6 90.7 89.6 90.2 89.9 90.0 90.0 gain 0.5, delay 2 : 45.0 90.0 135.0 157.5 157.5 135.0 101.2 67.5 45.0 39.4 50.6 73.1
Real world
Feedback loops all around you
- Hobby servo: a potentiometer measures the output angle; the internal chip runs a closed loop to match the pulse width you send.
- AC thermostat: measures room temperature and switches the compressor on and off to hold the setpoint. Most also use hysteresis, like the water tank in Lesson 1.
- Pressure cooker: the weight on the vent lifts when pressure gets too high and releases steam (the whistle). That’s a simple mechanical feedback that limits pressure.
- Car cruise control: measures speed, compares with the set speed, adjusts the throttle on hills.
- Robot vacuum wall-following: a side sensor measures distance to the wall; the controller steers to keep it constant.
Mistakes
Common mistakes and debugging
Hardware & safety
Hardware lab and safety notes
Feel a feedback loop with your hands: a potentiometer as the sensor, the LED as the output, and your own hand closing the loop — then let the servo close it for you.
- 10 kΩ potentiometer: outer legs to 5V and GND, middle leg to A0;
analogRead(A0)gives 0–1023. - Inside every hobby servo is the same idea: a potentiometer on the output shaft, a small controller and a motor — a complete closed loop in a box.
- If you add the servo from Lesson 4: signal on pin 9, power from a separate 5–6 V supply, common ground.
Debugging
Debugging: when it doesn’t work
| Symptom | Likely cause | Fix |
|---|---|---|
| Value oscillates around the target | Gain too high | Halve the gain |
| Stops short of the target | Load (gravity) with only P control | Expected steady error; integral action comes in Stage 7 |
| Moves the wrong way | Sign of error flipped | Use error = setpoint − measurement |
| Pot reads 0 or 1023 only | Middle leg not on A0 or outer legs swapped | Check wiring |
Think
Think about it
Think
If error is persistently positive (always short of target), name two mechanical and one sensing cause.
Experiment
Try it in the lab: make it overshoot, then tame it
Experiment
Closed-loop intuition
You will need
- The feedback visualisation below
- The p_controller.py script
- Notebook
Steps
- Use the feedback visual below.
- Move the target and observe how correction sign tracks error sign.
- Imagine delay: what would happen if correction arrived very late? (overshoot / oscillation intuition)
- Set delay to 0 and raise the gain step by step. Write down the first gain where “Overshoot” becomes more than 0°.
- Set delay to 3 ticks. Find the largest gain that still settles without obvious oscillation. Compare with step 4.
Interactive · Open-loop vs closed-loop
Current
20.0°
Error (seen)
40.0°
Correction
10.0
Overshoot
0.0°
Closed-loop: correction = gain × error, capped at ±12° per tick, applied every 120 ms. Open-loop holds the plant where it is — target changes do nothing without a model. Try gain 1.2 with 3 ticks of delay to see oscillation.
Knowledge check
Check your understanding
Knowledge check
1. In a basic feedback loop, error is…
2. Open-loop control does not…
3. A hobby servo’s internal potentiometer is used to…
4. Why might a loop oscillate?
5. Target 100°, measured 80°, gain 0.5. What correction does a P-controller apply?
Practice quiz
Mini assignment
Hands-on assignment
Mini assignment
Loop diagram
Draw setpoint → controller → actuator → plant → sensor → back to controller. Annotate where your Stage 6 servo pulse fits.
Deliverables
- Labeled loop diagram
Stretch (code): add a constant gravity = -3.0 to p_controller.py so each tick does angle += correction + gravity. Run with gain 0.5. Where does the angle settle? Explain the steady error in one sentence.
Do this, step by step
- In
p_controller.py, add print lines forrun(0.2)andrun(1.8)next to the existing ones. For gains 0.2, 0.5, 1.0, 1.5 and 1.8 write how many ticks it takes to get within 1° of 90°, and whether it overshoots. - Add
gravity = -3.0insiderun()so each tick doesangle += correction + gravity, and find where the angle settles for gain 0.5. - Draw the loop diagram: setpoint → controller → actuator → plant → sensor → back, and mark where the servo pulse fits.
Stuck? Hints, one at a time
- Hint 1: Count ticks until
abs(target - angle) < 1. - Hint 2: At steady state the correction exactly cancels gravity: gain × error = 3.
- Hint 3: The servo pulse is the *setpoint* you send; the servo’s own pot and chip close the inner loop.
Challenges (pick by level)
- 🟢 Easy: Change the target to 45° and rerun.
- 🟡 Medium: Find the highest gain that still does not overshoot.
- 🟠 Hard: Add a simple integral term and show the gravity error goes to zero.
Next step
Next, Lesson 8: Robot Types. Related: PID tuning in Stage 7 — Control Systems & PID.
Sources
- Wikipedia — Control loop — setpoint, error, open and closed loop.
- Wikipedia — PID controller — P, I and D terms; steady-state error.
- Wikipedia — Feedback — negative feedback in machines.
- Wikipedia — Servo (radio control)) — the feedback loop inside a hobby servo.
- Photos and diagrams: Wikimedia Commons / own diagrams (credits under each image).
Self-attested for now — keep artifacts in your lab notebook.
Complete
Today's goal
0 of 1 done today
Mark this lesson complete when you have worked the experiment, knowledge check, and assignment (even roughly).