Class SwerveModuleConfig
java.lang.Object
yams.core.mechanisms.config.SwerveModuleConfig
Swerve Module
Configuration Example
The following example shows how to build a complete SwerveModuleConfig for a single
swerve module using TalonFX motors and a CANcoder absolute encoder.
import static org.wpilib.units.Units.*;
import com.ctre.phoenix6.hardware.TalonFX;
import com.ctre.phoenix6.hardware.CANcoder;
import org.wpilib.math.system.DCMotor;
import yams.core.motorcontrollers.SmartMotorControllerConfig;
import yams.core.motorcontrollers.enums.ControlMode;
import yams.core.motorcontrollers.SmartMotorController;
import yams.core.motorcontrollers.remote.TalonFXWrapper;
import yams.core.mechanisms.config.SwerveModuleConfig;
import yams.core.gearing.GearBox;
// Drive motor: velocity closed-loop control (e.g. L2 Falcon gearing 6.75:1)
SmartMotorControllerConfig driveConfig = new SmartMotorControllerConfig()
.withMotorInverted(false)
.withStatorCurrentLimit(Amps.of(50))
.withGearing(new GearBox(new double[]{6.75}))
.withClosedLoopController(0.05, 0.0, 0.001)
.withControlMode(ControlMode.CLOSED_LOOP);
// Steer (azimuth) motor: position closed-loop control (e.g. 12.8:1 reduction)
SmartMotorControllerConfig steerConfig = new SmartMotorControllerConfig()
.withMotorInverted(true)
.withStatorCurrentLimit(Amps.of(30))
.withGearing(new GearBox(new double[]{12.8}))
.withContinuousWrapping(Rotations.of(-0.5), Rotations.of(0.5))
.withClosedLoopController(5.0, 0.0, 0.1)
.withControlMode(ControlMode.CLOSED_LOOP);
// Create motor controller instances directly
SmartMotorController driveMotor = new TalonFXWrapper(new TalonFX(1), DCMotor.getKrakenX60(1),
driveConfig); SmartMotorController steerMotor = new TalonFXWrapper(new TalonFX(2),
DCMotor.getFalcon500(1), steerConfig);
// Assemble the module config (front-left corner, 0.2 m from centre each axis)
CANcoder cancoder = new CANcoder(10);
SwerveModuleConfig frontLeftConfig = new SwerveModuleConfig(driveMotor, steerMotor)
.withWheelRadius(Inches.of(2))
.withLocation(Meters.of(0.2), Meters.of(0.2)) // front, left
.withAbsoluteEncoder(cancoder)
.withAbsoluteEncoderOffset(Rotations.of(0.25)) // bevel-left zero offset
.withCosineCompensation(true)
.withOptimization(true)
.withTelemetry("FrontLeft", TelemetryVerbosity.HIGH);
-
Constructor Summary
ConstructorsConstructorDescriptionCreate theSwerveModuleConfigfor theSwerveModuleSwerveModuleConfig(SmartMotorController drive, SmartMotorController azimuth) Create theSwerveModuleConfigfor theSwerveModule -
Method Summary
Modifier and TypeMethodDescriptionclone()org.wpilib.units.measure.AngleGet the absolute encoder angle for the azimuthSmartMotorController.Get the absolute encoder supplierGet the azimuthSmartMotorControllerfor theSwerveModule.doublegetCosineCompensationScalar(org.wpilib.math.geometry.Rotation2d desiredAngle) Get the cosine compensation factor for the given desired module angle, measured against the current azimuth angle.Get the steer to drive coupling ratio.Get the driveSmartMotorControllerfor theSwerveModule.Optional<org.wpilib.math.geometry.Translation2d> Get the location of theSwerveModulein meters from the center of rotation.org.wpilib.math.kinematics.SwerveModuleVelocitygetLockedState(org.wpilib.math.geometry.Rotation2d angle, SwerveModuleVelocityWithAzimuth measured) Get the state that turns the wheel toanglewithout driving it, as for locking the drive in place.org.wpilib.math.kinematics.SwerveModuleVelocitygetOptimizedState(org.wpilib.math.kinematics.SwerveModuleVelocity state) Get the optimizedSwerveModuleVelocityapplying all optional optimizations, measuring the wheel with the azimuth motor controller.org.wpilib.math.kinematics.SwerveModuleVelocitygetOptimizedState(org.wpilib.math.kinematics.SwerveModuleVelocity state, SwerveModuleVelocityWithAzimuth measured) Get the optimizedSwerveModuleVelocityapplying all optional optimizations.Supplier<org.wpilib.units.measure.Angle> Get the absolute encoder angle as a supplier without offsets applied.booleanEnable or disable state optimization.Get the user specifiedSwerveModuleTelemetryConfig, if configured viawithTelemetry(String, SwerveModuleTelemetryConfig).Get the telemetry name for theSwerveModule.Get the telemetry verbosity for theSwerveModule.withAbsoluteEncoder(Object absoluteEncoder) Set the absolute encoder for the azimuthSmartMotorControllerif it's the SAME VENDOR, only.withAbsoluteEncoder(DoubleSupplier rotationSupplier) Supply a non-alike vendor absolute encoder for the azimuthSmartMotorController.withAbsoluteEncoder(Supplier<org.wpilib.units.measure.Angle> locationProvider) Supply a non-alike vendor absolute encoder for the azimuthSmartMotorController.withAbsoluteEncoderGearing(GearBox gearing) Set the gearing of the absolute encoder if it's not 1:1.withAbsoluteEncoderOffset(org.wpilib.units.measure.Angle offset) Set the absolute encoder offset for the azimuthSmartMotorControllerso that the absolute encoder reads 0 while the wheel is facing forwards and bevel to the left.withCosineCompensation(boolean compensate) Cosine compensation for theSwerveModule, adjusting the velocity by the cosine of the (current_angle-desired_angle).withCouplingRatio(double driveMotorRotationsPerAzimuthRotation) Set the steer to drive coupling ratio as a single reduction; seewithCouplingRatio(GearBox).withCouplingRatio(GearBox couplingRatio) Set the steer to drive coupling ratio, compensated as in the CTRE swerve API.withDistanceFromCenterOfRotation(org.wpilib.units.measure.Distance front, org.wpilib.units.measure.Distance left) Set the distance from the center of rotation for theSwerveModule.withLocation(org.wpilib.math.geometry.Translation2d location) Set the location for theSwerveModulein Meters from the center of rotation.withLocation(org.wpilib.units.measure.Distance distance, org.wpilib.units.measure.Angle angle) Set the location for theSwerveModuleusing polar coordinates.withLocation(org.wpilib.units.measure.Distance front, org.wpilib.units.measure.Distance left) Set the distance from the center of rotation for theSwerveModule.withOptimization(boolean swerveModuleStateOptimization) UseSwerveModuleVelocity.optimize(Rotation2d)to optimize each state: turn the wheel at most 90 degrees and reverse the drive instead.withSmartMotorController(SmartMotorController driveMotor, SmartMotorController azimuthMotor) Set theSmartMotorControllerfor theSwerveModule.withTelemetry(String telemetryName, TelemetryVerbosity telemetryVerbosity) Configure telemetry for theSwerveModulemechanism.withTelemetry(String telemetryName, SwerveModuleTelemetryConfig telemetryConfig) Configure telemetry for theSwerveModulemechanism with aSwerveModuleTelemetryConfig.withVelocityDeadband(org.wpilib.units.measure.LinearVelocity speed) Set the velocity deadband: a requested wheel speed at or below it is too slow to steer by, so the module stops and holds its angle instead of turning toward controller noise.withWheelDiameter(org.wpilib.units.measure.Distance diameter) Set the wheel diameter for theSmartMotorController, ideally should be set inside the drive motorSmartMotorControllerConfig.withWheelRadius(org.wpilib.units.measure.Distance radius) Set the wheel radius for theSmartMotorController, ideally should be set inside the drive motorSmartMotorControllerConfig
-
Constructor Details
-
SwerveModuleConfig
Create theSwerveModuleConfigfor theSwerveModule- Parameters:
drive- Drive motor controller.azimuth- Azimuth motor controller.
-
SwerveModuleConfig
public SwerveModuleConfig()Create theSwerveModuleConfigfor theSwerveModule- Implementation Note:
- Required to use
withSmartMotorController(SmartMotorController, SmartMotorController)BEFORE passed intoSwerveModule
-
-
Method Details
-
clone
-
withSmartMotorController
public SwerveModuleConfig withSmartMotorController(SmartMotorController driveMotor, SmartMotorController azimuthMotor) Set theSmartMotorControllerfor theSwerveModule.- Parameters:
driveMotor-SmartMotorControllerfor the drive motor.azimuthMotor-SmartMotorControllerfor the azimuth motor.- Returns:
SwerveModuleConfigfor chaining.- Throws:
IllegalStateException- if the drive or azimuth motor controller was already set, either bySwerveModuleConfig(SmartMotorController, SmartMotorController)or a previous call to this method.
-
withCosineCompensation
Cosine compensation for theSwerveModule, adjusting the velocity by the cosine of the (current_angle-desired_angle).- Parameters:
compensate- Enable or disable cosine compensation.- Returns:
SwerveModuleConfigfor chaining.
-
withCouplingRatio
Set the steer to drive coupling ratio, compensated as in the CTRE swerve API. On most modules the drive gearing turns the wheel when the azimuth rotates, even with the drive motor still, so steering alone would drive the wheel and skew its speed and odometry. With the ratio set, the drive setpoint includes the motion steering causes, and it is removed from the measured wheel speed and distance.- Parameters:
couplingRatio- Coupling as aGearBoxfrom the drive motor (input) to the azimuth (output): the drive motor rotations caused by one azimuth rotation with the wheel held still are its reduction, e.g.GearBox.fromReductionStages(kCoupleRatio)withkCoupleRatiofrom CTRE Tuner X.- Returns:
SwerveModuleConfigfor chaining.
-
withCouplingRatio
Set the steer to drive coupling ratio as a single reduction; seewithCouplingRatio(GearBox).- Parameters:
driveMotorRotationsPerAzimuthRotation- Drive motor rotations caused by one azimuth rotation with the wheel held still, e.g.kCoupleRatiofrom CTRE Tuner X. 0 disables coupling compensation.- Returns:
SwerveModuleConfigfor chaining.
-
getCouplingRatio
-
withAbsoluteEncoder
Set the absolute encoder for the azimuthSmartMotorControllerif it's the SAME VENDOR, only.- Parameters:
absoluteEncoder- Same vendor, absolute encoder for theSmartMotorController- Returns:
SwerveModuleConfigfor chaining.
-
withDistanceFromCenterOfRotation
public SwerveModuleConfig withDistanceFromCenterOfRotation(org.wpilib.units.measure.Distance front, org.wpilib.units.measure.Distance left) Set the distance from the center of rotation for theSwerveModule.- Parameters:
front- Distance from the front of the robot, will be converted to Meters. (The X location)left- Distance from the left of the robot, will be converted to Meters. (The Y location)- Returns:
SwerveModuleConfigfor chaining.
-
withLocation
Set the location for theSwerveModulein Meters from the center of rotation.- Parameters:
location- Location of theSwerveModulein meters from the center of rotation.- Returns:
SwerveModuleConfigfor chaining.
-
withLocation
public SwerveModuleConfig withLocation(org.wpilib.units.measure.Distance front, org.wpilib.units.measure.Distance left) Set the distance from the center of rotation for theSwerveModule.- Parameters:
front- Distance from the front of the robot, will be converted to meters. (The X location)left- Distance from the left of the robot, will be converted to meters. (The Y location)- Returns:
SwerveModuleConfigfor chaining.
-
withLocation
public SwerveModuleConfig withLocation(org.wpilib.units.measure.Distance distance, org.wpilib.units.measure.Angle angle) Set the location for theSwerveModuleusing polar coordinates.- Parameters:
distance- Distance from the center of rotation will be converted to meters.angle- Angle from the center of rotation.- Returns:
SwerveModuleConfigfor chaining.
-
withAbsoluteEncoder
public SwerveModuleConfig withAbsoluteEncoder(Supplier<org.wpilib.units.measure.Angle> locationProvider) Supply a non-alike vendor absolute encoder for the azimuthSmartMotorController.- Parameters:
locationProvider- Provides the Rotation for the azimuthSmartMotorController.- Returns:
SwerveModuleConfigfor chaining.
-
withAbsoluteEncoder
Supply a non-alike vendor absolute encoder for the azimuthSmartMotorController.- Parameters:
rotationSupplier- Provides the Rotation for the azimuthSmartMotorController.- Returns:
SwerveModuleConfigfor chaining.
-
withAbsoluteEncoderGearing
Set the gearing of the absolute encoder if it's not 1:1.- Parameters:
gearing-GearBoxfor the absolute encoder to get the azimuth angle.- Returns:
SwerveModuleConfigfor chaining.
-
withAbsoluteEncoderOffset
Set the absolute encoder offset for the azimuthSmartMotorControllerso that the absolute encoder reads 0 while the wheel is facing forwards and bevel to the left.- Parameters:
offset- Offset for the absolute encoder.- Returns:
SwerveModuleConfigfor chaining.
-
withWheelRadius
Set the wheel radius for theSmartMotorController, ideally should be set inside the drive motorSmartMotorControllerConfig- Parameters:
radius- Radius of the wheel.- Returns:
SwerveModuleConfigfor chaining.
-
withWheelDiameter
Set the wheel diameter for theSmartMotorController, ideally should be set inside the drive motorSmartMotorControllerConfig.- Parameters:
diameter- Diameter of the wheel.- Returns:
SwerveModuleConfigfor chaining.
-
withVelocityDeadband
Set the velocity deadband: a requested wheel speed at or below it is too slow to steer by, so the module stops and holds its angle instead of turning toward controller noise.- Parameters:
speed- Velocity deadband, or null to always steer.- Returns:
SwerveModuleConfigfor chaining.
-
withOptimization
UseSwerveModuleVelocity.optimize(Rotation2d)to optimize each state: turn the wheel at most 90 degrees and reverse the drive instead. A wheel keeps the orientation it was last sent to unless the other is clearly better, so a wheel that is still turning does not reverse back and forth.- Parameters:
swerveModuleStateOptimization- True to enable optimization, false otherwise.- Returns:
SwerveModuleConfigfor chaining.
-
withTelemetry
public SwerveModuleConfig withTelemetry(String telemetryName, TelemetryVerbosity telemetryVerbosity) Configure telemetry for theSwerveModulemechanism.- Parameters:
telemetryName- Telemetry NetworkTable name to appear under "SmartDashboard/"telemetryVerbosity- Telemetry verbosity to apply.- Returns:
ArmConfigfor chaining.
-
withTelemetry
public SwerveModuleConfig withTelemetry(String telemetryName, SwerveModuleTelemetryConfig telemetryConfig) Configure telemetry for theSwerveModulemechanism with aSwerveModuleTelemetryConfig.- Parameters:
telemetryName- Telemetry NetworkTable name to appear under "SmartDashboard/"telemetryConfig- Config that specifies what to log.- Returns:
SwerveModuleConfigfor chaining.
-
getSwerveModuleTelemetryConfig
Get the user specifiedSwerveModuleTelemetryConfig, if configured viawithTelemetry(String, SwerveModuleTelemetryConfig).- Returns:
SwerveModuleTelemetryConfigif configured.
-
getAbsoluteEncoderAngle
public org.wpilib.units.measure.Angle getAbsoluteEncoderAngle()Get the absolute encoder angle for the azimuthSmartMotorController.- Returns:
- Absolute encoder
Angle. - Throws:
NoSuchElementException- if no azimuth motor controller is set, even when an absolute encoder supplier is configured.
-
getRawAbsoluteEncoderAngle
Get the absolute encoder angle as a supplier without offsets applied.- Returns:
Supplier<org.wpilib.units.measure.Angle>for the absolute encoder angle without offsets.- Throws:
NoSuchElementException- if no absolute encoder supplier is configured and no azimuth motor controller is set.
-
getAbsoluteEncoderSupplier
-
getStateOptimization
public boolean getStateOptimization()Enable or disable state optimization.- Returns:
- True if optimization is enabled, false otherwise.
-
getDriveMotor
Get the driveSmartMotorControllerfor theSwerveModule.- Returns:
SmartMotorControllerfor the drive motor.- Throws:
NoSuchElementException- if no drive motor controller is set, e.g. the no-arg constructor was used without callingwithSmartMotorController(SmartMotorController, SmartMotorController).
-
getAzimuthMotor
Get the azimuthSmartMotorControllerfor theSwerveModule.- Returns:
SmartMotorControllerfor the azimuth motor.- Throws:
NoSuchElementException- if no azimuth motor controller is set, e.g. the no-arg constructor was used without callingwithSmartMotorController(SmartMotorController, SmartMotorController).
-
getCosineCompensationScalar
public double getCosineCompensationScalar(org.wpilib.math.geometry.Rotation2d desiredAngle) Get the cosine compensation factor for the given desired module angle, measured against the current azimuth angle. Used to scale drive commands that bypassgetOptimizedState(SwerveModuleVelocity), e.g. a drive wheel feedforward force.- Parameters:
desiredAngle- Angle the module is asked to point at.- Returns:
- Factor from 0 to 1, or 1 if cosine compensation is disabled.
- Throws:
NoSuchElementException- if no azimuth motor controller was set withwithSmartMotorController(SmartMotorController, SmartMotorController).
-
getOptimizedState
public org.wpilib.math.kinematics.SwerveModuleVelocity getOptimizedState(org.wpilib.math.kinematics.SwerveModuleVelocity state) Get the optimizedSwerveModuleVelocityapplying all optional optimizations, measuring the wheel with the azimuth motor controller.- Parameters:
state-SwerveModuleVelocityto optimize.- Returns:
SwerveModuleVelocityoptimized.- Throws:
NoSuchElementException- if no azimuth motor controller is set.
-
getOptimizedState
public org.wpilib.math.kinematics.SwerveModuleVelocity getOptimizedState(org.wpilib.math.kinematics.SwerveModuleVelocity state, SwerveModuleVelocityWithAzimuth measured) Get the optimizedSwerveModuleVelocityapplying all optional optimizations.- Parameters:
state-SwerveModuleVelocityto optimize.measured- The wheel's measured state: its angle and how fast it is turning.- Returns:
SwerveModuleVelocityoptimized.
-
getLockedState
public org.wpilib.math.kinematics.SwerveModuleVelocity getLockedState(org.wpilib.math.geometry.Rotation2d angle, SwerveModuleVelocityWithAzimuth measured) Get the state that turns the wheel toanglewithout driving it, as for locking the drive in place. UnlikegetOptimizedState(SwerveModuleVelocity, SwerveModuleVelocityWithAzimuth), the velocity deadband does not hold the wheel where it is, since the wheel is meant to turn without driving. With optimization enabled the wheel turns to whichever ofangleand its opposite is nearer, as either holds the drive the same way.- Parameters:
angle- Angle to turn the wheel to.measured- The wheel's measured state: its angle and how fast it is turning.- Returns:
- Zero speed
SwerveModuleVelocityat the angle to turn the wheel to.
-
getTelemetryName
Get the telemetry name for theSwerveModule.- Returns:
- Telemetry name for the
SwerveModule.
-
getTelemetryVerbosity
Get the telemetry verbosity for theSwerveModule.- Returns:
TelemetryVerbosityfor theSwerveModule.
-
getLocation
Get the location of theSwerveModulein meters from the center of rotation.- Returns:
Translation2dof theSwerveModule
-