Skip to main content

Guided Vector Fields

Harddifficulty

Prerequisites and a starting path

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.

Live demoThe guiding vector field: test local recovery on either side
drag the robot or blue path handles

Drag the robot or a path handle, or focus one and use the arrow keys; hold Shift for a larger step.

cross-track error e: -22.6 infield here: (0.48, 0.88)low kN: glides in gently · high kN snaps in (overshoots)
path (○ = end)robotflow line it would followfield arrows (teal near path → blue far off)Bézier control points

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 p\mathbf{p} and find the closest point c\mathbf{c} on the path. At that point you know which way the path is going, a length-1 tangent t^\hat{\mathbf{t}}, and which way is sideways, the tangent turned 90°, called the normal n^\hat{\mathbf{n}}. The cross-track error is how far the robot is off the path along the normal, with a sign that says which side:

e=(p−c)⋅n^e = (\mathbf{p} - \mathbf{c}) \cdot \hat{\mathbf{n}}

For a straight path along the xx axis, the tangent is (1,0)(1, 0) and the normal is (0,1)(0, 1). A robot at (5,−3)(5, -3) has its closest point at (5,0)(5, 0), so e=(0,−3)⋅(0,1)=−3e = (0, -3) \cdot (0, 1) = -3: 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:

Live demoClosest-point projection: drag the robot and watch psi(p) move
phi(psi(p))drag the pink robot or blue Bezier handles

Drag a handle, or focus it and use the arrow keys; hold Shift for a larger step.

psi(p): 0.211distance: 11.9 insigned error e: -11.9 incondition: error line ⊥ tangent at the closest point
path phi(t)robot p and error vectorunit tangent Tunit normal NBezier control handles

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:

χ  =  t^⏟flow along  −  kN e n^⏟converge onto\boldsymbol{\chi} \;=\; \underbrace{\hat{\mathbf{t}}}_{\text{flow along}} \;-\; \underbrace{k_N\,e\,\hat{\mathbf{n}}}_{\text{converge onto}}

χ\boldsymbol{\chi} is the direction to travel. With the robot 3 in to the right of the path (e=−3e = -3) and kN=0.2k_N = 0.2 per inch, χ=(1,0)−0.2(−3)(0,1)=(1,0.6)\boldsymbol{\chi} = (1, 0) - 0.2(-3)(0, 1) = (1, 0.6): mostly forward, leaning 31° back toward the path.

pathrobottangent t̂error ev_des
A guided vector field: flow along, converge onto — Every point on the field gets a desired direction: the path tangent minus a pull toward the path that grows with the cross-track error. Far off the path the arrows point onto it; on the path they align with it. The robot just follows the arrow under it.

On the path, e=0e = 0 and the arrow points straight along it. The farther off the robot is, the more the arrow leans back toward the path. kNk_N 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 kNk_N 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 χ\boldsymbol{\chi} length 1 and multiply by the speed you want:

uxy=v χ∥χ∥,v=kS vmax⁡ send(t)\mathbf{u}_{xy} = v\,\frac{\boldsymbol{\chi}}{\lVert \boldsymbol{\chi}\rVert}, \qquad v = k_S\,v_{\max}\,s_{\mathrm{end}}(t)

kSk_S is a fraction of top speed, from 0 to 1, and sends_{\mathrm{end}} 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 κ\kappa and a sideways-acceleration limit amax⁡a_{\max}, the fastest safe speed is amax⁡/∣κ∣\sqrt{a_{\max}/|\kappa|}. KookyBotz's follower adds a tuning factor kCk_C:

vmax⁡  =  min⁡ ⁣(vmax⁡,  amax⁡kC ∣κ∣)v_{\max} \;=\; \min\!\left(v_{\max},\;\sqrt{\frac{a_{\max}}{k_C\,|\kappa|}}\right)

A larger kCk_C makes the robot slower through every bend. On a straight, κ\kappa 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.

FTCGains and path lifecycle
// 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());
}
FTCGuided vector field velocity
// 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.

ExerciseWhy the projection is seeded and clamped

The search starts from last loop's tt and only accepts a small change. What would a search over the whole path do on a path that doubles back near itself?

Show solution

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: tt can only move as far in one loop as the robot could actually travel.

ExerciseThe two extremes of chi

What is chi when the robot is on the path (e=0e = 0)? When ee 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

At e=0e = 0 the sideways term is zero and chi is just the tangent. For large ee the sideways term wins and chi points almost straight at the path.

As 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 ω=min⁡ ⁣(ωmax⁡,2 aθ ∣Δθ∣)\omega = \min\!\big(\omega_{\max}, \sqrt{2\,a_\theta\,|\Delta\theta|}\big), 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.

FTCLifecycle-aware GVF update and terminal handoff
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 t=1t = 1, so it approaches the end but never settles there. That's why it hands off to a point controller.

ExerciseRead the terminal condition closely

A timeout ends the move anywhere, but REACHED only counts once lastT says the robot is actually at the end of the path. What does that prevent?

Show solution

A path that ends near where it starts would report 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.

ExerciseWhy the turn uses a square root

That sqrt is v2=2asv^2 = 2as again, for turning. What does a plain proportional turn do differently on a large heading error?

Show solution

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 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 seeLikely causeFirst change
Long, lazy merge onto the pathkNk_N too smallraise kNk_N slightly
Zig-zag across the pathkNk_N or speed too highlower kNk_N, then lower speed
Closest point jumps to another branchprojection is nonunique/search is too broadconstrain the search around prior tt
Direction becomes zero or undefineddegenerate/near-zero path tangentrepair or reject that path segment
Robot sails through the endpointno terminal slowdown/handofftaper sends_{\mathrm{end}} and use MoveStatus
Finding the closest point reliably

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 kNk_N reasonable, since a huge value makes the robot zig-zag.

Where GVF sits in the toolbox

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.

Sources & references
  • 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 χ=E∇φ−kφ∇φ\boldsymbol{\chi}=E\nabla\varphi - k\varphi\nabla\varphi 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 kNk_N/kSk_S/kCk_C gains, signed cross-track error, the curvature speed cap amax⁡/(kC∣κ∣)\sqrt{a_{\max}/(k_C|\kappa|)}, seeded Newton-Raphson projection, the 2a∣Δθ∣\sqrt{2a|\Delta\theta|} 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.