A planner hands the robot a polyline. Driven as is, it means two full stops and two spins on the spot: 19.7 s. Draw a smooth x(t) and y(t) through the same corridor instead. The speed, heading and turn rate then fall out by algebra. The robot glides it in 17 s inside the same limits.
Pick a plan and watch the speed trace under the map. Then stretch the time T and find where the smooth plan first fits under the speed limit. After that we build, piece by piece, the cubic, the flatness map, the limits and the replanning loop.
You need a derivative, a 2 by 2 linear system and a little Python. The unicycle model comes from Lesson 2; we restate it here.
Lesson 5 of 15 in our follow-along of Stanford AA 274A, Principles of Robot Autonomy I. Every idea is re-taught here in our own words and devices. Next: Lesson 6, Trajectory Tracking.
Illustrative toy on an 8 by 5 m floor. Limits: speed 0.5 m/s, turn rate 1.0 rad/s. The polyline robot drives at the speed limit and spins at the turn limit, with instant starts and stops. The flat plan is two cubics through the middle of the polyline.
Chapter 0
Round a corner at speed and see what turn rate it asks of the wheels
In Lessons 3 and 4, A* and RRT searched a map and returned a route. Look at what they return: a list of waypoints, points joined by straight lines. The line threads between the obstacles. Now try to drive it.
At each corner the direction changes at once. To turn at a point while moving, the robot must turn infinitely fast. Its motors cannot. So it must stop, spin on the spot, and start again. The polyline also says nothing about time: where to be at second 3, and how fast.
The robot turns 90° on an arc of radius r. Shrink r toward a sharp corner, or raise the speed, and watch the turn rate it needs against the 1.0 rad/s limit.
Illustrative toy. The limit of 1.0 rad/s is a typical value for a small wheeled robot.
Why does a tight corner need a fast turn? Call the forward speed V, in metres per second. Call the radius of the arc r, in metres. Call the turn rate, how fast the heading changes, ω (omega), in radians per second.
Omega equals V divided by r.
Divide the speed V by the radius r. The result omega is how fast the robot must turn to stay on the arc.
At V = 0.5 m/s and r = 0.5 m, ω = 1.0 rad/s: right at the limit. At r = 0.25 m, ω = 2.0 rad/s: twice the limit. At r = 0, ω has no finite value.
This lesson is about trajectory generation: turn waypoints into a smooth, timed motion the robot can drive. It gives a speed and a turn rate at every instant. It sits between the planner and the controller.
The symptom is a robot that jerks to a halt at every waypoint. To measure it, log the commanded turn rate at each corner and compare it with the limit. The fix is a smooth trajectory with a finite radius everywhere.
Chapter 1
Run two robots along one path on two different clocks
A path is a shape on the floor: a set of positions with no time attached. It answers "where", never "when". A trajectory is a path plus a clock: it says where the robot is at every instant. A path is a photo of the route; a trajectory is the film.
Both robots follow the same warm path in T = 10 s. Drag the time slider and compare where each one is, and how fast it moves.
Illustrative toy. The path is 5 m long. The even clock jumps from 0 to 0.5 m/s at the start; the smooth clock starts and ends at rest.
Write the path as a function of a progress number. Call the progress s: 0 at the start, 1 at the end. Call the path p: it maps s to a pose on the floor. Call the timing law, the rule that says how far along you are at time t, s(t). The trajectory ξ (xi) chains the two.
Xi of t equals p of s of t.
Read the clock t. Ask the timing law s how far along you should be. Ask the path p where that progress is on the floor. That pose is xi of t.
A straight 5 m path, T = 10 s, t = 4 s. The even clock gives s = 4/10 = 0.4, so x = 2.0 m. The smooth clock s = 3(t/T)² − 2(t/T)³ gives s = 0.352, so x = 1.76 m. Same path, different places.
Because ξ(t) is a function of time, it has a derivative: the velocity. That is what the motors need. Its boundary conditions, the values it must take at the two ends, pin it down.
| Boundary condition | Symbol | Example |
|---|---|---|
| Start pose | x0, y0, θ0 | (0, 0), facing down |
| Start speed | V0 | 0.5 m/s |
| Goal pose | xf, yf, θf | (5, 5), facing down |
| Goal speed | Vf | 0.5 m/s |
| Total time | T | 15 s |
The symptom is a controller that lurches at the hand-off from the last motion. To measure it, compare the start velocity of the new trajectory with the robot's real velocity. The fix is to put the real velocity in as the start boundary condition.
Chapter 2
Turn four boundary numbers into four coefficients with one linear solve
We need a smooth function of time that meets the boundary conditions. A polynomial in t does it. It is smooth, its derivative is easy, and it is linear in its coefficients. Each coordinate gets its own polynomial. Here is one coordinate, x.
One coordinate has four conditions: position and velocity at both ends. So use four unknown coefficients, a0 to a3. Call time t, from 0 to the total time T.
x of t equals a-zero, plus a-one t, plus a-two t squared, plus a-three t cubed.
Pick four numbers a-zero to a-three. Then the position at any time t is that sum. The velocity is its derivative: a-one + 2 a-two t + 3 a-three t².
With the solved coefficients below, a = [0, 1, 2, −0.75]: at t = 1 s, x = 0 + 1 + 2 − 0.75 = 2.25 m.
Work one by hand. Over T = 2 s, start at 0 moving at 1 m/s, and end at 4 m at rest. Put each condition in.
The same four conditions fit in one matrix equation. Call the column of coefficients a and the column of boundary values b. Call the 4 by 4 matrix M: it reads the polynomial and its derivative at t = 0 and t = T.
M times a equals b.
Each row of M reads the polynomial, or its velocity, at one end. Solve M a = b for a. M depends only on T, so a new goal is a new b and one more solve.
T = 2 and b = [0, 1, 4, 0] give back a = [0, 1, 2, −0.75], the hand result.
Set the four boundary numbers and T. The page solves M a = b and plots x(t) in warm and its velocity dashed.
Units: metres, metres per second, seconds. The solve is exact.
To also fix the acceleration at both ends, you need six conditions and so six coefficients: a quintic, degree 5. Each derivative you pin at each end costs one coefficient.
The symptom is a curve that overshoots the goal by metres. To measure it, plug the coefficients back in and check all four conditions. The fix is usually a wrong row in M; a short T with large speeds also causes big swings.
Chapter 3
Try to move a robot one metre to the side and meet the rule that forbids the short way
Fit a smooth cubic to x and a smooth cubic to y, and you have a smooth curve. Can the robot drive it? Not always. Recall the unicycle model from Lesson 2. Call the forward speed V, the heading θ (theta) and the turn rate ω. The robot moves with ẋ = V cos θ, ẏ = V sin θ, and θ changes at rate ω.
So the velocity always points along the heading. A wheel rolls forward or turns. It cannot slide along its axle. This rule has a name: the nonholonomic constraint, or no-slip rule. Call the rates of change of x and y ẋ and ẏ (x-dot and y-dot).
x-dot sine theta minus y-dot cos theta equals zero.
Take the velocity x-dot, y-dot. Measure how much of it points sideways to the heading theta. That part must be zero.
Facing east (θ = 0), a slide north at 0.5 m/s has ẋ = 0, ẏ = 0.5. The value is 0 × 0 − 0.5 × 1 = −0.5: forbidden.
The robot faces east and must end up offset to its left, still facing east. Choose the move. Then shorten the S-curve and watch its turn rate.
Illustrative toy. Offset 1 m. The S-curve is a cubic, y = d(3u² − 2u³) with u = x/L, driven at 0.5 m/s along x.
The S-curve passes the no-slip test because the heading follows the curve. But a tight S needs a fast turn. Over 2 m the peak turn rate is 0.75 rad/s; over 1 m it is 3.0 rad/s. So a good curve must keep the velocity along the heading and keep ω under its limit.
The symptom is a robot that cannot hold the planned path and skids or lags behind. To measure it, compute the no-slip value and ω along the curve. The fix is the next chapter: choose the curve so the dynamics hold by construction.
Chapter 4
Slide along a position curve and read the speed, heading and turn rate from its derivatives
Some systems have a special set of outputs. If you know those outputs and a few of their derivatives, you know every state and every input, by algebra alone. Such a system is differentially flat. The special outputs are its flat outputs. (The idea is due to Fliess and coworkers.)
For the unicycle, the flat output is the position (x, y). Draw any smooth x(t) and y(t), and the speed, heading and turn rate follow. No differential equation to integrate. Call the second derivatives, the accelerations, ẍ and ÿ (x-double-dot, y-double-dot).
V equals the square root of x-dot squared plus y-dot squared. Theta equals atan-two of y-dot and x-dot. Omega equals x-dot y-double-dot minus y-dot x-double-dot, all over x-dot squared plus y-dot squared.
Differentiate x and y once. The length of (x-dot, y-dot) is the speed V, and its direction is the heading theta. Differentiate once more. The sideways bend over V squared is the turn rate omega.
The curve x = 0.5t, y = 0.1t² − 0.011t³ at t = 2 s: ẋ = 0.5, ẏ = 0.268, ẍ = 0, ÿ = 0.068. V = 0.567 m/s, θ = 0.492 rad (28.2°), ω = 0.034 / 0.322 = 0.106 rad/s.
Where does each come from? Square ẋ = V cos θ and ẏ = V sin θ and add: cos² + sin² = 1 leaves V². Divide the two: V cancels and tan θ is left. Differentiate θ = atan2(ẏ, ẋ) and you get ω.
The warm curve is x(t), y(t). Drag the time. The teal arrow is the heading; the dashed circle has the radius V/ω of the turn at that point.
Illustrative curve, the same one as in the Code Lab below: x = 0.5t, y = 0.1t² − 0.011t³ for t from 0 to 6 s.
The symptom is a heading that flips by 180° on one side of the floor. To measure it, check the heading where ẋ is negative. The fix is atan2, not arctan of ẏ/ẋ: plain arctan only covers the right half-plane.
Chapter 5
Turn two poses into two cubics, then into speed, heading and turn rate
The recipe has three steps.
Step 1 runs the unicycle model in reverse. Call the start speed V0 and the start heading θ0.
x-dot at zero equals V-zero cos theta-zero, and y-dot at zero equals V-zero sine theta-zero.
Split the start speed V-zero along the start heading theta-zero. The two parts are the start velocities of x and y. Do the same at the goal.
V0 = 0.5 m/s facing straight down, θ0 = −π/2: cos = 0, sin = −1. So ẋ(0) = 0 and ẏ(0) = −0.5 m/s.
Take the spec from Chapter 1: (0, 0) to (5, 5) in T = 15 s, at 0.5 m/s, facing down at both ends. Each coordinate now has four conditions.
| Step | x | y |
|---|---|---|
| Conditions | 0, 0, 5, 0 | 0, −0.5, 5, −0.5 |
| Cubic | 0.06667t² − 0.002963t³ | −0.5t + 0.16667t² − 0.007407t³ |
| At t = 7.5 s | ẋ = 0.5, ẍ = 0 | ẏ = 0.75, ÿ = 0 |
At t = 7.5 s: V = √(0.25 + 0.5625) = 0.901 m/s, θ = atan2(0.75, 0.5) = 0.983 rad (56.3°), and ω = 0 because both accelerations are zero there. At t = 0, ω = (0 × 0.333 + 0.5 × 0.133)/0.25 = 0.267 rad/s.
The robot goes from (0, 0) to the yellow goal at (5, 5). Turn the end headings and the boundary speed, then drag the time to read V, θ and ω.
T = 15 s. The spec is of the kind the course's homework uses; the speeds and headings you set here are illustrative.
The symptom: the speed is not constant. It starts at 0.5, peaks at 0.901 m/s in the middle, and returns to 0.5. With a 0.5 m/s limit, this design breaks it. Its turn rate also peaks at 1.57 rad/s near t = 13.6 s. Measure V and ω at every sample. Chapter 6 gives the fix.
Chapter 6
Find the one point where the map fails, then slow the clock to meet the limits
Look at the flatness map again. The turn rate divides by ẋ² + ẏ², which is V². If the robot stops, V = 0: ω divides by zero, and atan2(0, 0) has no direction. This is the singularity of the map at V = 0. A robot at rest has no direction of motion.
So a flat segment must keep V > 0 everywhere, including its ends. To stop, end the segment at a small speed and hand over to a stop behaviour; start the next segment from a small speed.
Away from V = 0, the inputs must still respect the limits: here |V| ≤ 0.5 m/s and |ω| ≤ 1.0 rad/s. The fix is time scaling: keep the path, change the timing law. Take a 1D move from x0 to xf. Use the smooth clock s(t) = 3(t/T)² − 2(t/T)³, which starts and ends at rest. Its peak speed comes at t = T/2. Call it ẋmax.
x-dot-max equals three times x-f minus x-zero, over two T.
Take the distance x-f minus x-zero. Multiply by 3, divide by 2T. That is the top speed. Double T and the top speed halves; the path does not move.
5 m in T = 10 s: ẋmax = 15/20 = 0.75 m/s, over the 0.5 limit. In T = 15 s: 0.5 m/s, at the limit.
The path from Chapter 5 stays fixed. Choose a timing. With Stretch, drag the factor. Red marks a broken limit.
Toy re-timing. "Slow spots" takes, at each point, the smallest of three speeds: the original, 0.5 m/s, and 1.0 rad/s over the curvature. It ignores acceleration limits.
The device shows two cures. An even stretch by c = 1.80 brings the peak speed to 0.50 m/s and the peak turn to 0.87 rad/s, in 27.0 s. But it also slows the ends to 0.28 m/s, which breaks the spec. Slowing only where needed keeps the ends at 0.5 m/s and finishes in 20.1 s. It uses ω = V × curvature: on a tight bend, lower V lowers ω.
The symptom is a NaN or a huge ω at the last sample. To measure it, print V at both ends. The cause is an end speed of 0; give it a small positive speed.
Chapter 7
Choose which derivative to punish and watch the motion change
Flatness gives a feasible trajectory fast. Often we want the best one: the smoothest, or the cheapest in energy. That is trajectory optimization, also called optimal control. We choose the motion that makes a cost smallest, subject to the dynamics, the limits and the boundary conditions.
For smoothness, the cost punishes a derivative. Call the order of the derivative k: 2 is acceleration, 3 is jerk, 4 is snap. Call the cost J.
J equals the integral from zero to T of the k-th derivative of x, squared, d-t.
At each instant, square the k-th derivative of x. Add the squares over the whole motion. The trajectory with the smallest sum J is the smoothest in that derivative.
Move 1 m in T = 2 s, from rest to rest, with k = 2. The best is the cubic with acceleration 1.5 − 1.5t. J = 1.5 m²/s³, and the peak speed is 0.75 m/s.
A point moves 1 m in 2 s from rest to rest. Choose the cost. Watch the acceleration at the two ends.
Exact solutions: a cubic for k = 2, a quintic for k = 3, degree 7 for k = 4. For jerk and snap, the lower derivatives are also zero at both ends.
| Cost | Peak speed | Peak accel. | At the ends |
|---|---|---|---|
| Acceleration | 0.75 m/s | 1.50 m/s² | acceleration jumps from 0 to 1.5 |
| Jerk | 0.94 m/s | 1.44 m/s² | acceleration starts at 0 |
| Snap | 1.09 m/s | 1.88 m/s² | jerk starts at 0 too |
Why is this fast to solve? Write x(t) as a polynomial again. The motion is linear in the coefficients, so J is a quadratic in them. The boundary conditions are the linear equations M a = b. A quadratic cost with linear constraints is a quadratic program (QP): convex, with one best answer. This is how minimum-snap planners for drones work (see Trajectory Optimization for drones).
For general dynamics, solvers cut time into steps and optimize the states and inputs together. Shooting, collocation and iLQR do this; Optimal Control and Planning teaches them.
The symptom is a jolt when the robot starts. To measure it, look at the acceleration at t = 0: the cubic jumps to 1.5 m/s² at once. The fix is a higher k, which buys a zero start acceleration at the price of a higher peak speed.
Chapter 8
Drag two poses, drive the robot, and read its commands
Here is the hero's floor with every piece exposed. Drag the start and the goal. Drag the small dot on each to set its heading. The page fits two cubics through a middle waypoint, applies the flatness map, and plots V(t) and ω(t) against the limits.
Drag the teal start, the yellow goal, or a heading dot. Press Drive. Stretch T until both plots sit under their limits.
Illustrative toy on an 8 by 5 m floor. Limits: 0.5 m/s and 1.0 rad/s. The end speeds equal the chord length over T, so T rescales the clock and leaves the shape alone. The middle waypoint sits off the straight line. The turn rate jumps there: the two cubics share position and velocity, not acceleration.
Three things to check as you play. The robot's heading always lies along the curve: that is θ = atan2(ẏ, ẋ). The page computes the plots from the cubics' derivatives; it assumes nothing. And a turn the robot can make may still clip the pink obstacle; flatness only handles the dynamics.
The symptom is a plan that meets every limit and still hits the obstacle. To measure it, check the distance from each sample to each obstacle. The fix is to move the waypoint, or to add the obstacle as a constraint in an optimizer.
Chapter 9
Push a robot off its plan and compare one fixed plan with replanning
Our trajectory is open loop: a function of time and the start pose only. If a wheel slips or the floor tilts, the plan does not know. The robot follows its commands and drifts off.
Two patterns fix this.
A pink push moves the robot sideways at every step. Choose a mode, set the push, and press Run.
Illustrative toy: 40 steps of 0.25 s. The replanner fits a fresh flat cubic from the measured (blue) pose to the goal at each step and runs its first slice. It measures the pose exactly.
Work the drift. The push is 0.02 m on each of 40 steps. Open loop keeps every push: it ends near 40 × 0.02 = 0.8 m off. The replanner starts each new plan from where the robot is, so the next steps correct each push. It ends within a few centimetres.
Each MPC solve is still an open-loop plan. Replanning from the measured state is what closes the loop. The price is compute: one optimization every tick, often 10 to 100 times a second.
The symptom is a robot that ends off target even though every command was right. To measure it, log the gap between the plan and the measured pose over time. The fix is feedback: a tracker, or replanning.
Chapter 10
Keep the terms, the numbers, the code and the next steps in one place
A planner gives a path; the controller needs a trajectory. For the unicycle, fit smooth polynomials to x and y from the end poses. The flatness map then gives V, θ and ω by algebra. Keep V above 0, check the limits, slow the clock where needed, and let feedback keep the robot on the plan.
| Term | Meaning | Number to remember |
|---|---|---|
| Turn rate on an arc | ω = V / r | 0.5 m/s on r = 0.25 m: 2.0 rad/s |
| Trajectory | ξ(t) = p(s(t)), path plus clock | 5 m at t = 4 of 10 s: 2.0 m or 1.76 m |
| Cubic | 4 conditions, M a = b | a = [0, 1, 2, −0.75] |
| No-slip | ẋ sin θ − ẏ cos θ = 0 | sideways speed 0 |
| Flatness map | V, θ, ω from ẋ, ẏ, ẍ, ÿ | t = 7.5 s: 0.901 m/s, 56.3°, 0 rad/s |
| Singularity | V = 0: θ and ω undefined | keep end speeds > 0 |
| Time scaling | ẋmax = 3(xf − x0)/(2T) | 5 m in 15 s: 0.5 m/s |
| Smoothness cost | J = ∫(dkx/dtk)² dt, a QP | 1 m in 2 s, k = 2: J = 1.5 |
| Open-loop drift | every push stays in | 40 × 0.02 m = 0.8 m |
python import numpy as np def cubic(p0, v0, pf, vf, T): # M a = b for one coordinate M = np.array([[1,0,0,0],[0,1,0,0],[1,T,T**2,T**3],[0,1,2*T,3*T**2]], float) return np.linalg.solve(M, [p0, v0, pf, vf]) def flat_trajectory(start, goal, T, n=200): # poses are (x, y, theta, V), V > 0 (x0, y0, th0, V0), (xf, yf, thf, Vf) = start, goal ax = np.polynomial.Polynomial(cubic(x0, V0*np.cos(th0), xf, Vf*np.cos(thf), T)) ay = np.polynomial.Polynomial(cubic(y0, V0*np.sin(th0), yf, Vf*np.sin(thf), T)) t = np.linspace(0, T, n) xd, yd = ax.deriv()(t), ay.deriv()(t) xdd, ydd = ax.deriv(2)(t), ay.deriv(2)(t) V = np.hypot(xd, yd) # speed theta = np.arctan2(yd, xd) # heading omega = (xd*ydd - yd*xdd) / V**2 # turn rate return t, ax(t), ay(t), theta, V, omega t, x, y, th, V, om = flat_trajectory((0, 0, -np.pi/2, 0.5), (5, 5, -np.pi/2, 0.5), 15.0) print(V.max(), np.abs(om).max()) # 0.901 1.569: both over 0.5 and 1.0, so re-time
Based on Stanford AA 274A, Principles of Robot Autonomy I. This page re-teaches the course's fifth lesson in our own words, devices and toy numbers.
Now press Present or Teach and explain from memory: the cubic, the flatness map, the singularity and time scaling. Then go back to the floor, pick the polyline, and say why it takes 19.7 s.