Skip to main content

Actuator Saturation & Anti-Windup

Mediumdifficulty

PID is usually written as if its output could be any number. On an FTC robot it can't. Motor power stops at ±1, the battery has a fixed voltage, servos have a range, and mechanisms hit hard stops. The controller can ask for more, and the robot just delivers the closest thing it can. That one limit is enough to break the integral term.

The controller asks for 19 V and the motor gets 12​

Picture an arm 90° from its target. kPk_P times that error already asks for 15 V, and the integral adds more every loop: 16, 17, 19 V. The battery has 12, so the motor sits at full power the whole way. By the time the arm reaches the target, the integral has piled up several volts of push that were never delivered, and it keeps pushing past the target until that pile unwinds.

The demo runs two copies of the same position controller on the same motor. The pink one keeps integrating while the motor is maxed out. The green one uses anti-windup: it caps the integral's share and stops adding to it when the motor is already pinned in the direction the error is pushing.

Live demoActuator saturation: the integral keeps charging unless you stop it
targetPID with windupPID with anti-windupwindup integral contributionanti-windup integral contribution
windup I: 0.00 Vanti I: 0.00 Vraw PID: 0.00 Vanti raw: 0.00 Vsaturated: no

The solid lines are mechanism position. The dashed lines are the integral term measured in output volts. When the raw PID command exceeds the actuator limit, the pink controller keeps integrating; the green controller clamps and conditionally pauses integration.

Asked for versus delivered​

Write what the controller asks for as the sum of its parts:

uraw=uP+uI+uD+uFFu_{raw} = u_P + u_I + u_D + u_{FF}

with uI=kIIu_I = k_I I and II the running total of error, and uFFu_{FF} any feedforward. The motor then gets that value clipped to its limits:

u=sat⁡(uraw,umin,umax)\boxed{ u = \operatorname{sat}(u_{raw}, u_{min}, u_{max}) }

sat⁡\operatorname{sat} passes the value through if it fits and returns the limit if it doesn't. For motor power the limits are −1-1 and 11. If your code works in volts, they're the measured battery voltage, ±Vbattery\pm V_{battery}, and the final power is u/Vbatteryu / V_{battery}.

The whole lesson is about the gap between urawu_{raw} and uu.

What windup is​

Each loop the integral grows by one rectangle, Ik+1=Ik+ekΔtI_{k+1} = I_k + e_k\Delta t, using the measured Δt\Delta t. Far from the target the error is large, so the controller asks for more than the motor has, uraw>umaxu_{raw} > u_{max}, and the motor gets umaxu_{max}. If the integral keeps growing anyway, uIu_I gets bigger even though none of the extra reaches the motor. When the arm finally arrives, the error is near zero but the stored integral is still big, so the controller keeps pushing and overshoots.

That's windup: the integral storing up a command the motor never had room to carry out.

The first fix: don't integrate further into the limit​

The most useful rule is to skip an integral update if it would push the output further past a limit it's already beyond:

FTCConditional integration: the failure and the repair
// THE FAILURE: integrates regardless of whether the actuator can obey.
integral += error * dt;

// THE REPAIR: discard the update only if it would push FARTHER into saturation.
double candidate = integral + error * dt;
double candidateRaw = nonIntegralVolts + kI * candidate;

boolean pushesFarther = (candidateRaw > max && error > 0)
|| (candidateRaw < min && error < 0);

if (!pushesFarther) integral = candidate; // otherwise, discard it

nonIntegralVolts is the P, D, and feedforward part of the command.

ExerciseRead the two conditions

What is each half of pushesFarther protecting against, and what does the rule deliberately still allow?

Show solution

If the command is already above max and the error is still positive, more integral only makes an unusable number bigger, and all of it has to be unwound later. The second half is the same thing for the negative limit.

What's still allowed is integration that pulls the command back toward the usable range. The rule isn't "stop integrating while saturated." It's "stop integrating in the direction that makes saturation worse." A fully frozen integrator would also be slow to respond once the error changes sign.

The second fix: cap the integral's share​

uI=clamp⁡(kII,Imin,Imax)u_I = \operatorname{clamp}(k_I I, I_{min}, I_{max})

Put the limits on the integral's contribution, in the same units as the output. If your command is in volts, cap the integral at, say, ±3 V. Capping kIIk_I I instead of II itself means the cap still means the same thing after you retune kIk_I.

The third fix: only integrate near the target​

I=0when∣e∣>ezoneI = 0 \quad \text{when} \quad |e| > e_{zone}

This is called an I-zone. Far from the target, let feedforward, P, and D do the moving, and reset the integral to zero. Only let it run close to the target, where it's correcting a small leftover error.

Idea
Often the best fix is no integral

For arms and slides, most of what the integral ends up doing is holding against gravity. A real feedforward term, kGk_G for slides and kGcos⁡(θ)k_G\cos(\theta) for arms, supplies that directly, so the integral doesn't have to slowly work it out.

