Class SwerveModule

java.lang.Object
com.marslib.swerve.SwerveModule

public class SwerveModule extends Object
Represents a singular Swerve Module (e.g. Front-Left, Front-Right). This class abstracts the actual hardware implementation (TalonFX vs Sim) via the SwerveModuleIO layer.

Students: This class handles the math converting raw Radians from the IO layer into standard WPILib Meters and Meters/Second parameters for the PoseEstimator.

  • Constructor Summary

    Constructors
    Constructor
    Description
    SwerveModule(int index, SwerveModuleIO io, SwerveConfig config)
    Constructs a generic Swerve Module boundary structure.
  • Method Summary

    Modifier and Type
    Method
    Description
    edu.wpi.first.math.kinematics.SwerveModulePosition
    Accesses the pre-allocated cache representing positional samples without creating arrays.
    int
    Returns the count of valid samples populated in the buffer during the last periodic cycle.
    edu.wpi.first.math.kinematics.SwerveModuleState
    Gets the previously cached desired state of the module for traction comparison.
    double
    Returns the actual applied voltage read from the motor controller or sim model.
    double
    Gets the actual stator current flowing through the drive motor.
    edu.wpi.first.math.kinematics.SwerveModulePosition
    Returns the most recent physical location recorded by the drive motors.
    edu.wpi.first.math.kinematics.SwerveModuleState
    Translates drive wheel RPS and turn module radians into WPILib Velocity metrics.
    double[]
    Returns the hardware timestamps of the high-frequency positional records.
    double
    Used strictly for extracting injected sim forces.
    void
    injectModuleSimulation(org.ironmaple.simulation.drivesims.SwerveModuleSimulation simModule)
    Injects the maple-sim module simulation reference into the underlying IO layer if it is a SwerveModuleIOSim.
    void
     
    void
    setDesiredState(edu.wpi.first.math.kinematics.SwerveModuleState desiredState)
    Optimizes and applies a desired module state (drive speed + turn angle) with closed-loop turn control and simple voltage feedforward for driving.
    void
    setDriveVoltage(double volts)
    Routes target voltage demands safely down into the IO execution layer.
    void
    setTurnVoltage(double volts)
    Routes steer voltage demands safely down into the IO execution layer.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • SwerveModule

      public SwerveModule(int index, SwerveModuleIO io, SwerveConfig config)
      Constructs a generic Swerve Module boundary structure.
      Parameters:
      index - The ID/Index (0=FL, 1=FR, 2=BL, 3=BR) for structural AdvantageKit logging keys.
      io - The implementation-specific IO layer (TalonFX or Sim).
      config - The global swerve configuration.
  • Method Details

    • periodic

      public void periodic()
    • getDeltaCount

      public int getDeltaCount()
      Returns the count of valid samples populated in the buffer during the last periodic cycle.
    • getCachedDelta

      public edu.wpi.first.math.kinematics.SwerveModulePosition getCachedDelta(int i)
      Accesses the pre-allocated cache representing positional samples without creating arrays.
    • getOdometryTimestamps

      public double[] getOdometryTimestamps()
      Returns the hardware timestamps of the high-frequency positional records.
      Returns:
      Array of FPGA/Hardware timestamps tightly synced with getCachedDelta(int).
    • getLatestPosition

      public edu.wpi.first.math.kinematics.SwerveModulePosition getLatestPosition()
      Returns the most recent physical location recorded by the drive motors.
      Returns:
      A singular SwerveModulePosition bounding distance traveled and heading.
    • getLatestState

      public edu.wpi.first.math.kinematics.SwerveModuleState getLatestState()
      Translates drive wheel RPS and turn module radians into WPILib Velocity metrics.
      Returns:
      A cached SwerveModuleState tracking linear velocity (m/s) and angular heading. The returned object is reused — do not store references across ticks.
    • setDesiredState

      public void setDesiredState(edu.wpi.first.math.kinematics.SwerveModuleState desiredState)
      Optimizes and applies a desired module state (drive speed + turn angle) with closed-loop turn control and simple voltage feedforward for driving.

      Students: This method first calls SwerveModuleState.optimize() to minimize turn motor rotation. It then applies a proportional voltage controller to steer the module and a linear voltage mapping for drive speed.

      Parameters:
      desiredState - The target speed and angle for this module.
    • setDriveVoltage

      public void setDriveVoltage(double volts)
      Routes target voltage demands safely down into the IO execution layer.
      Parameters:
      volts - Target requested feedforward / PID voltage calculated securely.
    • getSimDriveVoltage

      public double getSimDriveVoltage()
      Used strictly for extracting injected sim forces.
    • getDriveAppliedVoltage

      public double getDriveAppliedVoltage()
      Returns the actual applied voltage read from the motor controller or sim model. Useful for SysId extraction and telemetry.
    • getDriveCurrentAmps

      public double getDriveCurrentAmps()
      Gets the actual stator current flowing through the drive motor.
    • getDesiredState

      public edu.wpi.first.math.kinematics.SwerveModuleState getDesiredState()
      Gets the previously cached desired state of the module for traction comparison.
    • setTurnVoltage

      public void setTurnVoltage(double volts)
      Routes steer voltage demands safely down into the IO execution layer.
      Parameters:
      volts - Target requested feedforward / PID voltage calculated securely.
    • injectModuleSimulation

      public void injectModuleSimulation(org.ironmaple.simulation.drivesims.SwerveModuleSimulation simModule)
      Injects the maple-sim module simulation reference into the underlying IO layer if it is a SwerveModuleIOSim. This ensures the sim IO reads encoder readings from the single physics engine rather than running its own duplicate motor simulation.

      No-op if the IO layer is not a sim implementation.

      Parameters:
      simModule - The SwerveModuleSimulation instance to inject.