Package com.marslib.swerve
Class SwerveModule
java.lang.Object
com.marslib.swerve.SwerveModule
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
ConstructorsConstructorDescriptionSwerveModule(int index, SwerveModuleIO io, SwerveConfig config) Constructs a generic Swerve Module boundary structure. -
Method Summary
Modifier and TypeMethodDescriptionedu.wpi.first.math.kinematics.SwerveModulePositiongetCachedDelta(int i) Accesses the pre-allocated cache representing positional samples without creating arrays.intReturns the count of valid samples populated in the buffer during the last periodic cycle.edu.wpi.first.math.kinematics.SwerveModuleStateGets the previously cached desired state of the module for traction comparison.doubleReturns the actual applied voltage read from the motor controller or sim model.doubleGets the actual stator current flowing through the drive motor.edu.wpi.first.math.kinematics.SwerveModulePositionReturns the most recent physical location recorded by the drive motors.edu.wpi.first.math.kinematics.SwerveModuleStateTranslates drive wheel RPS and turn module radians into WPILib Velocity metrics.double[]Returns the hardware timestamps of the high-frequency positional records.doubleUsed strictly for extracting injected sim forces.voidinjectModuleSimulation(org.ironmaple.simulation.drivesims.SwerveModuleSimulation simModule) Injects the maple-sim module simulation reference into the underlying IO layer if it is aSwerveModuleIOSim.voidperiodic()voidsetDesiredState(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.voidsetDriveVoltage(double volts) Routes target voltage demands safely down into the IO execution layer.voidsetTurnVoltage(double volts) Routes steer voltage demands safely down into the IO execution layer.
-
Constructor Details
-
SwerveModule
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
SwerveModulePositionbounding 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
SwerveModuleStatetracking 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 aSwerveModuleIOSim. 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- TheSwerveModuleSimulationinstance to inject.
-