Skip to main content
emobility.academy
B.Tech / BELane keeping & lateralIntermediate About 5 weeksADAS & AUTOSAR

Path Tracking: Pure Pursuit vs Stanley Controller

Implement the pure pursuit and Stanley path-tracking controllers in MATLAB or Python on a kinematic bicycle model and compare their cross-track error, stability and steering effort on a common set of reference paths and speeds.

Tools: MATLAB · Python

Path Tracking: Pure Pursuit vs Stanley Controller: Students studying an electric car with DIYguru trainers in the workshop
Representative photo from DIYguru's EV lab, not of this specific project.

The problem

Every automated parking, valet or low-speed shuttle function needs a controller that makes the vehicle follow a planned path. Pure pursuit and Stanley are the two classic geometric controllers, each with known strengths and weaknesses. A fair, quantified comparison teaches how look-ahead, gain and speed shape tracking behaviour.

Objectives

  • Implement a kinematic bicycle model with steering and rate limits.
  • Implement pure pursuit with speed-dependent look-ahead.
  • Implement the Stanley controller with heading and cross-track terms.
  • Compare both on identical paths, speeds and initial errors with defined metrics.

What you need

Tools and toolboxes

  • MATLAB — Simulation loop, controller implementations and plotting; controllerPurePursuit from Navigation Toolbox can be used as a cross-check.
  • Automated Driving Toolbox (optional) — lateralControllerStanley as a reference implementation to check your own Stanley code.
  • Python with NumPy and Matplotlib — An alternative implementation route with the same models and metrics.

Software

  • MATLAB (with Navigation Toolbox and Automated Driving Toolbox optional) or Python 3 with NumPy, SciPy and Matplotlib

Extra parts and data

None beyond the software above.

Key concepts

Pure pursuit
Chooses a goal point on the path one look-ahead distance ahead and steers along the circular arc that reaches it. A longer look-ahead gives smoother but less accurate tracking.
Stanley controller
Combines the heading error with an arctangent of the cross-track error scaled by gain over speed, measured at the front axle. It converges quickly but can be sensitive to noise at low speed.
Cross-track error
The perpendicular distance from the reference point on the vehicle to the nearest point on the path; the main accuracy metric for path tracking.

Step-by-step guide

  1. 1

    Define the comparison

    State the scope: low to moderate speed tracking (2 to 15 m/s) of smooth paths, with a kinematic model and no tyre slip. Choose metrics: RMS and maximum cross-track error, heading error, peak steering angle, steering rate and settling distance from an initial offset. Fix the vehicle parameters, such as a 2.7 m wheelbase, 35 degree maximum steer and a steering rate limit, and use them for both controllers so the comparison is fair.

    Checkpoint: A written test plan with metrics, vehicle parameters and the list of paths and speeds.

  2. 2

    Build the vehicle model

    Implement the kinematic bicycle model with state x, y, heading and speed, integrated with a fixed step of 0.01 to 0.02 s. Add saturation and rate limiting on steering and an optional first-order lag to represent the actuator. Test it by commanding a constant steering angle and checking the circle radius equals wheelbase over the tangent of the steering angle.

    Checkpoint: Constant-steer circles match the analytical radius.

  3. 3

    Create reference paths

    Generate a straight line with a lateral offset start, a circle, a sine-wave lane-change path, a figure-of-eight and a parking-style path with tight curvature. Represent each as densely sampled points with heading and curvature, smoothed with a spline. Write a nearest-point search that is robust when the path crosses itself, for example by searching only a window ahead of the previous index.

    Checkpoint: Plots of each path with heading and curvature profiles, and a nearest-point function that never jumps backwards.

  4. 4

    Implement pure pursuit

    Measure from the rear axle. Find the goal point on the path at look-ahead distance Ld ahead of the nearest point, transform it into the vehicle frame and compute the steering angle as the arctangent of 2 times wheelbase times sin(alpha) divided by Ld. Make Ld depend on speed, Ld = k times v plus a minimum, and clamp it. Cross-check against controllerPurePursuit in MATLAB if available.

    Checkpoint: The vehicle follows the circle and sine path stably at 5 m/s with small error.

  5. 5

    Implement Stanley

    Measure from the front axle. Compute the heading error between path tangent and vehicle heading, and the signed cross-track error. Steering is heading error plus arctan of k times cross-track error divided by (v plus a softening constant). Add the softening constant to avoid division by near-zero speed and optionally a yaw-rate damping term. Compare with lateralControllerStanley outputs on a few states.

    Checkpoint: The vehicle converges from a 1 m initial offset without oscillation at 5 m/s.

  6. 6

    Tune both fairly

    For each controller, sweep its main parameters (look-ahead gain for pure pursuit, k and softening constant for Stanley) on a tuning set of paths at a middle speed. Choose the setting that minimises RMS cross-track error while keeping steering rate within limits. Record the chosen values and the sweep plots. Do not retune on the evaluation paths, so neither controller gets an unfair advantage.

    Checkpoint: Parameter sweep plots and a table of the chosen settings.

  7. 7

    Run the comparison

    Run every path at speeds of 2, 5, 10 and 15 m/s with initial offsets of 0, 0.5 and 2 m, and with added position noise of a few centimetres. Log all metrics for every run. Repeat noisy runs with several random seeds and report mean and spread. Watch for corner-cutting by pure pursuit on tight curves and for noise sensitivity in Stanley at low speed.

    Checkpoint: A complete metrics table for both controllers across paths, speeds, offsets and noise levels.

  8. 8

    Analyse and report

    Plot cross-track error against speed for both controllers and overlay their trajectories on the tightest paths. Explain the results with the geometry: pure pursuit cuts corners as look-ahead grows, while Stanley tracks closely but reacts strongly to noise. Recommend which to use for parking-speed and road-speed cases. State the limitations of the kinematic model at higher speed and lateral acceleration.

    Checkpoint: A report with comparison plots and a reasoned recommendation for each speed range.

Testing & validation

  • RMS and maximum cross-track error per path and speed.
  • Convergence distance from initial offsets.
  • Peak steering angle and steering rate.
  • Sensitivity of each controller to position noise.

Expected results

Two validated path-tracking controllers with a fair, quantified comparison showing where each performs best, and tuned parameters ready to reuse in parking or planning projects.

Take it further

  • Replace the kinematic model with a dynamic bicycle model and repeat at higher speeds.
  • Add an MPC tracker as a third controller.
  • Run the controllers on a small robot car or in CARLA.

Viva and review questions

Why does pure pursuit cut corners?

It aims at a point ahead on the path, so on a curve the arc to that point lies inside the path. The longer the look-ahead, the more it cuts.

Why is Stanley measured at the front axle?

Its control law was derived for front-axle error, which makes the closed-loop error dynamics converge exponentially for the kinematic model.

What is the softening constant for?

It stops the cross-track term from becoming very large at near-zero speed, which would cause aggressive steering.

Learn it at DIYguru

These DIYguru courses and programmes teach what this project needs.

Related EV projects

All 100 ADAS & AUTOSAR projects