SwerveModule

class SwerveModule(driveMotorId: Int, steerMotorId: Int, canCoderId: Int, val angleOffset: Rotation2d = Rotation2d())

Represents a single swerve module with drive and steer motors.

This class manages a swerve module consisting of:

  • A drive motor (for wheel rotation) using TonguFX

  • A steer motor (for changing module direction) using TonguFX

  • An Engu CANcoder for absolute angle measurement

  • Pingu PID controllers for drive and steer control

Usage Example:

val module = SwerveModule(
driveMotorId = 1,
steerMotorId = 2,
canCoderId = 3,
angleOffset = Rotation2d.fromDegrees(45.0)
)

module.configure {
driveGearRatio = 6.75
steerGearRatio = 150.0 / 7.0
wheelDiameterMeters = 0.1016
drivePingu.apply {
p = 0.1
v = 0.12
s = 0.2
}
steerPingu.apply {
p = 100.0
d = 0.1
}
}

// Set desired state
val desiredState = SwerveModuleState(2.0, Rotation2d.fromDegrees(90.0))
module.setDesiredState(desiredState)

Constructors

Link copied to clipboard
constructor(driveMotorId: Int, steerMotorId: Int, canCoderId: Int, angleOffset: Rotation2d = Rotation2d())

Types

Link copied to clipboard
data class Config(var driveGearRatio: Double = 6.75, var steerGearRatio: Double = 150.0 / 7.0, var wheelDiameter: Distance = Units.Meters.of(0.1016), val drivePingu: Pingu = Pingu(p = 0.1), val steerPingu: Pingu = Pingu(p = 100.0), var driveCurrentLimit: Current = Units.Amps.of(40.0), var steerCurrentLimit: Current = Units.Amps.of(30.0), var driveInverted: Boolean = false, var steerInverted: Boolean = true)

Configuration for a swerve module.

Properties

Link copied to clipboard
val angleOffset: Rotation2d

Absolute encoder offset for the module's zero position.

Link copied to clipboard

Provides access to the module's Engu encoder for advanced operations.

Functions

Link copied to clipboard

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

Link copied to clipboard
fun getAngle(): Rotation2d

Gets the current angle of the module from the CANcoder.

Link copied to clipboard

Gets the drive motor position in rotations.

Link copied to clipboard

Gets the drive motor velocity in rotations 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 getPosition(): SwerveModulePosition

Gets the current position of the swerve module.

Link copied to clipboard
fun getState(): SwerveModuleState

Gets the current state of the swerve module.

Link copied to clipboard

Gets the steer motor position in rotations.

Link copied to clipboard

Resets the steer motor position to match the absolute encoder.

Link copied to clipboard
fun setDesiredState(desiredState: SwerveModuleState, isOpenLoop: Boolean = false)

Sets the desired state for the swerve module.

Link copied to clipboard
fun setVoltage(voltage: Voltage, angle: Rotation2d)

Sets the module to a specific voltage for characterization.

Link copied to clipboard
fun stop()

Stops both motors.