Skip to main content

Motion Profiling

Harddifficulty

Prerequisites and a starting path

Read PID Control and Feedforward first. The math is the constant-acceleration formulas from physics class, like d=12at2d = \tfrac12 a t^2, and graphs made of straight pieces. Don't copy someone else's speed and acceleration limits. Measure safe ones on your own mechanism.

PID and feedforward decide what command to send right now. Motion profiling decides what the target should be right now. Instead of handing the controller the final position and hoping, you give it a target that moves at a pace the mechanism can keep up with.

A step command asks for infinite acceleration​

Tell a lift "go to the top" and the controller sees the whole error at once. The proportional term goes to full power immediately, the lift jerks, and the current spikes. Feedforward can't help either, because it works from the target's velocity, and a target that jumps doesn't have a sensible one. The lift either overshoots (high gain) or crawls (low gain). Jumping the target asks for an instant change in position, which would take infinite speed. Real mechanisms have a top speed and a top acceleration, so plan within them.

The trapezoidal profile​

The simplest profile that respects both limits is the trapezoid: accelerate at amaxa_\text{max} until you reach the cruise speed vmaxv_\text{max}, hold that speed, then slow down at amaxa_\text{max} so you arrive at the target stopped. Plot velocity against time and you get a trapezoid.

Try it with numbers first. A lift can accelerate at 20 in/s² and move at up to 10 in/s. Reaching 10 in/s from rest takes 10/20=0.510/20 = 0.5 s, and during that half second the lift averages 5 in/s, so it covers 2.5 in. Slowing down takes the same time and distance. For a 30 in move, that leaves 30−5=2530 - 5 = 25 in to cruise at 10 in/s, which takes 2.5 s. The whole move takes 0.5+2.5+0.5=3.50.5 + 2.5 + 0.5 = 3.5 s.

In general, the ramp up to cruise speed takes

tramp=vmaxamax,dramp=12amaxtramp2=vmax22amaxt_\text{ramp} = \frac{v_\text{max}}{a_\text{max}}, \qquad d_\text{ramp} = \tfrac12 a_\text{max} t_\text{ramp}^2 = \frac{v_\text{max}^2}{2 a_\text{max}}

and if the move is long enough to fit both ramps (2 dramp≤d2\,d_\text{ramp} \le d), the middle is spent cruising for

tcruise=d−2 drampvmaxt_\text{cruise} = \frac{d - 2\,d_\text{ramp}}{v_\text{max}}

where dd is the length of the move. If the move is too short to reach vmaxv_\text{max} at all, the trapezoid loses its flat top and becomes a triangle: speed up to a lower peak and immediately slow down again. The peak is vpeak=amax dv_\text{peak} = \sqrt{a_\text{max}\,d}. In the demo, shrink the distance or raise the cruise speed and watch the trapezoid turn into a triangle.

Live demoTrapezoidal motion profile — the move, replayed live
velocity setpointnormalized progress x/d (0–100%)cruise-speed cap
total time: 2.50 speak speed: 40 in/sshape: trapezoid

With the same lift limits, a 5 in move is the boundary case. Each full ramp needs 2.5 in, so there's nothing left to cruise: tacc=5/20=0.5 st_\text{acc}=\sqrt{5/20}=0.5\ \mathrm{s} and vpeak=10 in/sv_\text{peak}=10\ \mathrm{in/s}.

At every moment the profile gives a full target: position x(t)x(t), velocity v(t)v(t), and acceleration a(t)a(t). Send v(t)v(t) and a(t)a(t) to feedforward (kVv+kAak_V v + k_A a) so the voltage leads the motion, and send x(t)x(t) to PID so feedback only corrects the small tracking error.

Idea
The same idea, one loop at a time

The slowing-down half of the trapezoid obeys v=2adv = \sqrt{2 a d}, where dd is the distance left. You'll see that formula again in PID-to-Point, where it's used on the fly each loop to cap a drivetrain's speed instead of being planned ahead for the whole move.

Note
S-curves

A trapezoid's acceleration jumps straight between 00, +amax+a_\text{max}, and −amax-a_\text{max}. Those sudden changes (infinite jerk) can make a tall mechanism wobble. An S-curve profile also limits jerk, which rounds the corners at the cost of a slightly longer move. Start with a trapezoid and switch only if the wobble is a real problem.

Sample the profile each loop​

Build the profile once from your limits. Then, every loop, ask it for the target at the current time and hand that to your feedforward and PID.

FTCTrapezoidal profile: build once
private double sign, distance, aMax, tAcc, tFlat, vPeak, total;

