Self-Driving Cars
Topic 5 · Part 1: How a self-driving car works · ⏱ about 3 h

Better Steering: Pure Pursuit, Stanley & MPC

Controllers that use the geometry of the road ahead: pure pursuit chases a point on the path, Stanley corrects heading and offset at the front axle, and speed planning slows the car before bends. Then an introduction to model predictive control.

Softmaxima Simulator StudioTry this topic's code in the Studio: it opens with the pure pursuit project, ready to run.Open
Show:
In this topic
Beginner

In plain words

Good drivers look ahead, not at the bonnet. Pick a point on the road some distance ahead and steer towards it: that is the pure pursuit controller. Then slow down before bends and speed up after them, like any driver.

By the end of this topic you will be able to compare three ways of steering and set up a fast, clean lap.

Start with: the demo Try it: compare the controllers.

Beginner

Look where you want to go

PID only reacts to where the car is. Good human drivers look ahead to where the road is going. Geometric controllers use the path ahead, which the planner (or, for now, the map) provides as a list of points.

Beginner

Going deeper

The sections marked Intermediate · Maths, Code or Advanced go deeper: the equations, the code and the finer details. They are worth it when you are ready, but you can follow the whole idea without them. To hide them, press those buttons next to Show at the top of the page; the Beginner explanations and Try it demos stay.

Intermediate · Maths

Pure pursuit

Pick a point on the path a look-ahead distance Ld in front of the car, then steer along the circular arc that passes through it. If the point is at angle α from the car's heading, simple geometry gives the arc's curvature 2 sin α / Ld, and the bicycle model turns that into a steering angle:

δ=arctan⁡ ⁣(2Lsin⁡αLd)\delta = \arctan\!\Big(\frac{2L\sin\alpha}{L_d}\Big)
  • Short look-ahead: tracks the path tightly but reacts sharply, and can oscillate at speed.
  • Long look-ahead: smooth, but cuts corners, because it aims at a point that is already round the bend.
  • A common rule is to grow Ld with speed, for example Ld = 4 m + 0.3 s × v.

Pure pursuit was developed at Carnegie Mellon University for robot vehicles in the 1980s and 1990s, and is still widely used because it is simple and robust.

Intermediate · Maths

The Stanley controller

Stanford's car Stanley won the 2005 DARPA Grand Challenge, a 212 km desert race for driverless cars. Its steering law combines the two things that matter, measured at the front axle:

δ=−(ψe+arctan⁡k efv+ε)\delta = -\Big(\psi_e + \arctan\frac{k\, e_f}{v + \varepsilon}\Big)

Here ψe is the heading error (point the wheels along the road) and ef is the cross-track error at the front axle (then steer towards the path). Dividing by speed v means it corrects gently at high speed and firmly at low speed; the small ε avoids dividing by zero when stopped.

Try it

Try it: compare the controllers

Pick a controller and change its setting and the speed. The table runs all three on the same track and speed. Push the speed up until something fails, and look at the tyres-sliding message.

Interactive: this part runs in your browser.

Intermediate · Maths

Speed planning: slow in, fast out

Every controller fails if the car enters a bend faster than the tyres can grip. The fix is to plan speed from the road ahead. With the curvature κ = 1/R of each point ahead and a comfortable sideways acceleration alat (for passengers, about 2–4 m/s²; our challenge allows more):

vtarget=min⁡ ⁣(vmax⁡, alat∣κ∣max⁡)v_{\text{target}} = \min\!\Big(v_{\max},\ \sqrt{\frac{a_{\text{lat}}}{|\kappa|_{\max}}}\Big)

Taking the largest curvature over the next 20–30 m means the car brakes before the bend, not in it. Real planners also limit braking and acceleration so the ride is smooth.

Try it

Challenge: a fast, clean lap of the Hill road

Use pure pursuit for steering and plan your speed from the curvature ahead.

This runs your code in the simulator, as described in The Simulator: Run Your Code. That page also lets you try any track, keep your code, and download the simulator to run on your own computer.

Interactive: this part runs in your browser.

Advanced

Model predictive control (MPC)

The controllers so far decide from the current situation with a fixed rule. Model predictive control plans instead. Every time step it:

  1. uses the vehicle model to predict where the car will go over the next 1–3 seconds for a candidate sequence of steering and acceleration commands;
  2. scores that prediction with a cost: distance from the path, heading error, speed error, plus penalties for harsh steering and braking;
  3. uses an optimiser to find the command sequence with the lowest cost, while respecting the limits (maximum steering, steering rate, grip);
  4. applies only the first command, then repeats the whole thing on the next step with fresh measurements.

Because it looks ahead and handles limits directly, MPC is the standard in modern autonomous vehicles and advanced driver assistance. Its costs are computation (an optimisation 10–50 times a second) and the need for a good model, often the dynamic bicycle model with tyre forces. We use it again in Part 4, where the planner and controller work together.

Check yourself

Quiz

1. In pure pursuit, what happens if the look-ahead distance is very long?

2. Why does the Stanley controller divide the cross-track term by speed?

3. A bend ahead has radius 20 m. With a sideways-acceleration budget of 5 m/s², what is the target speed?

4. What does an MPC controller do with the command sequence it optimises?

Go deeper

📚 Further reading

Trusted, mostly free sources: official documentation, well-known textbooks and courses. When you want to confirm or go deeper on anything here, start with these.

Ready