Motion · Lesson 02b
Smooth it out
This is the written walk-through of the lesson: the idea, stage by stage, plus the deeper asides. The interactive version, where you write real Python in the browser and drive a simulated robot, lives on the lesson page.
steer = Kp × error. Tune the gain Kp in your code: too low and it drifts off the curves, too high and it oscillates wildly. Find the gain that glides.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
Build the correction from the error, then split it across the wheels. The steer is kp * offset: it grows with the error and, because offset is signed, it already flips direction on its own. No branch needed.
Slow one wheel and speed the other by that same amount and the robot turns back toward the line: (base - steer, base + steer). Multiply by kp so the gain actually does something. Write it, press Run, and watch the bot chase the line to the flag.
Find the gain that glides
It reaches the flag now, but watch the wobble meter under the sim: it climbs every time the bot swings across the line. The starter ships kp=6, a hot gain that overcorrects and rings back and forth. Reaching the end is not the same as reaching it smoothly, and smooth is the win here.
Your gain lives in the code: the kp default on the follow_line signature is the tuning surface, and the editor highlights it. Press Run at kp=6 and watch the wobble meter climb. Then lower the default (try easing it toward 2.4) and press Run again. Each Run reads the current default, so the new value drives from the first tick.
If it still shivers at every gain you try, ease the base speed down too (toward 26). The sensor reading arrives a beat late, and going faster makes that lag hurt more. Find the gain that glides, and the low wobble meter finishes the lesson.
Go deeper: why too much Kp rings
A bigger Kp corrects harder, so you would think more is always better. It is not, and the reason is timing. The sensor reading arrives a beat late, so every correction the robot makes is aimed at where the line was a moment ago, not where it is now. A gentle gain barely notices the delay. A hot gain slams the wheels over based on stale information, blows past the center, then slams back the other way on the next stale reading. That back-and-forth is the ring.
So there are two ways to fail. Too little Kp and the robot drifts wide on the curves and never quite centers. Too much and the delayed sensor turns every correction into an overshoot. The glide lives in the middle, and easing the base speed widens that middle: slower means the stale reading is less stale by the time the robot acts on it. Lesson 03 adds a second term that fixes the timing directly.
Ready to build it? The interactive lesson is where you write the code and watch the robot run.