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 02b
Smooth it out
Checkpoints · 0 of 2 (waiting for Run)
- Reach the end of the line (waiting for Run)
- Reach it smoothly (low wobble) (waiting for Run)
Why a number beats a branch
Your robot follows the line, but it shivers. In lesson 02a it steered with an if/else: over the line, jerk left; under it, jerk right. The same fixed nudge every time, whether it was a hair off or halfway into the weeds. That fixed nudge is the shiver. Time to smooth its hand.
The fix is to stop deciding which way and start measuring how much. Instead of a branch that picks a direction, use one number that scales with the error. Far off the line, steer hard; nearly centered, barely nudge. This is proportional control, and the whole law is one line:
steer = Kp × error
The error is offset, the signed distance from the line the sensor hands you each tick. Kp is the gain, the one knob that sets how hard the robot leans on its error. Read the skeleton in the editor: kp and base arrive as defaults on the follow_line signature, so tuning them means editing the number and pressing Run again.
Scale the correction by the offset
Find the gain that glides
▸API reference
- follow_line(offset, kp=2.4, base=26)
- Called every tick with the signed cross-track offset in px (+ = right of the line, already sensor-delayed). kp and base are defaults on the signature: edit them and Run again to retune. Return a (left, right) tuple (clamped to ±100).
- kp / base (defaults on the signature)
- The tuning surface lives in your code. Each Run builds from the current defaults, so a changed value drives from the first tick. The starter ships kp HOT (wobbly); ease it down, and the base speed with it, until the bot glides.
space pause · r reset · ctrl/cmd+enter run