Algorithm Search⌘K

Algorithmintro

Pure pursuit

Also called pure pursuit path tracking.

Steers along the circular arc that meets the path at a lookahead distance ahead of the vehicle.

Time
O(n) worst
Space
O(1) extra
Where
n = the number of vertices of the path polyline; the bounds are for one control step
Properties
deterministicnot exactonlinenot randomized
Origin
R. Craig Coulter, 1992

Pure pursuit is a steering law for a wheeled vehicle that follows a given path. At each control step it picks a goal point on the path, at a fixed distance ℓd\ell_d (the lookahead) ahead of the vehicle. It then computes the circle arc that starts at the rear axle, is tangent to the current heading and passes through the goal point, and it steers the wheels to drive on that arc. The curvature of the arc is κ=2sin⁡α/ℓd\kappa = 2\sin\alpha/\ell_d, where α\alpha is the angle of the goal point seen from the heading, and the steering angle of a car with wheelbase LL is δ=arctan⁡(κL)\delta = \arctan(\kappa L). The law is a heuristic: it has one tuning parameter, needs no model of the dynamics, and is used on many robots and autonomous cars at low and medium speed.

In plain words

Look at a point on the road some meters ahead of you. Turn the wheel so that, if you kept it still, the car would drive a smooth curve that hits that point. A moment later, look again at a point the same distance ahead, and correct the wheel. Repeat.

If you look far ahead, the car turns gently and smoothly, but it cuts corners. If you look close, the car follows the road more tightly, but it can weave from side to side. The look-ahead distance is the only knob.

The problem

The model. The vehicle is the kinematic bicycle model with the reference point at the center of the rear axle: x′=vcos⁡θx' = v\cos\theta, y′=vsin⁡θy' = v\sin\theta, θ′=(v/L)tan⁡δ\theta' = (v/L)\tan\delta, with the speed v>0v > 0 given by another loop and the steering angle δ\delta in [−δmax,δmax][-\delta_{max}, \delta_{max}]. A unicycle or a differential drive robot uses the same law with ω=vκ\omega = v\kappa in place of the steering angle. The path is a polyline in the plane. Arithmetic, square roots and trigonometric functions cost one step each, as in the real RAM.

Input. The path as a list of points in meters, the current pose (x,y,θ)(x, y, \theta) of the rear axle, the lookahead ℓd>0\ell_d > 0 in meters and the wheelbase LL in meters.

Output. A steering angle δ\delta in radians for the next control period.

How it differs from close pages. The Stanley controller uses the front axle, the closest point instead of a point ahead, and the heading error of the path; it reacts more directly to the cross-track error and needs a positive speed. Model predictive control optimizes the steering over a horizon with a model and constraints, at a much higher cost per step. A PID controller on the cross-track error has no geometry and needs tuning of three gains. The dynamic window approach chooses velocities to avoid obstacles and does not follow a given path.

The algorithm

  1. PURE-PURSUIT(path, (x, y, θ), ℓd, L, δmax)
  2. 1(i, u) ← the point of the path closest to (x, y), searching from the last index onward
  3. 2g ← the first point of the path after (i, u) at distance ℓd from (x, y), or the last point of the path
  4. 3(xg, yg) ← g in the vehicle frame: xg = cos θ (gx - x) + sin θ (gy - y), yg = -sin θ (gx - x) + cos θ (gy - y)
  5. 4κ ← 2 yg / (xg^2 + yg^2)
  6. 5δ ← clamp(arctan(κ L), -δmax, δmax)
  7. 6apply δ for one control period dt
  8. 7the vehicle moves; repeat from L1 at the next period

Line L2 intersects the circle of radius ℓd\ell_d around the rear axle with each segment, from the closest point onward, and takes the exit point of the circle on the first segment that has one. Searching only from the last closest index stops the vehicle from jumping back to an earlier part of a path that crosses itself. Line L4 is the curvature of the arc of Lemma 1 of the proof; when the goal point is at distance ℓd\ell_d, it equals 2sin⁡α/ℓd2\sin\alpha/\ell_d. In the implementation, line L7 integrates the model with the classical Runge-Kutta method and δ\delta held constant, so the simulation is exact to about 10−910^{-9} m and the only error is the zero-order hold of the controller.

Example

Let L=2.5L = 2.5 m, v=5v = 5 m/s and ℓd=5\ell_d = 5 m. The path is the xx axis, and the car starts at (0,1,0)(0, 1, 0): 1 m to the left of the path, with the heading of the path.

