Skip to main content

Voltage Compensation

Easydifficulty

This is a short lesson about one division. It's what makes feedforward, PID, and motion profiles behave the same on a fresh battery and a tired one.

The same command does less as the battery drains​

motor.setPower(0.6) doesn't apply a fixed voltage. It applies 60% of whatever the battery reads at that moment. A freshly charged 12 V battery usually reads around 13 V and can sag to 11 V or lower under heavy load. (The exact numbers depend on the battery's age, charge, and current draw.) So 0.6 means 0.6×13=7.80.6 \times 13 = 7.8 V early in the match and 0.6×11=6.60.6 \times 11 = 6.6 V late in it, for the same line of code. An auto tuned on a full battery starts coming up short, and a flywheel that was on speed falls behind. Your feedforward gains are also measured in volts, and they're being applied to a command that no longer means a fixed number of volts.

Divide by what you actually have​

Suppose you want 6 V at the motor. On a 13 V battery that's a power of 6/13=0.466/13 = 0.46. On an 11 V battery it's 6/11=0.556/11 = 0.55. In general, divide the voltage you want by the voltage you have:

power=VcmdVbattery\text{power} = \frac{V_\text{cmd}}{V_\text{battery}}

The motor then gets power×Vbattery=Vcmd\text{power} \times V_\text{battery} = V_\text{cmd}, whatever the battery is doing. The one limit is that you can't ask for more than the battery has. Once Vcmd>VbatteryV_\text{cmd} > V_\text{battery} the command hits full power and the output drops with the battery.

In the demo, the rose line is a raw power command, so the delivered voltage drifts as the battery changes. The green line is compensated and stays flat at the target until the battery can't reach it. Drag the battery from full to sagging and compare them.

Live demoVoltage compensation — the same command on a sagging battery
compensated — flat at targetraw command — drifts with batterybattery voltage now
Raw (no comp): —Compensated: —Raw drift vs target: —Match clock: —
Idea
Gains that stay correct

With the command in real volts, the feedforward constants you measured stay correct all match, and they carry over better between two robots with the same mechanism. Without it, every gain is really "correct at whatever voltage the battery had when I tuned," which is never the voltage you compete on.

One division, or one SDK call​

Do it last, after the controller has worked out a voltage. Read the battery once per loop, since each read takes time, and clamp the result to [−1,1][-1, 1].

FTCVoltage-compensated output
// A telemetry sanity floor, NOT a battery cutoff.
static final double MIN_VALID_BATTERY_VOLTS = 6.0;

void loop() {
double vbat = voltageSensor.getVoltage(); // ONCE per loop, shared below
setVoltage(leftMotor, leftDesiredVolts(), vbat);
setVoltage(rightMotor, rightDesiredVolts(), vbat);
}

void setVoltage(DcMotor motor, double desiredVolts, double vbat) {
if (!Double.isFinite(vbat) || !Double.isFinite(desiredVolts)
|| vbat < MIN_VALID_BATTERY_VOLTS) {
motor.setPower(0); // reject a disconnected sensor
return;
}

double power = desiredVolts / vbat; // volts -> the SDK's [-1, 1]
motor.setPower(Math.max(-1, Math.min(1, power)));
}

Every mechanism works out what it wants in volts, and this is the only place volts become motor power. All of them use the same vbat reading.

ExerciseWhy one reading, shared

Each motor could read the battery itself. What goes wrong when two drive motors read a sagging battery a few milliseconds apart and are both asked for the same voltage?

Show solution

They divide by slightly different numbers, so the same 6 V request turns into two different powers. On a drivetrain that's a steering error nobody asked for, and it only shows up when the battery is sagging, which is when you're pushing hardest. With one reading per loop, the whole robot agrees on the battery voltage.

ExerciseThe sign of the effect

You want 6 V. On a fresh 13.0 V battery the power is 0.46, and on a sagging 11.0 V battery it's 0.55. Why is a bigger number the right answer for a weaker battery?

Show solution

setPower is a fraction of the battery voltage, not a voltage. A weaker battery needs a bigger fraction to give the motor the same voltage. Without the division, 0.46 gives 6.0 V on a fresh battery and 5.06 V on a sagging one, and every gain you tuned drifts over the match.

In FRC you don't write the division yourself. setVoltage(...) divides by the measured bus voltage, and newer vendor APIs like Phoenix 6's VoltageOut take a voltage directly. Use either the roboRIO-side setVoltage or the motor controller's own enableVoltageCompensation(nominalVolts), not both. If you use both, setVoltage sends a percent Vcmd/VbusV_\text{cmd}/V_\text{bus}, the controller scales that percent by VnomV_\text{nom}, and the mechanism receives

Vdelivered=Vcmd VnomVbusV_\text{delivered} = V_\text{cmd}\,\frac{V_\text{nom}}{V_\text{bus}}

On a sagging battery that asks for too much: request 6 V with Vbus=11V_\text{bus} = 11 V and 6.55 V arrives, 9% high, in exactly the situation compensation is meant for. On a fresh battery above nominal it asks for too little.

Tip
It doesn't fix brownouts

Holding the voltage steady while the battery sags means drawing more current, which sags the battery further. Compensation keeps behavior consistent within what the battery can do. If you're browning out, look at the motor model: something is probably running too close to stall.

Check the voltage reading

Battery voltage dips sharply under a sudden load, like the drivetrain going to full power, so one reading can come back very low and make compensation ask for too much. Throw out readings that aren't finite or aren't physically possible, as the code does. If the reading is noisy, a very light low-pass filter is fine, but don't smooth heavily: real sag is exactly what the command needs to respond to.

Sources & references
  • WPILib: Feedforward Control - competition-robotics reference for commanding mechanisms in volts instead of relying on unitless power.
  • Game Manual 0, Motor Power - FTC motor current and stall context, and the 12 V basis for motor curves. It doesn't cover battery sag, so the sag numbers in this lesson are typical values rather than sourced figures.
  • How a Motor Works - Control Lab's motor-model lesson explaining why voltage, current, speed, and torque are coupled.