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. 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.
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:
with and the running total of error, and any feedforward. The motor then gets that value clipped to its limits:
passes the value through if it fits and returns the limit if it doesn't. For motor power the limits are and . If your code works in volts, they're the measured battery voltage, , and the final power is .
The whole lesson is about the gap between and .
What windup is
Each loop the integral grows by one rectangle, , using the measured . Far from the target the error is large, so the controller asks for more than the motor has, , and the motor gets . If the integral keeps growing anyway, 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:
// 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.
What is each half of If the command is already above 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.pushesFarther protecting against, and what does the rule deliberately still
allow?Show solution
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.
The second fix: cap the integral's share
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 instead of itself means the cap still means the same thing after you retune .
The third fix: only integrate near the target
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.
For arms and slides, most of what the integral ends up doing is holding against gravity. A real feedforward term, for slides and 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 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.
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;
}
The code caps A cap on the raw integral also changes meaning whenever 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.kI changes. Double kI and the same cap now
allows twice the voltage, so a limit you tuned carefully stops doing its job.
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:
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.
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? 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.Show solution
Use this anywhere the integral term isn't zero: slides, arms, heading hold, velocity hold, anything that can spend time at full output.
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.
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.
- WPILib, PID Control in WPILib and PIDController API - integrator range and I-zone tools used to prevent windup.
- CTRL ALT FTC, Improvements to the PID Controller - FTC-oriented discussion of practical PID upgrades, including anti-integral windup.
- Feedforward - the Control Lab lesson that replaces much of the integral's job with a model-based command.