Lab 7: Gyro and Field-Relative Driving

Goal: "stick up" always means "away from the driver", no matter which way the robot faces.

Why: rotating the stick into the robot frame

So far the stick has been robot-relative: up = the robot's front. Once the robot turns, the driver has to do the math in their head.

Field-relative: the stick describes motion in the field frame (up = away from the driver station). We know the robot's heading θ from the gyro, so we rotate the stick vector by −θ to get robot-frame speeds:

vx_robot =  vx_field × cos θ + vy_field × sin θ
vy_robot = −vx_field × sin θ + vy_field × cos θ

Check it by hand. Robot faces left (θ = 90°). Driver pushes up (vx_field = 1, vy_field = 0):

vx_robot = 1 × 0 + 0 × 1 = 0
vy_robot = −1 × 1 + 0 × 0 = −1   → the robot drives to its own RIGHT

Facing left, its right is "away from the driver". ✓

Gyro sign. WPILib (and our math) wants heading counterclockwise-positive. Some gyros report clockwise-positive. With the wrong sign, θ goes the wrong way and field-relative breaks as soon as the robot turns.

Gyro

The simulated gyro is a GyroIO (like ModuleIO, a hardware-abstraction interface). Get it from the sim world in SwerveSubsystem: private final GyroIO gyro = simWorld.getGyroIO();. Then write one method that our code always uses, and test the sign:

private Rotation2d headingOffset = new Rotation2d();

/** Robot heading, CCW positive (WPILib convention). */
public Rotation2d getHeading() {
  Rotation2d raw = gyro.getYaw();
  if (Tunable.toggle("Gyro/Invert", false)) {
    raw = raw.unaryMinus();
  }
  return raw.minus(headingOffset);
}

/** "The way the robot faces right now is 0°." */
public void zeroHeading() {
  headingOffset = headingOffset.plus(getHeading());
}

Log getHeading().getDegrees(). Spin the robot CCW: does it go up? If not, set Gyro/Invert. (On a real robot you'd test this by turning the robot by hand. Lab 11 adds the real gyro, e.g. a Pigeon 2 or NavX.)

Students write: field → robot

/** Rotate field-relative speeds into the robot frame. heading = robot heading, CCW+. */
public static ChassisSpeeds fieldToRobot(ChassisSpeeds fieldSpeeds, Rotation2d heading) {
  // TODO (students): rotate (vx, vy) by −heading. Omega doesn't change. Why not?
  return fieldSpeeds;
}

Unit test against ChassisSpeeds.fromFieldRelativeSpeeds (check the current docs: newer WPILib versions also have toRobotRelative).

Code

ChassisSpeeds speeds = new ChassisSpeeds(x * maxSpeed, y * maxSpeed, omega);
if (Tunable.toggle("Drive/FieldRelative", true)) {
  speeds = MyFieldRelative.fieldToRobot(speeds, swerve.getHeading());
}
swerve.drive(speeds);

Bind a button to reset heading (point the robot away from you first, then press):

controller.start().onTrue(Commands.runOnce(swerve::zeroHeading));

Try it

  1. Point the robot away from you. Press Start to zero the heading.
  2. Push the stick up: robot goes away from you. Spin the robot 90° and push up again: it should still go away from you, driving sideways relative to itself.
  3. Drive a square with the robot always facing the same way, then with it spinning.

Experiments

Change Predict What you'll see
FieldRelative off, robot facing you, push up Robot comes toward you (its front)
Sim/Faults/GyroClockwisePositive on (a gyro that counts CW as positive) Fine at 0°. At 90° it drives the opposite way you'd expect. Fix it with Gyro/Invert.
Sim/GyroDriftDegPerSec = 1, drive for a minute "Up" slowly rotates away from "away from me". Re-zero fixes it.
Zero the heading while the robot faces left "Up" now drives to the field's left. Always zero facing away.
Drive forward while spinning Translation stays roughly straight, but drifts in an arc. Lab 9 fixes this.

Alliance flip. On the real field, "away from the driver" points opposite ways for red and blue alliances. With odometry (Lab 8) and a field coordinate system, the red driver's stick has to be flipped by 180°. For now, we just zero the gyro facing away from us.

Checkpoint

Summary: Rotating the joystick vector by −heading turns field-relative commands into robot-relative ones.

Quiz

  1. Robot heading 180°, stick up. Which way does the robot drive relative to itself?
  2. Why rotate by −θ and not +θ?
  3. Why doesn't ω change between frames?
  4. What breaks first if the gyro is CW-positive?
  5. Why zero the gyro facing away from the driver?

What could go wrong in the real world: a gyro mounted upside down (sign flips); gyro drift over a long match; a robot hit hard enough to make the gyro jump; forgetting alliance flip.