Class SwerveDrive
java.lang.Object
yams.core.mechanisms.swerve.SwerveDrive
yams.commands2.swerve.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");
}
}
-
Field Summary
Fields inherited from class SwerveDrive
m_swerveTelemetry -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionorg.wpilib.command2.CommandCreate aCommandto drive the swerve drive with robot relative chassis speeds.org.wpilib.command2.CommanddriveToPose(org.wpilib.math.geometry.Pose2d pose) Drive the robot to the given pose.org.wpilib.command2.CommanddriveToPose(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.org.wpilib.command2.SubsystemSubsystem the drive's commands require.Methods inherited from class SwerveDrive
addVisionMeasurement, addVisionMeasurement, applyTuningValues, driveToPoseSetpoint, driveToPoseSetpoint, getAngleDifferenceFromPose, getConfig, getDesiredChassisSpeeds, getDesiredModuleStates, getDistanceFromPose, getField2d, getFieldRelativeSpeed, getGyroRotation3d, getKinematics, getModule, getModulePositions, getModules, getModuleStates, getName, getPose, getRobotRelativeChassisSpeedsFromState, getRobotRelativeSpeed, getSimPose, getStateFromRobotRelativeChassisSpeeds, isNear, lockPose, resetOdometry, resetRotationPID, resetTranslationPID, setAzimuthAngles, setFieldRelativeChassisSpeeds, setRobotRelativeChassisSpeeds, setRobotRelativeChassisSpeeds, setRotationPID, setSwerveModuleStates, setSwerveModuleStates, setTranslationPID, setVisionMeasurementStdDevs, simIterate, startDriveToPoseTuning, stop, updateTelemetry, zeroGyro
-
Constructor Details
-
SwerveDrive
Construct aSwerveDrive.- Parameters:
config-SwerveDriveConfigto use, with aSubsystemset viawithSubsystem(Subsystem).- Throws:
IllegalStateException- on a real robot (not in simulation), if no gyro supplier was set withwithGyro(Supplier).SmartMotorControllerConfigurationException- if a module's drive motor has no mechanism circumference configured (e.g. viaSwerveModuleConfig.withWheelRadius(Distance)).
-
-
Method Details
-
getSubsystem
public org.wpilib.command2.Subsystem getSubsystem()Subsystem the drive's commands require.- Returns:
Subsystemfor the drive.
-
drive
public org.wpilib.command2.Command drive(Supplier<org.wpilib.math.kinematics.ChassisVelocities> robotRelativeChassisSpeeds) Create aCommandto drive the swerve drive with robot relative chassis speeds.- Parameters:
robotRelativeChassisSpeeds-SupplierofChassisVelocitiesfor the robot relative chassis speeds. Could also useSwerveInputStream- Returns:
Commandto 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 withwithGyro(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-Pose2dto drive the robot to. Field relative, blue-origin where 0deg is facing towards RED- Returns:
Commandthat 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 withwithGyro(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-Pose2dto drive the robot to. Field relative, blue-origin where 0deg is facing towards REDtranslationTolerance- 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:
Commandto 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 withwithGyro(Supplier).- Implementation Note:
- Not compatible with AdvantageKit
-