Skip to main content

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 kP=0.1k_P = 0.1 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 kPk_P 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 vv while accelerating at aa splits into three parts:

V=kS sign⁡(v)⏟break friction+kV v⏟hold the speed+kA a⏟accelerateV = \underbrace{k_S\,\operatorname{sign}(v)}_{\text{break friction}} + \underbrace{k_V\,v}_{\text{hold the speed}} + \underbrace{k_A\,a}_{\text{accelerate}}

These come straight out of the motor model, V=IR+keωV = IR + k_e\omega. The back-EMF part grows with speed, which is the kVvk_V v term. The current part supplies torque, some of it a roughly constant amount to overcome friction (kSk_S) and some of it to accelerate the mechanism's inertia (kAak_A a).

TermPays forNotes
kS: staticfrictionthe minimum voltage to keep it moving; the sign follows the direction of travel
kV: velocityback-EMF and dragvolts per unit of speed; usually most of the command
kA: accelerationinertiaonly nonzero while the target speed is changing

The kSk_S 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 kS=0.9k_S = 0.9 V, kV=0.15k_V = 0.15 V per in/s, and kA=0.03k_A = 0.03 V per in/s². You want 40 in/s while still speeding up at 20 in/s²:

V=0.9+0.15(40)+0.03(20)=0.9+6.0+0.6=7.5 VV = 0.9 + 0.15(40) + 0.03(20) = 0.9 + 6.0 + 0.6 = 7.5\ \text{V}

Once it reaches 40 in/s and stops accelerating, the kAk_A 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.

Live demoVelocity control — feedforward vs. PID-only
TargetSpeed (FF + PID)Speed (PID only)
True value ≈ 2.40. Try mistuning it.
Feedforward
0 V
PID
0 V
Speed: —Target: —Error: —Voltage: —
Idea
Three things to try
  1. Feedforward on with a tiny kP. It reaches and holds the target, and the green prediction bar does most of the work.
  2. Feedforward off with the same kP. It settles below the target. Raising kP to make up for it causes overshoot and twitching.
  3. 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.

Live demoMechanism feedforward — switch gravity compensation off and watch the sag
Arm gravity hold: —Slide kG(height): —Arm error: —Slide error: —
arm linktargetinterpolated slide hold voltagemeasured calibration pointscurrent operating point

Slides: gravity is roughly constant​

For a vertical slide at height hh, with velocity vv (positive up) and acceleration aa, add a constant kGk_G, the voltage that holds the slide still:

Vslide=kS sign⁡(v)+kG+kV v+kA a\boxed{ V_{slide} = k_S\,\operatorname{sign}(v) + k_G + k_V\,v + k_A\,a }

kGk_G doesn't change sign when the slide moves down. Friction always opposes the motion, so kSsign⁡(v)k_S\operatorname{sign}(v) 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 kGk_G isn't enough, measure it at several heights and interpolate between them. For a height hh between two measured heights hih_i and hi+1h_{i+1}:

kG(h)=kG,i+h−hihi+1−hi(kG,i+1−kG,i)\boxed{ k_G(h) = k_{G,i} + \frac{h - h_i}{h_{i+1} - h_i} \left(k_{G,i+1} - k_{G,i}\right) }

The fraction is how far hh sits between the two measured heights, from 0 to 1. For example, with kG=1.2k_G = 1.2 V at 10 in and 1.61.6 V at 20 in, a height of 12.5 in is a quarter of the way along, so kG=1.2+0.25(0.4)=1.3k_G = 1.2 + 0.25(0.4) = 1.3 V.

Arms: gravity changes with angle​

Measure the arm angle θ\theta from horizontal, with ω\omega its angular velocity and α\alpha 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 cos⁡θ\cos\theta:

Varm=kS sign⁡(ω)+kGcos⁡(θ)+kV ω+kA α\boxed{ V_{arm} = k_S\,\operatorname{sign}(\omega) + k_G\cos(\theta) + k_V\,\omega + k_A\,\alpha }

Here kGk_G is the voltage that holds the arm horizontal. At θ=0\theta = 0, cos⁡θ=1\cos\theta = 1 and you get all of it. Pointing straight up, cos⁡90°=0\cos 90° = 0 and you get none.

If your encoder reads zero somewhere other than horizontal, convert it first:

θhorizontal=θencoder+θoffset\theta_{horizontal} = \theta_{encoder} + \theta_{offset}

where θoffset\theta_{offset} 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.

FTCFeedforward + feedback, battery-compensated
// 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.

ExerciseDelete one term at a time

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 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.

With all three, 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.

FTCArm feedforward with a gravity term
// 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;
}
ExerciseWhere the cosine comes from

Gravity pulls straight down with force mgmg, but only the part of that force perpendicular to the arm turns it around the pivot. Draw the arm at an angle θ\theta 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.

Show solution

The part along the arm just pushes on the pivot bearing. The part across the arm is mgcos⁡θmg\cos\theta, and it acts at the arm's length from the pivot, so the holding torque goes as cos⁡θ\cos\theta.

Horizontal (θ=0\theta = 0): cos⁡=1\cos = 1, the most holding voltage, which is the worst case. Straight up (90°90°): cos⁡=0\cos = 0, because the weight points straight through the pivot. Hanging down (−90°-90°): cos⁡=0\cos = 0 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.

ExerciseWhy the static term is zero near rest

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 2kS2k_S. The arm buzzes. The EPS band treats "barely moving" as "not moving" and leaves the friction term off.

FTCSlide feedforward with interpolated gravity
// 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 kGk_G at that height, and half the difference is kSk_S. 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 kG−kSk_G - k_S, so every entry would come out low and the separate kSk_S term would count friction twice. The PID lesson uses the same two readings. Five or six heights across the travel is plenty.

ExerciseWhy clamp at both ends

What would extrapolating past the ends of the table command, and why is holding the last measured value the safer choice?

Show solution

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.

ExerciseDo you need the table?

Measure the holding voltage at the bottom, middle, and top of your slide. What would tell you the table isn't worth it?

Show solution

If the three agree within a few percent, one 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:

FRCWPILib equivalent
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 kV≈12 V÷vmax⁡k_V \approx 12\,\text{V} \div v_{\max}, but system identification measures kSk_S, kVk_V, and kAk_A properly with a least-squares fit, the same way FRC's SysId does. For gravity, measure kGk_G on the mechanism at the positions that matter.

Warn
Check your units

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.

Sources & references
  • WPILib: Feedforward Control - competition-robotics reference for kSk_S, kVk_V, kAk_A, elevator kGk_G, arm kGcos⁡(θ)k_G\cos(\theta), and model-based voltage prediction.
  • WPILib ArmFeedforward API - documents the horizontal-angle convention used by the cos⁡(θ)\cos(\theta) 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.