Today’s mission 5 min
Turn your follower into a PID follower, and make it stop exactly where the mission needs it: at a junction, or after a set distance.
In Level 4 lessons 28 and 29 you added D and I in EV3 blocks. It took a screen of blocks. Today it takes six lines of Python. The harder part is the ending: a follower that stops in the wrong place scores nothing.
The idea — P, I and D, and where to stop 10 min
- P — now. How far off the edge you are. You built this last lesson.
- I — so far. Add every error together. If the robot has been off to one side for a while, this grows and pushes it back.
- D — changing. This error minus the last one. If the error is shrinking fast, D eases off the steering before the robot overshoots. It calms the wobble.
turn_rate = KP × error + KI × integral + KD × derivative
Then two ways to end a follow:
- For a distance. Keep going until
robot.distance()has grown by the amount you want. - At a junction. Keep going until the other sensor sees black. That only happens where another line crosses yours.
Tune in this order: P alone, then add D until the wobble goes, then a tiny I only if you need it. Many winning WRO robots use P and D only.
Blocks vs Python 15 min
SPIKE word blocks
when program starts :: events hat set [integral v] to (0) set [last error v] to (0) repeat until <(F v) is color [black v]? :: sensors> set [error v] to (((E v) reflected light :: sensors) - (50)) change [integral v] by (error) set [derivative v] to ((error) - (last error)) start moving ((((error) * (0.8)) + ((integral) * (0.001))) + ((derivative) * (4))) :: movement set [last error v] to (error) end stop moving :: movement
Pybricks Python
KP = 1.0 KI = 0.002 KD = 6.0 BLACK = 20 def follow_to_junction(speed=150): integral = 0 last_error = 0 while right_eye.reflection() > BLACK: error = left_eye.reflection() - TARGET integral = integral + error derivative = error - last_error turn = KP * error + KI * integral + KD * derivative robot.drive(speed, turn) last_error = error wait(10) robot.stop()
| Block | Python | Watch out |
|---|---|---|
| repeat until F is color black ? | while right_eye.reflection() > BLACK: | Python keeps going while the condition is true. Blocks stop when it becomes true. |
| change integral by error | integral = integral + error | |
| set last error to error | last_error = error | Must come after the steering, or D is always 0. |
| set integral to 0 | integral = 0 (inside the function) | A fresh start for every follow. Old error must not carry over. |
The block version works, but look at the steering block. Four nested operators make it hard to check. In Python the formula reads like the one on the board.
Write it from scratch 20 min
Open your follow_p program from last lesson. Save a copy as follow_pid. Then:
- Add D only (
KI = 0). Start KD at 5 times your KP. Run the bend. Does the wobble shrink? - Write
follow_to_junction(speed)using the right sensor to end it. - Write
follow_for(distance, speed)using the same PID lines and thestartdistance trick from last lesson. - Only then add I, tiny: 0.001 to 0.005.
Two functions now share the same five PID lines. That is fine for today. Next lesson you will tidy them into one library.
After a junction: drive the sensors past it
follow_to_junction(150) robot.straight(40)
The robot stops with its sensors on the junction. The wheels are still behind it. Measure how far forward the wheels must go to sit on the crossing, and write that number down.
Test and measure 15 min
The race from Level 4 lesson 30, in Python. Same line, same speed.
- P only: three timed runs, and does it stay on?
- PD: three timed runs.
- PID: three timed runs.
- Junction stop: put a tape mark at the crossing. How far from it does the robot stop each time?
Then raise the speed until P alone fails. Does PD still work at that speed? That speed difference is what D is worth to you.
Break it, fix it 10 min
None of these crash. They just behave wrongly, which makes them harder. Run, watch, then read the code.
Bug 1 — the second follow lurches at the start
integral = 0
def follow_to_junction(speed=150):
global integral
last_error = 0
while right_eye.reflection() > BLACK:
error = left_eye.reflection() - TARGET
integral = integral + error
derivative = error - last_error
robot.drive(speed, KP * error + KI * integral + KD * derivative)
last_error = error
wait(10)
robot.stop()Bug 2 — adding D changes nothing
last_error = 0
while right_eye.reflection() > BLACK:
error = left_eye.reflection() - TARGET
derivative = error - last_error
robot.drive(speed, KP * error + KD * derivative)
wait(10)
robot.stop()Bug 3 — it stops as soon as it starts
while left_eye.reflection() > BLACK:
error = left_eye.reflection() - TARGET
robot.drive(speed, KP * error)
wait(10)
robot.stop()Bugs that do not crash cost the most. A team can run Bug 2 for a month, thinking D is useless. When a change seems to do nothing, print the value it is supposed to change.
Solo challenge — no coach 10 min
No coach, no help. At WRO your coach may not touch the robot or the code. Close this page. You may use your own earlier programs and the Pybricks documentation, nothing else.
Second crossing. Follow the line from the start, pass the first junction without stopping, and stop with the wheels on the second junction.
- The robot must not stop or pause at the first junction.
- Beep once as it passes the first junction.
- Turn 90° at the second junction and drive 200 mm.
Done means: three runs in a row, and the wheels stop within 10 mm of the second crossing each time.
Engineering journal 5 min
- your KP, KI and KD, and what each change did
- the P / PD / PID race table, next to your Level 4 one
- the distance from sensors to wheels, in mm
- why the junction sensor cannot be the following sensor
- one bug today that did not crash, and how you found it