Skip to main content

Common Implementation

Easydifficulty

Set up your localization library with the Pose Estimation Contract first. After that, the lessons in this module call things like robot.vec(), wrap(error), moveStatus(...), and drive(vx, vy, omega), and this page is where those are defined. Libraries like Road Runner and FTCLib have their own versions; these are the minimal ones the lessons assume.

What matters here are the names and signatures. Write the method bodies yourself, but keep the names as spelled, because later lessons refer to them.

Geometry: Vector2d, Pose2d, and PoseSample​

A vector type supplies what the followers need: add, subtract, scale, dot, cross, length, and rotate. A Pose2d is just a position and a heading. The pose source returns one PoseSample holding the measured pose, its velocity in the field frame, and a timestamp, all from the same reading. Field coordinates are +x+x toward the far wall, +y+y to the left, and heading counter-clockwise.

The blocks below use plain Java 8 classes, since that's what the FTC SDK compiles by default.

Vector2d
// A 2-D vector, in field or robot coordinates.
final class Vector2d {
final double x, y;
Vector2d(double x, double y) { this.x = x; this.y = y; }
double x() { return x; }
double y() { return y; }

Vector2d plus(Vector2d b) { ... }
Vector2d minus(Vector2d b) { ... }
Vector2d times(double s) { ... }

double dot(Vector2d b) { ... } // zero when perpendicular
double cross(Vector2d b) { ... } // signed; the z of the 3-D cross

double norm() { ... }
Vector2d unit() { ... } // unchanged if norm is ~ 0

// (x cos t - y sin t, x sin t + y cos t) -- CCW positive
Vector2d rotate(double t) { ... }
}

The comment above rotate is the rotation formula: each output coordinate is one row of a rotation matrix.

Pose2d and PoseSample
final class Pose2d {
final double x, y, heading; // inches, inches, radians
// constructor and x(), y(), heading() accessors as in Vector2d
Vector2d vec() { return new Vector2d(x, y); }
}

// One reading from the pose source; all three fields from the same update.
final class PoseSample {
final Pose2d pose; // where we are
final Vector2d fieldVelocity; // field frame, inches/second
final double timestampSeconds; // when this reading was taken
// constructor and pose(), fieldVelocity(), timestampSeconds() accessors
}

Never build a PoseSample from a pose read this loop and a velocity read two loops ago. Pose2d has no velocity on purpose. If it did, a target pose would carry a velocity that was never measured, and sooner or later some code would read it.

ExerciseCheck rotate on an easy case

Rotating (1,0)(1, 0) by 90° has to give (0,1)(0, 1). Does it?

Show solution

At t=π/2t = \pi/2, cos⁡t=0\cos t = 0 and sin⁡t=1\sin t = 1, so the first row gives 1(0)−0(1)=01(0) - 0(1) = 0 and the second gives 1(1)+0(0)=11(1) + 0(0) = 1. That's (0,1)(0, 1), pointing +y+y, to the left, which is counter-clockwise. If you get (0,−1)(0, -1), a sine has the wrong sign, and every heading in every follower will be mirrored.

Angle wrapping​

Heading errors are wrapped to [−π,π)[-\pi, \pi) so the robot always turns the short way, as explained in Angle Wrapping:

wrap(angle) -> [-pi, pi)
// The angle in [-pi, pi) pointing the same direction as a.
static double wrap(double a) {
double turn = 2 * Math.PI;
return ((a + Math.PI) % turn + turn) % turn - Math.PI;
}
ExerciseThree values by hand

What should wrap give for 3.5π3.5\pi, for −3.0-3.0 rad, and for exactly π\pi?

Show solution

3.5π3.5\pi wraps to −0.5π-0.5\pi. −3.0-3.0 rad is already in range (just under π\pi in size), so it comes back unchanged. Exactly π\pi comes back as −π-\pi, since the range includes −π-\pi but not +π+\pi. For a kP * error term that doesn't matter, because both directions are the same distance. It does matter for code that logs headings or compares them for equality.

The completion outcome: MoveStatus​

This is step 5 from the module overview. A follower has to tell the difference between reaching the target and running out of time. Both stop the drivetrain, but only REACHED should let the autonomous routine carry on as planned.

One completion outcome, used by every follower
// Tolerances: tuned once, here, and nowhere else.
static final double POS_TOL = 1.0; // inches
static final double HEADING_TOL = Math.toRadians(2);
static final double VEL_TOL = 2.0; // inches/second
static final double TIMEOUT = 3.0; // seconds, one move

enum MoveStatus { RUNNING, REACHED, TIMED_OUT }

static MoveStatus moveStatus(PoseSample sample, Pose2d target,
double timeInMove) {
double dist = sample.pose().vec().minus(target.vec()).norm();
double headingErr = Math.abs(
wrap(target.heading() - sample.pose().heading()));

boolean settled = dist < POS_TOL
&& headingErr < HEADING_TOL
&& sample.fieldVelocity().norm() < VEL_TOL;

if (timeInMove > TIMEOUT) return TIMED_OUT;
if (settled) return REACHED;
return RUNNING;
}
ExerciseOrder of the two checks

