System Identification
Harddifficulty
Read Feedforward first, and Signals & Noise for the difference between random noise and a steady bias. The math here is a line of best fit: a scatter plot, its slope and intercept, and how far the points sit from the line. The derivation of the fitting formulas is an optional exercise.
The feedforward lesson left a question open: where do , , and come from? A guess, or another team's numbers, gives a model that's wrong by some unknown amount, and then feedback has to cover the difference, which is the problem feedforward was supposed to solve. The reliable answer is to measure them on your own robot.
You can't trust a guessed model
You can estimate from the top speed, but real mechanisms have friction, gearbox losses, and motors that don't match the datasheet. If the feedforward is 20% off, feedback quietly makes up the difference. The mechanism still works, but it's sluggish and you won't know why. You want the actual constants for this robot, and some idea of how much to trust them.
Running the test safely
Set the experiment up before worrying about the math:
- Test a drivetrain on the floor, in a clear lane. For a mechanism, secure the robot but leave the part you're measuring free to move. Don't characterize a drivetrain with its wheels in the air.
- Set a voltage limit and keep a way to stop immediately.
- Measure the battery voltage instead of assuming 12 V.
- Pick one velocity unit and log time, applied volts, velocity, and acceleration every loop.
- Throw out the samples from before the mechanism starts moving.
- Fit the data afterwards, then check the constants on a separate run.
Before pressing Run a new test, guess where the fitted intercept will land. Then set Noise level to zero (which turns off both voltage and velocity noise) and run it. There's still a small error in the intercept. It comes from how fast the ramp rises compared with how slowly the mechanism responds, not from the demo running at 12× speed.
Fitting a line to the data
From feedforward, the voltage to hold velocity while accelerating at is
The unknowns, , , and , only appear multiplied by things you can measure, so you can fit them with ordinary line fitting. The trick is designing tests that isolate them.
The slow ramp gives kS and kV
Raise the voltage very slowly, so slowly that the mechanism is barely accelerating and is close to zero. Moving forward, the model becomes a straight line:
Plot every logged sample with velocity across and voltage up. The points fall roughly on a line. Its slope is , the extra volts per unit of speed. Where it crosses is , the friction voltage.
To see where the fitting formulas come from, take three samples: . The average velocity is and the average voltage is . Each sample's distance from those averages is , , . The slope is how much voltage changes per unit of velocity across the samples:
and the line has to pass through the average point, so V. In general, with samples, that's the ordinary least squares fit:
The bars mean averages over all the samples, and the subscript numbers the samples. It's the line that makes the total of the squared vertical misses as small as possible.
In the demo, each blue dot is a logged sample, the amber line is the fit, and the green dot where it crosses is . Turn the noise up and watch the estimate wobble. More samples help with random noise but not with everything. Least squares assumes the velocity readings are accurate. This demo adds noise to both axes, and noise in velocity tends to pull the slope toward zero. Turn noise off to see the small bias that's left from the ramp, and always check a real fit on a separate run.
A step test gives kA
At a steady speed does nothing, so the ramp can't see it. Run a second test: jump to a fixed voltage and log the acceleration as the mechanism speeds up. Rearranging the model,
Don't compute that for each sample, though. Near the end of the step the acceleration gets close to zero and the division blows up. Instead, fit all the accelerating samples at once, with columns and target . One least-squares solve with three unknowns returns , , and together.
The sign column also lets you fit forward and reverse data together, since it flips with the direction of travel. That assumes friction is the same both ways. If you're not sure it is, fit each direction separately and compare. A big difference is worth knowing about.
For a motor, system identification is fitting a line, or a plane, to data. The math is the easy part. The skill is in designing tests that isolate one term at a time.
Running the ramp and fitting it
The demo ramps at in real time. With a mechanism that takes about to respond, that adds at most about of error to the intercept. The display runs 12× faster so you don't have to wait two minutes, but the simulated mechanism runs at normal speed.
// Raise the command slowly and log every loop.
void rampStep(double elapsedSeconds) {
double vbat = batteryVoltage();
double applied = Math.min(RAMP_VOLTS_PER_SEC * elapsedSeconds / vbat, 1.0);
motor.setPower(applied);
double v = motor.getVelocity() * UNITS_PER_TICK; // real units, not ticks/s
double volts = applied * vbat; // estimated applied voltage
samples.add(new Sample(v, volts));
}
The ramp has to be slow enough that acceleration hardly matters. How slow is that for your mechanism,
and what would the fit show if you ramped too fast? Slow enough that is tiny next to . In practice that means reaching top speed over tens of
seconds, not one. If you ramp too fast, the voltage spent on acceleration ends up in the fit, and it's
worth seeing where. A ramp rising at volts per second settles into a nearly constant acceleration
, so the missing term is a constant, and a constant shifts the line's
intercept, not its slope: So a too-fast ramp gives a that's too high, by about the ramp rate times the mechanism's time
constant . That's the above. Every feedforward built on it then
sends more voltage than the mechanism needs. The slope barely changes, which is why this is easy to
miss: the number you'd think to check looks fine. If comes out suspiciously large, slow the ramp
down before blaming friction.Show solution
Two reasons. The command is clamped at 1.0, so past that point you keep asking for more and the motor
keeps getting the same. And the battery sags under load, so the same power fraction gives different
voltages during the test. Fitting against the requested voltage fits a line to numbers the motor never
saw.volts is the applied power times the measured battery voltage, not the voltage you asked for. Why are
those different?Show solution
// Fit V = kS + kV * v to the logged samples.
double[] fitLine(List<Sample> samples) {
if (samples.size() < 3) return null;
double meanV = 0, meanVolts = 0;
for (Sample s : samples) {
meanV += s.v;
meanVolts += s.volts;
}
meanV /= samples.size();
meanVolts /= samples.size();
double sXY = 0, sXX = 0;
for (Sample s : samples) {
sXY += (s.v - meanV) * (s.volts - meanVolts);
sXX += (s.v - meanV) * (s.v - meanV);
}
if (!Double.isFinite(sXX) || sXX < MIN_SPREAD) return null;
double kV = sXY / sXX; // the slope
double kS = meanVolts - kV * meanV; // the intercept
return new double[] {kS, kV};
}
Write the total squared miss . Find the and that make it smallest
by taking its derivative with respect to each one and setting both to zero. Then say what and
mean physically, and what a fitted of zero would tell you. Setting the derivative to zero gives right away. Substituting that
into the equation leaves , the two lines in the code. is the friction voltage while moving, and is volts per unit of speed. A fitted near
zero could mean low friction, or noisy data, or a poor fit. It doesn't prove the mechanism starts
moving at zero volts. Look at how the points scatter around the line, and make sure the samples cover a
useful range of speeds.Show solution
Zero spread means every sample had the same velocity, so there's no way to tell the slope from the
intercept. Dividing by zero gives a non-finite result that could end up as a motor command. Returning
sXX measures how spread out the velocity samples are. What does a near-zero sXX do to the division
after it, and why is refusing to fit better than returning a number?Show solution
null makes the failure obvious so the caller can reject it.
In FRC, SysId runs the ramp and step tests, fits all three gains, and reports an that says how well the model fits. In FTC, run a small ramp OpMode like the one above or use a library tuner. The math is the same.
Throw away samples where the mechanism hasn't started moving yet, with velocity near zero while the voltage climbs. They follow different physics and pull the intercept off. Start the fit from the first sample with clearly nonzero velocity.
Velocity that wasn't converted to real units, so ends up in volts per tick (see
units). Forward and reverse samples mixed without the sign
column. And a sagging battery during the test, if you logged 12 V instead of power × batteryVoltage.
Even that is an estimate of the voltage at the motor: wiring losses and timing differences between the
voltage and velocity readings can make it a little off.
- WPILib SysId routines: the official ramp and step test definitions, safety structure, and least-squares fit of that this lesson follows.