You don't need all three fixes every time. The controller below combines them, but start with only what your logs show you need, and always log both the requested and the delivered output.

A PID controller with anti-windup​

This controller works in volts, which makes the limit physically meaningful: clamp to what the battery can deliver, then divide by the battery voltage when you set the motor power. The same code works with limits of [−1,1][-1, 1] if you prefer to work in power.

It does five things in order: check the I-zone, compute the rate of change of the measurement, propose a new integral and cap its contribution, decide whether to keep it, and build the output from the integral that was kept.

FTCPID with integrator clamp, I-zone, and conditional integration
private double integral, previousMeasurement;
private boolean firstLoop = true;

// Output is in VOLTS, not motor power.
double calculate(double setpoint, double measurement, double dt,
double minVolts, double maxVolts) {
dt = Math.max(MIN_DT, dt); // measured loop time, never assumed
double error = setpoint - measurement;

boolean insideIZone = Math.abs(error) <= I_ZONE;
if (!insideIZone) integral = 0; // reset, do not merely freeze

double rate = firstLoop ? 0 : (measurement - previousMeasurement) / dt;
previousMeasurement = measurement; // derivative on the MEASUREMENT
firstLoop = false;

double candidate = insideIZone ? integral + error * dt : integral;
double clampedVolts = clamp(kI * candidate, MIN_I_VOLTS, MAX_I_VOLTS);
candidate = kI == 0 ? 0 : clampedVolts / kI; // clamp the CONTRIBUTION

double candidateRaw = kP * error + kI * candidate - kD * rate;
boolean pushesFarther = (candidateRaw > maxVolts && error > 0)
|| (candidateRaw < minVolts && error < 0);
if (insideIZone && !pushesFarther) integral = candidate;

double raw = kP * error + kI * integral - kD * rate; // the one we kept
return clamp(raw, minVolts, maxVolts);
}

void reset() {
integral = 0;
firstLoop = true;
}
ExerciseWhy the cap is on kI * integral

The code caps kI * candidate and then divides kI back out, instead of capping the raw integral. Which of the two has units you can reason about, and what happens to a cap on the raw integral when you retune kI?

Show solution

kI * integral is in volts, the same as the output and the limits, so MAX_I_VOLTS = 3 means "the integral can contribute at most 3 V." The raw integral is in error times seconds, which nobody has a feel for.

A cap on the raw integral also changes meaning whenever kI changes. Double kI and the same cap now allows twice the voltage, so a limit you tuned carefully stops doing its job.

ExerciseThree situations, three fixes

Each of these is handled by a different part of the controller. Which one?

  • the arm is 90° away for four seconds before it gets close
  • the output sits at maximum while a jammed mechanism works itself free
  • you raise kI, and an integral that used to be fine becomes huge
Show solution

The first is the I-zone: four seconds of big error builds an integral that has nothing to do with the small leftover error it's meant to fix. The second is conditional integration: the output is maxed out, so integrating only stores up something to unwind later. The third is the voltage cap on the integral's contribution.

They respond to different things, distance from the target, saturation, and size, and none of them covers the other two.

To use it from a subsystem, measure both the loop time and the battery voltage:

FTCApplying the saturated voltage to a motor
void update(double target) {
double dt = timer.seconds(); // MEASURE it; never hardcode 0.02
timer.reset();

double vbat = voltageSensor.getVoltage();
if (!Double.isFinite(vbat) || vbat < MIN_VALID_BATTERY_VOLTS) {
motor.setPower(0); // bad telemetry: refuse to guess
return;
}

// The actuator limits ARE the battery, right now.
double commandVolts = controller.calculate(
target, motor.getCurrentPosition(), dt, -vbat, +vbat);

motor.setPower(clamp(commandVolts / vbat, -1, +1));
}

Passing the battery voltage in as the limit means the controller's idea of "maxed out" follows the real battery instead of a number someone typed in.

ExerciseFresh battery versus tired battery

Same mechanism, same error. What does the anti-windup logic do differently on a 13.0 V battery than on an 11.0 V one?

Show solution

The limit moves with the battery, so on the tired one the controller recognizes it's maxed out 2 V sooner and stops integrating 2 V sooner. With a fixed ±12 V limit it would keep integrating in a range the motor can't actually reach, and the overshoot would only show up late in matches when the battery is low, which is a hard bug to reproduce in the shop.

Use this anywhere the integral term isn't zero: slides, arms, heading hold, velocity hold, anything that can spend time at full output.

Saturation isn't only motor power

setPower(1.0) is the obvious limit, but current limits, brownout protection, skipping gears, slipping belts, servo travel, hard stops, and motion-profile speed limits are all saturations too. Whenever the controller asks for effort the robot can't deliver, the integral can wind up.

Fix the model before adding integral

If a mechanism needs a lot of integral just to hold still, it's probably missing feedforward. Add the gravity term first, then use a small integral for whatever is left. A small integral gives anti-windup very little to do.

Sources & references