The deadline is checked before settled. Suppose the robot drifts into tolerance just after the timeout. Which answer do you want, and what goes wrong if the routine calls it a success?

Show solution

Say a robot got pushed, recovered slowly, and reached tolerance at 3.1 s on a 3.0 s deadline. You want TIMED_OUT. The deadline is there to limit how long auto spends on one move, and calling it REACHED lets the routine carry on as if everything went to plan, already behind schedule, with nothing flagging that anything went wrong.

ExerciseWhy velocity is in the settled test

Delete sample.fieldVelocity().norm() < VEL_TOL. What does a robot crossing the target at full speed report now?

Show solution

REACHED, for the one loop it happens to be within an inch of the target, while moving at 40 inches per second and about to be a foot past it. The next move then starts from a place the robot isn't. The velocity check makes "reached" mean "arrived and stopped," not "passed through."

Pick TIMEOUT from how long the move should take plus some time to settle.

Idea
Why only one copy

If every follower writes its own version of this check, they drift apart: one checks velocity and heading, another only distance. With one moveStatus, every follower stops the same way, and the autonomous routine decides separately whether to carry on, retry, park, or give up.

kStatic: the static-friction nudge​

Near the target, the proportional command gets too small to overcome friction, and the robot stops just short. A small constant push toward the target, only within a few inches of it, fixes that. It's the kSk_S term from feedforward, and PID-to-point uses it.

FTCkStatic(error)
static final double K_STATIC = 0.06; // power units
static final double K_STATIC_RANGE = 4.0; // inches, outer edge of the band

// error is robot-frame and signed. Returns a constant push in the direction
// of the error, or zero.
static double kStatic(double error) {
double e = Math.abs(error);
if (e > K_STATIC_RANGE) return 0; // far out: kP is strong enough alone
if (e < POS_TOL) return 0; // settled: stop pushing
return K_STATIC * Math.signum(error);
}
ExerciseBoth edges of the band

What happens if you remove the inner edge of the band, the POS_TOL check? Remember that sign(error) flips every time the robot crosses the target.

Show solution

The push never turns off, so at the target it flips between +0.06+0.06 and −0.06-0.06 whenever noise flips the error's sign, which can be every loop. The robot jitters back and forth, its velocity never drops below VEL_TOL, and the move times out on a target it reached two seconds earlier.

The drivetrain entry point: drive(vx, vy, omega)​

Every follower ends by handing a drive command (vx,vy,ω)(v_x, v_y, \omega) to the mecanum mixer from Mecanum Kinematics, which turns it into four wheel powers and scales them down if needed:

FTCThe one call every follower ends with
// A robot-frame chassis command in NORMALIZED POWER, each component in [-1, 1]:
// vx forward, vy left, omega CCW. Writes four wheel powers to the motors.
void drive(double vx, double vy, double omega) {
double[] powers = mixWheelPowers(vx, vy, omega);

double largest = 1;
for (double p : powers) largest = Math.max(largest, Math.abs(p));

for (int i = 0; i < powers.length; i++) powers[i] /= largest;
setWheelPowers(powers);
}
Convert to normalized power before you call this

The scaling step compares each wheel against 1, which only makes sense if the inputs are fractions of full power. Divide physical speeds by MAX_SPEED and MAX_TURN before calling it. If you pass raw inches per second instead, almost every command has a wheel above 1, so everything gets scaled up to full power. Driving straight at 5, 20, or 40 in/s all come out as full speed, and your speed plan is thrown away without any error.

ExerciseWhat desaturation protects

Teams often leave out the scaling step. What happens to a command of (1.0,1.0,0.5)(1.0, 1.0, 0.5) without it?

Show solution

The mixer gives FL = −0.5, FR = 2.5, BL = 1.5, BR = 0.5, and setPower clips FR and BL to 1 separately. That changes the ratios between wheels, and the ratios set the direction and turn rate, so the robot goes somewhere different, not just slower. It happens when the command is biggest, which is when the follower is trying hardest to correct.

How the lessons use this page

When a lesson uses robot.vec(), wrap(...), moveStatus(...), kStatic(...), or drive(...), this is where it's defined. In your own code, put these in a small util package (or use a library's versions) and import them everywhere instead of copying them.

Write these first. Every follower assumes they work, and a bug in wrap or moveStatus will look like a bug in whatever follower calls it.

Sources & references
  • Road Runner - production FTC examples of shared geometry, pose, and follower utilities.
  • FTCLib - FTC utility/controller library whose structure motivates shared helpers instead of copy-pasted OpMode code.
  • Mecanum Kinematics, PID-to-Point, and Pure Pursuit - Control Lab lessons that reuse these helpers.