Planning · Lesson 08
Follow the route
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.
Fix a point ahead, steer at it
Your robot has a route on paper. Teach it to actually drive the line.
In lesson 7 you planned a path around the walls; now the robot has to follow it. The tempting move is to hug the line, steering by how far off it you are, but you have already felt where that leads: a hot correction rings, a soft one lags. Pure pursuit throws that framing out. Instead of chasing the nearest point, the robot fixes a single point a set distance ahead on the route, its lookahead, and simply steers toward it, tick after tick.
That one distance is the whole personality of the follower. Look too close and it oversteers and snakes (a hot Kp all over again). Look too far and it cuts the corners, rounding them off and sliding wide (too much damping). Somewhere between is a lookahead that glides. First you write the steering, then you find that distance.
Steer along the arc
Read the skeleton in the editor. Every tick, aim_at(L)hands you aim: the angle from your heading to the point L px ahead on the route, positive to your left. The starter reads that angle and then ignores it, returning the same speed to both wheels, so the bot barrels dead straight off the route.
Your one move is the steering arc. Bend toward the aim point by an amount that grows with math.sin(aim), and scale it by 55 / self.L so a tight lookahead bends the same angle harder. Then split the base speed by that steer, slowing one wheel and speeding the other, exactly the wheel split you wired in earlier lessons. Press Run and watch the bot curve onto the route and drive it to the goal.
Go deeper: the geometry of the arc
Picture a circle of radius L centred on the robot: the lookahead circle. The aim point is where the route crosses it, and the robot follows the single arc that starts along its own heading and curves onto that point. Geometry fixes the tightness of that arc: its curvature is 2·sin(aim) / L, twice the sine of the aim angle over the lookahead distance.
That is the whole reason the same angle bends you harder when L is small: dividing by a shorter lookahead makes a sharper arc. It is also why the follower has one personality knob. A short L demands a hard curve to reach a nearby point, so it overcorrects and snakes; a long L aims past the corner and curves too gently to make the turn, so it slides wide. Your 55 / self.L scale is that curvature law, written so the steer rises as the lookahead shrinks.
Tune the lookahead that glides
The route already reaches the goal, but watch the corners as it goes, and watch the wobble meter under the sim: it climbs every time the bot swings across the line. The shipped lookahead=55 threads the turns cleanly, so the meter stays low. Now feel why.
Your lookahead lives in the code: the lookahead default in __init__ is the tuning surface, and the editor highlights it. Pull it in tight (try lookahead=20) and press Run: the aim point sits so close that the follower fights every wiggle, and the bot snakes down the straights. Each Run builds a fresh follower from your source, so the new value drives from the first tick.
Now push it far the other way (try lookahead=100) and Run again: the aim point reaches so far ahead that the bot starts turning before the corner and slides wide, cutting it off. Somewhere between the snake and the shortcut is the value that glides, and 55 sits right in that pocket. Set it back and Run to finish the lesson.
Ready to build it? The interactive lesson is where you write the code and watch the robot run.