Control, PID, MPC, Pure Pursuit, and Stanley
Control turns a planned trajectory into steering, throttle, and braking commands. It is where elegant trajectories meet tire friction, actuator delay, sensor noise, slopes, load transfer, and imperfect vehicle models. In autonomous driving, longitudinal control manages speed and headway, while lateral control manages path tracking. A good controller is stable, smooth, robust, and honest about the limits of the plan it is asked to follow.
This page introduces PID, pure pursuit, Stanley control, model predictive control, and the kinematic and dynamic bicycle models. It connects motion planning to embedded implementation and engineering math, because control is both mathematical and deeply physical.
Definitions
Longitudinal control regulates speed, acceleration, and spacing using throttle and brakes. Adaptive cruise control and stop-and-go traffic control are longitudinal problems.
Lateral control regulates cross-track error and heading error using steering. Lane keeping, path following, and obstacle-avoidance tracking are lateral problems.
PID control uses proportional, integral, and derivative terms:
The proportional term reacts to current error, the integral term removes steady-state bias, and the derivative term damps fast changes.
Pure pursuit chooses a lookahead point on the reference path and steers toward it. For wheelbase , lookahead distance , and angle to the lookahead point, a common steering law is:
Stanley control combines heading error and cross-track error:
where is heading error, cross-track error, speed, gain, and prevents division by zero.
MPC solves a constrained control problem over a finite horizon, using a vehicle model and cost function. It can handle actuator limits and coupled lateral-longitudinal behavior.
The kinematic bicycle model assumes no tire slip and is useful at low to moderate speeds. The dynamic bicycle model includes tire forces and slip angles, which matter near handling limits, high speed, or low-friction surfaces.
Key results
Ackermann steering geometry relates steering angle and turn radius:
This relation appears in planning and control. A planned path with curvature beyond cannot be tracked by steering alone.
Pure pursuit is geometrically intuitive. Increasing lookahead distance smooths steering but cuts corners and reacts slowly. Decreasing lookahead improves tight tracking but can cause oscillation. A common heuristic is speed-dependent lookahead:
Stanley control is attractive because it directly corrects cross-track error. The cross-track term weakens at high speed because the denominator includes , reducing aggressive steering when fast.
MPC can be written:
MPC is powerful but not magic. It depends on model fidelity, solver timing, horizon length, constraints, and warm starts. A late or infeasible MPC solution must be handled safely.
Control must account for delay. If steering actuator delay is 100 ms at 25 m/s, the vehicle moves 2.5 m before the commanded steering fully appears. Delay compensation, preview, and robust margins are essential.
Good tracking usually combines feedforward and feedback. Feedforward steering can be computed from planned curvature, for example , while feedback corrects residual cross-track and heading errors. Feedforward alone fails when the model is wrong; feedback alone can lag and oscillate if the path curvature changes quickly. A production controller also needs command filtering, rate limits, saturation handling, and health checks for actuator response.
Friction limits connect control to physics. The available tire force must support braking, acceleration, and cornering. A simple lateral acceleration estimate is:
At high speed, modest curvature can demand more lateral acceleration than the road can provide, especially in rain, snow, or on worn tires. A controller should not be blamed for failing to track a trajectory that violates the friction envelope; the planner and safety monitors must prevent such references.
Controller evaluation should separate steady-state accuracy, transient response, passenger comfort, and safety margin. A controller that minimizes cross-track error by using sharp steering may be uncomfortable or unstable on low-friction roads. A controller that is very smooth may cut corners or react too slowly to a suddenly changing reference. Typical metrics include RMS tracking error, maximum error, steering rate, jerk, acceleration, actuator saturation time, and recovery after disturbances.
Control handoff also matters. When a planner replans, cancels a lane change, or enters fallback, the controller should transition references smoothly. Discontinuous reference trajectories can create command spikes even when each individual trajectory is feasible.
Visual
| Controller | Main input | Strengths | Weaknesses | Typical use |
|---|---|---|---|---|
| PID | Scalar error | Simple, robust, easy to tune | Limited for coupled constraints | Speed, acceleration, low-level loops |
| Pure pursuit | Lookahead point | Geometric, stable with good lookahead | Corner cutting, sensitive lookahead | Path tracking, low-speed robots |
| Stanley | Heading and cross-track error | Good path convergence, simple | Needs sign conventions and tuning | Lane/path tracking |
| MPC | Model, constraints, reference | Handles multivariable constraints | Compute, model mismatch, infeasibility | Advanced trajectory tracking |
| Dynamic control | Tire force model | Better near limits | Parameter-heavy | High-speed, low-friction, research |
Worked example 1: Pure-pursuit steering
Problem: A vehicle has wheelbase m. The lookahead point is at distance m and angle relative to the vehicle heading. Compute the pure-pursuit steering command.
- Convert angle:
- Use the steering law:
- Compute numerator:
- Divide by lookahead:
- Take arctangent:
- Convert to degrees:
Answer: pure pursuit commands about of steering.
Check: The lookahead angle is 8 degrees, but steering is smaller because the lookahead point is 10 m away and the wheelbase is 2.8 m.
Worked example 2: Stanley steering on a curve
Problem: A vehicle travels at m/s. Heading error to the path is . Cross-track error is m to the left of the path, gain , and . Compute the Stanley steering correction assuming positive error requires positive steering.
- Convert heading error:
- Compute cross-track term:
- Approximate arctangent:
- Sum terms:
- Convert to degrees:
Answer: Stanley control commands about .
Check: At higher speed, the cross-track term would shrink. At lower speed, the same lateral error would produce stronger steering, which is why and saturation matter.
Code
import numpy as np
def pure_pursuit_delta(wheelbase, lookahead_dist, alpha_rad):
return np.arctan2(2.0 * wheelbase * np.sin(alpha_rad), lookahead_dist)
def stanley_delta(heading_error, cross_track_error, speed, gain=1.0, eps=0.1):
return heading_error + np.arctan2(gain * cross_track_error, speed + eps)
def pid_step(error, prev_error, integral, dt, kp, ki, kd):
integral += error * dt
derivative = (error - prev_error) / dt
command = kp * error + ki * integral + kd * derivative
return command, integral
delta_pp = pure_pursuit_delta(2.8, 10.0, np.deg2rad(8.0))
delta_st = stanley_delta(np.deg2rad(2.0), 0.5, 12.0, gain=1.2)
print("pure pursuit deg:", np.rad2deg(delta_pp))
print("stanley deg:", np.rad2deg(delta_st))
Common pitfalls
- Mixing sign conventions for cross-track error. A controller can steer away from the path if the coordinate convention is inconsistent.
- Tuning at one speed and deploying at another. Lookahead, gains, and delay effects all change with speed.
- Ignoring actuator saturation and rate limits. A controller that asks for impossible steering produces tracking errors and instability.
- Letting PID integral wind up during saturation. Anti-windup logic is needed for braking and throttle loops.
- Using a kinematic model near tire limits. Wet roads, snow, high curvature, and high speed require stronger dynamic awareness.
- Validating only in open-loop logs. Control must be tested closed-loop because small tracking errors change future states.
Connections
- Motion planning
- Decision making and behavior planning
- Simulation and data
- Embedded systems
- Engineering math for controls and ODEs
- Physics of signals and systems
- Further reading: Stanley controller from the DARPA Grand Challenge, pure pursuit robotics literature, Rajamani's vehicle dynamics, MPC texts, and PID control references.