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

## Class CTRESwerveMotor

- java.lang.Object
- - frc.robot.subsystems.swervedrive.swerve.SwerveMotor
  - - frc.robot.subsystems.swervedrive.swerve.motors.CTRESwerveMotor

    public class CTRESwerveMotor
    extends SwerveMotor

### Nested Class Summary

Nested Classes
`(package private) static class` `CTRESwerveMotor.CTRE_pidIdx` The TalonSRX PID to use onboard.
`(package private) static class` `CTRESwerveMotor.CTRE_remoteSensor`
`(package private) static class` `CTRESwerveMotor.CTRE_slotIdx` The Talon SRX Slot profile used to configure the motor to use for the PID.

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

`SwerveMotor.ModuleMotorType`

### Field Summary

Fields
`private double` `m_allowableClosedLoopError`
`private SwerveEncoder` `m_angleEncoder`
`private com.ctre.phoenix.motorcontrol.ControlMode` `m_controlMode`
`private java.util.function.Supplier<java.lang.Double>` `m_encoderRet`
`private boolean` `m_integratedAbsEncoder`
`private int` `m_mainPidId`
`private int` `m_mainPIDSlotId`
`private double` `m_moduleRadkV` kV feed forward for PID
`private com.ctre.phoenix.motorcontrol.can.WPI_TalonFX` `m_motor`

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

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

### Constructor Summary

Constructors
`CTRESwerveMotor​(com.ctre.phoenix.motorcontrol.can.WPI_TalonFX motor, SwerveEncoder<?> encoder, SwerveMotor.ModuleMotorType type, double gearRatio, double wheelDiameter, double freeSpeedRPM, double powerLimit)`

### 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` `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_angleEncoder

    private final SwerveEncoder m_angleEncoder

#### m_integratedAbsEncoder

    private final boolean m_integratedAbsEncoder

#### m_allowableClosedLoopError

    private final double m_allowableClosedLoopError

#### m_controlMode

    private final com.ctre.phoenix.motorcontrol.ControlMode m_controlMode

#### m_mainPIDSlotId

    private final int m_mainPIDSlotId

#### m_mainPidId

    private final int m_mainPidId

#### m_encoderRet

    private final java.util.function.Supplier<java.lang.Double> m_encoderRet

#### m_motor

    private final com.ctre.phoenix.motorcontrol.can.WPI_TalonFX m_motor

#### m_moduleRadkV

    private final double m_moduleRadkV

kV feed forward for PID

### Constructor Detail

#### CTRESwerveMotor

    public CTRESwerveMotor​(com.ctre.phoenix.motorcontrol.can.WPI_TalonFX motor,
                           SwerveEncoder<?> encoder,
                           SwerveMotor.ModuleMotorType type,
                           double gearRatio,
                           double wheelDiameter,
                           double freeSpeedRPM,
                           double powerLimit)

### 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` - The feedforward for this target.

#### 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.

#### 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.

#### 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.

#### getAmps

    public double getAmps()

Description copied from class: `SwerveMotor`
Get the current output.

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