Class SwerveDrive


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

The typical pattern is to wrap SwerveDrive inside a WPILib SubsystemBase.

public class SwerveSubsystem extends SubsystemBase {
    private final SwerveDrive drive;

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

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

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

  • Method Details

    • getSubsystem

      public org.wpilib.command2.Subsystem getSubsystem()
      Subsystem the drive's commands require.
      Returns:
      Subsystem for the drive.
    • drive

      public org.wpilib.command2.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. Could also use SwerveInputStream
      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.command2.Command driveToPose(org.wpilib.math.geometry.Pose2d pose)
      Drive the robot to the given pose. The command runs until it is interrupted 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 that drives the robot toward the given pose until interrupted.
      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.command2.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 interrupted.
      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