Unit 08 · Phase 2 · Movement & control
Feedback and basic PID
Mission: Hold one wheel at 2 rev/s, within 15 % for 5 seconds, by letting the rover correct its own error, then use the same idea to stop on Unit 01's line.
The interactive version of this unit needs JavaScript. Below are its steps as text.
Steps
1. A fixed command cannot hold a speed
Unit 07 ended with wheels that do not match, even on the same command. Here one wheel gets a fixed 40 % duty. Press the button; at 4 s the floor turns to carpet.
Why it matters: Sending a fixed command and hoping is called open loop. The encoder saw the speed fall, but its reading went nowhere. Any change in load, battery or wheel makes an open-loop wheel drift, and that is part of why Unit 07's straight command veered. To correct, the rover must compare what it wants with what it gets.
2. Error: target minus measurement
The target is 2 rev/s. Press the button to wire the encoder back to a comparison block and watch the gap on the graph.
Why it matters: Error is the target minus the measurement, here in rev/s. Positive error means too slow, negative means too fast. Measuring it does not yet change anything; the duty is still fixed. Feedback means using that error to set the next command.
3. Proportional correction
The controller now sets duty = Kp × error, with Kp = 0.4 (duty per rev/s of error). Press the button to close the loop.
Why it matters: The gain Kp says how much duty to add per rev/s of error; this is proportional control. A proportional controller only pushes while there is error, so it settles where the push from the remaining error balances the drag. That leftover gap is called steady-state error. A bigger Kp shrinks it.
4. More gain, smaller gap
Raise Kp to 1.5. The faint grey trace is the Kp = 0.4 run. Press the button.
Why it matters: More gain means more correction per unit of error, so the steady-state error shrinks. The response is faster and starts to overshoot and wobble. Gain trades accuracy against calm. The question is how far you can push it.
5. Too much gain oscillates
Raise Kp to 6. The grey trace is Kp = 1.5. Press the button and watch the duty strip as well as the speed.
Why it matters: The encoder counts over the last 100 ms and the motor takes time to respond, so the controller always acts on slightly old news. With high gain, each correction is large and arrives late, overshoots, and triggers a large correction the other way. That is oscillation. Too much gain is as bad as too little.
6. The pass band: within 15 % for 5 seconds
Press the button to draw the syllabus pass band on the Kp = 1.5 run and start the 5 s hold timer.
Why it matters: The pass evidence is concrete: measured speed stays within 15 % of the target for 5 continuous seconds. Overshoot is how far the speed goes past the target; settling time is when it enters the band for good. A band turns “looks fine” into a yes or no. It also tells you what to fix.
7. Integral removes the leftover error
Kp = 1, plus an integral term with Ki = 1. At 4 s the carpet arrives again. Press the button and watch the I block's bar.
Why it matters: The integral term adds up error over time, so any leftover error keeps growing its push until the error is gone. That is what lets a loop hold the target under a changed load. Its push is slow to build and slow to let go. Ki sets how fast it accumulates.
8. Windup: the integral remembers too much
Same Kp = 1 and Ki = 1. From 1 s to 3 s a hand holds the wheel still. Press the button and watch what happens when the hand lets go.
Why it matters: While the wheel cannot move, the integral keeps accumulating error it cannot fix, called windup. When the wheel is free, all that stored push comes out at once as overshoot. This model limits the integral to what full duty can use, which is one common fix; without a limit the overshoot is worse. Stalls, steep ramps and saturated duty all cause windup.
9. Derivative reacts to the rate of change
Back to Kp = 3, which wobbles (grey). Add a derivative term, Kd = 0.08. Press the button and watch the speed and the duty strip.
Why it matters: The derivative term uses how fast the error is changing, in rev/s per second, and pushes against fast changes, which damps swings. A rate computed from whole counts jumps every time one count arrives early or late. So derivative turns encoder noise into duty noise. It does not remove the steady-state gap either; that is the integral's job.
10. Encoder noise and battery sag
Now the encoder reading jitters by up to 3 counts and the battery voltage sags by up to 25 % under load. Grey: proportional only (Kp = 1.5) with the same sag. Press the button.
Why it matters: A sagging battery means the same duty gives less voltage, so an open-loop or P-only wheel slows. The integral term quietly raises the duty to compensate, which is feedback earning its keep. Noise blurs the measurement, so keep derivative small or filter the reading before using it. Add I or D only when a test shows you need it.
11. Unit 01's stop rule on a rover with mass
Unit 01 drove at full speed and cut the motor when the reading reached the line. That model stopped instantly. This rover has mass (Unit 05) and odometry (Unit 07). Press the button.
Why it matters: A full-speed-then-stop rule ignores how far away the line is until it is too late. Mass carries the rover on after drive is cut. Feedback on distance can slow down early. The same subtract, multiply and correct loop works with distance in place of speed.
12. Proportional on distance
Now the target is the line at 1000 mm, the measurement is the odometry distance, and the output is a speed command: speed = Kp × distance error, with Kp = 5 per second, capped at 500 mm/s. Press the button.
Why it matters: Speed proportional to remaining distance means slowing down as the line gets close, which is what the instant-stop model could not do. With a high gain the rover is still fast near the line and its mass carries it past. Lower Kp would be calmer but slower. Something has to react to how fast the gap is closing.
13. Stop on the line
Keep Kp = 5 and add Kd = 1: the derivative of the distance error is minus the rover's speed. Press the button.
Why it matters: On distance, the derivative of the error is just speed, so Kd brakes in proportion to how fast the rover approaches. That damping lets you keep a high Kp and still arrive calmly. This answers the Unit 01 mission with feedback instead of a fixed rule. It closes Phase 2: an encoded rover moving under closed-loop control.
14. Your wheel-speed trials
Lift the rover so the wheel spins freely for the first test. Press the button to see what your log needs, then open the Notebook tab.
Why it matters: Start with proportional control only and a small Kp; raise it until the wheel responds briskly without swinging. Add integral only if a steady gap remains, and derivative only if swings remain that you cannot fix with gain. Log target and measured speed and check 15 % for 5 s in three trials. If it oscillates, suspect high gain, encoder noise or battery sag before rewriting code.
Go deeper
The maths in this unit
Error, proportional gain and derivative as a rate, written as the controller runs them every Δt seconds.
error e(t) = target − measured proportional P = Kp × e integral I = Ki × Σ e × Δt (limited so I alone never asks for more than 100 % duty) derivative D = Kd × (e − e_previous) ÷ Δt command u = P + I + D, limited to −100 % … +100 % duty pass rule 0.85 × target ≤ measured ≤ 1.15 × target for 5 s
Units keep the gains honest: in the speed loop Kp is duty per rev/s, Ki is duty per (rev/s × s), and Kd is duty per (rev/s per second). In the stop loop Kp is (mm/s) per mm, which is per second.
Model limits
- SIMULATED, simplified motor: a first-order response with a 0.25 s time constant, a 50 ms driver lag, an 8 % duty deadband and a sag factor. Real motors add friction that changes with speed, backlash and heating.
- Speed comes from whole encoder counts over a 100 ms window, read every 20 ms. Shorter windows react faster but show bigger count steps.
- The stop-on-line rover follows its speed command with a 0.3 s lag and measures distance by odometry, so Unit 07's slip and wheel errors would still apply on a real floor.
- Derivative here is a plain difference with no filter. Real controllers usually filter it or take the derivative of the measurement instead of the error.
- Tuning numbers here suit this model only. Your motor needs its own.
Watch / read
- Stanford CS223A (Introduction to Robotics), Lecture 1: introduction. Archived course; matrix algebra helps; a selected overview, including where control fits in a robot.
- Lynch and Park, Modern Robotics: Mechanics, Planning, and Control, the robot control chapter, as a reference for feedback and PID.