Lab 8: Odometry (Where Are We?)¶
Goal: the robot tracks its own position on the field. Drive a square and compare where the robot thinks it is to where the simulator says it really is.
Why: dead reckoning with wheels and a gyro¶
Every 20 ms, each module tells us how far its wheel rolled and which way it pointed. That's a SwerveModulePosition (distance in meters, angle). From the changes since last loop, we can work out how the robot moved, then add that up over time.
each loop:
for each module: displacement = (Δdistance) in the direction of its angle (robot frame)
robot displacement = average of the 4 module displacements (robot frame)
rotate it by the heading from the gyro (→ field frame)
pose += that
Why positions and not velocities? Velocities are measured at one instant and are noisy and laggy (Lab 6). Distances are counted continuously, so nothing between samples is lost.
Why use the gyro for heading? You can work out rotation from the wheels, but wheels slip when the robot spins. The gyro is far more accurate for heading.
This is forward kinematics (modules → robot), the opposite of Lab 5. Four wheels give 8 numbers (an x and y each) for only 3 unknowns (x, y, θ), so there's extra information. Averaging is the simple version; WPILib uses least squares to find the best fit, which gives the same answer when the wheels agree.
Students write: MyOdometry¶
package frc.robot.lib;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.kinematics.SwerveModulePosition;
public class MyOdometry {
private Pose2d poseMeters;
private double[] lastDistancesMeters = new double[4];
public MyOdometry(Pose2d initialPose, SwerveModulePosition[] positions) {
poseMeters = initialPose;
for (int i = 0; i < 4; i++) {
lastDistancesMeters[i] = positions[i].distanceMeters;
}
}
/** Call every loop. heading: gyro, CCW+. positions: FL, FR, BL, BR. */
public Pose2d update(Rotation2d heading, SwerveModulePosition[] positions) {
// TODO (students):
// 1. For each module: delta = distance − lastDistance. Its displacement vector is
// (delta × cos(angle), delta × sin(angle)) in the ROBOT frame.
// 2. Average the 4 displacement vectors.
// 3. Rotate the average by `heading` into the field frame.
// 4. New pose = (old x + dx, old y + dy, heading).
// 5. Save the distances for next time.
return poseMeters;
}
}
Compare to WPILib's SwerveDriveOdometry in a unit test: feed both the same fake module positions for "drive 1 m forward", "drive 1 m left", and "drive forward while facing 90°".
Code¶
In SwerveModule:
public SwerveModulePosition getPosition() {
return new SwerveModulePosition(getDriveDistanceMeters(), getAngle());
}
In SwerveSubsystem:
private final Field2d field = new Field2d();
private MyOdometry odometry; // create in the constructor, after the modules
// in periodic():
Pose2d pose = odometry.update(getHeading(), getModulePositions());
field.setRobotPose(pose);
SmartDashboard.putData("Field", field);
Also publish pose as a struct (Pose2d.struct) so AdvantageScope can show it on its 2D field.
Add a "reset pose" button that sets the pose to (0, 0, current heading).
Try it¶
- In the reset-pose button, start odometry at
Constants.Sim.START_POSE(where the simulated robot starts), so both poses begin in the same place. - In AdvantageScope's 2D Field, show both
Sim/TruePose(the truth) and your odometry pose. They should sit on top of each other. - Drive a 2 m × 2 m square (robot always facing the same way). Stop back at the start. Write down the difference between the two poses.
(On a real robot there's no "true pose": you'd put tape on the floor and use a tape measure.)
Experiments¶
| Change | Predict | What you'll see |
|---|---|---|
Sim/TrueWheelSizeScale = 0.97 (worn tread: wheels 3% smaller than Constants says). Drive 3 m straight. |
Is odometry ahead or behind the truth? | Odometry thinks you went 3% farther. Off by the same % every time = wheel diameter is wrong. |
Sim/GyroDriftDegPerSec = 0.5, drive the square 5 times |
The odometry path slowly rotates away from the truth | |
With TrueWheelSizeScale = 0.97, drive the square 5 times without resetting |
Does the error grow? | Yes. Odometry error only adds up. That's why vision helps (Lab 10). |
| Drive the square while spinning | More or less error? | A little more. On a real robot, much more: spinning makes wheels slip (our simulator doesn't model slip). |
| Use the wheel-only heading instead of the gyro (WPILib's math can compute it) | Heading drifts much faster |
Checkpoint¶
Summary: Odometry adds up small module displacements, rotated by the gyro heading, to track the robot's pose.
Quiz
- Why use distance deltas instead of velocity × time?
- Why use the gyro for heading instead of the wheels?
- Four modules give how many equations? For how many unknowns?
- Odometry says 3.10 m, the true pose (or a tape measure) says 3.00 m. What's likely wrong?
- Why does odometry error grow over time?
What could go wrong in the real world: wheel tread wear (diameter changes over a season); slipping while pushing another robot; bumping over field elements; a gyro reset during a match putting the pose at the wrong heading.