Feedforward (kS, kV, kA, kG)
Mediumdifficulty
A feedback controller only acts once something is already wrong. But for most mechanisms you know roughly what voltage a given speed needs before you ask for it. Feedforward sends that voltage up front, and feedback only has to handle the difference. It's usually what separates a sluggish velocity loop from a crisp one.
Proportional control needs an error to hold a speed
Take a flywheel you want at 300 rad/s. Holding that speed takes about 6 V. A proportional controller only outputs voltage when there's an error. With V per rad/s, the flywheel settles where the error supplies just enough voltage for the speed it actually reached: about 250 rad/s, which needs 5 V, supplied by a 50 rad/s error. Raising shrinks the gap, but it also amplifies sensor noise and makes overshoot worse. An integral term can build up the missing voltage over time, but it's slow to do it.
You already know the flywheel needs about 6 V at that speed, so you can just send it.
Driving up a hill. A good driver presses the accelerator as the road starts to climb. A purely reactive driver waits until the car has slowed down, then overcorrects. Feedforward is pressing early. Feedback is the small adjustment after.
Where the voltage goes
The voltage to hold a velocity while accelerating at splits into three parts:
These come straight out of the motor model, . The back-EMF part grows with speed, which is the term. The current part supplies torque, some of it a roughly constant amount to overcome friction () and some of it to accelerate the mechanism's inertia ().
| Term | Pays for | Notes |
|---|---|---|
| kS: static | friction | the minimum voltage to keep it moving; the sign follows the direction of travel |
| kV: velocity | back-EMF and drag | volts per unit of speed; usually most of the command |
| kA: acceleration | inertia | only nonzero while the target speed is changing |
The you get from a fit is the friction while moving. The voltage needed to break a stationary mechanism loose can be a bit higher, and it can differ between directions.
Working out one command
Say system identification gave a drivetrain V, V per in/s, and V per in/s². You want 40 in/s while still speeding up at 20 in/s²:
Once it reaches 40 in/s and stops accelerating, the term drops out and the command falls to 6.9 V. Before sending either number, clamp it to what the battery can supply.
In the demo the motor has to reach the dashed target speed. Turn feedforward on and off with the same gains. The bars show how the command splits between the feedforward prediction and the feedback correction.
- Feedforward on with a tiny kP. It reaches and holds the target, and the green prediction bar does most of the work.
- Feedforward off with the same kP. It settles below the target. Raising kP to make up for it causes overshoot and twitching.
- Set kV too low. The prediction falls short and feedback has to cover the gap, so most of the benefit is gone.
Adding gravity
A flywheel doesn't need any voltage to stay still. A vertical slide does, and an arm needs an amount that changes with its angle.
Slides: gravity is roughly constant
For a vertical slide at height , with velocity (positive up) and acceleration , add a constant , the voltage that holds the slide still:
doesn't change sign when the slide moves down. Friction always opposes the motion, so flips, but gravity always pulls down, so the motor always needs an upward holding term.
Real slides aren't always that tidy. Cable routing, stage engagement, elastic tension, and carriage friction can make the holding voltage change with height. If one isn't enough, measure it at several heights and interpolate between them. For a height between two measured heights and :
The fraction is how far sits between the two measured heights, from 0 to 1. For example, with V at 10 in and V at 20 in, a height of 12.5 in is a quarter of the way along, so V.
Arms: gravity changes with angle
Measure the arm angle from horizontal, with its angular velocity and its angular acceleration. Gravity pulls hardest on a horizontal arm and not at all on a vertical one, and the factor that captures that is :
Here is the voltage that holds the arm horizontal. At , and you get all of it. Pointing straight up, and you get none.
If your encoder reads zero somewhere other than horizontal, convert it first:
where is a constant you measure once. If the sign comes out wrong, don't flip the whole controller. Fix the angle convention and motor direction until a horizontal arm gets an upward holding voltage.
In practice, feedforward and feedback run together: the model supplies most of the command right away, and PID corrects what's left and rejects disturbances.
output = feedforward(goal) + PID(measurement, goal)
Feedforward in code, in volts
FTC motors take a power from −1 to 1, but the model is in volts. Divide by the measured battery voltage, as in voltage compensation, so the behavior is the same at 13 V and 11 V.
// goalVelocity and goalAccel are in real units, e.g. metres/second.
// The motor runs in RUN_WITHOUT_ENCODER: this method closes its own loop.
void run(double goalVelocity, double goalAccel) {
double measured = flywheel.getVelocity() * UNITS_PER_TICK;
double predict = kS * Math.signum(goalVelocity)
+ kV * goalVelocity
+ kA * goalAccel;
double correct = kP * (goalVelocity - measured);
flywheel.setPower((predict + correct) / batteryVoltage());
}
Compare the two lines. predict only uses the goal. It never looks at the sensor, so it can act the
moment you ask for a new speed. correct uses the error, so it can only act once the mechanism is
already off.
What happens without each term?
- no
kS: the flywheel never quite gets going from rest. - no
kV: the command doesn't grow with the speed you ask for. - no
kA: steady speeds are fine, but it lags whenever the goal changes.
Once all three are in, what's left for With all three, kP to do, and why does that let you use a smaller kP than
with feedback alone?Show solution
kS pays for friction, so without it every command comes up short by exactly that much. kV pays for
back-EMF, which grows with speed, so without it the model is just a constant. kA pays for the extra
torque it takes to change speed, so without it the model is only right when nothing is changing.kP only has to correct model error and disturbances, a few percent of the command.
That's good, because kP amplifies sensor noise and can only react after the fact. The less it has to
do, the less noise reaches the motor and the less it overshoots.
// Angle convention: 0 means the arm is HORIZONTAL. If your encoder zero is
// elsewhere, add the offset before calling this. Output is volts.
double calculate(double angle, double velocity, double acceleration) {
double staticVolts = Math.abs(velocity) < EPS
? 0
: kS * Math.signum(velocity);
return staticVolts
+ kG * Math.cos(angle) // <- the gravity term
+ kV * velocity
+ kA * acceleration;
}
Gravity pulls straight down with force , but only the part of that force perpendicular to the arm
turns it around the pivot. Draw the arm at an angle above horizontal, split the weight into a
part along the arm and a part across it, and find the one that makes torque. Then check your answer at
horizontal, straight up, and hanging straight down. The part along the arm just pushes on the pivot bearing. The part across the arm is , and
it acts at the arm's length from the pivot, so the holding torque goes as . Horizontal (): , the most holding voltage, which is the worst case. Straight up
(): , because the weight points straight through the pivot. Hanging down ():
again, for the same reason. Those two zeros are very different situations, though. Straight
up is unstable: nudge it and gravity pulls it over. Hanging down is stable: gravity pulls it back.
Feedforward can't tell them apart. Feedback can.Show solution
staticVolts is forced to zero at very low speeds instead of just using Math.signum(velocity). What
is Math.signum(0), and what would a stationary arm do if this term kept flickering?Show solution
Math.signum(0) is 0, so exactly zero is already handled. The problem is speeds near zero. A
stationary arm's measured velocity jitters slightly positive and negative, so signum flips every few
loops and the command jumps by . The arm buzzes. The EPS band treats "barely moving" as "not
moving" and leaves the friction term off.
// For a vertical slide where ONE constant kG is not accurate across the
// travel: rigging friction and stage engagement change with height.
private double[] heights; // the heights you tested at
private double[] holdVolts; // the voltage that held the slide at each one
double calculate(double height, double velocity, double acceleration) {
double staticVolts = Math.abs(velocity) < EPS
? 0
: kS * Math.signum(velocity);
return staticVolts
+ interpolateHoldVolts(height) // instead of a constant kG
+ kV * velocity
+ kA * acceleration;
}
double interpolateHoldVolts(double height) {
if (heights.length < 2) return 0;
if (height <= heights[0]) return holdVolts[0];
for (int i = 0; i < heights.length - 1; i++) {
if (height <= heights[i + 1]) {
double t = (height - heights[i]) / (heights[i + 1] - heights[i]);
return holdVolts[i] + t * (holdVolts[i + 1] - holdVolts[i]);
}
}
return holdVolts[holdVolts.length - 1]; // clamp above the last sample
}
Build the table by measuring. At each height, take two readings: the smallest voltage that just moves the slide up, and the largest at which it still creeps down. Their average is at that height, and half the difference is . Don't use "the smallest voltage that holds it still." Friction lets it hold anywhere in a band, and the bottom of that band is , so every entry would come out low and the separate term would count friction twice. The PID lesson uses the same two readings. Five or six heights across the travel is plenty.
What would extrapolating past the ends of the table command, and why is holding the last measured value
the safer choice? Extrapolating carries the last segment's slope on forever. A height slightly past the top gives a
voltage slightly past the last one, and a sensor glitch reading far past the top gives an enormous
voltage, from a model with no data out there at all. Clamping caps the command at the nearest measured
value. It stops the runaway, but it doesn't make the model right outside the measured range. If you
need feedforward out there, measure out there, and enforce travel and voltage limits separately.Show solution
Measure the holding voltage at the bottom, middle, and top of your slide. What would tell you the
table isn't worth it? If the three agree within a few percent, one Show solution
kG is fine, and the table just adds tuning work to
reproduce a constant. It's worth it when the numbers differ noticeably, which usually means cascade
rigging where stages engage at different heights.
In FRC, WPILib bundles the same recipe:
SimpleMotorFeedforward ff = new SimpleMotorFeedforward(kS, kV, kA);
PIDController pid = new PIDController(kP, 0, 0);
motor.setVoltage(ff.calculate(goalVel) + pid.calculate(measVel, goalVel));
The constants come from measurement. A rough first guess is , but system identification measures , , and properly with a least-squares fit, the same way FRC's SysId does. For gravity, measure on the mechanism at the positions that matter.
kV in volts per m/s only means something if getVelocity() has really been
converted to m/s. A wrong ticks-to-units factor gives a wrong model. See
From Math to a Loop.
Feedforward on its own is open loop, so it can't see anything the model left out: a jam, a worn gear, a tired battery. Pair it with PID to catch the rest.
- WPILib: Feedforward Control - competition-robotics reference for , , , elevator , arm , and model-based voltage prediction.
- WPILib ArmFeedforward API - documents the horizontal-angle convention used by the arm term.
- Tyler Veness, Controls Engineering in FRC - derivation of motor feedforward and how it combines with feedback.
- System Identification - the Control Lab lesson that measures the feedforward constants from data.