Skip to main content

System Identification

Harddifficulty

Before you start

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 kSk_S, kVk_V, and kAk_A 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 kV≈12 V÷vmaxk_V \approx 12\,\text{V} \div v_\text{max} 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:

  1. 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.
  2. Set a voltage limit and keep a way to stop immediately.
  3. Measure the battery voltage instead of assuming 12 V.
  4. Pick one velocity unit and log time, applied volts, velocity, and acceleration every loop.
  5. Throw out the samples from before the mechanism starts moving.
  6. Fit the data afterwards, then check the constants on a separate run.
Predict first

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 vv while accelerating at aa is

V=kS sign⁡(v)+kV v+kA aV = k_S\,\operatorname{sign}(v) + k_V\,v + k_A\,a

The unknowns, kSk_S, kVk_V, and kAk_A, 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 kAak_A a is close to zero. Moving forward, the model becomes a straight line:

V=kS+kV vV = k_S + k_V\,v

Plot every logged sample with velocity across and voltage up. The points fall roughly on a line. Its slope is kVk_V, the extra volts per unit of speed. Where it crosses v=0v = 0 is kSk_S, the friction voltage.

To see where the fitting formulas come from, take three samples: (v,V)=(10,2.4),(20,3.9),(30,5.4)(v, V) = (10, 2.4), (20, 3.9), (30, 5.4). The average velocity is vˉ=20\bar v = 20 and the average voltage is Vˉ=3.9\bar V = 3.9. Each sample's distance from those averages is (−10,−1.5)(-10, -1.5), (0,0)(0, 0), (10,1.5)(10, 1.5). The slope is how much voltage changes per unit of velocity across the samples:

kV=(−10)(−1.5)+(0)(0)+(10)(1.5)(−10)2+02+102=30200=0.15k_V = \frac{(-10)(-1.5) + (0)(0) + (10)(1.5)}{(-10)^2 + 0^2 + 10^2} = \frac{30}{200} = 0.15

and the line has to pass through the average point, so kS=Vˉ−kVvˉ=3.9−0.15(20)=0.9k_S = \bar V - k_V \bar v = 3.9 - 0.15(20) = 0.9 V. In general, with nn samples, that's the ordinary least squares fit:

kV=∑(vi−vˉ)(Vi−Vˉ)∑(vi−vˉ)2,kS=Vˉ−kV vˉk_V = \frac{\sum (v_i - \bar v)(V_i - \bar V)}{\sum (v_i - \bar v)^2}, \qquad k_S = \bar V - k_V\,\bar v

The bars mean averages over all the samples, and the subscript ii 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 v=0v = 0 is kSk_S. 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.

Live demoSystem identification — 0.10 V/s ramp shown at 12x simulated time
ramp samples (v, V)rejected — not yet movingleast-squares fit (live)true lineintercept = kS
1× adds up to ±1 V and ±8 rpm; 0× disables both.
kS (fit / true): —kV (fit / true): —Samples: —Status: —

A step test gives kA​

At a steady speed kAk_A 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,

kA=V−kS−kV vak_A = \frac{V - k_S - k_V\,v}{a}

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 [sign⁡(vi),vi,ai][\operatorname{sign}(v_i), v_i, a_i] and target ViV_i. One least-squares solve with three unknowns returns kSk_S, kVk_V, and kAk_A together.

The sign column also lets you fit forward and reverse data together, since it flips kSk_S 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.

Idea
It's just line fitting

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 0.10 V/s0.10\ \mathrm{V/s} in real time. With a mechanism that takes about 0.35 s0.35\ \mathrm{s} to respond, that adds at most about 0.035 V0.035\ \mathrm{V} 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.

FTCQuasistatic ramp: collect (velocity, voltage) samples
// 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));
}
ExerciseHow slow is slow enough?

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?

Show solution

Slow enough that kAak_A a is tiny next to kVvk_V v. 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 cc volts per second settles into a nearly constant acceleration a≈c/kVa \approx c/k_V, so the missing kAak_A a term is a constant, and a constant shifts the line's intercept, not its slope:

V=(kS+kAckV)⏟intercept, too high by c τ+kV vV = \underbrace{\left(k_S + \frac{k_A c}{k_V}\right)}_{\text{intercept, too high by } c\,\tau} + k_V\,v

So a too-fast ramp gives a kSk_S that's too high, by about the ramp rate times the mechanism's time constant τ=kA/kV\tau = k_A/k_V. That's the 0.035 V0.035\ \mathrm{V} 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 kSk_S comes out suspiciously large, slow the ramp down before blaming friction.

ExerciseWhy log the applied voltage

volts is the applied power times the measured battery voltage, not the voltage you asked for. Why are those different?

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.

FTCOrdinary least squares -> kS (intercept), kV (slope)
// 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};
}
ExerciseWhere the formulas come from (optional, uses derivatives)

Write the total squared miss ∑(V−kS−kVv)2\sum (V - k_S - k_V v)^2. Find the kSk_S and kVk_V that make it smallest by taking its derivative with respect to each one and setting both to zero. Then say what kSk_S and kVk_V mean physically, and what a fitted kSk_S of zero would tell you.

Show solution

Setting the kSk_S derivative to zero gives kS=Vˉ−kVvˉk_S = \bar V - k_V \bar v right away. Substituting that into the kVk_V equation leaves kV=Sxy/Sxxk_V = S_{xy}/S_{xx}, the two lines in the code.

kSk_S is the friction voltage while moving, and kVk_V is volts per unit of speed. A fitted kSk_S 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.

ExerciseWhy check the spread

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

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 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 R2R^2 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.

Tip
Only fit the moving samples

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.

Common ways a fit goes wrong

Velocity that wasn't converted to real units, so kVk_V 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.

Sources & references
  • WPILib SysId routines: the official ramp and step test definitions, safety structure, and least-squares fit of kS/kV/kAk_S/k_V/k_A that this lesson follows.