Class SwerveModuleConfig

java.lang.Object
yams.core.mechanisms.config.SwerveModuleConfig

public class SwerveModuleConfig extends Object
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 Details

  • Method Details

    • clone

      public SwerveModuleConfig clone()
      Overrides:
      clone in class Object
    • withSmartMotorController

      public SwerveModuleConfig withSmartMotorController(SmartMotorController driveMotor, SmartMotorController azimuthMotor)
      Parameters:
      driveMotor - SmartMotorController for the drive motor.
      azimuthMotor - SmartMotorController for the azimuth motor.
      Returns:
      SwerveModuleConfig for chaining.
      Throws:
      IllegalStateException - if the drive or azimuth motor controller was already set, either by SwerveModuleConfig(SmartMotorController, SmartMotorController) or a previous call to this method.
    • withCosineCompensation

      public SwerveModuleConfig withCosineCompensation(boolean compensate)
      Cosine compensation for the SwerveModule, adjusting the velocity by the cosine of the (current_angle-desired_angle).
      Parameters:
      compensate - Enable or disable cosine compensation.
      Returns:
      SwerveModuleConfig for chaining.
    • withCouplingRatio

      public SwerveModuleConfig withCouplingRatio(GearBox couplingRatio)
      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 a GearBox from 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) with kCoupleRatio from CTRE Tuner X.
      Returns:
      SwerveModuleConfig for chaining.
    • withCouplingRatio

      public SwerveModuleConfig withCouplingRatio(double driveMotorRotationsPerAzimuthRotation)
      Set the steer to drive coupling ratio as a single reduction; see withCouplingRatio(GearBox).
      Parameters:
      driveMotorRotationsPerAzimuthRotation - Drive motor rotations caused by one azimuth rotation with the wheel held still, e.g. kCoupleRatio from CTRE Tuner X. 0 disables coupling compensation.
      Returns:
      SwerveModuleConfig for chaining.
    • getCouplingRatio

      public Optional<GearBox> getCouplingRatio()
      Get the steer to drive coupling ratio.
      Returns:
      Coupling from the drive motor (input) to the azimuth (output), or empty if coupling is not compensated.
    • withAbsoluteEncoder

      public SwerveModuleConfig withAbsoluteEncoder(Object absoluteEncoder)
      Set the absolute encoder for the azimuth SmartMotorController if it's the SAME VENDOR, only.
      Parameters:
      absoluteEncoder - Same vendor, absolute encoder for the SmartMotorController
      Returns:
      SwerveModuleConfig for 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 the SwerveModule.
      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:
      SwerveModuleConfig for chaining.
    • withLocation

      public SwerveModuleConfig withLocation(org.wpilib.math.geometry.Translation2d location)
      Set the location for the SwerveModule in Meters from the center of rotation.
      Parameters:
      location - Location of the SwerveModule in meters from the center of rotation.
      Returns:
      SwerveModuleConfig for 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 the SwerveModule.
      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:
      SwerveModuleConfig for chaining.
    • withLocation

      public SwerveModuleConfig withLocation(org.wpilib.units.measure.Distance distance, org.wpilib.units.measure.Angle angle)
      Set the location for the SwerveModule using polar coordinates.
      Parameters:
      distance - Distance from the center of rotation will be converted to meters.
      angle - Angle from the center of rotation.
      Returns:
      SwerveModuleConfig for chaining.
    • withAbsoluteEncoder

      public SwerveModuleConfig withAbsoluteEncoder(Supplier<org.wpilib.units.measure.Angle> locationProvider)
      Supply a non-alike vendor absolute encoder for the azimuth SmartMotorController.
      Parameters:
      locationProvider - Provides the Rotation for the azimuth SmartMotorController.
      Returns:
      SwerveModuleConfig for chaining.
    • withAbsoluteEncoder

      public SwerveModuleConfig withAbsoluteEncoder(DoubleSupplier rotationSupplier)
      Supply a non-alike vendor absolute encoder for the azimuth SmartMotorController.
      Parameters:
      rotationSupplier - Provides the Rotation for the azimuth SmartMotorController.
      Returns:
      SwerveModuleConfig for chaining.
    • withAbsoluteEncoderGearing

      public SwerveModuleConfig withAbsoluteEncoderGearing(GearBox gearing)
      Set the gearing of the absolute encoder if it's not 1:1.
      Parameters:
      gearing - GearBox for the absolute encoder to get the azimuth angle.
      Returns:
      SwerveModuleConfig for chaining.
    • withAbsoluteEncoderOffset

      public SwerveModuleConfig withAbsoluteEncoderOffset(org.wpilib.units.measure.Angle offset)
      Set the absolute encoder offset for the azimuth SmartMotorController so 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:
      SwerveModuleConfig for chaining.
    • withWheelRadius

      public SwerveModuleConfig withWheelRadius(org.wpilib.units.measure.Distance radius)
      Set the wheel radius for the SmartMotorController, ideally should be set inside the drive motor SmartMotorControllerConfig
      Parameters:
      radius - Radius of the wheel.
      Returns:
      SwerveModuleConfig for chaining.
    • withWheelDiameter

      public SwerveModuleConfig withWheelDiameter(org.wpilib.units.measure.Distance diameter)
      Set the wheel diameter for the SmartMotorController, ideally should be set inside the drive motor SmartMotorControllerConfig.
      Parameters:
      diameter - Diameter of the wheel.
      Returns:
      SwerveModuleConfig for chaining.
    • withVelocityDeadband

      public SwerveModuleConfig 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.
      Parameters:
      speed - Velocity deadband, or null to always steer.
      Returns:
      SwerveModuleConfig for chaining.
    • withOptimization

      public SwerveModuleConfig withOptimization(boolean swerveModuleStateOptimization)
      Use SwerveModuleVelocity.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:
      SwerveModuleConfig for chaining.
    • withTelemetry

      public SwerveModuleConfig withTelemetry(String telemetryName, TelemetryVerbosity telemetryVerbosity)
      Configure telemetry for the SwerveModule mechanism.
      Parameters:
      telemetryName - Telemetry NetworkTable name to appear under "SmartDashboard/"
      telemetryVerbosity - Telemetry verbosity to apply.
      Returns:
      ArmConfig for chaining.
    • withTelemetry

      public SwerveModuleConfig withTelemetry(String telemetryName, SwerveModuleTelemetryConfig telemetryConfig)
      Configure telemetry for the SwerveModule mechanism with a SwerveModuleTelemetryConfig.
      Parameters:
      telemetryName - Telemetry NetworkTable name to appear under "SmartDashboard/"
      telemetryConfig - Config that specifies what to log.
      Returns:
      SwerveModuleConfig for chaining.
    • getSwerveModuleTelemetryConfig

      public Optional<SwerveModuleTelemetryConfig> getSwerveModuleTelemetryConfig()
      Returns:
      SwerveModuleTelemetryConfig if configured.
    • getAbsoluteEncoderAngle

      public org.wpilib.units.measure.Angle getAbsoluteEncoderAngle()
      Get the absolute encoder angle for the azimuth SmartMotorController.
      Returns:
      Absolute encoder Angle.
      Throws:
      NoSuchElementException - if no azimuth motor controller is set, even when an absolute encoder supplier is configured.
    • getRawAbsoluteEncoderAngle

      public Supplier<org.wpilib.units.measure.Angle> 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

      public Optional<Supplier<org.wpilib.units.measure.Angle>> getAbsoluteEncoderSupplier()
      Get the absolute encoder supplier
      Returns:
      Absolute encoder supplier.
    • getStateOptimization

      public boolean getStateOptimization()
      Enable or disable state optimization.
      Returns:
      True if optimization is enabled, false otherwise.
    • getDriveMotor

      public SmartMotorController getDriveMotor()
      Get the drive SmartMotorController for the SwerveModule.
      Returns:
      SmartMotorController for the drive motor.
      Throws:
      NoSuchElementException - if no drive motor controller is set, e.g. the no-arg constructor was used without calling withSmartMotorController(SmartMotorController, SmartMotorController).
    • getAzimuthMotor

      public SmartMotorController getAzimuthMotor()
      Get the azimuth SmartMotorController for the SwerveModule.
      Returns:
      SmartMotorController for the azimuth motor.
      Throws:
      NoSuchElementException - if no azimuth motor controller is set, e.g. the no-arg constructor was used without calling withSmartMotorController(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 bypass getOptimizedState(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 with withSmartMotorController(SmartMotorController, SmartMotorController).
    • getOptimizedState

      public org.wpilib.math.kinematics.SwerveModuleVelocity getOptimizedState(org.wpilib.math.kinematics.SwerveModuleVelocity state)
      Get the optimized SwerveModuleVelocity applying all optional optimizations, measuring the wheel with the azimuth motor controller.
      Parameters:
      state - SwerveModuleVelocity to optimize.
      Returns:
      SwerveModuleVelocity optimized.
      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 optimized SwerveModuleVelocity applying all optional optimizations.
      Parameters:
      state - SwerveModuleVelocity to optimize.
      measured - The wheel's measured state: its angle and how fast it is turning.
      Returns:
      SwerveModuleVelocity optimized.
    • getLockedState

      public org.wpilib.math.kinematics.SwerveModuleVelocity getLockedState(org.wpilib.math.geometry.Rotation2d angle, SwerveModuleVelocityWithAzimuth measured)
      Get the state that turns the wheel to angle without driving it, as for locking the drive in place. Unlike getOptimizedState(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 of angle and 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 SwerveModuleVelocity at the angle to turn the wheel to.
    • getTelemetryName

      public Optional<String> getTelemetryName()
      Get the telemetry name for the SwerveModule.
      Returns:
      Telemetry name for the SwerveModule.
    • getTelemetryVerbosity

      public Optional<TelemetryVerbosity> getTelemetryVerbosity()
      Get the telemetry verbosity for the SwerveModule.
      Returns:
      TelemetryVerbosity for the SwerveModule.
    • getLocation

      public Optional<org.wpilib.math.geometry.Translation2d> getLocation()
      Get the location of the SwerveModule in meters from the center of rotation.
      Returns:
      Translation2d of the SwerveModule