Skip to main content

Compensate for Robot Motion

Mediumdifficulty

Builder outcome and prerequisites

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:

  1. the flywheel command;
  2. the hood command;
  3. 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)5060768595110130
Rim flight time (s)0.500.540.600.700.800.951.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 tdt_d plus the flight time tft_f, it drifts by about the robot's velocity vR\mathbf v_R times that time. Aim that far the other way. This robot also used a measured gain GG for how much of the robot's velocity the ball actually keeps:

pv=pg−GvR(td+tf).\mathbf p_v=\mathbf p_g-G\mathbf v_R(t_d+t_f).

pg\mathbf p_g is the real goal and pv\mathbf p_v is the adjusted aim point, the "virtual target." G=0.9G=0.9 and td=0.05 st_d=0.05\ \mathrm{s}. 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:

  1. Guess flight time from the real-target distance.
  2. Lead by shifting the virtual target through the predicted platform displacement.
  3. Adjust by looking up flight time at the virtual distance.
  4. 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.

Predict the correction direction

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.

Moving-shot experimentAim at the target the ball needs
top view · update 1 of 2robotreal targetvirtual targetfield position in the horizontal plane
real targetvirtual aim pointrobot velocity
50 msfeeder delay
0.800 srim flight time
0.850 s from command to rim
Initial real-target time0.800 sfrom the archived map
Virtual-target shift1.148 mopposite inherited velocity
Virtual distance2.672 m105.2 inches
Solver statusConverged2 of 5 updates
Final time residual0.024 sinside the archived tolerance
Local contractionNeed 3 updatesno reliable ratio yet
Updatetime usedvirtual distancenext time|Δt|
10.800 s105.2 in0.907 s0.107 s
20.907 s107.8 in0.932 s0.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 tf=0.80 st_f=0.80\ \mathrm{s}. For a sideways chassis speed of 1.5 m/s1.5\ \mathrm{m/s},

∥Δp∥=0.9(1.5)(0.05+0.80)=1.1475 m.\lVert\Delta\mathbf p\rVert =0.9(1.5)(0.05+0.80) =\mathbf{1.1475\ m}.

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

tk+1=tf ⁣(∥pg−GvR(td+tk)−ps∥).t_{k+1}=t_f\!\left(\left\lVert \mathbf p_g-G\mathbf v_R(t_d+t_k)-\mathbf p_s \right\rVert\right).

Once you have three estimates, you can see how fast they're closing in:

ρ^=∣tk+1−tktk−tk−1∣.\widehat\rho=\left|\frac{t_{k+1}-t_k}{t_k-t_{k-1}}\right|.

A small ρ^\widehat\rho 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 M=0.4867 s/mM=0.4867\ \mathrm{s/m}, so

q=G∥vR∥M<1q=G\lVert\mathbf v_R\rVert M<1

guarantees the loop settles on one answer. With G=0.9G=0.9 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.

SettingPaper's season snapshotNewer snapshot defaults
Inheritance gain0.91.30
TOF at 95 inches0.80 s0.70 s
TOF at 130 inches1.05 s0.90 s
Flight-time weightingone full TOF0.55 radial, 0.7 lateral
Acceleration correctiondisabledradial factor 0.17; lateral factor 0
Position reference in the updaterobot gettersshooter 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:

E(t)=t−tf(D(t))=0,E(t)=t-t_f(D(t))=0,

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 tt in the drift calculation with

α(t)=1−e−ktk,\alpha(t)=\frac{1-e^{-kt}}{k},

so the drift becomes vRα(t)\mathbf v_R\alpha(t), 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 G=0.9G=0.9 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

∥δpa∥≤∥aR∥(tdtf+td22),\lVert\delta\mathbf p_a\rVert\le \lVert\mathbf a_R\rVert\left(t_dt_f+\frac{t_d^2}{2}\right),

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 stateStationary0.5 m/s0.8 m/s1.5 m/s1.7 m/s
SOTM OFF15/1513/1510/155/152/15
SOTM ON15/1515/1515/1513/1511/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​

FTCSolve and report a moving-shot aim point
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​

  1. Verify pose and velocity timestamps, units, and field frame.
  2. Compute the shooter world position, including its offset from the robot center.
  3. Solve the virtual target and expose convergence, clamping, residual, and iteration count.
  4. Reject invalid, stale, out-of-range, or unconverged results.
  5. Use virtual distance for wheel and hood; use virtual bearing for turret aim.
  6. Require wheel, hood, turret, and robot-state gates before feeding.
  7. 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.

ExerciseChoose the sign

The robot moves at +1.0 m/s+1.0\ \mathrm{m/s} along the field's yy axis. The delay plus flight time is 0.75 s0.75\ \mathrm{s} and G=0.9G=0.9. What yy correction should be applied to the real target?

Show solution

−0.9(1.0)(0.75)=−0.675 m-0.9(1.0)(0.75)=-0.675\ \mathrm{m}. The virtual target moves 0.675 m in the negative yy direction, opposite the inherited chassis velocity.

Sources and further reading

Next: test, diagnose, and hand off the system.