Search requires an index. Run npm run build once, then restart dev.
Search needs internet. Offline lessons: use the sidebar or download a track under Reading preferences (Aa).
Three-Wheel Odometry
advanced55 min
Why Use Three Wheels?
Two parallel encoders measure forward travel and rotation; a perpendicular wheel measures lateral motion. Together they support full planar pose (X, Y, heading) on the field.
Mathematics of Three-Wheel Odometry
Each control loop, take the delta (change) in ticks since the last loop and plug them into kinematics that separate forward, lateral, and rotational motion. See gm0: Odometry for the full derivation.
Basic Position Update Logic
Core update using encoder deltas (simplified; units depend on your calibration):
Implementing Three-Wheel Odometry in Android Studio
Read encoders every loop in runOpMode(), compute deltas against the previous sample, then update pose and store the new encoder values for the next iteration.
Sample Loop for Odometry Update
Typical loop structure:
@Overridepublic void runOpMode() { // Initialize encoders and variables... waitForStart(); while (opModeIsActive()) { // Read current encoder values int left = leftEncoder.getCurrentPosition(); int right = rightEncoder.getCurrentPosition(); int center = centerEncoder.getCurrentPosition(); // Calculate deltas and update position (see above) // ... telemetry.addData("X", robotX); telemetry.addData("Y", robotY); telemetry.addData("Heading", Math.toDegrees(robotHeading)); telemetry.update(); // Store previous values for next loop // ... }}
Calibration and Tuning
Measure track width (parallel wheels) and center wheel offset with a tape measure or CAD. Drive straight and in place turns; if pose drifts, tweak constants before long auto paths.
Common Pitfalls and How to Avoid Them
Reversed encoder signs, swapped left/right, or using diameter in the wrong units are frequent bugs. Test one axis at a time—straight line, pure rotate, short strafe—before full autonomous.
Practice Exercise
Write a function that takes in the previous and current encoder values for all three wheels and returns the robot's new X, Y, and heading.
Implement the position update logic in Java.
Test your function with sample encoder values.
Verify the output by making the robot create a simple “S” shape.
Implement the position update logic in Java.
Implement the position update logic in Java.
public class OdometryPose {
public double x, y, heading;
public OdometryPose(double x, double y, double heading) {
this.x = x; this.y = y; this.heading = heading;
}
}
public OdometryPose updatePose(
int prevLeft, int prevRight, int prevCenter,
int currLeft, int currRight, int currCenter,
double prevX, double prevY, double prevHeading,
double trackWidth, double centerWheelOffset
) {
double leftDelta = currLeft - prevLeft;
double rightDelta = currRight - prevRight;
double centerDelta = currCenter - prevCenter;
double forwardMovement = (leftDelta + rightDelta) / 2.0;
double headingChange = (rightDelta - leftDelta) / trackWidth;
double lateralMovement = centerDelta - (headingChange * centerWheelOffset);
double newX = prevX + forwardMovement * Math.cos(prevHeading) - lateralMovement * Math.sin(prevHeading);
double newY = prevY + forwardMovement * Math.sin(prevHeading) + lateralMovement * Math.cos(prevHeading);
double newHeading = prevHeading + headingChange;
return new OdometryPose(newX, newY, newHeading);
}
Test your function with sample encoder values.
Test your function with sample encoder values.
You can test your updatePose method with sample values. For example:
OdometryPose pose = updatePose(0, 0, 0, 1000, 1000, 0, 0, 0, 0, 15.0, 7.5);
System.out.println("X: " + pose.x + " Y: " + pose.y + " Heading: " + Math.toDegrees(pose.heading));
// This should show a forward movement with no rotation or lateral movement.
Verify the output by making the robot create a simple "S" shape.
Verify the output by making the robot create a simple "S" shape.
To verify, simulate a sequence of encoder values that would make the robot move forward, turn, and then move forward again in the opposite direction. After each update, print the pose. You should see the X, Y, and heading values change in a way that traces an "S" path.
Put it all together: Modular Odometry Update Example
Put it all together: Modular Odometry Update Example
public class OdometryPose {
public double x, y, heading;
public OdometryPose(double x, double y, double heading) {
this.x = x; this.y = y; this.heading = heading;
}
}
public OdometryPose updatePose(
int prevLeft, int prevRight, int prevCenter,
int currLeft, int currRight, int currCenter,
double prevX, double prevY, double prevHeading,
double trackWidth, double centerWheelOffset
) {
double leftDelta = currLeft - prevLeft;
double rightDelta = currRight - prevRight;
double centerDelta = currCenter - prevCenter;
double forwardMovement = (leftDelta + rightDelta) / 2.0;
double headingChange = (rightDelta - leftDelta) / trackWidth;
double lateralMovement = centerDelta - (headingChange * centerWheelOffset);
double newX = prevX + forwardMovement * Math.cos(prevHeading) - lateralMovement * Math.sin(prevHeading);
double newY = prevY + forwardMovement * Math.sin(prevHeading) + lateralMovement * Math.cos(prevHeading);
double newHeading = prevHeading + headingChange;
return new OdometryPose(newX, newY, newHeading);
}
// In your op mode loop:
OdometryPose pose = new OdometryPose(0, 0, 0);
int prevLeft = 0, prevRight = 0, prevCenter = 0;
while (opModeIsActive()) {
int currLeft = leftEncoder.getCurrentPosition();
int currRight = rightEncoder.getCurrentPosition();
int currCenter = centerEncoder.getCurrentPosition();
pose = updatePose(prevLeft, prevRight, prevCenter, currLeft, currRight, currCenter, pose.x, pose.y, pose.heading, TRACK_WIDTH, CENTER_WHEEL_OFFSET);
telemetry.addData("X", pose.x);
telemetry.addData("Y", pose.y);
telemetry.addData("Heading", Math.toDegrees(pose.heading));
telemetry.update();
prevLeft = currLeft; prevRight = currRight; prevCenter = currCenter;
}