Class SparkMaxBrushedMotorSwerve

java.lang.Object
swervelib.motors.SwerveMotor
swervelib.motors.SparkMaxBrushedMotorSwerve

public class SparkMaxBrushedMotorSwerve extends SwerveMotor
Brushed motor control with SparkMax.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    com.revrobotics.AbsoluteEncoder
    Absolute encoder attached to the SparkMax (if exists)
    com.revrobotics.RelativeEncoder
    Integrated encoder.
    private boolean
    Factory default already occurred.
    private Alert
    An Alert for if there is an error configuring the motor.
    com.revrobotics.CANSparkMax
    SparkMAX Instance.
    private Alert
    An Alert for if the motor has no encoder.
    private Alert
    An Alert for if the motor has no encoder defined.
    com.revrobotics.SparkPIDController
    Closed-loop PID controller.

    Fields inherited from class swervelib.motors.SwerveMotor

    isDriveMotor, maximumRetries
  • Constructor Summary

    Constructors
    Constructor
    Description
    SparkMaxBrushedMotorSwerve(int id, boolean isDriveMotor, com.revrobotics.SparkRelativeEncoder.Type encoderType, int countsPerRevolution, boolean useDataPortQuadEncoder)
    Initialize the SwerveMotor as a CANSparkMax connected to a Brushless Motor.
    SparkMaxBrushedMotorSwerve(com.revrobotics.CANSparkMax motor, boolean isDriveMotor, com.revrobotics.SparkRelativeEncoder.Type encoderType, int countsPerRevolution, boolean useDataPortQuadEncoder)
    Initialize the swerve motor.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Save the configurations from flash to EEPROM.
    void
    Clear the sticky faults on the motor controller.
    void
    configureCANStatusFrames(int CANStatus0, int CANStatus1, int CANStatus2, int CANStatus3, int CANStatus4)
    Set the CAN status frames.
    void
    configureIntegratedEncoder(double positionConversionFactor)
    Configure the integrated encoder for the swerve module.
    void
    Configure the PIDF values for the closed loop controller.
    void
    configurePIDWrapping(double minInput, double maxInput)
    Configure the PID wrapping for the position closed loop controller.
    private void
    configureSparkMax(Supplier<com.revrobotics.REVLibError> config)
    Run the configuration until it succeeds or times out.
    void
    Configure the factory defaults.
    double
    Get the applied dutycycle output.
    Get the motor object from the module.
    double
    Get the position of the integrated encoder.
    double
    Get the velocity of the integrated encoder.
    double
    Get the voltage output of the motor controller.
    boolean
    Queries whether the absolute encoder is directly attached to the motor controller.
    void
    set(double percentOutput)
    Set the percentage output.
    Set the absolute encoder to be a compatible absolute encoder.
    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
    setInverted(boolean inverted)
    Set the motor to be inverted.
    void
    setLoopRampRate(double rampRate)
    Set the maximum rate the open/closed loop output can change by.
    void
    setMotorBrake(boolean isBrakeMode)
    Set the idle mode.
    void
    setPosition(double position)
    Set the integrated encoder position.
    void
    setReference(double setpoint, double feedforward)
    Set the closed loop PID controller reference point.
    void
    setReference(double setpoint, double feedforward, double position)
    Set the closed loop PID controller reference point.
    void
    setVoltage(double voltage)
    Set the voltage of the motor.
    void
    setVoltageCompensation(double nominalVoltage)
    Set the voltage compensation for the swerve module motor.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • motor

      public com.revrobotics.CANSparkMax motor
      SparkMAX Instance.
    • absoluteEncoder

      public com.revrobotics.AbsoluteEncoder absoluteEncoder
      Absolute encoder attached to the SparkMax (if exists)
    • encoder

      public com.revrobotics.RelativeEncoder encoder
      Integrated encoder.
    • pid

      public com.revrobotics.SparkPIDController pid
      Closed-loop PID controller.
    • factoryDefaultOccurred

      private boolean factoryDefaultOccurred
      Factory default already occurred.
    • noEncoderAlert

      private Alert noEncoderAlert
      An Alert for if the motor has no encoder.
    • failureConfiguringAlert

      private Alert failureConfiguringAlert
      An Alert for if there is an error configuring the motor.
    • noEncoderDefinedAlert

      private Alert noEncoderDefinedAlert
      An Alert for if the motor has no encoder defined.
  • Constructor Details

    • SparkMaxBrushedMotorSwerve

      public SparkMaxBrushedMotorSwerve(com.revrobotics.CANSparkMax motor, boolean isDriveMotor, com.revrobotics.SparkRelativeEncoder.Type encoderType, int countsPerRevolution, boolean useDataPortQuadEncoder)
      Initialize the swerve motor.
      Parameters:
      motor - The SwerveMotor as a SparkMax object.
      isDriveMotor - Is the motor being initialized a drive motor?
      encoderType - SparkRelativeEncoder.Type of encoder to use for the CANSparkMax device.
      countsPerRevolution - The number of encoder pulses for the SparkRelativeEncoder.Type encoder per revolution.
      useDataPortQuadEncoder - Use the encoder attached to the data port of the spark max for a quadrature encoder.
    • SparkMaxBrushedMotorSwerve

      public SparkMaxBrushedMotorSwerve(int id, boolean isDriveMotor, com.revrobotics.SparkRelativeEncoder.Type encoderType, int countsPerRevolution, boolean useDataPortQuadEncoder)
      Initialize the SwerveMotor as a CANSparkMax connected to a Brushless Motor.
      Parameters:
      id - CAN ID of the SparkMax.
      isDriveMotor - Is the motor being initialized a drive motor?
      encoderType - SparkRelativeEncoder.Type of encoder to use for the CANSparkMax device.
      countsPerRevolution - The number of encoder pulses for the SparkRelativeEncoder.Type encoder per revolution.
      useDataPortQuadEncoder - Use the encoder attached to the data port of the spark max for a quadrature encoder.
  • Method Details

    • configureSparkMax

      private void configureSparkMax(Supplier<com.revrobotics.REVLibError> config)
      Run the configuration until it succeeds or times out.
      Parameters:
      config - Lambda supplier returning the error state.
    • 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.
    • setLoopRampRate

      public void setLoopRampRate(double rampRate)
      Set the maximum rate the open/closed loop output can change by.
      Specified by:
      setLoopRampRate in class SwerveMotor
      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:
      getMotor in class SwerveMotor
      Returns:
      Motor object.
    • isAttachedAbsoluteEncoder

      public boolean isAttachedAbsoluteEncoder()
      Queries whether the absolute encoder is directly attached to the motor controller.
      Specified by:
      isAttachedAbsoluteEncoder in class SwerveMotor
      Returns:
      connected absolute encoder state.
    • factoryDefaults

      public void factoryDefaults()
      Configure the factory defaults.
      Specified by:
      factoryDefaults in class SwerveMotor
    • clearStickyFaults

      public void clearStickyFaults()
      Clear the sticky faults on the motor controller.
      Specified by:
      clearStickyFaults in class SwerveMotor
    • setAbsoluteEncoder

      public SwerveMotor setAbsoluteEncoder(SwerveAbsoluteEncoder encoder)
      Set the absolute encoder to be a compatible absolute encoder.
      Specified by:
      setAbsoluteEncoder in class SwerveMotor
      Parameters:
      encoder - The encoder to use.
      Returns:
      The SwerveMotor for 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:
      configureIntegratedEncoder in class SwerveMotor
      Parameters:
      positionConversionFactor - The conversion factor to apply.
    • configurePIDF

      public void configurePIDF(PIDFConfig config)
      Configure the PIDF values for the closed loop controller.
      Specified by:
      configurePIDF in class SwerveMotor
      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:
      configurePIDWrapping in class SwerveMotor
      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 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
    • setMotorBrake

      public void setMotorBrake(boolean isBrakeMode)
      Set the idle mode.
      Specified by:
      setMotorBrake in class SwerveMotor
      Parameters:
      isBrakeMode - Set the brake mode.
    • setInverted

      public void setInverted(boolean inverted)
      Set the motor to be inverted.
      Specified by:
      setInverted in class SwerveMotor
      Parameters:
      inverted - State of inversion.
    • burnFlash

      public void burnFlash()
      Save the configurations from flash to EEPROM.
      Specified by:
      burnFlash in class SwerveMotor
    • set

      public void set(double percentOutput)
      Set the percentage output.
      Specified by:
      set in class SwerveMotor
      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:
      setReference in class SwerveMotor
      Parameters:
      setpoint - Setpoint in MPS or Angle in degrees.
      feedforward - Feedforward in volt-meter-per-second or kV.
    • setReference

      public void setReference(double setpoint, double feedforward, double position)
      Set the closed loop PID controller reference point.
      Specified by:
      setReference in class SwerveMotor
      Parameters:
      setpoint - Setpoint in meters per second or angle in degrees.
      feedforward - Feedforward in volt-meter-per-second or kV.
      position - Only used on the angle motor, the position of the motor in degrees.
    • getVoltage

      public double getVoltage()
      Get the voltage output of the motor controller.
      Specified by:
      getVoltage in class SwerveMotor
      Returns:
      Voltage output.
    • setVoltage

      public void setVoltage(double voltage)
      Set the voltage of the motor.
      Specified by:
      setVoltage in class SwerveMotor
      Parameters:
      voltage - Voltage to set.
    • getAppliedOutput

      public double getAppliedOutput()
      Get the applied dutycycle output.
      Specified by:
      getAppliedOutput in class SwerveMotor
      Returns:
      Applied dutycycle output to the motor.
    • getVelocity

      public double getVelocity()
      Get the velocity of the integrated encoder.
      Specified by:
      getVelocity in class SwerveMotor
      Returns:
      velocity
    • getPosition

      public double getPosition()
      Get the position of the integrated encoder.
      Specified by:
      getPosition in class SwerveMotor
      Returns:
      Position
    • setPosition

      public void setPosition(double position)
      Set the integrated encoder position.
      Specified by:
      setPosition in class SwerveMotor
      Parameters:
      position - Integrated encoder position.