Motion Profiling
Harddifficulty
Read PID Control and Feedforward first. The math is the constant-acceleration formulas from physics class, like , 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 until you reach the cruise speed , hold that speed, then slow down at 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 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 in to cruise at 10 in/s, which takes 2.5 s. The whole move takes s.
In general, the ramp up to cruise speed takes
and if the move is long enough to fit both ramps (), the middle is spent cruising for
where is the length of the move. If the move is too short to reach 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 . In the demo, shrink the distance or raise the cruise speed and watch the trapezoid turn into a triangle.
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: and .
At every moment the profile gives a full target: position , velocity , and acceleration . Send and to feedforward () so the voltage leads the motion, and send to PID so feedback only corrects the small tracking error.
The slowing-down half of the trapezoid obeys , where 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.
A trapezoid's acceleration jumps straight between , , and . 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.
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;
}
// 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);
}
If the move is too short to reach Accelerating for at covers , and slowing down covers the same. So
and . The peak speed comes out below 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
vMax, which is why the code
took this branch.
Every x line is for one phase, where 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
, exactly half. Speeding up and slowing down are mirror
images of each other, so halfway through the time is halfway through the distance.
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.
Whenever the mechanism can't keep up: a sagging battery, an extra game piece, a jam. 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
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.
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.
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?).
- WPILib: Trapezoidal Motion Profiles - practical motion-profile constraints for robot mechanisms.
- Tyler Veness, Controls Engineering in FRC - control-theory context for profiles, feedforward, and actuator limits.
- Road Runner and Pedro Pathing - FTC libraries that combine path geometry, constraints, and follower/controller logic.