Class SparkMaxSwerve
- java.lang.Object
-
- frc.robot.subsystems.swervedrive2.swervelib.motors.SwerveMotor
-
- frc.robot.subsystems.swervedrive2.swervelib.motors.SparkMaxSwerve
-
public class SparkMaxSwerve extends SwerveMotor
An implementation ofCANSparkMaxas aSwerveMotor.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classSparkMaxSwerve.SparkMAX_slotIdxREV Slots for PID configuration.
-
Field Summary
Fields Modifier and Type Field Description com.revrobotics.AbsoluteEncoderabsoluteEncoderAbsolute encoder attached to the SparkMax (if exists)com.revrobotics.RelativeEncoderencoderIntegrated encoder.private booleanfactoryDefaultOccurredFactory default already occurred.com.revrobotics.CANSparkMaxmotorSparkMAX Instance.com.revrobotics.SparkMaxPIDControllerpidClosed-loop PID controller.-
Fields inherited from class frc.robot.subsystems.swervedrive2.swervelib.motors.SwerveMotor
isDriveMotor
-
-
Constructor Summary
Constructors Constructor Description SparkMaxSwerve(int id, boolean isDriveMotor)Initialize theSwerveMotoras aCANSparkMaxconnected to a Brushless Motor.SparkMaxSwerve(com.revrobotics.CANSparkMax motor, boolean isDriveMotor)Initialize the swerve motor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidburnFlash()Save the configurations from flash to EEPROM.voidclearStickyFaults()Clear the sticky faults on the motor controller.voidconfigureCANStatusFrames(int CANStatus0, int CANStatus1, int CANStatus2, int CANStatus3, int CANStatus4)Set the CAN status frames.voidconfigureIntegratedEncoder(double positionConversionFactor)Configure the integrated encoder for the swerve module.voidconfigurePIDF(PIDFConfig config)Configure the PIDF values for the closed loop controller.voidconfigurePIDWrapping(double minInput, double maxInput)Configure the PID wrapping for the position closed loop controller.voidfactoryDefaults()Configure the factory defaults.ObjectgetMotor()Get the motor object from the module.doublegetPosition()Get the position of the integrated encoder.doublegetVelocity()Get the velocity of the integrated encoder.booleanisAttachedAbsoluteEncoder()Queries whether the absolute encoder is directly attached to the motor controller.voidset(double percentOutput)Set the percentage output.SwerveMotorsetAbsoluteEncoder(SwerveAbsoluteEncoder encoder)Set the absolute encoder to be a compatible absolute encoder.voidsetCurrentLimit(int currentLimit)Set the current limit for the swerve drive motor, remember this may cause jumping if used in conjunction with voltage compensation.voidsetInverted(boolean inverted)Set the motor to be inverted.voidsetLoopRampRate(double rampRate)Set the maximum rate the open/closed loop output can change by.voidsetMotorBrake(boolean isBrakeMode)Set the idle mode.voidsetPosition(double position)Set the integrated encoder position.voidsetReference(double setpoint, double feedforward)Set the closed loop PID controller reference point.voidsetVoltageCompensation(double nominalVoltage)Set the voltage compensation for the swerve module motor.
-
-
-
Field Detail
-
motor
public com.revrobotics.CANSparkMax motor
SparkMAX Instance.
-
encoder
public com.revrobotics.RelativeEncoder encoder
Integrated encoder.
-
absoluteEncoder
public com.revrobotics.AbsoluteEncoder absoluteEncoder
Absolute encoder attached to the SparkMax (if exists)
-
pid
public com.revrobotics.SparkMaxPIDController pid
Closed-loop PID controller.
-
factoryDefaultOccurred
private boolean factoryDefaultOccurred
Factory default already occurred.
-
-
Constructor Detail
-
SparkMaxSwerve
public SparkMaxSwerve(com.revrobotics.CANSparkMax motor, boolean isDriveMotor)Initialize the swerve motor.- Parameters:
motor- The SwerveMotor as a SparkMax object.isDriveMotor- Is the motor being initialized a drive motor?
-
SparkMaxSwerve
public SparkMaxSwerve(int id, boolean isDriveMotor)Initialize theSwerveMotoras aCANSparkMaxconnected to a Brushless Motor.- Parameters:
id- CAN ID of the SparkMax.isDriveMotor- Is the motor being initialized a drive motor?
-
-
Method Detail
-
setVoltageCompensation
public void setVoltageCompensation(double nominalVoltage)
Set the voltage compensation for the swerve module motor.- Specified by:
setVoltageCompensationin classSwerveMotor- Parameters:
nominalVoltage- Nominal voltage for operation to output to.
-
setCurrentLimit
public void setCurrentLimit(int currentLimit)
Set the current limit for the swerve drive motor, remember this may cause jumping if used in conjunction with voltage compensation. This is useful to protect the motor from current spikes.- Specified by:
setCurrentLimitin classSwerveMotor- Parameters:
currentLimit- Current limit in AMPS at free speed.
-
setLoopRampRate
public void setLoopRampRate(double rampRate)
Set the maximum rate the open/closed loop output can change by.- Specified by:
setLoopRampRatein classSwerveMotor- Parameters:
rampRate- Time in seconds to go from 0 to full throttle.
-
getMotor
public Object getMotor()
Get the motor object from the module.- Specified by:
getMotorin classSwerveMotor- Returns:
- Motor object.
-
isAttachedAbsoluteEncoder
public boolean isAttachedAbsoluteEncoder()
Queries whether the absolute encoder is directly attached to the motor controller.- Specified by:
isAttachedAbsoluteEncoderin classSwerveMotor- Returns:
- connected absolute encoder state.
-
factoryDefaults
public void factoryDefaults()
Configure the factory defaults.- Specified by:
factoryDefaultsin classSwerveMotor
-
clearStickyFaults
public void clearStickyFaults()
Clear the sticky faults on the motor controller.- Specified by:
clearStickyFaultsin classSwerveMotor
-
setAbsoluteEncoder
public SwerveMotor setAbsoluteEncoder(SwerveAbsoluteEncoder encoder)
Set the absolute encoder to be a compatible absolute encoder.- Specified by:
setAbsoluteEncoderin classSwerveMotor- Parameters:
encoder- The encoder to use.- Returns:
- The
SwerveMotorfor easy instantiation.
-
configureIntegratedEncoder
public void configureIntegratedEncoder(double positionConversionFactor)
Configure the integrated encoder for the swerve module. Sets the conversion factors for position and velocity.- Specified by:
configureIntegratedEncoderin classSwerveMotor- Parameters:
positionConversionFactor- The conversion factor to apply.
-
configurePIDF
public void configurePIDF(PIDFConfig config)
Configure the PIDF values for the closed loop controller.- Specified by:
configurePIDFin classSwerveMotor- Parameters:
config- Configuration class holding the PIDF values.
-
configurePIDWrapping
public void configurePIDWrapping(double minInput, double maxInput)Configure the PID wrapping for the position closed loop controller.- Specified by:
configurePIDWrappingin classSwerveMotor- Parameters:
minInput- Minimum PID input.maxInput- Maximum PID input.
-
configureCANStatusFrames
public void configureCANStatusFrames(int CANStatus0, int CANStatus1, int CANStatus2, int CANStatus3, int CANStatus4)Set the CAN status frames.- Parameters:
CANStatus0- Applied Output, Faults, Sticky Faults, Is FollowerCANStatus1- Motor Velocity, Motor Temperature, Motor Voltage, Motor CurrentCANStatus2- Motor PositionCANStatus3- Analog Sensor Voltage, Analog Sensor Velocity, Analog Sensor PositionCANStatus4- Alternate Encoder Velocity, Alternate Encoder Position
-
setMotorBrake
public void setMotorBrake(boolean isBrakeMode)
Set the idle mode.- Specified by:
setMotorBrakein classSwerveMotor- Parameters:
isBrakeMode- Set the brake mode.
-
setInverted
public void setInverted(boolean inverted)
Set the motor to be inverted.- Specified by:
setInvertedin classSwerveMotor- Parameters:
inverted- State of inversion.
-
burnFlash
public void burnFlash()
Save the configurations from flash to EEPROM.- Specified by:
burnFlashin classSwerveMotor
-
set
public void set(double percentOutput)
Set the percentage output.- Specified by:
setin classSwerveMotor- Parameters:
percentOutput- percent out for the motor controller.
-
setReference
public void setReference(double setpoint, double feedforward)Set the closed loop PID controller reference point.- Specified by:
setReferencein classSwerveMotor- Parameters:
setpoint- Setpoint in MPS or Angle in degrees.feedforward- Feedforward in volt-meter-per-second or kV.
-
getVelocity
public double getVelocity()
Get the velocity of the integrated encoder.- Specified by:
getVelocityin classSwerveMotor- Returns:
- velocity
-
getPosition
public double getPosition()
Get the position of the integrated encoder.- Specified by:
getPositionin classSwerveMotor- Returns:
- Position
-
setPosition
public void setPosition(double position)
Set the integrated encoder position.- Specified by:
setPositionin classSwerveMotor- Parameters:
position- Integrated encoder position.
-
-