Click & Whirr needs a wider screen
Open it on a laptop or desktop (1280px or wider recommended). The Learn · Code · Watch workspace needs the room to breathe.
Lesson 02a
Follow the line
Checkpoints · 0 of 2 (waiting for Run)
- Catch the line (waiting for Run)
- Reach the end of the line (waiting for Run)
A sensor out front
Your robot can drive, but it wanders: nothing tells it where the line is. This lesson gives it eyes. A line sensor is mounted out front, and you write the logic to steer back.
The sensor reads one number every tick, the offset: how far off-center the robot sits from the line, in pixels. Watch the readout on the right as the bot drives. When the offset is positive the robot has drifted to the right of the line; when it is negative, to the left; at zero it is dead on.
That signed number is the error, the gap between where the robot is and where it should be. Every controller you build from here reads an error and returns a correction. This one is the simplest possible version: look at the sign, and steer the other way.
React to which side you drifted
Ride it to the flag
▸API reference
- follow_line(offset)
- Called every tick. Return a (left, right) wheel-speed tuple. offset is the line sensor reading: > 0 = robot is right of the line, < 0 = left, 0 = centered.
- offset (float)
- Signed cross-track distance from the line, in pixels. Sign convention: positive means the robot has drifted to the RIGHT of the line.
- robot.set_wheels(left, right)
- Alternative to returning a tuple: set the wheel speeds directly. If you both call this and return a tuple, the returned tuple wins. Speeds are clamped to ±100.
- print(...)
- Prints to the console panel below the editor: handy for watching offset change as the robot drives.
space pause · r reset · ctrl/cmd+enter run