Class SwerveModule
SwerveModule coordinates the drive motor (velocity control) and the azimuth (steer)
motor (position control) that make up one corner of a swerve drivetrain. On construction it reads
the absolute encoder and seeds the azimuth relative encoder so the wheel starts at the correct
angle. Each periodic cycle you call setSwerveModuleState(SwerveModuleVelocity) to
command both motors, and getState() / getPosition() to read back the current
wheel velocity and heading.
Typical usage
In almost every case you should not instantiate SwerveModule directly. Instead,
pass one SwerveModuleConfig per corner to
SwerveDriveConfig and let
SwerveDrive create and manage the modules internally:
SwerveDriveConfig driveConfig = new SwerveDriveConfig()
.withSwerveModuleConfig(frontLeft, frontRight, backLeft, backRight);
SwerveDrive swerveDrive = new SwerveDrive(driveConfig);
Direct instantiation (advanced)
If you need direct access to a module for example when writing unit tests or custom
characterisation routines you can construct one from a fully-configured
SwerveModuleConfig:
// Assumes 'frontLeftConfig' has already been built with drive/steer motors,
// wheel radius, module location, absolute encoder offset, and telemetry name.
SwerveModule frontLeft = new SwerveModule(frontLeftConfig);
// Command a specific state (angle + speed)
SwerveModuleVelocity desiredState = new SwerveModuleVelocity(1.5, Rotation2d.fromDegrees(45));
frontLeft.setSwerveModuleState(desiredState);
// Read back current state
SwerveModuleVelocity current = frontLeft.getState();
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final SmartMotorControllerAzimuth motor controller.protected final SmartMotorControllerDrive motor controller. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionGet the azimuthSmartMotorController.Get theSwerveModuleConfigfor the module.org.wpilib.units.measure.LinearVelocityWheel speed the drive gearing produces from the azimuth turning at its current rate.Get the driveSmartMotorController.getName()Get the name of the module.org.wpilib.math.kinematics.SwerveModulePositionGet theSwerveModulePositionof the module.org.wpilib.units.measure.AngleGet the absolute encoder angle without any offsets applied.getState()Get theSwerveModuleVelocityof the module, with how fast its azimuth is turning.voidSeed the azimuth encoder with the absolute encoder angle.org.wpilib.math.kinematics.SwerveModuleVelocitysetAzimuthAngle(org.wpilib.math.geometry.Rotation2d angle) Turn the wheel to the given angle without driving it, as for locking the drive in place or tuning the azimuth motor.org.wpilib.math.kinematics.SwerveModuleVelocitysetSwerveModuleState(org.wpilib.math.kinematics.SwerveModuleVelocity state) Set theSwerveModuleVelocityof the module.org.wpilib.math.kinematics.SwerveModuleVelocitysetSwerveModuleState(org.wpilib.math.kinematics.SwerveModuleVelocity state, org.wpilib.units.measure.Force feedforwardForce) Set theSwerveModuleVelocityof the module with an additional drive wheel feedforwardForceapplied on top, e.g.voidsetupTelemetry(String mechName) Setup telemetry for the module; theSwerveModuleTelemetryconfig used is either the one supplied viaSwerveModuleConfig.withTelemetry(String, SwerveModuleTelemetryConfig)or a default built fromSwerveModuleConfig.getTelemetryVerbosity()(defaulting toTelemetryVerbosity.HIGH).voidUpdate the simulation of the module.voidUpdate the telemetry of the module.
-
Field Details
-
m_driveMotorController
Drive motor controller. -
m_azimuthMotorController
Azimuth motor controller.
-
-
Constructor Details
-
SwerveModule
Create a SwerveModule.- Parameters:
config-SwerveModuleConfigfor the module.- Throws:
NoSuchElementException- if the drive or azimuth motor controller is not set in the config.IllegalArgumentException- if the config has no telemetry name.IllegalArgumentException- if the config has no location.SmartMotorControllerConfigurationException- if the azimuth motor controller has an external encoder configured but external feedback is not enabled withwithUseExternalFeedbackEncoder(true).
-
-
Method Details
-
setupTelemetry
Setup telemetry for the module; theSwerveModuleTelemetryconfig used is either the one supplied viaSwerveModuleConfig.withTelemetry(String, SwerveModuleTelemetryConfig)or a default built fromSwerveModuleConfig.getTelemetryVerbosity()(defaulting toTelemetryVerbosity.HIGH).- Parameters:
mechName- Telemetry name of the parentSwerveDrive.
-
getRawAbsoluteEncoderAngle
public org.wpilib.units.measure.Angle getRawAbsoluteEncoderAngle()Get the absolute encoder angle without any offsets applied.- Returns:
Angleof the absolute encoder without offsets.
-
seedAzimuthEncoder
public void seedAzimuthEncoder()Seed the azimuth encoder with the absolute encoder angle. -
getName
-
getConfig
Get theSwerveModuleConfigfor the module.- Returns:
SwerveModuleConfigfor the module.
-
setAzimuthAngle
public org.wpilib.math.kinematics.SwerveModuleVelocity setAzimuthAngle(org.wpilib.math.geometry.Rotation2d angle) Turn the wheel to the given angle without driving it, as for locking the drive in place or tuning the azimuth motor. The velocity deadband, which holds a wheel commanded too slowly to steer by where it is, does not apply (seeSwerveModuleConfig.getLockedState(Rotation2d, SwerveModuleVelocityWithAzimuth)).- Parameters:
angle- Angle to turn the wheel to.- Returns:
- The zero speed
SwerveModuleVelocitythe module was set to. - Throws:
SmartMotorControllerConfigurationException- if the drive motor has no mechanism circumference configured (e.g. viaSwerveModuleConfig.withWheelRadius(org.wpilib.units.measure.Distance)).
-
setSwerveModuleState
public org.wpilib.math.kinematics.SwerveModuleVelocity setSwerveModuleState(org.wpilib.math.kinematics.SwerveModuleVelocity state) Set theSwerveModuleVelocityof the module.- Parameters:
state- State to set.- Returns:
- The optimized
SwerveModuleVelocity. - Throws:
SmartMotorControllerConfigurationException- if the drive motor has no mechanism circumference configured (e.g. viaSwerveModuleConfig.withWheelRadius(org.wpilib.units.measure.Distance)).
-
setSwerveModuleState
public org.wpilib.math.kinematics.SwerveModuleVelocity setSwerveModuleState(org.wpilib.math.kinematics.SwerveModuleVelocity state, org.wpilib.units.measure.Force feedforwardForce) Set theSwerveModuleVelocityof the module with an additional drive wheel feedforwardForceapplied on top, e.g. from a PathPlanner set-point generator.- Parameters:
state- State to set.feedforwardForce- FeedforwardForceapplied at the drive wheel, in the direction of travel of the requested state. It is reversed along with the drive direction when optimization turns the wheel around, and scaled by the cosine compensation factor when that is enabled.- Returns:
- The optimized
SwerveModuleVelocity. - Throws:
SmartMotorControllerConfigurationException- if the drive motor has no mechanism circumference configured (e.g. viaSwerveModuleConfig.withWheelRadius(org.wpilib.units.measure.Distance)).
-
getState
Get theSwerveModuleVelocityof the module, with how fast its azimuth is turning.- Returns:
SwerveModuleVelocityWithAzimuthof the module.- Throws:
SmartMotorControllerConfigurationException- if the drive motor has no mechanism circumference configured (e.g. viaSwerveModuleConfig.withWheelRadius(org.wpilib.units.measure.Distance)).
-
getPosition
public org.wpilib.math.kinematics.SwerveModulePosition getPosition()Get theSwerveModulePositionof the module.- Returns:
SwerveModulePositionof the module.- Throws:
SmartMotorControllerConfigurationException- if the drive motor has no mechanism circumference configured (e.g. viaSwerveModuleConfig.withWheelRadius(org.wpilib.units.measure.Distance)).
-
getCouplingVelocity
public org.wpilib.units.measure.LinearVelocity getCouplingVelocity()Wheel speed the drive gearing produces from the azimuth turning at its current rate.- Returns:
- Wheel speed caused by the coupling, zero if coupling is not compensated.
- Throws:
SmartMotorControllerConfigurationException- if the drive motor has no mechanism circumference configured.
-
updateTelemetry
public void updateTelemetry()Update the telemetry of the module. -
simIterate
public void simIterate()Update the simulation of the module. -
getAzimuthMotorController
Get the azimuthSmartMotorController.- Returns:
- Azimuth
SmartMotorController.
-
getDriveMotorController
Get the driveSmartMotorController.- Returns:
- Drive
SmartMotorController.
-