Class SwerveModule

java.lang.Object
com.ctre.phoenix6.mechanisms.swerve.SwerveModule

public class SwerveModule
extends Object
Swerve Module class that encapsulates a swerve module powered by CTR Electronics devices.

This class handles the hardware devices and configures them for swerve module operation using the Phoenix 6 API.

This class constructs hardware devices internally, so the user only specifies the constants (IDs, PID gains, gear ratios, etc). Getters for these hardware devices are available.

  • Constructor Details

    • SwerveModule

      public SwerveModule​(SwerveModuleConstants constants, String canbusName, boolean supportsPro)
      Construct a SwerveModule with the specified constants.
      Parameters:
      constants - Constants used to construct the module
      canbusName - The name of the CAN bus this module is on
      supportsPro - True if the devices are licensed to use Pro features
  • Method Details

    • getPosition

      public SwerveModulePosition getPosition​(boolean refresh)
      Gets the state of this module and passes it back as a SwerveModulePosition object with latency compensated values.
      Parameters:
      refresh - True if the signals should be refreshed
      Returns:
      SwerveModulePosition containing this module's state.
    • apply

      public void apply​(SwerveModuleState state, boolean isOpenLoop)
      Applies the desired SwerveModuleState to this module.
      Parameters:
      state - Speed and direction the module should target
      isOpenLoop - True if this should use open-loop control.
    • getCachedPosition

      Gets the last cached swerve module position. This differs from getPosition(boolean) in that it will not perform any latency compensation or refresh the signals.
      Returns:
      Last cached SwerveModulePosition
    • getCurrentState

      Get the current state of the module.

      This is typically used for telemetry, as the SwerveModulePosition is used for odometry.

      Returns:
      Current state of the module
    • resetPosition

      public void resetPosition()
      Resets this module's drive motor position to 0 rotations.
    • getDriveMotor

      Gets this module's Drive Motor TalonFX reference.

      This should be used only to access signals and change configurations that the swerve drivetrain does not configure itself.

      Returns:
      This module's Drive Motor reference
    • getSteerMotor

      Gets this module's Steer Motor TalonFX reference.

      This should be used only to access signals and change configurations that the swerve drivetrain does not configure itself.

      Returns:
      This module's Steer Motor reference
    • getCANcoder

      Gets this module's CANcoder reference.

      This should be used only to access signals and change configurations that the swerve drivetrain does not configure itself.

      Returns:
      This module's CANcoder reference