Class SwerveModule
public class SwerveModule extends Object
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.
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
SwerveModule.ClosedLoopOutputType
Supported closed-loop output types.static class
SwerveModule.DriveRequestType
All possible control requests for the module drive motor.static class
SwerveModule.SteerRequestType
All possible control requests for the module steer motor. -
Constructor Summary
Constructors Constructor Description SwerveModule(SwerveModuleConstants constants, String canbusName)
Construct a SwerveModule with the specified constants. -
Method Summary
Modifier and Type Method Description void
apply(SwerveModuleState state, SwerveModule.DriveRequestType driveRequestType)
Applies the desired SwerveModuleState to this module.void
apply(SwerveModuleState state, SwerveModule.DriveRequestType driveRequestType, SwerveModule.SteerRequestType steerRequestType)
Applies the desired SwerveModuleState to this module.void
applyCharacterization(Rotation2d steerTarget, TorqueCurrentFOC driveRequest)
Controls this module to the specified steer target, and applies the specific drive request.void
applyCharacterization(Rotation2d steerTarget, VoltageOut driveRequest)
Controls this module to the specified steer target, and applies the specific drive request.StatusCode
configNeutralMode(NeutralModeValue neutralMode)
Configures the neutral mode to use for the module's drive motor.SwerveModulePosition
getCachedPosition()
Gets the last cached swerve module position.CANcoder
getCANcoder()
Gets this module's CANcoder reference.SwerveModuleState
getCurrentState()
Get the current state of the module.TalonFX
getDriveMotor()
Gets this module's Drive Motor TalonFX reference.SwerveModulePosition
getPosition(boolean refresh)
Gets the state of this module and passes it back as a SwerveModulePosition object with latency compensated values.TalonFX
getSteerMotor()
Gets this module's Steer Motor TalonFX reference.SwerveModuleState
getTargetState()
Get the target state of the module.void
resetPosition()
Resets this module's drive motor position to 0 rotations.
-
Constructor Details
-
SwerveModule
Construct a SwerveModule with the specified constants.- Parameters:
constants
- Constants used to construct the modulecanbusName
- The name of the CAN bus this module is on
-
-
Method Details
-
getPosition
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
Applies the desired SwerveModuleState to this module.- Parameters:
state
- Speed and direction the module should targetdriveRequestType
- TheSwerveModule.DriveRequestType
to apply
-
apply
public void apply(SwerveModuleState state, SwerveModule.DriveRequestType driveRequestType, SwerveModule.SteerRequestType steerRequestType)Applies the desired SwerveModuleState to this module.- Parameters:
state
- Speed and direction the module should targetdriveRequestType
- TheSwerveModule.DriveRequestType
to applysteerRequestType
- TheSwerveModule.SteerRequestType
to apply; defaults toSwerveModule.SteerRequestType.MotionMagic
-
applyCharacterization
Controls this module to the specified steer target, and applies the specific drive request.This is intended only to be used for characterization of the robot, do not use this for normal use.
- Parameters:
steerTarget
- The angle the wheels should face for characterizationdriveRequest
- The direct voltage to apply to the motor for use during characterization
-
applyCharacterization
Controls this module to the specified steer target, and applies the specific drive request.This is intended only to be used for characterization of the robot, do not use this for normal use.
- Parameters:
steerTarget
- The angle the wheels should face for characterizationdriveRequest
- The direct Torque Current to apply to the motor for use during characterization
-
configNeutralMode
Configures the neutral mode to use for the module's drive motor.- Parameters:
neutralMode
- The drive motor neutral mode- Returns:
- Status code response of the request
-
getCachedPosition
Gets the last cached swerve module position. This differs fromgetPosition(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
-
getTargetState
Get the target state of the module.This is typically used for telemetry.
- Returns:
- Target state of the module
-
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
-