Class SwerveDrive


public class SwerveDrive extends SwerveDrive
Command-based extension of SwerveDrive that adds the Mechanism binding and Command factories.

The typical pattern is to wrap SwerveDrive inside a class implementing Mechanism.

public class SwerveSubsystem implements Mechanism {
    private final SwerveDrive drive;

    public SwerveSubsystem() {
        drive = new SwerveDrive(new SwerveDriveConfig(this, modules)....);
    }

    public Command driveWithJoystick(CommandNiDsXboxController controller) {
        return drive.drive(() -> new ChassisVelocities(...));
    }

    public Command driveFieldRelative(Supplier<ChassisVelocities> speedsSupplier) {
        return run(coroutine -> {
            while (true) {
                drive.setFieldRelativeChassisSpeeds(speedsSupplier.get());
                coroutine.yield();
            }
        }).named("Drive Field Relative");
    }
}
  • Constructor Details

  • Method Details

    • getMechanism

      public org.wpilib.command3.Mechanism getMechanism()
      Mechanism the drive's commands require.
      Returns:
      Mechanism for the drive.
    • drive

      public org.wpilib.command3.Command drive(Supplier<org.wpilib.math.kinematics.ChassisVelocities> robotRelativeChassisSpeeds)
      Create a Command to drive the swerve drive with robot relative chassis speeds.
      Parameters:
      robotRelativeChassisSpeeds - Supplier of ChassisVelocities for the robot relative chassis speeds. For driver input, use a SwerveInputStream (field relative) in your own drive loop instead.
      Returns:
      Command to drive the swerve drive.
      Throws:
      IllegalStateException - when the returned command runs, if a gyro angular velocity scale factor is configured but no gyro supplier was set with withGyro(Supplier).
      Implementation Note:
      Not compatible with AdvantageKit
    • driveToPose

      public org.wpilib.command3.Command driveToPose(org.wpilib.math.geometry.Pose2d pose)
      Drive the robot to the given pose. The command runs until it is canceled and stops the drive when it is.
      Parameters:
      pose - Pose2d to drive the robot to. Field relative, blue-origin where 0deg is facing towards RED
      Returns:
      Command to drive the robot to the given pose.
      Throws:
      SwerveDriveConfigurationException - when the returned command runs, if the translation PID controller is not configured.
      SwerveDriveConfigurationException - when the returned command runs, if the rotation PID controller is not configured.
      IllegalStateException - when the returned command runs, if a gyro angular velocity scale factor is configured but no gyro supplier was set with withGyro(Supplier).
      Implementation Note:
      Not compatible with AdvantageKit
    • driveToPose

      public org.wpilib.command3.Command driveToPose(org.wpilib.math.geometry.Pose2d pose, org.wpilib.units.measure.Distance translationTolerance, org.wpilib.units.measure.Angle rotationTolerance)
      Drive the robot to the given pose, ending once the robot is within the given tolerances. The drive is stopped when the command ends or is canceled.
      Parameters:
      pose - Pose2d to drive the robot to. Field relative, blue-origin where 0deg is facing towards RED
      translationTolerance - Maximum distance from the pose to be considered at the pose.
      rotationTolerance - Maximum heading error from the pose to be considered at the pose.
      Returns:
      Command to drive the robot to the given pose.
      Throws:
      SwerveDriveConfigurationException - when the returned command runs, if the translation PID controller is not configured.
      SwerveDriveConfigurationException - when the returned command runs, if the rotation PID controller is not configured.
      IllegalStateException - when the returned command runs, if a gyro angular velocity scale factor is configured but no gyro supplier was set with withGyro(Supplier).
      Implementation Note:
      Not compatible with AdvantageKit