Class SwerveDrive
java.lang.Object
yams.core.mechanisms.swerve.SwerveDrive
yams.commands3.swerve.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");
}
}
-
Field Summary
Fields inherited from class SwerveDrive
m_swerveTelemetry -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionorg.wpilib.command3.CommandCreate aCommandto drive the swerve drive with robot relative chassis speeds.org.wpilib.command3.CommanddriveToPose(org.wpilib.math.geometry.Pose2d pose) Drive the robot to the given pose.org.wpilib.command3.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.command3.MechanismMechanism 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 aMechanismset viawithMechanism(Mechanism).- 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
-
getMechanism
public org.wpilib.command3.Mechanism getMechanism()Mechanism the drive's commands require.- Returns:
Mechanismfor the drive.
-
drive
public org.wpilib.command3.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. For driver input, use aSwerveInputStream(field relative) in your own drive loop instead.- 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.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-Pose2dto drive the robot to. Field relative, blue-origin where 0deg is facing towards RED- 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
-
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-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
-