Line L1 gives the closest point (0,0)(0, 0). Line L2 gives the goal point (52−12,0)=(4.8990,0)(\sqrt{5^2 - 1^2}, 0) = (4.8990, 0). In the vehicle frame it is (4.8990,−1)(4.8990, -1), so line L4 gives κ=2⋅(−1)/25=−0.08\kappa = 2 \cdot (-1)/25 = -0.08 1/m, a right turn of radius 12.5 m. Line L5 gives δ=arctan⁡(−0.2)=−0.1974\delta = \arctan(-0.2) = -0.1974 rad, which is −11.3-11.3 degrees.

For a small error, the linear analysis of the proof gives e(t)=e0e−at(cos⁡at+sin⁡at)e(t) = e_0 e^{-at}(\cos at + \sin at) with a=v/ℓd=1a = v/\ell_d = 1 1/s. The error crosses zero at t=3π/(4a)=2.356t = 3\pi/(4a) = 2.356 s and reaches its least value −e0e−π=−0.0432-e_0 e^{-\pi} = -0.0432 m at t=π/a=3.14t = \pi/a = 3.14 s, an overshoot of 4.3%4.3\%. Measured: the simulation with e0=1e_0 = 1 m and a step of 1 ms crosses zero at 2.352 s, reaches −0.0434-0.0434 m at 3.137 s, and stays within 0.001 m of the linear formula for 6 s. Even at e0=1e_0 = 1 m, the error is small compared with ℓd\ell_d.

The lookahead sets the trade-off. Measured with the same start and a step of 10 ms:

ℓd\ell_d (m) Largest steering (rad) Time until the error stays within 5 cm (s) Overshoot (m)
2 0.896 0.81 0.044
5 0.197 2.05 0.043
10 0.050 4.13 0.043

The time scales with ℓd/v\ell_d/v and the overshoot does not change, as the theorem says: the damping ratio is 1/21/\sqrt{2} for every ℓd\ell_d.

Correctness

Pure pursuit is a heuristic, so its correctness is a set of narrower claims, proved in the proof. Lemma 1 is geometric: among circles tangent to the heading at the rear axle, exactly one passes through the goal point, and its curvature is 2yg/(xg2+yg2)2y_g/(x_g^2 + y_g^2). Lemma 2: the steering angle arctan⁡(κL)\arctan(\kappa L) makes the rear axle of the kinematic bicycle drive on that circle. Theorem 1 (local): on a straight path, the linearized closed loop is e¨+(2v/ℓd)e˙+(2v2/ℓd2)e=0\ddot e + (2v/\ell_d)\dot e + (2v^2/\ell_d^2)e = 0, with damping ratio 1/21/\sqrt{2}, so the path is locally exponentially stable with rate v/ℓdv/\ell_d. Theorem 2: on a circle of radius R>ℓd/2R > \ell_d/2, a car that starts on the circle stays on it with δ=arctan⁡(L/R)\delta = \arctan(L/R). There is no global guarantee.

Proof: Pure pursuit steers along the arc through the goal point and converges on a straight pathfull

Line numbers refer to the pseudocode on the pure pursuit page. The vehicle is the rear-axle kinematic bicycle model: x′=vcos⁡θx' = v\cos\theta, y′=vsin⁡θy' = v\sin\theta, θ′=(v/L)tan⁡δ\theta' = (v/L)\tan\delta. Pure pursuit is a heuristic for path tracking: it does not minimize a cost, and nothing guarantees that it tracks every path. This proof shows three things that do hold. Lemma 1 and Lemma 2 say that lines L3 to L5 do what the page says. Theorem 1 gives the local behavior on a straight path, and Theorem 2 shows that a circle is tracked with zero error.

Lemma 1 (the arc through the goal point). Let the goal point have coordinates (xg,yg)(x_g, y_g) in the vehicle frame of line L3, with D2=xg2+yg2>0D^2 = x_g^2 + y_g^2 > 0. The circle that passes through the rear axle, is tangent to the heading there and passes through the goal point has signed curvature κ=2yg/D2\kappa = 2y_g/D^2. If yg=0y_g = 0 and xg>0x_g > 0, the arc is the straight line, with κ=0\kappa = 0.

