Class REVSwerveMotor
- java.lang.Object
-
- frc.robot.subsystems.swervedrive.swerve.SwerveMotor
-
- frc.robot.subsystems.swervedrive.swerve.motors.REVSwerveMotor
-
public class REVSwerveMotor extends SwerveMotor
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classREVSwerveMotor.REV_slotIdxREV Slots for PID configuration.-
Nested classes/interfaces inherited from class frc.robot.subsystems.swervedrive.swerve.SwerveMotor
SwerveMotor.ModuleMotorType
-
-
Field Summary
Fields Modifier and Type Field Description private com.revrobotics.MotorFeedbackSensorm_encoderprivate java.util.function.Supplier<java.lang.Double>m_encoderPosRetprivate java.util.function.Supplier<java.lang.Double>m_encoderRetprivate com.revrobotics.SparkMaxPIDController.ArbFFUnitsm_feedforwardUnitsprivate booleanm_integratedAbsEncoderprivate intm_mainPidSlotprivate doublem_moduleRadkVkV feed forward for PIDprivate com.revrobotics.CANSparkMaxm_motorprivate com.revrobotics.SparkMaxPIDControllerm_pidprivate com.revrobotics.CANSparkMax.ControlTypem_pidControlTypeprivate intm_secondaryPidSlot-
Fields inherited from class frc.robot.subsystems.swervedrive.swerve.SwerveMotor
kD, kF, kI, kIZ, kP, m_motorType, target
-
-
Constructor Summary
Constructors Constructor Description REVSwerveMotor(com.revrobotics.CANSparkMax motor, SwerveEncoder<?> absoluteEncoder, SwerveMotor.ModuleMotorType type, double gearRatio, double wheelDiameter, double freeSpeedRPM, double powerLimit)Constructor for REV Swerve Motor, expecting CANSparkMax.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description doubleget()Get the current value of the encoder corresponding to the PID.doublegetAmps()Get the current output.doublegetPosition()Get the current value of the encoder.voidoptimizeCANFrames()Optimize the CAN status frames to reduce utilization.booleanreachable()Check that the link is good on the swerve module and CAN bus is able to retrieve data.booleanremoteIntegratedEncoder()Check if the absolute encoder is used inplace of the integrated encoder.voidsaveConfig()Save configuration data to the motor controller so it is persistent on reboot.voidset(double speed)Set the speed of the drive motor from -1 to 1.voidsetCANStatusFrames(int CANStatus0, int CANStatus1, int CANStatus2, int CANStatus3, int CANStatus4)Set the CAN status frames.voidsetConversionFactor(double conversionFactor)Configures the conversion factor based upon which motor.voidsetCurrentLimit(int currentLimit)Set the current limit for the swerve drive motor, remember this may cause jumping if used in conjunction with voltage compensation.voidsetEncoder(double value)Set the encoder value.voidsetInverted(boolean inverted)Invert the motor.voidsetPIDF(double P, double I, double D, double F, double integralZone)Set the PIDF coefficients for the closed loop PID onboard the SparkMax.voidsetPIDOutputRange(double min, double max)Configure the maximum power (-1 to 1) the PID can output.voidsetTarget(double target, double feedforward)Set the target for the PID loop.voidsetVoltageCompensation(double nominalVoltage)Set the voltage compensation for the swerve module motor.voidstop()Stop the motor by sending 0 volts to it.-
Methods inherited from class frc.robot.subsystems.swervedrive.swerve.SwerveMotor
fromMotor
-
-
-
-
Field Detail
-
m_mainPidSlot
private final int m_mainPidSlot
-
m_secondaryPidSlot
private final int m_secondaryPidSlot
-
m_pidControlType
private final com.revrobotics.CANSparkMax.ControlType m_pidControlType
-
m_motor
private final com.revrobotics.CANSparkMax m_motor
-
m_encoder
private final com.revrobotics.MotorFeedbackSensor m_encoder
-
m_integratedAbsEncoder
private final boolean m_integratedAbsEncoder
-
m_pid
private final com.revrobotics.SparkMaxPIDController m_pid
-
m_feedforwardUnits
private final com.revrobotics.SparkMaxPIDController.ArbFFUnits m_feedforwardUnits
-
m_encoderRet
private final java.util.function.Supplier<java.lang.Double> m_encoderRet
-
m_encoderPosRet
private final java.util.function.Supplier<java.lang.Double> m_encoderPosRet
-
m_moduleRadkV
private final double m_moduleRadkV
kV feed forward for PID
-
-
Constructor Detail
-
REVSwerveMotor
public REVSwerveMotor(com.revrobotics.CANSparkMax motor, SwerveEncoder<?> absoluteEncoder, SwerveMotor.ModuleMotorType type, double gearRatio, double wheelDiameter, double freeSpeedRPM, double powerLimit)Constructor for REV Swerve Motor, expecting CANSparkMax. Clears sticky faults and restores factory defaults.- Parameters:
motor- SparkMAX motor controller.absoluteEncoder- The absolute encoder used for the module, if the motor is a turning motor and the encoder is compatible it will set the encoder as the remote integrated encoder and does not need periodic synchronization.type- Swerve module motor type.gearRatio- Gear ratio.wheelDiameter- Wheel diameter in meters.freeSpeedRPM- Free speed RPM of the motor.powerLimit- Power limit for the motor.
-
-
Method Detail
-
setPIDOutputRange
public void setPIDOutputRange(double min, double max)Configure the maximum power (-1 to 1) the PID can output. This helps manage voltage pull for the drive base.- Specified by:
setPIDOutputRangein classSwerveMotor- Parameters:
min- Minimum output.max- Maximum output.
-
setPIDF
public void setPIDF(double P, double I, double D, double F, double integralZone)Set the PIDF coefficients for the closed loop PID onboard the SparkMax.- Specified by:
setPIDFin classSwerveMotor- Parameters:
P- Proportional gain for closed loop. This is multiplied by closed loop error in sensor units. Default is 1.0I- Integral gain for closed loop. This is multiplied by closed loop error in sensor units every PID Loop.D- Derivative gain for closed loop. This is multiplied by derivative error (sensor units per PID loop). Default is 0.1F- Feed Fwd gain for Closed loop.integralZone- Integral Zone can be used to auto clear the integral accumulator if the sensor pos is too far from the target. This prevents unstable oscillation if the kI is too large. Value is in sensor units.
-
setConversionFactor
public void setConversionFactor(double conversionFactor)
Configures the conversion factor based upon which motor.- Specified by:
setConversionFactorin classSwerveMotor- Parameters:
conversionFactor- Conversion from RPM to MPS for drive motor, and rotations to degrees for the turning motor.
-
setTarget
public void setTarget(double target, double feedforward)Set the target for the PID loop.- Specified by:
setTargetin classSwerveMotor- Parameters:
target- The PID target to aim for.feedforward- feedForward value.
-
stop
public void stop()
Stop the motor by sending 0 volts to it.- Specified by:
stopin classSwerveMotor
-
set
public void set(double speed)
Set the speed of the drive motor from -1 to 1.- Specified by:
setin classSwerveMotor- Parameters:
speed- Speed from -1 to 1.
-
get
public double get()
Get the current value of the encoder corresponding to the PID.- Specified by:
getin classSwerveMotor- Returns:
- Current value of the encoder.
-
getAmps
public double getAmps()
Get the current output.- Specified by:
getAmpsin classSwerveMotor- Returns:
- Output amps.
-
getPosition
public double getPosition()
Get the current value of the encoder.- Specified by:
getPositionin classSwerveMotor- Returns:
- Current value of the encoder.
-
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.
-
setEncoder
public void setEncoder(double value)
Set the encoder value.- Specified by:
setEncoderin classSwerveMotor- Parameters:
value- Value to set the encoder to.
-
setCANStatusFrames
public void setCANStatusFrames(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
-
reachable
public boolean reachable()
Check that the link is good on the swerve module and CAN bus is able to retrieve data.- Specified by:
reachablein classSwerveMotor- Returns:
- true on all devices are accessible over CAN.
-
remoteIntegratedEncoder
public boolean remoteIntegratedEncoder()
Check if the absolute encoder is used inplace of the integrated encoder.- Specified by:
remoteIntegratedEncoderin classSwerveMotor- Returns:
- true, if the absolute encoder is being used as the integrated controller.
-
optimizeCANFrames
public void optimizeCANFrames()
Optimize the CAN status frames to reduce utilization.- Specified by:
optimizeCANFramesin classSwerveMotor
-
saveConfig
public void saveConfig()
Save configuration data to the motor controller so it is persistent on reboot.- Specified by:
saveConfigin classSwerveMotor
-
setInverted
public void setInverted(boolean inverted)
Invert the motor.- Specified by:
setInvertedin classSwerveMotor- Parameters:
inverted- Set the motor as inverted.
-
-