Today’s mission 5 min
Write a proportional line follower from a blank file, then tune it until it runs the line smoothly and fast.
You built one in EV3 blocks in Level 4 lesson 12. Find your gain and race times from that lesson now. The idea has not changed at all. Only the language has, and one unit, which is where today’s bugs will come from.
The idea — steer by how far off you are 10 min
Sit the sensor on the edge of the line, half black and half white. Its reflection there is the target. Work it out from your own readings:
target = (black + white) ÷ 2
Then, many times a second:
error = reflection − targetturn_rate = KP × errorrobot.drive(speed, turn_rate)
robot.drive() is new. It starts the robot moving and returns at once, without waiting. That is why it lives inside a loop: each pass replaces the last instruction with a fresh one. Speed is in mm/s. Turn rate is in degrees per second, and positive turns right.
Your Level 4 gain will not work here. EV3 steering was a value up to 100. Pybricks turn rate is in degrees per second. Same idea, different scale. Start small, around 1, and tune.
Blocks vs Python 15 min
SPIKE word blocks
when program starts :: events hat set movement motors to (A+B v) :: movement set movement speed to (30) % :: movement forever set [error v] to (((E v) reflected light :: sensors) - (50)) start moving ((error) * (0.8)) :: movement end
Pybricks Python
from pybricks.pupdevices import ColorSensor left_eye = ColorSensor(Port.E) TARGET = 50 KP = 1.0 SPEED = 150 robot.reset() while robot.distance() < 800: error = left_eye.reflection() - TARGET robot.drive(SPEED, KP * error) wait(10) robot.stop()
| Block | Python | Watch out |
|---|---|---|
| forever | while robot.distance() < 800: | Python stops after 800 mm. Decide where a follow ends. |
| E reflected light | left_eye.reflection() | Both are 0-100 %. |
| set error to … | error = … | A variable needs no block to create it. |
| start moving (error × 0.8) | robot.drive(SPEED, KP * error) | SPIKE steering is −100 to 100. Pybricks turn rate is deg/s. |
| set movement speed to 30 % | SPEED = 150 | mm/s, not percent. |
| (nothing) | robot.stop() | Without it the robot drives on after the loop. |
Capital names like KP and TARGET are a Python habit for numbers you set once and tune. You change them at the top, and the loop never needs touching.
Write it from scratch 20 min
New file, follow_p. Type everything from memory: the set-up, the sensor, the loop. Work in this order:
- Measure. Print the reflection over black and over white. Calculate
TARGET. - Slow first.
SPEED = 100,KP = 1.0. Does the robot follow, or dive off? - Fix the sign if it dives off. Change
KPto-1.0. Do not change anything else. - Make it a function. Wrap the loop in
follow_line(distance, speed), like lesson 6.
Step 4 — the shape to aim for
def follow_line(distance, speed=150): start = robot.distance() while robot.distance() - start < distance: error = left_eye.reflection() - TARGET robot.drive(speed, KP * error) wait(10) robot.stop()
Why start? So the function measures its own distance, wherever the robot already is. It no longer depends on a reset() somewhere else.
Test and measure 15 min
Use a line with at least one bend. For each speed, find the KP that runs it without losing the line.
SPEED = 100: try KP 0.5, 1, 2, 4. Watch the shape of the path, not just the clock.SPEED = 250: does the same KP still work?SPEED = 400: what is the fastest speed that still makes the bend?- Change
wait(10)towait(100). What happens? Why?
Too little KP looks lazy and drifts wide on bends. Too much KP wobbles on the straight. Record every value with one word about what it did.
Break it, fix it 10 min
Bug 1 — the robot does not move at all
robot.reset()
while robot.distance() > 800:
error = left_eye.reflection() - TARGET
robot.drive(SPEED, KP * error)
wait(10)
robot.stop()Bug 2 — it crashes on the first pass
while robot.distance() < 800:
error = left_eye.reflection - TARGET
robot.drive(SPEED, KP * error)
wait(10)
robot.stop()Bug 3 — it follows, then keeps driving
while robot.distance() < 800:
error = left_eye.reflection() - TARGET
robot.drive(SPEED, KP * error)
wait(10)
hub.speaker.beep()Bug 2’s message is a TypeError. Python is trying to subtract a number from a method. The brackets are what turn reflection into a reading. You met the same mistake with the heading in lesson 7.
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.
Two-speed follower. Follow a line for 1200 mm. Run the first 600 mm fast and the last 600 mm slow, so the robot arrives gently and stops on its mark.
- Use your
follow_linefunction twice. - Each speed needs its own KP. Find both.
- Print the time for the whole run.
Done means: three runs in a row stay on the line and stop within 15 mm of the mark.
Engineering journal 5 min
- your black, white and TARGET values
- the speed and KP table, with a word on each run
- which sign KP needed, and why, for your sensor and edge
- your Level 4 times beside today’s
- what the wait in the loop does