Proof. In the vehicle frame the rear axle is the origin and the heading is the xx axis. A circle tangent to the xx axis at the origin has its center at (0,r)(0, r) for a signed radius rr, with r>0r > 0 for a left turn. The goal point is on it when xg2+(yg−r)2=r2x_g^2 + (y_g - r)^2 = r^2, that is xg2+yg2=2rygx_g^2 + y_g^2 = 2 r y_g. If yg≠0y_g \ne 0, then r=D2/(2yg)r = D^2/(2y_g) and κ=1/r=2yg/D2\kappa = 1/r = 2y_g/D^2. If yg=0y_g = 0, no circle of finite radius passes through (xg,0)(x_g, 0) with xg≠0x_g \ne 0, and the line y=0y = 0 does. □\square

When the goal point is at the lookahead distance, D=ℓdD = \ell_d, and with α\alpha the angle of the goal point seen from the heading, yg=ℓdsin⁡αy_g = \ell_d\sin\alpha, so κ=2sin⁡α/ℓd\kappa = 2\sin\alpha/\ell_d, the form of Coulter's report. At the end of the path the goal point can be closer than ℓd\ell_d; the code uses DD, so Lemma 1 still holds.

Lemma 2 (the steering angle). With δ=arctan⁡(κL)\delta = \arctan(\kappa L) held constant, the rear axle moves on a circle of curvature κ\kappa.

Proof. The curvature of the rear axle path is θ′/∣(x′,y′)∣=(v/L)tan⁡δ/∣v∣=tan⁡(δ)/L\theta'/|(x', y')| = (v/L)\tan\delta / |v| = \tan(\delta)/L for v>0v > 0, which is κ\kappa when tan⁡δ=κL\tan\delta = \kappa L. □\square

So, if the steering acted at once and the vehicle were exactly the model, the rear axle would leave each pose along the arc of Lemma 1. The test test_the_arc_passes_through_the_goal_point checks Lemma 1 on 2000 random cases, and test_rk4_follows_the_exact_arc checks that the integrator of line L7 reproduces the circle of Lemma 2 to 10−910^{-9} m.

Theorem 1 (local behavior on a straight path). Let the path be the xx axis, let v>0v > 0 be constant and let the steering be updated continuously (the limit of lines L6 and L7 as the time step goes to 0). Let e=ye = y be the cross-track error of the rear axle and ψ=θ\psi = \theta the heading error. The linearization of the closed loop at e=ψ=0e = \psi = 0 is

e¨+2vℓd e˙+2v2ℓd2 e=0,\ddot e + \frac{2v}{\ell_d}\,\dot e + \frac{2v^2}{\ell_d^2}\,e = 0,

with natural frequency ωn=2 v/ℓd\omega_n = \sqrt{2}\,v/\ell_d and damping ratio ζ=1/2\zeta = 1/\sqrt{2}. The origin is locally exponentially stable, with rate v/ℓdv/\ell_d, and with e˙(0)=0\dot e(0) = 0 the linear solution is e(t)=e0 e−at(cos⁡at+sin⁡at)e(t) = e_0\, e^{-at}(\cos at + \sin at), a=v/ℓda = v/\ell_d.

Proof. Near the origin the closest point is (x,0)(x, 0) and the goal point is (x+ℓd2−e2,0)(x + \sqrt{\ell_d^2 - e^2}, 0). In the vehicle frame its lateral coordinate is yg=−ecos⁡ψ−ℓd2−e2 sin⁡ψy_g = -e\cos\psi - \sqrt{\ell_d^2 - e^2}\,\sin\psi, and D=ℓdD = \ell_d. To first order, yg=−e−ℓdψy_g = -e - \ell_d\psi, so line L4 gives κ=−2(e+ℓdψ)/ℓd2\kappa = -2(e + \ell_d\psi)/\ell_d^2. Lemma 2 gives ψ˙=vκ\dot\psi = v\kappa exactly, and e˙=vsin⁡ψ≈vψ\dot e = v\sin\psi \approx v\psi. Then e¨=vψ˙=−(2v2/ℓd2) e−(2v/ℓd) e˙\ddot e = v\dot\psi = -(2v^2/\ell_d^2)\,e - (2v/\ell_d)\,\dot e. The characteristic roots of r2+(2v/ℓd)r+2v2/ℓd2r^2 + (2v/\ell_d) r + 2v^2/\ell_d^2 are r=(−1±i) v/ℓdr = (-1 \pm i)\,v/\ell_d. Both have real part −v/ℓd<0-v/\ell_d < 0, so the linearization is exponentially stable, and by the Jacobian linearization principle the nonlinear origin is locally exponentially stable. Matching ωn2=2v2/ℓd2\omega_n^2 = 2v^2/\ell_d^2 and 2ζωn=2v/ℓd2\zeta\omega_n = 2v/\ell_d gives the stated values. The general solution is e−at(Acos⁡at+Bsin⁡at)e^{-at}(A\cos at + B\sin at); e(0)=e0e(0) = e_0 gives A=e0A = e_0, and e˙(0)=a(B−A)=0\dot e(0) = a(B - A) = 0 gives B=e0B = e_0. □\square

