Skip to main content

PID-to-Point

Mediumdifficulty

You now have what autonomous needs: a pose source checked against the Pose Estimation Contract, a mecanum mixer that turns a drive command into wheel powers, and angle wrapping for heading. PID-to-point connects them.

Get to a target pose, with nothing but feedback​

Drive from wherever the robot is to a target pose (x∗,y∗,θ∗)(x^*, y^*, \theta^*) and stop there. The star marks the target. No planned path, no speed plan: each loop, look at how far off you are and push toward the goal.

"Drive at the point at full power until you're close" overshoots, reverses, overshoots again, and circles the target. You want it to come in fast when it's far, ease off when it's close, and end at the right heading without spinning the long way around.

Feedback on the error, in the robot's frame​

Start with how far the target is from the robot, measured along the field's axes. Say the robot is at (10,20)(10, 20) facing 90°90° (toward +y+y), and the target is (10,50)(10, 50) facing 90°90°:

ex=x∗−x,ey=y∗−y,eθ=wrap⁡(θ∗−θ)e_x = x^* - x, \qquad e_y = y^* - y, \qquad e_\theta = \operatorname{wrap}(\theta^* - \theta)

Here ex=0e_x = 0, ey=30e_y = 30 in, and eθ=0e_\theta = 0. The heading error is wrapped, as in Angle Wrapping.

The drivetrain doesn't take field directions, though. It takes forward and sideways in the robot's frame. The robot is facing +y+y, so that 30 in along the field's yy axis is straight ahead of it: 30 in forward, 0 sideways. In general, rotate the error by −θ-\theta, the same rotation as field-centric driving:

[exroboteyrobot]=[cos⁡θsin⁡θ−sin⁡θcos⁡θ][exey]\begin{bmatrix} e_x^\text{robot} \\ e_y^\text{robot} \end{bmatrix} = \begin{bmatrix} \cos\theta & \sin\theta \\ -\sin\theta & \cos\theta \end{bmatrix} \begin{bmatrix} e_x \\ e_y \end{bmatrix}

Check it with the numbers: cos⁡90°=0\cos 90° = 0 and sin⁡90°=1\sin 90° = 1, so exrobot=0⋅0+1⋅30=30e_x^\text{robot} = 0 \cdot 0 + 1 \cdot 30 = 30 and eyrobot=−1⋅0+0⋅30=0e_y^\text{robot} = -1 \cdot 0 + 0 \cdot 30 = 0. Thirty inches forward, as expected.

eₓ (forward)e_y (strafe)field error (eₓ, e_y)robottarget posee_θheading error
PID-to-point: rotate the field error into the robot frame — The blue vector is the field-frame error to the target. Projecting it onto the robot's own forward and left axes splits it into the forward command (amber) and strafe command (green) the mecanum mixer actually wants — plus a separate wrapped heading error.

Now there's a forward distance and a sideways distance, which the drivetrain can act on directly. Run a controller on each. Proportional control is often enough for the two translation directions, with heading on its own gain:

vx=kpxy exrobot,vy=kpxy eyrobot,ω=kpθ eθv_x = k_p^{xy}\, e_x^\text{robot}, \qquad v_y = k_p^{xy}\, e_y^\text{robot}, \qquad \omega = k_p^{\theta}\, e_\theta

Translation uses one gain kpxyk_p^{xy} for both directions so forward and sideways respond the same way. Heading gets its own gain, because its error is in radians and the drivetrain turns and translates with different strength. Those three numbers are exactly the (vx,vy,ω)(v_x, v_y, \omega) command the mecanum mixer takes, and its scaling step keeps the robot heading the right way when the gains ask for more than full power.

