Compensate for Robot Motion
Mediumdifficulty
You'll build a movingAim() function that returns where to aim, how far that is, the flight time, and
whether the calculation settled. You need field-frame vectors, an up-to-date robot velocity, a flight-time
table, and the feeder delay. The optional math section explains why the calculation settles.
Throw a ball to someone who is running and you do not aim at where they are. You lead them. A launcher on a moving robot has the same problem even when the field target is fixed: the ball leaves with part of the chassis velocity, so the target moves relative to the projectile.
A draft series of WPILib articles on fire control, currently only on a preview build and not yet in the published docs, calls this lead-and-adjust. It works for any shooter, because the shooter's behavior is already captured in a firing table.
First build a firing table
A stationary firing table maps target distance to three values:
- the flywheel command;
- the hood command;
- the measured or modeled time for the ball to reach the scoring event.
The first two make the shot happen. The flight time is what makes aiming on the move possible: the moving-shot calculation only needs geometry and time, and everything messy about the launcher, like drag, spin, and contact, is already baked into the measured table.
Keep neighboring distances on the same kind of shot. If nearby rows jump between a high arc and a low one, interpolating between them gives nonsense, and so does the moving-shot calculation built on them. The draft WPILib static firing-table article suggests starting with a coarse table, adding rows where testing shows you need them, and recording flight time while you're at it.
For this robot, the archived table stores the descending rim-event time:
| Distance (in) | 50 | 60 | 76 | 85 | 95 | 110 | 130 |
|---|---|---|---|---|---|---|---|
| Rim flight time (s) | 0.50 | 0.54 | 0.60 | 0.70 | 0.80 | 0.95 | 1.05 |
These are the seven knots in Sensors.java at firmware commit 75db349d. They describe time to the rim event, not the later time when a plotted path reaches the floor.
Lead, look up, and adjust
The ball leaves with part of the robot's velocity, so during the feeder delay plus the flight time , it drifts by about the robot's velocity times that time. Aim that far the other way. This robot also used a measured gain for how much of the robot's velocity the ball actually keeps:
is the real goal and is the adjusted aim point, the "virtual target." and . The minus sign moves the aim point opposite to the robot's motion, and with the robot stopped the correction is zero.
There's a catch: the flight time depends on the distance, and the distance depends on where you aim. So it goes in a loop:
- Guess flight time from the real-target distance.
- Lead by shifting the virtual target through the predicted platform displacement.
- Adjust by looking up flight time at the virtual distance.
- Repeat until successive flight times differ by less than the chosen tolerance.
This is the dynamic shooting method from the draft WPILib articles, with a fixed FTC goal and this robot's measured gain.
Choose Sideways and predict which side of the real target becomes the virtual target. Compare Approaching and Retreating, then step through the rows. Direct approach and retreat change virtual distance strongly, so they also expose more of the time map.
| Update | time used | virtual distance | next time | |Δt| |
|---|---|---|---|---|
| 1 | 0.800 s | 105.2 in | 0.907 s | 0.107 s |
| 2 | 0.907 s | 107.8 in | 0.932 s | 0.024 s |
The archived solver assumes constant chassis velocity over the feeder delay and flight. Its acceleration term was disabled. A clamped time lookup can still return a number, but the status warns that the virtual distance is outside the calibrated map.
Worked example: the first shift is 1.1475 m
At 95 inches, the real-target lookup starts at . For a sideways chassis speed of ,
That first aim point is farther from the shooter than the real goal, so its flight time is longer, and the next step moves the aim point a bit more. You don't need wheel and hood settings for the in-between steps. Only the final distance is used to look up the actual shot.
Decide whether the iteration is trustworthy
The robot's code starts from the real goal's flight time, stops when two estimates in a row differ by 0.05 s or less, and gives up after five steps. Report that last difference with the result, and don't fire if it didn't settle.
Optional math: why the fixed-point update converges
The scalar update is
Once you have three estimates, you can see how fast they're closing in:
A small means the estimates are settling fast. Close to one means the answer is fragile, and a small error in velocity or timing can move the aim point a lot. (Skip this when the bottom of the fraction is basically zero.) The demo shows it as information, not a fire or no-fire rule.
The paper also gives a guaranteed bound. The steepest part of the flight-time table changes by , so
guarantees the loop settles on one answer. With that holds up to 2.28 m/s. A computer check of 1,399,392 cases up to 1.7 m/s all settled within five steps, and 170 of them needed all five. That's a check of the math, not of how often shots go in.
Before releasing, check all of the following:
- pose and velocity are finite, recent, and expressed in the same field frame;
- the virtual distance remains inside the calibrated table or clamping is explicitly permitted;
- the iteration converged within its budget and the final residual is below the tuned threshold;
- chassis speed and estimated contraction rate lie inside a range validated on the real robot;
- flywheel and hood have reached their final commands.
There's no universal threshold for these. Set them from real shots at the speeds and angles your drivers actually use.
Read the residual in metres as well as seconds
Stopping at a 0.05 s difference doesn't mean the aim point is exact. At 1.7 m/s, the paper's worst-case bound is 0.146 s, or 0.223 m. In practice, running the robot's code over a grid of cases, the aim point was within 8.3 cm of the fully settled answer 99% of the time and 17.1 cm in the worst case.
Three different numbers get called "error" here, so keep them apart: the change between the last two flight-time estimates, how far the final estimate is from satisfying the equation exactly (at most 0.031 s in the paper), and how far the aim point is from where it would settle.
The demo shows each step of a simplified version in metric units. The robot's Java differs in small
ways, like reading distance from getX()/getY(), and the lesson's code returns the status
explicitly instead of copying every detail.
What changed in the newer robot?
The newer Sensors.java splits velocity along the target line and perpendicular to it. Radial drift changes the virtual distance used for wheel/hood commands; lateral drift changes turret lead.
| Setting | Paper's season snapshot | Newer snapshot defaults |
|---|---|---|
| Inheritance gain | 0.9 | 1.30 |
| TOF at 95 inches | 0.80 s | 0.70 s |
| TOF at 130 inches | 1.05 s | 0.90 s |
| Flight-time weighting | one full TOF | 0.55 radial, 0.7 lateral |
| Acceleration correction | disabled | radial factor 0.17; lateral factor 0 |
| Position reference in the update | robot getters | shooter world position with turret offset |
Both use a 0.05 s feeder delay and five steps, but the newer formula is different enough that the season's 2.28 m/s bound and scoring results don't carry over. Recheck both before relying on them.
Next step: Newton can solve faster
The repeat-until-it-settles loop is easy to write and usually takes three to five steps, but in some directions it settles slowly. A follow-on draft WPILib article turns it into a root-finding problem:
and solves it with Newton's method. For a measured table, it uses a smooth estimate of the slope instead of differentiating the noisy table values.
Newton's method takes fewer steps. It doesn't make a fragile shot any less fragile, and it depends more on a good starting guess. Use it if running out of steps is actually your problem, and keep all the same safety checks.
Next step: decide how drag changes inherited motion
The draft WPILib linear-drag article replaces the time in the drift calculation with
so the drift becomes , which accounts for air slowing the ball's sideways motion.
This robot's ball has holes in it and is modeled with drag that grows with speed squared, and its code used the measured gain instead of that formula. The paper shows 0.9 is a reasonable value, but that doesn't make the two approaches interchangeable. Pick one, measure its setting from moving shots, and test it, rather than stacking both.
The season code didn't correct for acceleration. If the robot is still speeding up or slowing down during the delay, the paper bounds the extra drift by
which is 2.6 to 5.4 cm for every 1 m/s² across the robot's flight-time table. So the first thing to do about acceleration is measure it and avoid firing while it's large, before adding another term to the controller.
What the controlled launches establish
The paper alternated compensation OFF and ON within each commanded-speed block, with 15 launches per state:
| Chassis state | Stationary | 0.5 m/s | 0.8 m/s | 1.5 m/s | 1.7 m/s |
|---|---|---|---|---|---|
| SOTM OFF | 15/15 | 13/15 | 10/15 | 5/15 | 2/15 |
| SOTM ON | 15/15 | 15/15 | 15/15 | 13/15 | 11/15 |
Standing still, both scored 15/15, so turning compensation on didn't break the stationary shot. While moving, entries went from 30/60 to 54/60. That supports the method as a whole, though it can't say which part (the gain, the delay, the velocity filter, the table, or the firing checks) mattered most. Source: research paper §8.3, Table 3.
Only the totals were kept, not a log of each shot. The paper reports a 40-point difference in percentage (95% interval 24.1 to 53.4 points). That's good evidence compensation helps, but it can't tell you why any individual shot missed.
Java: return the solution and its evidence
AimResult movingAim(Vec2 shooter, Vec2 target, Vec2 robotVelocity) {
Lookup initial = flightTime.lookup(target.minus(shooter).norm());
double time = initial.value();
Vec2 virtualTarget = target;
double residual = Double.POSITIVE_INFINITY;
boolean clamped = initial.clamped();
for (int update = 0; update < 5; update++) {
double totalTime = 0.05 + time;
virtualTarget = target.minus(robotVelocity.times(0.9 * totalTime));
Lookup next = flightTime.lookup(virtualTarget.minus(shooter).norm());
residual = Math.abs(next.value() - time);
clamped |= next.clamped();
if (residual <= 0.05) {
return new AimResult(virtualTarget, true, clamped, residual, update + 1);
}
time = next.value();
}
return new AimResult(virtualTarget, false, clamped, residual, 5);
}
The firing logic should check the status, not just the aim point. An aim point from a calculation that didn't settle, or that ran off the end of the table, shouldn't be trusted unless you've tested that case.
Put it in the robot loop in this order
- Verify pose and velocity timestamps, units, and field frame.
- Compute the shooter world position, including its offset from the robot center.
- Solve the virtual target and expose convergence, clamping, residual, and iteration count.
- Reject invalid, stale, out-of-range, or unconverged results.
- Use virtual distance for wheel and hood; use virtual bearing for turret aim.
- Require wheel, hood, turret, and robot-state gates before feeding.
- Log the solution and the measured release event.
Before field testing, check that zero velocity gives the real goal, reversing the velocity reverses the correction, old velocity data blocks firing, a distance off the table reports clamping, and one example matches a hand calculation. These catch sign and frame mistakes that otherwise look like bad tuning.
The robot moves at along the field's axis. The delay plus flight time is and . What correction should be applied to the real target? . The virtual target moves 0.675 m in the negative direction, opposite the inherited chassis velocity.Show solution
- WPILib Fire Control (draft) — the lead-and-adjust overview. These articles are on a preview build of the WPILib docs and may move when published.
- Static Shooting via Look-Up Table — how firing-table resolution, smooth strategy selection, and recorded flight time prepare dynamic shooting.
- Dynamic Shooting via Time-of-Flight Recursion — the mechanism-independent recursion and convergence-based quality diagnostics used to organize this lesson.
- Newton's Method for Dynamic Shooting — a faster root solver and the distinction between numerical convergence and physical sensitivity.
- Linear Drag — an optional induced-windage correction for systems where a first-order drag model is justified.
- This elective's audited research paper — the quadratic-drag model, archived controller constants, convergence audit, acceleration bound, and controlled FTC launch counts.
- Pinned season firmware
75db349d— the deployed flight-time knots and five-update implementation state.