The test test_small_error_matches_the_linear_closed_form runs the code with v=5v = 5 m/s, ℓd=5\ell_d = 5 m, e0=1e_0 = 1 mm and a time step of 1 ms, and finds the simulated error within 0.1%0.1\% of e0e_0 of the formula for 6 s. The residual comes from the zero-order hold of line L6. The theorem is local. With a steering limit, a large heading error, or a path with corners, the closed loop is not covered by it.

Theorem 2 (a circle is tracked exactly). Let the path be a circle of radius RR with ℓd<2R\ell_d < 2R, and let the rear axle be on the circle with the heading of the circle, driving forward. Then line L5 returns δ=arctan⁡(L/R)\delta = \arctan(L/R), and the vehicle stays on the circle.

Proof. The goal point is on the circle, at distance ℓd\ell_d ahead, so it exists because ℓd<2R\ell_d < 2R. The path circle passes through the rear axle, is tangent to the heading there and passes through the goal point. By the uniqueness in Lemma 1 it is the arc of line L4, so κ=1/R\kappa = 1/R with the sign of the turn, and by Lemma 2 the vehicle moves on it. Its pose then stays on the circle with the same heading, so the same argument holds at every later time. □\square

The test test_tracks_a_circle_exactly drives 25 m on a polyline of 4000 chords around a circle of radius 10 m and finds a radial error below 10−510^{-5} m and a steering angle within 10−510^{-5} rad of arctan⁡(L/R)=0.2450\arctan(L/R) = 0.2450 rad.

What is not proved. Pure pursuit has no global guarantee. A steering limit can saturate line L5, a corner of the path can make the goal point jump, and a vehicle far from the path can choose a goal point behind a bend. The choice of ℓd\ell_d is a tuning decision, not a result; see the page.

Complexity

  • Time per step. Line L1 and line L2 scan the segments from the last closest index, so a step costs O(n)O(n) in the worst case for a path of nn vertices. A search window of a few segments around the last index makes it O(1)O(1) when the vehicle moves less than a segment per step. The rest is O(1)O(1). Measured: one call to the steering function takes 2 to 4 microseconds in CPython on a laptop, for a path of two points.
  • Space. O(1)O(1) beyond the path.

Properties

  • Geometric and model-light. The law uses only the geometry of the path and the wheelbase. It does not use the speed, the mass or the tires.
  • Corner cutting. On a curve, the goal point is inside the curve, and the vehicle cuts it. On a circle the cut is zero only when the vehicle is already on the circle (Theorem 2); at a corner of the path, the vehicle starts to turn up to ℓd\ell_d before the corner.
  • Smooth steering. The steering angle changes continuously with the pose when the goal point moves continuously along the path.
  • Speed scaling. With a fixed ℓd\ell_d, the closed loop gets faster with vv (rate v/ℓdv/\ell_d), and at high speed the steering becomes too aggressive for the real tires. A lookahead proportional to the speed, ℓd=kv\ell_d = k v, keeps the response in distance the same.
  • Not exact. The vehicle does not converge to the path in finite time, and on a curved path with a varying curvature there is a steady error.

Variants and improvements

  • Speed-dependent lookahead. ℓd=kv\ell_d = k v, clamped to a range. Snider's report uses this form with a lower and an upper limit, and tunes kk on the vehicle. The value of kk is chosen by experiment, not derived.
  • Adaptive pure pursuit. Change ℓd\ell_d with the curvature of the path ahead: shorter on tight curves, longer on straight parts.
  • Regulated pure pursuit. Reduce the speed when the curvature of the arc is large or an obstacle is near. The ROS 2 Nav2 stack uses a variant of this kind.
  • Vector pursuit. Also matches the heading of the path at the goal point, with screw theory.
  • Front-axle or center reference. Using another reference point changes the arc and the steering formula; the rear axle is the point where the kinematic bicycle has no side slip.

