Lab 10: Autonomous Basics (Drive to a Pose)

Goal: press a button and the robot drives itself to a spot on the field, facing a chosen direction.

Why: three controllers

We know where we are (odometry, Lab 8) and how to drive field-relative (Lab 7). So "drive to a pose" is three separate PID controllers:

vx_field = PID_x(current x → target x)          m of error  → m/s
vy_field = PID_y(current y → target y)          m of error  → m/s
omega    = PID_θ(current θ → target θ)          rad of error → rad/s   (continuous input!)
then: fieldToRobot → drive

Since swerve can move in any direction while rotating, the three don't interfere with each other.

Code

public Command driveToPose(Pose2d target) {
  PIDController xPid = new PIDController(0, 0, 0);
  PIDController yPid = new PIDController(0, 0, 0);
  PIDController thetaPid = new PIDController(0, 0, 0);
  thetaPid.enableContinuousInput(-Math.PI, Math.PI);

  return run(() -> {
    double kPTranslation = Tunable.number("Auto/kPTranslation", 1.0);   // (m/s) per m of error
    double kPRotation = Tunable.number("Auto/kPRotation", 2.0);         // (rad/s) per rad of error
    double maxSpeed = Tunable.number("Auto/MaxSpeedMps", 0.5);
    xPid.setP(kPTranslation);
    yPid.setP(kPTranslation);
    thetaPid.setP(kPRotation);

    Pose2d pose = getPose();
    double vx = MathUtil.clamp(xPid.calculate(pose.getX(), target.getX()), -maxSpeed, maxSpeed);
    double vy = MathUtil.clamp(yPid.calculate(pose.getY(), target.getY()), -maxSpeed, maxSpeed);
    double omega = thetaPid.calculate(pose.getRotation().getRadians(), target.getRotation().getRadians());

    drive(MyFieldRelative.fieldToRobot(new ChassisSpeeds(vx, vy, omega), getHeading()));
  });
}

Bind it: controller.a().whileTrue(swerve.driveToPose(new Pose2d(3, 4, Rotation2d.fromDegrees(90)))); (1 m in front of where the simulated robot starts). Holding the button runs it; releasing it stops the robot.

Log the target pose next to the real pose in AdvantageScope.

Try it

  1. Reset the pose. Hold A. The robot should drive to the target and end up facing left.
  2. Compare where it really stopped (Sim/TruePose) to the target.

Experiments

Change Predict What you'll see
kPTranslation 0.5 → 3 Faster approach; too high and it overshoots and wiggles
Target 2 m away with x and y clamped separately Straight line to the target? Not quite: diagonal moves are faster than straight ones. How would you fix it? (Hint: clamp the vector's length.)
Release A halfway Stops. Why is whileTrue safer than onTrue here?
Sim/TrueWheelSizeScale = 0.95 The robot "arrives" according to odometry, but Sim/TruePose stops short. Auto is only as good as odometry.

Next: trajectories, PathPlanner, Choreo, vision

  • A trajectory is a path plus a speed at each moment. Following one = feedforward from the path + these same three PIDs to correct error. That's WPILib's HolonomicDriveController.
  • PathPlanner and Choreo draw paths on a field map and generate those trajectories; they still need our drive(ChassisSpeeds) and getPose().
  • Vision: AprilTag cameras give occasional absolute poses. SwerveDrivePoseEstimator blends them with odometry, using standard deviations as "how much do I trust each one". That fixes Lab 8's ever-growing error.

Checkpoint

Summary: Drive-to-pose is three PIDs (x, y, θ) in the field frame, sent through field-relative driving.

Quiz

  1. Why does θ need continuous input but x and y don't?
  2. Why does auto accuracy depend on odometry?
  3. What does a trajectory add on top of drive-to-pose?
  4. Why does vision fix odometry drift?

What could go wrong in the real world: odometry not reset at the start of auto; the wrong alliance side; a target pose inside a wall.