Guided Vector Fields
Harddifficulty
Read Pure Pursuit and Splines first, and check your localization with the Pose Estimation Contract. The math is vectors, unit vectors, dot products, and finding the closest point on a path.
Pure pursuit chases a point a fixed distance ahead. It works, but it's following the point, not the path, so it cuts corners and behaves differently as you change the lookahead. A guided vector field replaces the point with a field of arrows the robot follows. Drag the robot around in the demo and watch the flow line bend back onto the path.
Drag the robot or a path handle, or focus one and use the arrow keys; hold Shift for a larger step.
Chasing a point is not the same as being on the path
A lookahead follower aims at a point ahead of itself. On a curve, the straight line to that point runs inside the curve, so the robot cuts the corner. Shorten the lookahead and it weaves; lengthen it and it cuts more. The controller never directly works on "how far am I from the path."
A GVF does. The distance from the path is exactly what the field works to remove. Knock the robot sideways and it just follows the arrows back. The THOR library describes it as: "you could run up and kick the robot during auto, and it will move back to the programmed path automagically."
Two desires, blended into one direction
Start from the robot's position and find the closest point on the path. At that point you know which way the path is going, a length-1 tangent , and which way is sideways, the tangent turned 90°, called the normal . The cross-track error is how far the robot is off the path along the normal, with a sign that says which side:
For a straight path along the axis, the tangent is and the normal is . A robot at has its closest point at , so : three inches off, on the right-hand side.
Everything depends on finding that closest point correctly. Drag the point in the demo and check that the closest point, tangent, and normal do what you expect:
Drag a handle, or focus it and use the arrow keys; hold Shift for a larger step.
The closest point is only clearly defined when the robot is fairly near the path. If the path doubles back close to itself, or the robot is far away, two points can be equally close, so real code only searches near where it was last loop.
The field combines two directions into one:
is the direction to travel. With the robot 3 in to the right of the path () and
per inch, : mostly forward, leaning 31°
back toward the path.
On the path, and the arrow points straight along it. The farther off the robot is, the more the arrow leans back toward the path. sets how hard it leans. Too small and the robot merges back lazily. Too large and the arrows point almost straight at the path, so the robot overshoots and zig-zags. It's the one setting really worth tuning, so move in the demo and watch the merge go from long and gentle to sharp.
Why it converges
Near the path, the leaning term always pushes the robot toward it, so the distance from the path keeps shrinking and never grows. That holds close to the path, where the closest point is clearly defined, for the ideal field. It isn't a promise for any spline from any starting point, or for a drivetrain that's already at full power. The full proof is in Kapitanyuk, Proskurnikov, and Cao's A Guiding Vector Field Algorithm for Path Following Control.
To turn the direction into a velocity, make length 1 and multiply by the speed you want:
is a fraction of top speed, from 0 to 1, and eases from 1 down to 0 over the last
stretch of the path so the robot slows as it arrives. At the very end, hand over to the shared terminal
controller, which reports REACHED or TIMED_OUT.
Slowing down for curves
The field says which way to go, not how fast. As in adaptive pure pursuit, limit speed where the path bends so the robot can actually make the turn. With curvature and a sideways-acceleration limit , the fastest safe speed is . KookyBotz's follower adds a tuning factor :
A larger makes the robot slower through every bend. On a straight, is zero and the limit goes away. In a tight curve it pulls the speed down.
Sampling the field each loop
Each loop: find the closest point on the path (with its tangent and curvature), build the field direction and limit the speed, then rotate the field-frame velocity into the robot's frame for the mixer.
// kN converge gain, 1/inch: how hard the field pulls back to the path
// kS speed scale, 0..1; kC curvature caution: bigger is slower in bends
// kStatic stiction feedforward on the strafe axis
void startPath(Path nextPath) {
path = nextPath;
lastT = 0;
timer.reset();
}
Vector2d unitTangent(double t) {
Vector2d raw = path.tangent(t);
if (!raw.isFinite() || raw.norm() < EPS) {
drive(0, 0, 0);
throw new IllegalStateException("cusp: no forward direction at " + t);
}
return raw.times(1 / raw.norm());
}
// Returns the desired FIELD-frame chassis velocity.
Vector2d gvfVelocity(Pose2d robot) {
double prior = lastT;
double candidate = path.project(robot.vec(), prior);
double t = clamp(candidate, prior - MAX_BACKTRACK, prior + MAX_FORWARD);
lastT = t;
Vector2d c = path.get(t); // the closest point
Vector2d tangent = unitTangent(t); // along-track direction
Vector2d normal = tangent.rotate(Math.PI / 2);
Vector2d offset = robot.vec().minus(c);
double e = offset.dot(normal); // + means on the +normal side
Vector2d chi = tangent.minus(normal.times(kN * e)).unit();
double k = Math.abs(path.curvature(t));
double vmax = MAX_LINEAR_SPEED;
if (k > EPS) vmax = Math.min(vmax, Math.sqrt(MAX_ACCEL / (kC * k)));
double endScale = clamp((1 - t) / END_SLOWDOWN, 0, 1);
return chi.times(vmax * kS * endScale);
}
A zero tangent means the path has a sharp point or a duplicated waypoint, where there's no "forward." The follower stops instead of dividing by zero and heading off in a random direction.
The search starts from last loop's and only accepts a small change. What would a search over the
whole path do on a path that doubles back near itself? It picks whichever branch is closer by a hair, which can be the later one even though the robot hasn't
driven the loop yet. The follower jumps ahead, thinks it's nearly done, and cuts across. A few
millimeters of wobble can also flip the answer between branches from one loop to the next, so the
direction swings back and forth. Limiting the change keeps progress realistic: can only move as far
in one loop as the robot could actually travel.Show solution
What is At the sideways term is zero and As chi when the robot is on the path ()? When is large? What happens as kN gets very
large? Compare the lean for a 1 in error with kN = 0.1 and with kN = 5.Show solution
chi is just the tangent. For large the sideways term wins
and chi points almost straight at the path.kN grows, the lean approaches 90° for any error, so the robot dives at the path, arrives moving
across it instead of along it, overshoots, and oscillates. With kN = 0.1, a 1 in error leans the arrow
about 6°, a slow, gentle merge. With kN = 5, the same inch leans it about 79°, nearly straight at the
path.
Progress comes from where the robot is, not from a clock. Pick MAX_BACKTRACK and MAX_FORWARD from how
far the robot can move in one loop.
Heading has its own controller. KookyBotz uses a nice trick: instead of a plain P term, command
, the fastest turn rate that
can still stop on the target heading. It's the same braking formula as
PID-to-point, for turning. A small kStatic push on the strafe axis also keeps tiny
corrections from getting stuck on friction.
MoveStatus update(PoseSample sample) {
Pose2d robot = sample.pose();
Vector2d v = gvfVelocity(robot); // this also advances lastT
MoveStatus completion = moveStatus(sample, path.endPose(), timer.seconds());
if (completion == TIMED_OUT
|| (completion == REACHED && lastT >= 1 - TERMINAL_TOL)) {
drive(0, 0, 0);
return completion;
}
if (lastT >= 1 - TERMINAL_HANDOFF) {
return terminalController.update(sample, path.endPose());
}
Vector2d body = v.rotate(-robot.heading());
double fwd = body.x() / MAX_LINEAR_SPEED;
double str = body.y() / MAX_LINEAR_SPEED;
Vector2d tangent = unitTangent(lastT);
double targetHeading = Math.atan2(tangent.y(), tangent.x());
double dTheta = wrap(targetHeading - robot.heading());
double turn = Math.min(MAX_TURN, Math.sqrt(2 * A_THETA * Math.abs(dTheta)))
* Math.signum(dTheta) / MAX_TURN;
// Strafe is the axis mecanum wheels fight hardest.
str += Math.signum(str) * kStatic;
drive(fwd, str, turn);
return RUNNING;
}
The field's speed goes to zero at , so it approaches the end but never settles there. That's why it hands off to a point controller.
A timeout ends the move anywhere, but A path that ends near where it starts would report REACHED only counts once lastT says the robot is actually at
the end of the path. What does that prevent?Show solution
REACHED on the first loop: the robot is within
POS_TOL of the end and not moving yet, so moveStatus is satisfied before it has driven anywhere.
Requiring lastT >= 1 - TERMINAL_TOL means the robot has to have actually driven the path.
That A proportional turn commands a rate proportional to the error, so on a big error it maxes out and then
starts slowing far too late, reaching the target heading while still turning fast. The square root
follows the braking curve: at each error it asks for the fastest rate that can still slow to zero at
sqrt is again, for turning. What does a plain proportional turn do differently on a
large heading error?Show solution
A_THETA. On big errors that's much slower than proportional would ask for.
drive sends fwd, str, and turn through the mecanum mixer, and the
heading error uses angle wrapping. The types and MoveStatus come from
Common Implementation.
Tune by symptom
| What you see | Likely cause | First change |
|---|---|---|
| Long, lazy merge onto the path | too small | raise slightly |
| Zig-zag across the path | or speed too high | lower , then lower speed |
| Closest point jumps to another branch | projection is nonunique/search is too broad | constrain the search around prior |
| Direction becomes zero or undefined | degenerate/near-zero path tangent | repair or reject that path segment |
| Robot sails through the endpoint | no terminal slowdown/handoff | taper and use MoveStatus |
Everything depends on finding the closest point. On a spline that's a small search every loop, so start from last loop's value and take a few steps from there. Otherwise it's slow, and worse, it can jump to a far part of a path that comes back near itself. If the direction ever comes out as zero, the path has a bad spot, like a duplicated point, and if it jumps suddenly, the closest-point search jumped. Fix those path segments rather than hiding the problem. And keep reasonable, since a huge value makes the robot zig-zag.
For point-to-point moves, PID-to-point is plenty. For planned curves at speed, a GVF, or a well-tuned pure pursuit on a quintic spline, tracks most tightly and repeatably. What GVF does especially well is recover: bump it and it slides right back onto the path.
- Y. A. Kapitanyuk, A. V. Proskurnikov & M. Cao, A Guiding Vector Field Algorithm for Path Following Control (arXiv:1610.04391): the rigorous theory. The implicit/level-set field and its Lyapunov convergence proof come from here.
- FTC-9974-THOR/GuidedVectorField: a reference FTC GVF library. Bézier-path guidance and the disturbance-rejection framing ("kick the robot and it comes back") this lesson quotes.
- KookyBotz/CenterStage
GVFCommand— the shipped implementation we model the Deploy on. The // gains, signed cross-track error, the curvature speed cap , seeded Newton-Raphson projection, the heading profile, and the kStatic feedforward all trace to this. - FTC Tech Toolbox — Guided Vector Fields — the FTC-level intro (field of vectors over a Bézier spline; closest-point + derivative → field-centric command) that frames this lesson.
All code here is an independent reimplementation, not a copy. See the full references.