When to use it

Use it to follow a path at low or moderate speed, when the path is smooth and a simple, robust law is enough: field robots, warehouse vehicles, small cars and course tracking in research. It handles noisy poses well, because a long lookahead averages them. For high speed or tight tracking, use a controller with a dynamic model, such as the Stanley controller at moderate speed or model predictive control. For reverse motion, run the law on the mirrored vehicle.

Pitfalls

  • Lookahead tuning. Too short gives oscillation and, with a time delay in the steering actuator, instability. Too long cuts corners and reacts late. The theorem on the page assumes no delay and no steering limit.
  • The goal point jumps. When the vehicle is far from the path, the circle of radius ℓd\ell_d may not meet the path ahead, or may meet a later part of a path that loops. Searching only forward from the last closest index, and taking the end of the path as a fallback, are the usual fixes.
  • End of the path. Near the end, the goal point is the last point, closer than ℓd\ell_d. Use the actual distance in line L4, not ℓd\ell_d, or the curvature is wrong.
  • Wrong reference point. The formula δ=arctan⁡(κL)\delta = \arctan(\kappa L) holds for the rear axle. Measuring the pose at the center of mass or the front axle and using the same formula gives a biased steering.
  • Sign conventions. Left turns must have positive ygy_g, positive κ\kappa and positive δ\delta in the same frame; a sign error makes the vehicle turn away from the path.

Implementations

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

pure_pursuit.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/pure-pursuit/. The authors are listed in prov/RC03NN.jsonl.

"""Pure pursuit path tracking (Wallace et al. 1985, Amidi 1990, Coulter 1992).

The vehicle is the kinematic bicycle model with its reference point at the center of the
rear axle: x' = v cos(theta), y' = v sin(theta), theta' = v tan(delta) / L, with the
wheelbase L in meters and the steering angle delta in radians. The path is a polyline.

At each step the controller picks the goal point: the first point of the path, ahead of
the point closest to the rear axle, at distance ld (the lookahead) from the rear axle.
It steers along the circle arc that starts at the rear axle, is tangent to the heading
and passes through the goal point. That arc has curvature kappa = 2 y_g / ld^2, where
y_g is the lateral coordinate of the goal point in the vehicle frame, and the steering
angle is delta = atan(kappa L).
"""

import math


def wrap(a):
    return (a + math.pi) % (2.0 * math.pi) - math.pi


def closest_on_path(path, p, start):
    """Index i and parameter u in [0, 1] of the point of segment i closest to p,
    searching the segments from index start onward."""
    best = (math.inf, start, 0.0)
    for i in range(start, len(path) - 1):
        ax, ay = path[i]
        bx, by = path[i + 1]
        dx, dy = bx - ax, by - ay
        den = dx * dx + dy * dy
        u = 0.0 if den == 0.0 else max(0.0, min(1.0, ((p[0] - ax) * dx + (p[1] - ay) * dy) / den))
        qx, qy = ax + u * dx, ay + u * dy
        d2 = (qx - p[0]) ** 2 + (qy - p[1]) ** 2
        if d2 < best[0]:
            best = (d2, i, u)
    return best[1], best[2]


def goal_point(path, p, i, u, ld):
    """First point of the path after (i, u) at distance ld from p, or the last point."""
    for j in range(i, len(path) - 1):
        ax, ay = path[j]
        bx, by = path[j + 1]
        dx, dy = bx - ax, by - ay
        fx, fy = ax - p[0], ay - p[1]
        a = dx * dx + dy * dy
        if a == 0.0:
            continue
        b = 2.0 * (fx * dx + fy * dy)
        c = fx * fx + fy * fy - ld * ld
        disc = b * b - 4.0 * a * c
        if disc < 0.0:
            continue
        s = (-b + math.sqrt(disc)) / (2.0 * a)  # the exit point of the circle
        lo = u if j == i else 0.0
        if lo <= s <= 1.0:
            return ax + s * dx, ay + s * dy
    return path[-1]