If proportional alone overshoots, add a D term on each axis. As in the PID lesson, take the rate of change of the measurement (here, the robot's velocity along that axis) so a new target doesn't cause a kick:

u=kp e−kd vmeasuredu = k_p\, e - k_d\, v_\text{measured}

kStatic: getting over the last bit of friction​

Near the target the error is tiny, so kpek_p e drops below what it takes to overcome friction, and the robot stops a centimeter short. You could add an integral term, but there's a simpler fix: a small, constant push in the direction of the error, only when the robot is already close:

u=kp e−kd vmeasured+kS sign⁡(e)⏟only when ∣e∣ is smallu = k_p\, e - k_d\, v_\text{measured} + \underbrace{k_S\,\operatorname{sign}(e)}_{\text{only when } |e| \text{ is small}}

Only applying it near the target keeps it from acting as a constant bias the rest of the time. Real FTC code does this: SimplicityFTC's PDFSController adds sign(error) * kStatic once |error| is under a threshold, and FTCclueless's drivetrain adds a flat 0.05 * sign(error) near the goal. It's the kSk_S from feedforward, used for position. In code it's called kStatic, from Common Implementation.

Idea
Three small loops, one drivetrain

PID-to-point is three one-dimensional controllers, forward, sideways, and heading, sharing one mixer. Tune heading first, so the robot frame is right, then translation.

Braking on time: predictive deceleration​

Proportional control still has a problem. v=kpev = k_p e is large when far and small when close, which sounds right, but the robot can only slow down so fast. Far out, kpek_p e is well above the speed the robot could stop from in time, so it charges in, brakes too late, and sails past. Lowering kpk_p fixes the overshoot and makes the whole approach sluggish. No single gain is fast and clean.

Ask a different question: given how far there is to go, how fast is the robot allowed to be moving? Say it can slow down at 60 in/s². From 30 in/s, braking takes 30/60=0.530/60 = 0.5 s, at an average of 15 in/s, so it covers 7.5 in. So at 7.5 in from the target, the robot shouldn't be going faster than 30 in/s. In general, stopping from speed vv over distance dd at deceleration aa gives:

v2=2ad⟹vcap=2adv^2 = 2 a d \quad\Longrightarrow\quad v_\text{cap} = \sqrt{2 a d}

Check: 2×60×7.5=900=30\sqrt{2 \times 60 \times 7.5} = \sqrt{900} = 30 in/s. Command the smallest of three speeds: the braking cap, the robot's top speed, and the proportional term for a gentle final approach:

vcmd=min⁡(2ad, vmax, kp d)v_\text{cmd} = \min\big(\sqrt{2 a d},\ v_\text{max},\ k_p\, d\big)
distance to target → (target at left)speedv_maxv = √(2·a·d) limitcommanded profilepure kP·d (overshoots)
Cap the command by the distance left to stop — The braking limit v = √(2·a·d) (green) is the fastest you can go and still stop at the target. Commanding the speed cap (amber) gives a clean trapezoid; a pure-proportional command (rose) is too fast far out, so the robot overshoots.

Far from the target, the braking cap is bigger than vmaxv_\text{max}, so the robot cruises. Closer in, the cap drops below vmaxv_\text{max} and brings the speed down to zero at the target. Following the cap means slowing down at a steady rate the whole way in.

Idea
A motion profile, one loop at a time

This is the slowing-down half of a motion profile. A full profile plans position, speed, and acceleration for the whole move in advance. The 2ad\sqrt{2 a d} cap works it out each loop from the distance left, which is simpler and enough for point-to-point moves.

Real braking isn't quite constant

v=2adv = \sqrt{2ad} assumes the robot always brakes at the same rate. Measure a real drivetrain and its stopping distance fits D(v)=av2+bvD(v) = av^2 + bv better, because of drag and the way motor braking changes with speed. The constant-rate version is the right one to learn with. The Pedro Pathing lesson shows how Pedro fits the real curve.

Another way: predict where the robot would coast to. Flip the same formula around to solve for distance: a robot moving at vv needs v2/2av^2/2a to stop. From 30 in/s at 60 in/s², that's 7.5 in. Add that distance, in the direction the robot is moving, to its current position. That's where it would stop if you cut the power now. Then run the ordinary controller as if the robot were already there:

ppredicted=pnow+v ∥v∥2a\mathbf{p}_\text{predicted} = \mathbf{p}_\text{now} + \frac{\mathbf{v}\,\lVert\mathbf{v}\rVert}{2 a}

The fraction v∥v∥/2a\mathbf{v}\lVert\mathbf{v}\rVert/2a is just "the stopping distance, pointed the way the robot is moving." Filter the velocity first, because it's noisy and gets squared.

While the robot races in, the predicted stopping point is ahead of it. If it's going too fast, the predicted point passes the target, the error flips sign, and the controller brakes on its own, with no separate speed cap. Cyliis's Odo class computes predictedX/Y this way from filtered velocity, and FTCclueless feeds an expected pose into what they call a "predictive PID."

Live demoPredictive braking — the same PID, aimed at where you'd coast to
plain PID — error from the live posepredictive PID — error from the predicted stopbraking limit v = √(2·a·d)
ChallengeCrank the gain until the plain PID overshoots by 40 cm or more while the predictive robot stays within 3 cm — same kP, same decel limit.not yet
Plain PID: —Predictive: —Glide distance now: —t: —

Chaining points: follow a list without stopping​

A real autonomous is a list of poses. Driving to each one and waiting to settle works, but the robot stops at every point along the way, which is slow and jerky. Instead, keep an index into the list, drive toward the current point, and move on to the next one as soon as you're within some radius of it, without settling. Only the last point uses the full "arrived and stopped" check.

if ∥ptarget(i)−probot∥<radvance  and not last ⇒ i←i+1\text{if } \lVert \mathbf{p}_\text{target}^{(i)} - \mathbf{p}_\text{robot}\rVert < r_\text{advance} \ \text{ and not last } \Rightarrow\ i \leftarrow i + 1

The advance radius is looser than the final tolerance, so the robot switches targets while still moving and only brakes for the last point. SimplicityFTC's PidToPointFollower (pointAdvanceRadius) and FTCclueless's drivetrain (pathRadius) both work this way. It's also how a spline, once turned into a list of points, can be followed with PID-to-point.

What's still missing​

kStatic and early braking fix how the robot arrives. What's left is that it always aims straight at one point. To follow a smooth curve through many waypoints, later lessons cover splines and pure pursuit.

A go-to-pose loop​

One pose sample becomes a wrapped field error, robot-frame commands, a mecanum drive call, and a MoveStatus. Lengths are in inches, speeds in inches per second, heading in radians, and the mixer takes normalized power.

FTCPID-to-point (proportional core)
void startMove(Pose2d target) {
activeTarget = target;
timer.reset(); // never reset this inside updateMove()
}

MoveStatus updateMove() {
if (activeTarget == null) return null;
PoseSample sample = poseSource.getSample();
MoveStatus status = moveStatus(sample, activeTarget, timer.seconds());

if (status != RUNNING) {
drive(0, 0, 0); // REACHED and TIMED_OUT both stop
return status;
}

commandToward(activeTarget, sample.pose());
return RUNNING;
}

void commandToward(Pose2d target, Pose2d poseForError) {
double ex = target.x() - poseForError.x(); // field frame
double ey = target.y() - poseForError.y();
double eh = wrap(target.heading() - poseForError.heading());

double c = Math.cos(poseForError.heading());
double s = Math.sin(poseForError.heading());
double exBot = ex * c + ey * s; // into the robot frame
double eyBot = -ex * s + ey * c;

double vx = kPxy * exBot + kStatic(exBot);
double vy = kPxy * eyBot + kStatic(eyBot);
double w = kPheading * eh;

drive(vx, vy, w); // the mixer desaturates
}

commandToward takes the target and the pose to measure the error from as separate arguments. Here the second one is just the robot's pose. The predictive version further down passes the predicted stopping point instead, and nothing else changes.

ExerciseWhy rotate by minus the heading

The robot faces +90°+90° and the target is straight ahead of it, which on the field is the +y+y direction. What should exBot and eyBot come out to? What would happen if you rotated by +heading+\text{heading} instead?

Show solution

exBot should be the whole distance and eyBot zero, since straight ahead is +x+x for the robot. With heading=90°\text{heading} = 90°, c=0c = 0 and s=1s = 1, so exBot = ey and eyBot = -ex. The target is purely in +y+y, so that's the full distance in exBot and nothing in eyBot. Rotating by +heading+\text{heading} would send the robot sideways, at right angles to the target.

The "done" check matters as much as the controller. Check position, heading, and that the robot is actually slow, or a move will report done while the robot coasts through the point. The timeout keeps a stuck move, jammed on a wall or confused by a bad pose, from hanging the routine. That's the shared MoveStatus check. Both outcomes stop the robot, but only REACHED is a success.

To follow a list of poses without stopping at each one:

FTCFollowing a list of poses (point-advance)
void startPath(List<Pose2d> newPoints) {
points = new ArrayList<>(newPoints);
index = 0;
timer.reset();
}

MoveStatus follow() {
if (points.isEmpty()) {
drive(0, 0, 0);
return REACHED; // nothing to do is not a failure
}

PoseSample sample = poseSource.getSample();
Pose2d robot = sample.pose();
boolean last = index == points.size() - 1;

if (!last && robot.vec().minus(points.get(index).vec()).norm()
< ADVANCE_RADIUS) {
index++;
timer.reset(); // each leg gets its own deadline
last = index == points.size() - 1;
}

MoveStatus status = moveTowardTarget(sample, points.get(index)); // as in updateMove()

if (status == TIMED_OUT) return TIMED_OUT;
if (last) return status; // only the last leg may say REACHED
return RUNNING;
}

moveTowardTarget is the body of updateMove() above with the target passed in: check moveStatus, stop if it's done, otherwise call commandToward.

ExerciseWhy ADVANCE_RADIUS is looser than POS_TOL

What would the robot do if it had to settle inside POS_TOL at every point along the way?

Show solution

Settling also means being slow, because of the velocity check in moveStatus, so the robot would slow to a stop at every point, wait, and speed up again. The path turns into a series of lurches, takes several times as long, and every stop and start pulls a current spike. ADVANCE_RADIUS means "close enough to start aiming at the next one," so corners round off and the robot keeps moving.

To use the braking cap, limit the size of the command before the mixer:

FTCDistance-capped approach speed
static final double MAX_SPEED = 40; // inches/second
static final double MAX_DECEL = 60; // inches/second^2, positive magnitude

double brakingCap(double distance) {
double vStop = Math.sqrt(2 * MAX_DECEL * distance);
double vProportional = kPxy * distance * MAX_SPEED;
return Math.min(Math.min(vStop, MAX_SPEED), vProportional) / MAX_SPEED;
}

void driveCapped(double exBot, double eyBot, double w) {
double dist = Math.hypot(exBot, eyBot);
double cap = brakingCap(dist);
double dir = Math.atan2(eyBot, exBot);
drive(cap * Math.cos(dir), cap * Math.sin(dir), w);
}
ExerciseDerive the braking cap

A robot at speed vv braking at MAX_DECEL needs v2/(2a)v^2/(2a) inches to stop. Turn that around into "the fastest I can be going when I'm this far away."

Then: why does the code work out the direction once and cap the speed, instead of capping exBot and eyBot separately? Try exBot = 30, eyBot = 40, and cap each at 20.

Show solution

Set the stopping distance equal to the distance left: v2/(2a)=sv^2/(2a) = s, so v=2asv = \sqrt{2as}.

(30,40)(30, 40) points 53.1° off the robot's nose. Capped separately to (20,20)(20, 20), it points at 45°, so the robot drives somewhere it wasn't asked to go. Capping the size and rebuilding from dir keeps the direction, for the same reason the mecanum mixer scales all four wheels together.

Predictive stopping​

The predictive version changes the pose the error is measured from, never the target:

FTCGlide-distance predicted pose
// Where this sample says the robot will coast to a stop.
Pose2d predictedPose(PoseSample sample) {
Pose2d p = sample.pose();
Vector2d v = sample.fieldVelocity(); // filter this; raw velocity is noisy
double speed = v.norm();
if (speed < EPS) return p; // standing still: no glide

double glide = speed * speed / (2 * MAX_DECEL); // the braking distance
return new Pose2d(
p.x() + (v.x() / speed) * glide,
p.y() + (v.y() / speed) * glide,
p.heading()); // only translation is predicted
}

MoveStatus updatePredictiveMove() {
if (activeTarget == null) return null;
PoseSample sample = poseSource.getSample();
MoveStatus status = moveStatus(sample, activeTarget, timer.seconds());
if (status != RUNNING) {
drive(0, 0, 0);
return status;
}

commandToward(activeTarget, predictedPose(sample));
return RUNNING;
}

In that last call the target is the same, and only the pose the error comes from is predicted. Say the target is at x=10x = 10, the robot is at x=2x = 2, and its predicted stopping point is x=8x = 8. The error is +2+2 instead of +8+8, so the robot eases off early instead of charging in and overshooting.

ExerciseThe unit vector, and a noisy velocity

Why is v divided by speed, and what happens if you forget? And what does this controller do when fieldVelocity() is noisy and briefly reads double the true value?

Show solution

v / speed is a length-1 vector pointing the way the robot is moving, so multiplying by glide moves exactly glide inches in that direction. Skip the division and you move speed * glide inches, which grows with the cube of the speed and puts the predicted point across the field.

A doubled velocity reading makes glide four times bigger, since it depends on v2v^2. The predicted point jumps past the target, the error flips sign, and the robot brakes hard or even reverses for a loop. That's why the velocity needs filtering: the prediction amplifies its noise, and velocity is the noisiest thing the pose source gives you.

If you use an integral term

An integral can also fix stopping short, but if it keeps adding up while the robot is maxed out or stuck against a wall, it grows huge and the robot lurches when it comes free. Cap it and only let it run near the target, as in Anti-Windup.

Tune heading first

A wrong heading rotates the whole translation command, so a heading bug looks like a translation bug. Get kpθk_p^{\theta} holding heading cleanly before tuning translation.

Real braking is weaker than you'd think

MAX_DECEL is limited by traction, battery voltage, and weight shifting, not by the motors. Measure it on the actual field surface and leave some margin so a slippery tile doesn't turn your planned stop into a slide. And filter the velocity before computing the glide distance, since it gets squared. (Cyliis filters their odometry velocity first.)

Sources & references

The production techniques on this page are adapted, with attribution, from these open-source FTC team repositories:

  • kStatic static feedforward (a constant push toward the target applied only once inside a small error band) — SimplicityFTC-Quickstart (PDFSController) and FTCclueless/Centerstage (subsystems/drive/Drivetrain.java).
  • Point-advance sequencing (advancing to the next waypoint within an advance radius so the robot never stops mid-path) — SimplicityFTC-Quickstart (PidToPointFollower, pointAdvanceRadius) and FTCclueless/Centerstage (Drivetrain.java, pathRadius).
  • Predictive deceleration — glide-distance predicted stopping pose (compute p+v∥v∥/2a\mathbf{p} + \mathbf{v}\lVert\mathbf{v}\rVert/2a from low-pass-filtered velocity and run PID against it) — Cyliis_Into_The_Deep_OfficialCode (Wrappers/Odo.java, predictedX/Y) and FTCclueless/Centerstage (Drivetrain.java, the "predictive PID" on the expected pose).

The code here is an independent reimplementation of these ideas, not a copy of any one repository.