Stage 00 · Lesson 07/10

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.
Two block diagrams. Open loop: Command → Plant, with no measurement. Closed loop: Setpoint → Controller → Plant → Sensor, with a feedback arrow back to the controller where error = target − measured.
Open loop has no way to notice drift. Closed loop measures the output and feeds the error back to the controller.

The vocabulary of a control loop

TermMeaningAC exampleServo example
Setpoint (target)What you want24 °C90°
Measured valueWhat the sensor says now28 °C70°
ErrorSetpoint − measured−4 °C (too hot)+20°
ControllerDecides the correction from the errorThermostat logicChip inside the servo
ActuatorApplies the correctionCompressorDC motor + gears
PlantThe thing being controlledThe roomThe 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.
NoteSimplification: full PID control (adding Integral and Derivative terms), tuning methods and stability maths come in Stage 7. Here we build the intuition with P only.

How it works

How one control tick actually works

  1. Read the sensor

    measured = 70° (from the servo potentiometer or an encoder).

  2. Compute the error

    error = 90° − 70° = +20°. The sign tells the direction: positive means “go up”.

  3. Compute the correction

    With gain 0.5: correction = 0.5 × 20 = 10°.

  4. Act

    Command the motor to move 10° up. The joint is now at about 80°.

  5. 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

target 60°last ~11 s

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).

Joint angle after each tick
TickGain 0.5Gain 1.0Gain 1.5
145.090.0135.0 (overshoot!)
267.590.067.5
378.890.0101.2
484.490.084.4
587.290.092.8
688.690.088.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 · Python
# 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
SymptomLikely causeFix
Value oscillates around the targetGain too highHalve the gain
Stops short of the targetLoad (gravity) with only P controlExpected steady error; integral action comes in Stage 7
Moves the wrong waySign of error flippedUse error = setpoint − measurement
Pot reads 0 or 1023 onlyMiddle leg not on A0 or outer legs swappedCheck wiring
NoteChange one thing at a time and write down what happened. Most first-day problems are wiring, power or the wrong port, not the code.

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

  1. Use the feedback visual below.
  2. Move the target and observe how correction sign tracks error sign.
  3. Imagine delay: what would happen if correction arrived very late? (overshoot / oscillation intuition)
  4. Set delay to 0 and raise the gain step by step. Write down the first gain where “Overshoot” becomes more than 0°.
  5. 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

target 60°last ~11 s

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

Loading 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

  1. In p_controller.py, add print lines for run(0.2) and run(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.
  2. Add gravity = -3.0 inside run() so each tick does angle += correction + gravity, and find where the angle settles for gain 0.5.
  3. Draw the loop diagram: setpoint → controller → actuator → plant → sensor → back, and mark where the servo pulse fits.

Stuck? Hints, one at a time

NoteTry the task first. Read one hint, try again, and only then read the next one.
  1. Hint 1: Count ticks until abs(target - angle) < 1.
  2. Hint 2: At steady state the correction exactly cancels gravity: gain × error = 3.
  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

Self-attested for now — keep artifacts in your lab notebook.

Complete

Today's goal

0 of 1 done today

0-day streak
Daily goal:lessons, quizzes or assignments a day

Mark this lesson complete when you have worked the experiment, knowledge check, and assignment (even roughly).

Stuck somewhere?

Ask a doubt about this lesson

Saved with this lesson's id and your email so we can reply. Privacy · or email contact@aihubstation.in