Package swervelib
Class SwerveModule
java.lang.Object
swervelib.SwerveModule
- All Implemented Interfaces:
AutoCloseable
The Swerve Module class which represents and controls Swerve Modules for the swerve drive.
-
Field Summary
FieldsModifier and TypeFieldDescriptionAbsolute encoder position cache.Swerve module configuration options.Drive motor position cache.Drive motor velocity cache.final intModule number for kinematics, usually 0 to 3. -
Constructor Summary
ConstructorsConstructorDescriptionSwerveModule(int moduleNumber, SwerveModuleConfiguration moduleConfiguration) Construct the swerve module and initialize the swerve module motors and absolute encoder. -
Method Summary
Modifier and TypeMethodDescriptionvoidapplyAntiJitter(SwerveModuleState desiredState, boolean force) Apply anti-jitter to the desired state.voidapplyStateOptimizations(SwerveModuleState desiredState) Apply theSwerveModuleState.optimize(Rotation2d)function if the module state optimization is enabled while debugging.voidclose()voidconfigureModuleSimulation(SwerveModuleSimulation swerveModuleSimulation, SwerveModulePhysicalCharacteristics physicalCharacteristics) Configure thesimModulewith the MapleSimSwerveModuleSimulationGet theSwerveAbsoluteEncoderfor theSwerveModule.booleanGet if the last Absolute Encoder had a read issue, such as it does not exist.doubleGet the absolute position.Get the angleSwerveMotorfor theSwerveModule.Get the current angle/azimuth/steering motor PIDF values.Fetch theSwerveModuleConfigurationfor theSwerveModulewith the parsed configurations.Get the defaultSimpleMotorFeedforwardfor the swerve module drive motor.Get the driveSwerveMotorfor theSwerveModule.Get the current drive motor PIDF values.Get the maximum module angular velocity as aAngularVelocitybased on the RPM and gear ratio.doubleGet the maximum drive velocity of the module in Meters Per Second.Get the maximum module velocity as aLinearVelocitybased on the RPM and gear ratio.booleanCheck if the module state optimization used bySwerveModuleState.optimize(Rotation2d)is enabled.Get the position of the swerve module.doubleGet the absolute position.doubleGet the relative angle in degrees.Obtains theSwerveModuleSimulationused in simulation.getState()Get the Swerve Module state.voidInvalidate theCacheobjects used bySwerveModule.voidDeprecated.voidQueue synchronization of the integrated angle encoder with the absolute encoder.voidRestore internal offset in YAGSL and either sets absolute encoder offset to 0 or restores old value.voidsetAngle(double angle) Set the angle for the module.voidsetAngleMotorConversionFactor(double conversionFactor) Set the conversion factor for the angle/azimuth motor controller.voidsetAngleMotorVoltageCompensation(double optimalVoltage) Set the voltage compensation for the swerve module motor.voidsetAnglePIDF(PIDFConfig config) Set the angle/azimuth/steering motor PIDvoidsetAntiJitter(boolean antiJitter) Set the antiJitter functionality, if true the modules will NOT auto center.voidsetDesiredState(SwerveModuleState desiredState, boolean isOpenLoop, boolean force) Set the desired state of the swerve module.voidsetDesiredState(SwerveModuleState desiredState, boolean isOpenLoop, double driveFeedforwardVoltage) Set the desired state of the swerve module.voidsetDriveMotorConversionFactor(double conversionFactor) Set the conversion factor for the drive motor controller.voidsetDriveMotorVoltageCompensation(double optimalVoltage) Set the voltage compensation for the swerve module motor.voidsetDrivePIDF(PIDFConfig config) Set the drive PIDF values.voidsetEncoderAutoSynchronize(boolean enabled) Enable auto synchronization for encoders during a match.voidsetEncoderAutoSynchronize(boolean enabled, double deadband) Enable auto synchronization for encoders during a match.voidSet the feedforward attributes to the given parameters.voidsetModuleStateOptimization(boolean optimizationState) Set utilization ofSwerveModuleState.optimize(Rotation2d)which should be disabled for some debugging.voidsetMotorBrake(boolean brake) Set the brake mode.voidUpdate data sent toSmartDashboard.voidUse external sensors for the feedback of the angle/azimuth/steer controller.voidUse external sensors for the feedback of the angle/azimuth/steer controller.
-
Field Details
-
configuration
Swerve module configuration options. -
absolutePositionCache
Absolute encoder position cache. -
drivePositionCache
Drive motor position cache. -
driveVelocityCache
Drive motor velocity cache. -
moduleNumber
public final int moduleNumberModule number for kinematics, usually 0 to 3. front left -> front right -> back left -> back right.
-
-
Constructor Details
-
SwerveModule
Construct the swerve module and initialize the swerve module motors and absolute encoder.- Parameters:
moduleNumber- Module number for kinematics.moduleConfiguration- Module constants containing CAN ID's and offsets.
-
-
Method Details
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-
getDefaultFeedforward
Get the defaultSimpleMotorFeedforwardfor the swerve module drive motor.- Returns:
SimpleMotorFeedforwardusing motor details.
-
setModuleStateOptimization
public void setModuleStateOptimization(boolean optimizationState) Set utilization ofSwerveModuleState.optimize(Rotation2d)which should be disabled for some debugging.- Parameters:
optimizationState- Optimization enabled.
-
getModuleStateOptimization
public boolean getModuleStateOptimization()Check if the module state optimization used bySwerveModuleState.optimize(Rotation2d)is enabled.- Returns:
- optimization state.
-
setAngleMotorVoltageCompensation
public void setAngleMotorVoltageCompensation(double optimalVoltage) Set the voltage compensation for the swerve module motor.- Parameters:
optimalVoltage- Nominal voltage for operation to output to.
-
setDriveMotorVoltageCompensation
public void setDriveMotorVoltageCompensation(double optimalVoltage) Set the voltage compensation for the swerve module motor.- Parameters:
optimalVoltage- Nominal voltage for operation to output to.
-
queueSynchronizeEncoders
public void queueSynchronizeEncoders()Queue synchronization of the integrated angle encoder with the absolute encoder. -
setEncoderAutoSynchronize
public void setEncoderAutoSynchronize(boolean enabled, double deadband) Enable auto synchronization for encoders during a match. This will only occur when the modules are not moving for a few seconds.- Parameters:
enabled- Enable statedeadband- Deadband in degrees, default is 3 degrees.
-
setEncoderAutoSynchronize
public void setEncoderAutoSynchronize(boolean enabled) Enable auto synchronization for encoders during a match. This will only occur when the modules are not moving for a few seconds.- Parameters:
enabled- Enable state
-
setAntiJitter
public void setAntiJitter(boolean antiJitter) Set the antiJitter functionality, if true the modules will NOT auto center. Pushes the offsets to the angle motor controllers as well.- Parameters:
antiJitter- Anti-Jitter state desired.
-
setFeedforward
Set the feedforward attributes to the given parameters.- Parameters:
drive- Drive motor feedforward for the module.
-
getDrivePIDF
Get the current drive motor PIDF values.- Returns:
PIDFConfigof the drive motor.
-
setDrivePIDF
Set the drive PIDF values.- Parameters:
config-PIDFConfigof that should be set.
-
getAnglePIDF
Get the current angle/azimuth/steering motor PIDF values.- Returns:
PIDFConfigof the angle motor.
-
setAnglePIDF
Set the angle/azimuth/steering motor PID- Parameters:
config-PIDFConfigof that should be set.
-
setDesiredState
Set the desired state of the swerve module.
WARNING: If you are not using one of the functions fromSwerveDriveyou may screw upSwerveDrive.kinematics- Parameters:
desiredState- Desired swerve module state.isOpenLoop- Whether to use open loop (direct percent) or direct velocity control.force- Disables optimizations that prevent movement in the angle motor and forces the desired state onto the swerve module.
-
setDesiredState
public void setDesiredState(SwerveModuleState desiredState, boolean isOpenLoop, double driveFeedforwardVoltage) Set the desired state of the swerve module.
WARNING: If you are not using one of the functions fromSwerveDriveyou may screw upSwerveDrive.kinematics- Parameters:
desiredState- Desired swerve module state.isOpenLoop- Whether to use open loop (direct percent) or direct velocity control.driveFeedforwardVoltage- Drive motor controller feedforward as a voltage.
-
applyStateOptimizations
Apply theSwerveModuleState.optimize(Rotation2d)function if the module state optimization is enabled while debugging.- Parameters:
desiredState- The desired state to apply the optimization to.
-
applyAntiJitter
Apply anti-jitter to the desired state. This will prevent the module from rotating if the speed requested is too low. If force is true, the anti-jitter will not be applied.- Parameters:
desiredState- The desired state to apply the anti-jitter to.force- Whether to ignore theantiJitterEnabledstate and apply the anti-jitter anyway.
-
setAngle
public void setAngle(double angle) Set the angle for the module.- Parameters:
angle- Angle in degrees.
-
getState
Get the Swerve Module state.- Returns:
- Current SwerveModule state.
-
getPosition
Get the position of the swerve module.- Returns:
SwerveModulePositionof the swerve module.
-
getAbsolutePosition
public double getAbsolutePosition()Get the absolute position. Falls back to relative position on reading failure.- Returns:
- Absolute encoder angle in degrees in the range [0, 360).
-
getRawAbsolutePosition
public double getRawAbsolutePosition()Get the absolute position. Falls back to relative position on reading failure.- Returns:
- Absolute encoder angle in degrees in the range [0, 360).
-
getRelativePosition
public double getRelativePosition()Get the relative angle in degrees.- Returns:
- Angle in degrees.
-
setMotorBrake
public void setMotorBrake(boolean brake) Set the brake mode.- Parameters:
brake- Set the brake mode.
-
setAngleMotorConversionFactor
public void setAngleMotorConversionFactor(double conversionFactor) Set the conversion factor for the angle/azimuth motor controller.- Parameters:
conversionFactor- Angle motor conversion factor for PID, should be generated fromSwerveMath.calculateDegreesPerSteeringRotation(double, double)or calculated.
-
setDriveMotorConversionFactor
public void setDriveMotorConversionFactor(double conversionFactor) Set the conversion factor for the drive motor controller.- Parameters:
conversionFactor- Drive motor conversion factor for PID, should be generated fromSwerveMath.calculateMetersPerRotation(double, double, double)or calculated.
-
getAngleMotor
Get the angleSwerveMotorfor theSwerveModule.- Returns:
SwerveMotorfor the angle/steering motor of the module.
-
getDriveMotor
Get the driveSwerveMotorfor theSwerveModule.- Returns:
SwerveMotorfor the drive motor of the module.
-
getAbsoluteEncoder
Get theSwerveAbsoluteEncoderfor theSwerveModule.- Returns:
SwerveAbsoluteEncoderfor the swerve module.
-
getConfiguration
Fetch theSwerveModuleConfigurationfor theSwerveModulewith the parsed configurations.- Returns:
SwerveModuleConfigurationfor theSwerveModule.
-
useExternalFeedbackSensor
public void useExternalFeedbackSensor()Use external sensors for the feedback of the angle/azimuth/steer controller. -
useInternalFeedbackSensor
public void useInternalFeedbackSensor()Use external sensors for the feedback of the angle/azimuth/steer controller. -
pushOffsetsToEncoders
Deprecated.Push absolute encoder offset in the memory of the encoder or controller. Also removes the internal angle offset. -
restoreInternalOffset
public void restoreInternalOffset()Restore internal offset in YAGSL and either sets absolute encoder offset to 0 or restores old value. -
getAbsoluteEncoderReadIssue
public boolean getAbsoluteEncoderReadIssue()Get if the last Absolute Encoder had a read issue, such as it does not exist.- Returns:
- If the last Absolute Encoder had a read issue, or absolute encoder does not exist.
-
getMaxVelocity
Get the maximum module velocity as aLinearVelocitybased on the RPM and gear ratio.- Returns:
LinearVelocitymax velocity of the drive wheel.
-
getMaxDriveVelocityMetersPerSecond
public double getMaxDriveVelocityMetersPerSecond()Get the maximum drive velocity of the module in Meters Per Second.- Returns:
- Maximum drive motor velocity in Meters Per Second.
-
getMaxAngularVelocity
Get the maximum module angular velocity as aAngularVelocitybased on the RPM and gear ratio.- Returns:
AngularVelocitymax velocity of the angle/azimuth.
-
updateTelemetry
public void updateTelemetry()Update data sent toSmartDashboard. -
invalidateCache
public void invalidateCache()Invalidate theCacheobjects used bySwerveModule. -
getSimModule
Obtains theSwerveModuleSimulationused in simulation.- Returns:
- the module simulation, null if this method is called on a real robot
-
configureModuleSimulation
public void configureModuleSimulation(SwerveModuleSimulation swerveModuleSimulation, SwerveModulePhysicalCharacteristics physicalCharacteristics) Configure thesimModulewith the MapleSimSwerveModuleSimulation- Parameters:
swerveModuleSimulation- MapleSimSwerveModuleSimulationto configure with.physicalCharacteristics-SwerveModulePhysicalCharacteristicsthat represent the swerve drive.
-