class PurePursuit:
    """Pure pursuit controller for a polyline path."""

    def __init__(self, path, lookahead, wheelbase, max_steer=math.pi / 2.0):
        self.path = [tuple(p) for p in path]
        self.ld = lookahead
        self.L = wheelbase
        self.max_steer = max_steer
        self.index = 0
        self.goal = None
        self.kappa = 0.0

    def steer(self, pose):
        """Steering angle for the rear axle pose (x, y, theta)."""
        x, y, th = pose
        i, u = closest_on_path(self.path, (x, y), self.index)  # @line L1
        self.index = i  # @line L1
        gx, gy = goal_point(self.path, (x, y), i, u, self.ld)  # @line L2
        self.goal = (gx, gy)
        dx, dy = gx - x, gy - y  # @line L3
        xv = math.cos(th) * dx + math.sin(th) * dy  # @line L3
        yv = -math.sin(th) * dx + math.cos(th) * dy  # @line L3
        d2 = xv * xv + yv * yv
        self.kappa = 0.0 if d2 == 0.0 else 2.0 * yv / d2  # @line L4
        delta = math.atan(self.kappa * self.L)  # @line L5
        return max(-self.max_steer, min(self.max_steer, delta))  # @line L5


def bicycle_rhs(state, v, delta, wheelbase):
    x, y, th = state
    return v * math.cos(th), v * math.sin(th), v * math.tan(delta) / wheelbase


def rk4_step(state, v, delta, wheelbase, dt):
    """One RK4 step of the rear-axle kinematic bicycle with v and delta held constant."""
    def add(s, k, h):
        return s[0] + h * k[0], s[1] + h * k[1], s[2] + h * k[2]

    k1 = bicycle_rhs(state, v, delta, wheelbase)
    k2 = bicycle_rhs(add(state, k1, dt / 2.0), v, delta, wheelbase)
    k3 = bicycle_rhs(add(state, k2, dt / 2.0), v, delta, wheelbase)
    k4 = bicycle_rhs(add(state, k3, dt), v, delta, wheelbase)
    return tuple(state[j] + dt / 6.0 * (k1[j] + 2.0 * k2[j] + 2.0 * k3[j] + k4[j]) for j in range(3))


def simulate(controller, pose, v, dt, steps):
    """Closed loop: compute the steering, hold it for dt, integrate with RK4."""
    poses = [tuple(pose)]
    steers = []
    for _ in range(steps):  # @line L6
        delta = controller.steer(pose)  # @line L6
        pose = rk4_step(pose, v, delta, controller.L, dt)  # @line L7
        poses.append(pose)
        steers.append(delta)
    return poses, steers
test_pure_pursuit.py 97 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/pure-pursuit/. The authors are listed in prov/RC03NN.jsonl.

import math
import random

from pure_pursuit import PurePursuit, goal_point, closest_on_path, rk4_step, simulate, wrap


def test_the_arc_passes_through_the_goal_point():
    # The arc of curvature kappa, tangent to the heading at the rear axle, has its center at
    # distance 1 / kappa to the left of the vehicle. The goal point must lie on that circle.
    rng = random.Random(1)
    for _ in range(2000):
        path = [(rng.uniform(-20, 20), rng.uniform(-20, 20)) for _ in range(rng.randint(2, 6))]
        pose = (rng.uniform(-20, 20), rng.uniform(-20, 20), rng.uniform(-math.pi, math.pi))
        pp = PurePursuit(path, rng.uniform(0.5, 10.0), 2.5)
        pp.steer(pose)
        gx, gy = pp.goal
        if abs(pp.kappa) < 1e-9:
            # A straight line: the goal point is on the heading line.
            assert abs(-math.sin(pose[2]) * (gx - pose[0]) + math.cos(pose[2]) * (gy - pose[1])) < 1e-6
            continue
        r = 1.0 / pp.kappa
        cx, cy = pose[0] - r * math.sin(pose[2]), pose[1] + r * math.cos(pose[2])
        assert abs(math.hypot(gx - cx, gy - cy) - abs(r)) < 1e-9 * max(1.0, abs(r))


def test_goal_point_is_at_the_lookahead_distance():
    rng = random.Random(2)
    for _ in range(2000):
        path = [(rng.uniform(-20, 20), rng.uniform(-20, 20)) for _ in range(rng.randint(2, 6))]
        p = (rng.uniform(-20, 20), rng.uniform(-20, 20))
        ld = rng.uniform(0.5, 10.0)
        i, u = closest_on_path(path, p, 0)
        g = goal_point(path, p, i, u, ld)
        if g != path[-1]:
            assert abs(math.hypot(g[0] - p[0], g[1] - p[1]) - ld) < 1e-9


