Splines
Harddifficulty
Read PID-to-Point first. This lesson uses polynomials and derivatives (rates of change, as in From Math to a Loop). You can use the Bézier viewer without any of the derivations, and solving for a many-point spline is best left to a library.
PID-to-point drives to one target at a time. A real autonomous goes through many waypoints, and the path between them has to be drivable: smooth in position, and also smooth in how sharply it bends.
The robot jerks at every waypoint
Picture a straight line that joins a circle of radius 20 in, heading in the same direction. There's no corner, so the path looks smooth. But on the straight the robot turns at zero degrees per inch, and the moment it enters the circle it has to turn at a steady rate. That sudden change in how tightly the path bends, its curvature, means the robot has to change its turning instantly at the join. It can't, so it jerks and drifts off the path right where the pieces meet.
So a good path description has to control curvature, not just position and direction.
How smooth is smooth enough?
"Smoothness" is graded by how many derivatives match across a join:
| Class | Matches across the join | What stays continuous | The drivetrain feels |
|---|---|---|---|
| C⁰ | position | the path connects | a corner — a kink |
| C¹ | + first derivative | heading (tangent direction) | a sudden curvature change — a jerk |
| C² | + second derivative | second derivative; curvature on a regular curve | smooth, followable steering |
C² means position, first derivative, and second derivative all match at each join. As long as the path
never stops dead (its first derivative isn't zero), that makes the curvature continuous, so the follower
never has to change its turning instantly. Freya Holmér explains the same C⁰, C¹, C² idea visually and then goes further, into how Bézier,
Hermite, and B-splines relate. It's long, and worth watching.
Bézier curves
A cubic Bézier curve is built from four control points, through . The curve starts at and ends at , and the middle two act like handles that pull it. At each value of from 0 to 1, the point on the curve is a weighted average of the four control points:
isn't time or distance. It just runs from 0 at the start of the segment to 1 at the end. At the weights are 0.125, 0.375, 0.375, and 0.125. They're never negative and always add up to 1, so the curve stays inside the shape formed by the control points. It only touches and ; the handles and pull without touching, and they set the direction at each end:
That's why Béziers are nice to draw with: drag a handle and the curve leans toward it. You can also find the point at any by de Casteljau's algorithm, which repeatedly takes the point a fraction of the way along each line between control points. Scrub in the demo to watch it, and drag the handles to see how they steer the ends:
Drag a point, or focus it and use the arrow keys; hold Shift for a larger step.
The trouble comes when you chain Béziers together. For C¹, the next segment's first handle has to mirror the previous one: . For C², the second handle is pinned too: . So in a chain, smoothness takes away exactly the handles that made Béziers easy to shape.
Quintic Hermite splines
A different approach: instead of handles, say what the curve should be doing at each waypoint, its position, velocity, and acceleration, and find a polynomial that does all of it. Since you pin the positions, the curve goes through every waypoint. Here are the same four points used both ways:
Drag a point, or focus it and use the arrow keys; hold Shift for a larger step.
The green curve goes through all four points and the blue one only through the ends. For a drivetrain you usually want green: "go through these poses," not "go near them."
Matching position, velocity, and acceleration at both ends of a segment is six conditions. A polynomial with six coefficients can meet six conditions, and that's a degree-5, or quintic, polynomial, one for and one for :
At every term with a in it vanishes, so the start conditions give the first three coefficients directly:
The is there because the second derivative of is . The end conditions at give the other three:
Chaining segments is then easy: give each waypoint one velocity and one acceleration, and use them as the end of one segment and the start of the next. Position, velocity, and acceleration all match at the join, which is C², with no handles lost. Do the same for and .
Natural cubic splines
Quintic Hermite asks you for the velocity and acceleration at every waypoint. If you only have the points, a natural cubic spline uses one cubic per gap and works out the slopes itself, by requiring the first and second derivatives to match at every interior waypoint. That leaves two conditions missing, and the "natural" choice is to make the second derivative zero at both ends, so the path leaves each end straight, like a flexible drafting strip held only at the points.
All those matching conditions turn into one system of equations with a simple banded shape (tridiagonal), which the Thomas algorithm solves quickly without a general matrix inverse.
Drag the waypoints below. The spline always passes through every point, the curvature comb stays continuous, and it tapers to zero at the ends.
Drag a waypoint, or focus it and use the arrow keys; hold Shift for a larger step.
Compared with quintic Hermite, a natural cubic is already C² without you supplying any slopes. What you
give up is control: the ends are forced straight, and you can't choose the velocity or acceleration at
each waypoint. The quintic's extra coefficients are what let you set those. (A path that doubles back
can't be written as as a function of , so it's fit as two splines, and .)
Whichever kind you use, what a pure-pursuit or feedforward follower actually needs is the curvature at each point, one over the radius of the circle that best fits the path there:
The primes are derivatives with respect to . Check it on a circle of radius 10, , : then and , so , one over the radius. The power is what makes the answer not depend on how fast moves along the curve.
Bézier when someone draws the path by eye. The handles make a good editor, and Pedro Pathing's tools
use them. Quintic Hermite when you have exact poses to hit. Road Runner's splineTo uses quintics.
Natural cubic when you only have points and want the simplest smooth curve through them, and
straight ends are fine. Béziers and Hermite splines can be converted: a cubic Bézier is a cubic Hermite
with end slopes and , so an editor can show handles while the math uses Hermite
form.
Evaluating the curves
Either way, it comes down to evaluating a polynomial, and its derivatives, for each coordinate.
// Four control points, t running 0 -> 1. Returns one coordinate at t.
double bezier(double p0, double p1, double p2, double p3, double t) {
double u = 1 - t;
return u * u * u * p0
+ 3 * u * u * t * p1
+ 3 * u * t * t * p2
+ t * t * t * p3;
}
private double c0, c1, c2, c3, c4, c5;
// Position, velocity and acceleration pinned at both ends: six constraints,
// so six coefficients.
void build(double p0, double v0, double a0,
double p1, double v1, double a1) {
c0 = p0;
c1 = v0;
c2 = a0 / 2;
double d0 = p1 - c0 - c1 - c2; // what c3..c5 must absorb at t = 1
double d1 = v1 - c1 - 2 * c2;
double d2 = a1 - 2 * c2;
c3 = 10 * d0 - 4 * d1 + 0.5 * d2;
c4 = -15 * d0 + 7 * d1 - 1.0 * d2;
c5 = 6 * d0 - 3 * d1 + 0.5 * d2;
}
double pos(double t) {
return c0 + c1 * t + c2 * t * t
+ c3 * t * t * t + c4 * t * t * t * t + c5 * t * t * t * t * t;
}
double vel(double t) { ... } // the derivative of pos
double acc(double t) { ... } // the derivative of vel
Check that the four weights always add up to 1. Try , , and by hand, then see if
you can show it for every . At the weights are and the curve is at Show solution
p0. At they're and it's
at p3. At they're . For every : the four weights are the
terms you get when you expand , and , so they add up to .
Write out pos(t), vel(t), and acc(t), set , and read off , , and . Then set
in all three to get three equations in , , and . Solving those is where the ,
, and come from.
A cubic segment has four coefficients, enough to match position and velocity at both ends. If each
segment is built that way, what doesn't match at the joins, and what does the robot feel? Acceleration. It can jump at every join. Acceleration takes force, so a jump is a sudden change in wheel
torque: the robot lurches at each waypoint, the wheels can slip, and that slip throws off the odometry
the follower depends on. Matching acceleration too takes six conditions, so a quintic. (A natural cubic
spline gets matching accelerations a different way, by solving for the slopes instead of letting you
pick them.)Show solution
Build one curve for and one for per segment. For a chain of quintics, sharing each waypoint's velocity and acceleration between the two segments that meet there makes the whole path C².
For a natural cubic, solve the system once, then evaluate whichever piece the parameter falls in, , once for and once for .
Robots don't usually follow the curve as a formula. You turn it into a list of evenly spaced points (see the warning below) and give that to a follower. Planning the speed at each point is covered in Pure Pursuit → Following a spline at speed.
Equal steps in are not equal distances along the curve. They tend to be spread out on straight parts and bunched up in turns. Before a follower uses the path, resample it by distance: add up along the curve to measure length, and place points at equal lengths. Otherwise speed control surges and stalls around curves.
The curve and follower techniques on this page are adapted, with attribution, from these references and open-source repositories:
- Cubic Bézier path (Bernstein form, arc-length parametrization) —
petrustoica/CenterStage (
teamcode/pathfollower,CubicBezierPath) and SimplicityFTC-Quickstart (path/BezierPath). - Natural cubic spline interpolation (continuity conditions, natural boundary, the tridiagonal moment system) — Richard L. Burden, J. Douglas Faires, and Annette M. Burden, Numerical Analysis (Cengage Learning), the "Natural Cubic Spline" algorithm (§3.5), which builds this same tridiagonal system in the second derivatives and solves it by elimination specialised to a tridiagonal matrix (equivalent to the Thomas algorithm); see also John D. Cook, Tridiagonal systems, determinants & natural cubic splines (johndcook.com) for a freely available worked derivation of the same moment system.
The code here is an independent reimplementation of these ideas, not a copy of any one source.