SwerveDrive

class SwerveDrive(gyroId: Int, frontLeftModule: SwerveModule, frontRightModule: SwerveModule, backLeftModule: SwerveModule, backRightModule: SwerveModule, canBus: CANBus = CANBus.roboRIO()) : SubsystemBase

Swerve drive subsystem managing four swerve modules and odometry.

This subsystem handles:

  • Swerve drive kinematics and control

  • Pose estimation with vision integration

  • Field-oriented and robot-oriented driving

  • Gyroscope integration

Usage Example:

val drive = SwerveDrive(
gyroId = 0,
frontLeftModule = SwerveModule(1, 2, 3, Rotation2d.fromDegrees(0.0)),
frontRightModule = SwerveModule(4, 5, 6, Rotation2d.fromDegrees(90.0)),
backLeftModule = SwerveModule(7, 8, 9, Rotation2d.fromDegrees(180.0)),
backRightModule = SwerveModule(10, 11, 12, Rotation2d.fromDegrees(270.0))
)

drive.configure {
trackWidthMeters = 0.6
wheelBaseMeters = 0.6
maxLinearVelocityMPS = 4.5
maxAngularVelocityRadPS = 2.0 * PI
}

// Drive field-oriented
drive.drive(
xSpeed = 1.0,
ySpeed = 0.5,
rotSpeed = 0.2,
fieldOriented = true
)

Constructors

Link copied to clipboard
constructor(gyroId: Int, frontLeftModule: SwerveModule, frontRightModule: SwerveModule, backLeftModule: SwerveModule, backRightModule: SwerveModule, canBus: CANBus = CANBus.roboRIO())

Types

Link copied to clipboard
data class Config(var trackWidth: Distance = Units.Meters.of(0.6), var wheelBase: Distance = Units.Meters.of(0.6), var maxLinearVelocity: LinearVelocity = Units.MetersPerSecond.of(4.5), var maxAngularVelocity: AngularVelocity = Units.RadiansPerSecond.of(2.0 * PI), var driveDeadband: Double = 0.02, var rotationDeadband: Double = 0.02)

Configuration for the swerve drive.

Properties

Link copied to clipboard
open val currentCommand: Command?
Link copied to clipboard
open var defaultCommand: Command?
Link copied to clipboard
open var name: String?
Link copied to clipboard
open var subsystem: String?

Functions

Link copied to clipboard
open fun addChild(name: String?, child: Sendable?)
Link copied to clipboard
fun addVisionMeasurement(visionPose: Pose2d, timestamp: Time)

Adds a vision measurement to the pose estimator.

Link copied to clipboard
fun SwerveDrive.alignToTarget(camera: Camera, translationSpeed: () -> Double = { 0.0 }, strafeSpeed: () -> Double = { 0.0 }): AlignToTarget

Creates an AlignToTarget command for this swerve drive.

Link copied to clipboard

Configures the swerve drive using a DSL-style configuration block.

Link copied to clipboard
open fun defer(supplier: Supplier<Command?>?): Command?
Link copied to clipboard
fun drive(chassisSpeeds: ChassisSpeeds, isOpenLoop: Boolean = false)

Drives the robot with chassis speeds.

fun drive(xSpeed: Double, ySpeed: Double, rotSpeed: Double, fieldOriented: Boolean = true, isOpenLoop: Boolean = false)

Drives the robot with specified velocities.

Link copied to clipboard
fun driveCommand(xSupplier: () -> Double, ySupplier: () -> Double, rotSupplier: () -> Double, fieldOriented: Boolean = true): Command

Creates a command to drive with joystick inputs.

Link copied to clipboard
fun SwerveDrive.driveToAngleCommand(xSupplier: () -> Double, ySupplier: () -> Double, targetAngle: Rotation2d, block: DriveToAngle.Config.() -> Unit = {}): Command

Creates a drive to angle command with DSL configuration.

Link copied to clipboard
fun driveToPoseCommand(targetPose: Pose2d): Command

Creates a command to drive to a specific pose using PathPlanner.

Link copied to clipboard
fun getChassisSpeeds(): ChassisSpeeds

Gets the current chassis speeds.

Link copied to clipboard
fun getGyroRotation(): Rotation2d

Gets the current rotation from the gyroscope.

Link copied to clipboard
fun getMaxAngularVelocity(): AngularVelocity

Gets the maximum angular velocity.

Link copied to clipboard

Gets the maximum angular velocity in radians per second.

Link copied to clipboard
fun getMaxLinearVelocity(): LinearVelocity

Gets the maximum linear velocity.

Link copied to clipboard

Gets the maximum linear velocity in meters per second.

Link copied to clipboard
fun getModulePositions(): Array<SwerveModulePosition>

Gets the current positions of all modules.

Link copied to clipboard
fun getModuleStates(): Array<SwerveModuleState>

Gets the current states of all modules.

Link copied to clipboard
fun getPose(): Pose2d

Gets the current estimated pose of the robot.

Link copied to clipboard
open fun idle(): Command?
Link copied to clipboard
open fun initSendable(builder: SendableBuilder?)
Link copied to clipboard

Extension to easily integrate a vision system with a swerve drive.

Link copied to clipboard
fun SwerveDrive.lockCommand(): Command

Creates a lock swerve command.

Link copied to clipboard
open override fun periodic()
Link copied to clipboard
open fun register()
Link copied to clipboard
Link copied to clipboard
fun resetGyro()

Resets the gyro heading to zero.

Link copied to clipboard
fun SwerveDrive.resetGyroCommand(angle: Rotation2d = Rotation2d()): Command

Creates a reset gyro command.

Link copied to clipboard
fun resetPose(pose: Pose2d)

Resets the pose to a specific position and rotation.

Link copied to clipboard

Creates a ResetPoseFromVision command for this swerve drive.

Link copied to clipboard
open fun run(action: Runnable?): Command?
Link copied to clipboard
open fun runEnd(run: Runnable?, end: Runnable?): Command?
Link copied to clipboard
open fun runOnce(action: Runnable?): Command?
Link copied to clipboard
fun setGyro(angle: Rotation2d)

Sets the gyro heading to a specific angle.

Link copied to clipboard
fun setModuleStates(desiredStates: Array<SwerveModuleState>, isOpenLoop: Boolean = false)

Sets the desired states for all modules.

Link copied to clipboard
Link copied to clipboard
open fun startEnd(start: Runnable?, end: Runnable?): Command?
Link copied to clipboard
open fun startRun(start: Runnable?, run: Runnable?): Command?
Link copied to clipboard
fun stop()

Stops all modules.

Link copied to clipboard
fun SwerveDrive.teleopCommand(xSupplier: () -> Double, ySupplier: () -> Double, rotSupplier: () -> Double, fieldOriented: Boolean = true, block: TeleopSwerve.Config.() -> Unit = {}): Command

Creates a teleop swerve command with DSL configuration.

Link copied to clipboard
fun SwerveDrive.trackTarget(camera: Camera, translationSpeed: () -> Double, strafeSpeed: () -> Double): TrackTarget

Creates a TrackTarget command for this swerve drive.

Link copied to clipboard
fun SwerveDrive.turnToAngleCommand(targetAngle: Rotation2d, block: TurnToAngle.Config.() -> Unit = {}): Command

Creates a turn to angle command with DSL configuration.