def test_straight_line_convergence():
    # Start 1 m to the left of the x axis and 0.3 rad off the heading.
    path = [(-10.0, 0.0), (500.0, 0.0)]
    pp = PurePursuit(path, 5.0, 2.5)
    poses, steers = simulate(pp, (0.0, 1.0, 0.3), 5.0, 0.02, 1500)
    x, y, th = poses[-1]
    assert abs(y) < 1e-6 and abs(wrap(th)) < 1e-6
    assert abs(steers[-1]) < 1e-6


def test_small_error_matches_the_linear_closed_form():
    # Linearized on a straight line: e'' + (2 v / ld) e' + (2 v^2 / ld^2) e = 0, so with
    # a = v / ld and zero heading error at t = 0, e(t) = e0 exp(-a t) (cos a t + sin a t).
    v, ld, e0, dt = 5.0, 5.0, 1e-3, 0.001
    a = v / ld
    pp = PurePursuit([(-10.0, 0.0), (500.0, 0.0)], ld, 2.5)
    poses, _ = simulate(pp, (0.0, e0, 0.0), v, dt, 6000)
    worst = 0.0
    for k in range(0, 6001, 50):
        t = k * dt
        lin = e0 * math.exp(-a * t) * (math.cos(a * t) + math.sin(a * t))
        worst = max(worst, abs(poses[k][1] - lin))
    # The steering is held for dt, which acts like a delay of dt / 2.
    assert worst < 1e-3 * e0


def test_tracks_a_circle_exactly():
    # On a circle of radius R the goal point stays on the circle, the arc through it is the
    # circle itself, and the steering is atan(L / R).
    R, L, n = 10.0, 2.5, 4000
    path = [(R * math.sin(2 * math.pi * k / n), R - R * math.cos(2 * math.pi * k / n)) for k in range(n + 1)]
    pp = PurePursuit(path, 4.0, L)
    poses, steers = simulate(pp, (0.0, 0.0, 0.0), 2.0, 0.05, 250)
    for (x, y, _), d in zip(poses, steers):
        # The polyline has 4000 chords, so the circle is exact to about 1e-5 m.
        assert abs(math.hypot(x, y - R) - R) < 1e-5
        assert abs(d - math.atan(L / R)) < 1e-5


def test_steering_limit_is_respected():
    rng = random.Random(3)
    for _ in range(500):
        path = [(rng.uniform(-5, 5), rng.uniform(-5, 5)) for _ in range(3)]
        pp = PurePursuit(path, rng.uniform(0.5, 3.0), 2.5, max_steer=0.5)
        pose = (rng.uniform(-5, 5), rng.uniform(-5, 5), rng.uniform(-math.pi, math.pi))
        assert -0.5 <= pp.steer(pose) <= 0.5


def test_rk4_follows_the_exact_arc():
    # With a constant steering angle the rear axle moves on a circle of radius L / tan(delta).
    L, delta, v, dt = 2.5, 0.3, 3.0, 0.01
    q = (0.0, 0.0, 0.0)
    for _ in range(300):
        q = rk4_step(q, v, delta, L, dt)
    r = L / math.tan(delta)
    th = v * 3.0 / r
    assert abs(q[0] - r * math.sin(th)) < 1e-9 and abs(q[1] - r * (1 - math.cos(th))) < 1e-9

History

Pure pursuit came from the road-following work at Carnegie Mellon University in the 1980s. Wallace, Stentz, Thorpe, Moravec, Whittaker and Kanade described an early form in 1985, and Amidi compared it with other path trackers in his report of 1990. Coulter's report of 1992 wrote down the geometry of the arc and the implementation steps, and most later code follows it. Snider's report of 2009 described pure pursuit, the Stanley controller and other steering laws for cars side by side, with their tuning.

References

  1. Implementation of the Pure Pursuit Path Tracking Algorithm, the whole report (the curvature of the arc through the goal point and the steps of the implementation)
  2. Automatic Steering Methods for Autonomous Automobile Path Tracking, the section on pure pursuit (the law kappa = 2 sin(alpha) / ld, the steering angle of the bicycle model, and a lookahead proportional to speed with a lower and an upper limit)

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.worst
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • at least three sources
  • a scene preset for the example