Class SwerveModule<DriveMotorT extends CommonTalon, SteerMotorT extends CommonTalon, EncoderT extends ParentDevice>
- Type Parameters:
DriveMotorT- Type of the drive motorSteerMotorT- Type of the steer motorEncoderT- Type of the steer encoder
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 ClassesModifier and TypeClassDescriptionstatic enumAll possible control requests for the module drive motor.static classContains everything the swerve module needs to apply a request.static enumAll possible control requests for the module steer motor. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final intNumber of times to attempt config applies.protected final intID of the native drivetrain instance, used for JNI calls.protected final intIndex of this module in the native drivetrain, used for JNI calls. -
Constructor Summary
ConstructorsConstructorDescriptionSwerveModule(SwerveDrivetrain.DeviceConstructor<DriveMotorT> driveMotorConstructor, SwerveDrivetrain.DeviceConstructor<SteerMotorT> steerMotorConstructor, SwerveDrivetrain.DeviceConstructor<EncoderT> encoderConstructor, SwerveModuleConstants<?, ?, ?> constants, CANBus canbus, int drivetrainId, int index) Construct a SwerveModule with the specified constants. -
Method Summary
Modifier and TypeMethodDescriptionvoidapply(ControlRequest driveRequest, ControlRequest steerRequest) Controls this module using the specified drive and steer control requests.voidapply(SwerveModule.ModuleRequest moduleRequest) Applies the desiredSwerveModule.ModuleRequestto this module.final SwerveModulePositionGets the last cached swerve module position.final SwerveModuleVelocityGet the current velocity of the module.Gets the closed-loop output type to use for the drive motor.final DriveMotorTGets this module's Drive Motor reference.final EncoderTGets this module's azimuth encoder reference.final SwerveModulePositiongetPosition(boolean refresh) Gets the state of this module and passes it back as a SwerveModulePosition object with latency compensated values.Gets the closed-loop output type to use for the steer motor.final SteerMotorTGets this module's Steer Motor reference.final SwerveModuleVelocityGet the target velocity of the module.voidResets this module's drive motor position to 0 rotations.
-
Field Details
-
kNumConfigAttempts
protected static final int kNumConfigAttemptsNumber of times to attempt config applies.- See Also:
-
m_drivetrainId
protected final int m_drivetrainIdID of the native drivetrain instance, used for JNI calls. -
m_moduleIdx
protected final int m_moduleIdxIndex of this module in the native drivetrain, used for JNI calls.
-
-
Constructor Details
-
SwerveModule
public SwerveModule(SwerveDrivetrain.DeviceConstructor<DriveMotorT> driveMotorConstructor, SwerveDrivetrain.DeviceConstructor<SteerMotorT> steerMotorConstructor, SwerveDrivetrain.DeviceConstructor<EncoderT> encoderConstructor, SwerveModuleConstants<?, ?, ?> constants, CANBus canbus, int drivetrainId, int index) Construct a SwerveModule with the specified constants.- Parameters:
driveMotorConstructor- Constructor for the drive motor, such asTalonFX::newsteerMotorConstructor- Constructor for the steer motor, such asTalonFX::newencoderConstructor- Constructor for the azimuth encoder, such asCANcoder::newconstants- Constants used to construct the modulecanbus- The CAN bus this module is ondrivetrainId- ID of the swerve drivetrainindex- Index of this swerve module
-
-
Method Details
-
apply
Applies the desiredSwerveModule.ModuleRequestto this module.- Parameters:
moduleRequest- The request to apply to this module
-
apply
Controls this module using the specified drive and steer control requests. This is intended only to be used for characterization of the robot; do not use this for normal use.- Parameters:
driveRequest- The control request to apply to the drive motorsteerRequest- The control request to apply to the steer motor
-
getPosition
Gets the state of this module and passes it back as a SwerveModulePosition object with latency compensated values.This function is blocking when it performs a refresh.
- Parameters:
refresh- True if the signals should be refreshed- Returns:
- SwerveModulePosition containing this module's state
-
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
-
getCurrentVelocity
Get the current velocity of the module.This is typically used for telemetry, as the SwerveModulePosition is used for odometry.
- Returns:
- Current velocity of the module
-
getTargetVelocity
Get the target velocity of the module.This is typically used for telemetry.
- Returns:
- Target velocity of the module
-
resetPosition
public void resetPosition()Resets this module's drive motor position to 0 rotations. -
getDriveClosedLoopOutputType
Gets the closed-loop output type to use for the drive motor.- Returns:
- Drive motor closed-loop output type
-
getSteerClosedLoopOutputType
Gets the closed-loop output type to use for the steer motor.- Returns:
- Steer motor closed-loop output type
-
getDriveMotor
Gets this module's Drive Motor 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 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
-
getEncoder
Gets this module's azimuth encoder reference.This should be used only to access signals and change configurations that the swerve drivetrain does not configure itself.
- Returns:
- This module's azimuth encoder reference
-