// Built ONCE per move; sampled every loop afterwards.
void build(double d, double vMax, double a) {
if (!Double.isFinite(d) || vMax <= 0 || a <= 0) {
total = 0; // zero-length: sample() just holds
return;
}

sign = Math.signum(d); // plan in positive distance,
distance = Math.abs(d); // put the sign back when sampling
aMax = a;

double tRamp = vMax / aMax; // time to reach cruise speed
double dRamp = 0.5 * aMax * tRamp * tRamp; // distance used doing it

if (2 * dRamp >= distance) { // TRIANGLE: too short to ever cruise
tAcc = Math.sqrt(distance / aMax);
vPeak = aMax * tAcc;
tFlat = 0;
} else { // full TRAPEZOID
tAcc = tRamp;
vPeak = vMax;
tFlat = (distance - 2 * dRamp) / vMax;
}
total = 2 * tAcc + tFlat;
}
FTCTrapezoidal profile: sample every loop
// Target position, velocity and acceleration at time t.
ProfileState sample(double t) {
if (t >= total) return new ProfileState(sign * distance, 0, 0);
t = Math.max(0, t);

double a, v, x;
if (t < tAcc) { // accelerating
a = aMax;
v = aMax * t;
x = 0.5 * aMax * t * t;
} else if (t < tAcc + tFlat) { // cruising
a = 0;
v = vPeak;
x = 0.5 * aMax * tAcc * tAcc + vPeak * (t - tAcc);
} else { // decelerating
double td = t - tAcc - tFlat;
a = -aMax;
v = vPeak - aMax * td;
x = 0.5 * aMax * tAcc * tAcc + vPeak * tFlat
+ vPeak * td - 0.5 * aMax * td * td;
}
return new ProfileState(sign * x, sign * v, sign * a);
}
ExerciseDerive the triangle case

If the move is too short to reach vMax, the profile speeds up and then slows down with no cruise. Set "distance while speeding up + distance while slowing down = total distance" and solve for tAcc.

Show solution

Accelerating for tat_a at aa covers 12ata2\tfrac12 a t_a^2, and slowing down covers the same. So ata2=da t_a^2 = d and ta=d/at_a = \sqrt{d/a}. The peak speed ataa t_a comes out below vMax, which is why the code took this branch.

ExerciseCheck it by hand

Every x line is x0+v0t+12at2x_0 + v_0 t + \tfrac12 a t^2 for one phase, where x0x_0 is where the mechanism was when that phase began. Try distance = 100, vMax = 10, aMax = 5: compute tRamp and dRamp, decide triangle or trapezoid, predict total, and check that sample(total / 2) lands at half the distance.

Show solution

tRamp = 2 s, dRamp = 10, and 2 * dRamp = 20 < 100, so it's a full trapezoid: tAcc = 2, vPeak = 10, tFlat = 8, total = 12 s. At t = 6 it's cruising, and x=12(5)(22)+10(6−2)=10+40=50x = \tfrac12(5)(2^2) + 10(6-2) = 10 + 40 = 50, exactly half. Speeding up and slowing down are mirror images of each other, so halfway through the time is halfway through the distance.

FTCFollowing the profile with feedforward + PID
void loop() {
double t = timer.seconds();
ProfileState target = profile.sample(t);

double ff = kS * Math.signum(target.v) + kV * target.v + kA * target.a;
double fb = pid(measuredPosition, target.x); // correct what ff missed

motor.setPower((ff + fb) / batteryVoltage());

boolean done = t >= profile.duration()
&& Math.abs(finalTarget - measuredPosition) < TOL;
}

The feedforward comes from the profile, not from the measurement. It's a prediction of the voltage this motion needs, so the PID only has to clean up the difference, and its gains can be much smaller than they would be on their own.

ExerciseWhy done checks two things

done checks both the clock and the position. When would the profile be finished but the mechanism not there yet, and which check catches it?

Show solution

Whenever the mechanism can't keep up: a sagging battery, an extra game piece, a jam. t >= duration() only says the plan is over, and the mechanism can still be a few centimeters short. The position check catches that. The clock check catches the opposite case, where the mechanism passes through the target early and briefly reads inside TOL while still moving fast.

Plan with limits the mechanism can really reach

A profile only works if the mechanism can follow it. Use about 80% of the measured top speed and acceleration, so a tired battery or an extra game piece doesn't make the plan impossible. Once the mechanism falls behind, the tracking error grows fast and feedback maxes out.

Profiles in path-following libraries

A drivetrain trajectory is a motion profile run along a path: the path says where to go, and the profile says how fast to move along it. Road Runner works this way, with feedforward and PID on top, which is the same stack as this module in 2-D. Pedro Pathing takes a different approach and decides its speed from where the robot actually is on the path each loop (see How Does Pedro Pathing Work?).

Sources & references