Package frc.robot.subsystems.swervedrive.swerve.motors

## 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
`(package private) static class` `REVSwerveMotor.REV_slotIdx` REV Slots for PID configuration.

### Nested classes/interfaces inherited from class frc.robot.subsystems.swervedrive.swerve.SwerveMotor

`SwerveMotor.ModuleMotorType`

### Field Summary

Fields
`private com.revrobotics.MotorFeedbackSensor` `m_encoder`
`private java.util.function.Supplier<java.lang.Double>` `m_encoderPosRet`
`private java.util.function.Supplier<java.lang.Double>` `m_encoderRet`
`private com.revrobotics.SparkMaxPIDController.ArbFFUnits` `m_feedforwardUnits`
`private boolean` `m_integratedAbsEncoder`
`private int` `m_mainPidSlot`
`private double` `m_moduleRadkV` kV feed forward for PID
`private com.revrobotics.CANSparkMax` `m_motor`
`private com.revrobotics.SparkMaxPIDController` `m_pid`
`private com.revrobotics.CANSparkMax.ControlType` `m_pidControlType`
`private int` `m_secondaryPidSlot`

### Fields inherited from class frc.robot.subsystems.swervedrive.swerve.SwerveMotor

`kD, kF, kI, kIZ, kP, m_motorType, target`

### Constructor Summary

Constructors
`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
`double` `get()` Get the current value of the encoder corresponding to the PID.
`double` `getAmps()` Get the current output.
`double` `getPosition()` Get the current value of the encoder.
`void` `optimizeCANFrames()` Optimize the CAN status frames to reduce utilization.
`boolean` `reachable()` Check that the link is good on the swerve module and CAN bus is able to retrieve data.
`boolean` `remoteIntegratedEncoder()` Check if the absolute encoder is used inplace of the integrated encoder.
`void` `saveConfig()` Save configuration data to the motor controller so it is persistent on reboot.
`void` `set​(double speed)` Set the speed of the drive motor from -1 to 1.
`void` `setCANStatusFrames​(int CANStatus0, int CANStatus1, int CANStatus2, int CANStatus3, int CANStatus4)` Set the CAN status frames.
`void` `setConversionFactor​(double conversionFactor)` Configures the conversion factor based upon which motor.
`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.
`void` `setEncoder​(double value)` Set the encoder value.
`void` `setInverted​(boolean inverted)` Invert the motor.
`void` `setPIDF​(double P, double I, double D, double F, double integralZone)` Set the PIDF coefficients for the closed loop PID onboard the SparkMax.
`void` `setPIDOutputRange​(double min, double max)` Configure the maximum power (-1 to 1) the PID can output.
`void` `setTarget​(double target, double feedforward)` Set the target for the PID loop.
`void` `setVoltage​(double voltage)` Set the voltage of the motor.
`void` `setVoltageCompensation​(double nominalVoltage)` Set the voltage compensation for the swerve module motor.
`void` `stop()` Stop the motor by sending 0 volts to it.

### Methods inherited from class frc.robot.subsystems.swervedrive.swerve.SwerveMotor

`fromMotor`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### 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:
  `setPIDOutputRange` in class `SwerveMotor`
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:
  `setPIDF` in class `SwerveMotor`
Parameters:
  `P` - Proportional gain for closed loop. This is multiplied by closed loop error in sensor units. Default is 1.0
  `I` - 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.1
  `F` - 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:
  `setConversionFactor` in class `SwerveMotor`
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:
  `setTarget` in class `SwerveMotor`
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:
  `stop` in class `SwerveMotor`

#### set

    public void set​(double speed)

Set the speed of the drive motor from -1 to 1.

Specified by:
  `set` in class `SwerveMotor`
Parameters:
  `speed` - Speed from -1 to 1.

#### setVoltage

    public void setVoltage​(double voltage)

Set the voltage of the motor.

Specified by:
  `setVoltage` in class `SwerveMotor`
Parameters:
  `voltage` - Voltage to output.

#### get

    public double get()

Get the current value of the encoder corresponding to the PID.

Specified by:
  `get` in class `SwerveMotor`
Returns:
  Current value of the encoder.

#### getAmps

    public double getAmps()

Get the current output.

Specified by:
  `getAmps` in class `SwerveMotor`
Returns:
  Output amps.

#### getPosition

    public double getPosition()

Get the current value of the encoder.

Specified by:
  `getPosition` in class `SwerveMotor`
Returns:
  Current value of the encoder.

#### setVoltageCompensation

    public void setVoltageCompensation​(double nominalVoltage)

Set the voltage compensation for the swerve module motor.

Specified by:
  `setVoltageCompensation` in class `SwerveMotor`
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:
  `setCurrentLimit` in class `SwerveMotor`
Parameters:
  `currentLimit` - Current limit in AMPS at free speed.

#### setEncoder

    public void setEncoder​(double value)

Set the encoder value.

Specified by:
  `setEncoder` in class `SwerveMotor`
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 Follower
  `CANStatus1` - Motor Velocity, Motor Temperature, Motor Voltage, Motor Current
  `CANStatus2` - Motor Position
  `CANStatus3` - Analog Sensor Voltage, Analog Sensor Velocity, Analog Sensor Position
  `CANStatus4` - 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:
  `reachable` in class `SwerveMotor`
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:
  `remoteIntegratedEncoder` in class `SwerveMotor`
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:
  `optimizeCANFrames` in class `SwerveMotor`

#### saveConfig

    public void saveConfig()

Save configuration data to the motor controller so it is persistent on reboot.

Specified by:
  `saveConfig` in class `SwerveMotor`

#### setInverted

    public void setInverted​(boolean inverted)

Invert the motor.

Specified by:
  `setInverted` in class `SwerveMotor`
Parameters:
  `inverted` - Set the motor as inverted.