Package swervelib.motors
Class SparkMaxBrushedMotorSwerve
java.lang.Object
swervelib.motors.SwerveMotor
swervelib.motors.SparkMaxBrushedMotorSwerve
- All Implemented Interfaces:
AutoCloseable
Brushed motor control with
SparkMax
.-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionAbsolute encoder attached to the SparkMax (if exists)Integrated encoder.Closed-loop PID controller.Fields inherited from class swervelib.motors.SwerveMotor
isDriveMotor, maximumRetries, simMotor
-
Constructor Summary
ConstructorsConstructorDescriptionSparkMaxBrushedMotorSwerve
(int id, boolean isDriveMotor, SparkMaxBrushedMotorSwerve.Type encoderType, int countsPerRevolution, boolean useDataPortQuadEncoder, DCMotor motorType) Initialize theSwerveMotor
as aSparkMax
connected to a Brushless Motor.SparkMaxBrushedMotorSwerve
(SparkMax motor, boolean isDriveMotor, SparkMaxBrushedMotorSwerve.Type encoderType, int countsPerRevolution, boolean useDataPortQuadEncoder, DCMotor motorType) Initialize the swerve motor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Save the configurations from flash to EEPROM.void
Clear the sticky faults on the motor controller.void
close()
void
configureIntegratedEncoder
(double positionConversionFactor) Configure the integrated encoder for the swerve module.void
configurePIDF
(PIDFConfig config) 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.void
Disable PID Wrapping on the motor.void
Configure the factory defaults.double
Get the applied dutycycle output.Get the current configuration of theSparkMax
getMotor()
Get the motor object from the module.double
Get the position of the integrated encoder.Get theDCMotor
of the motor class.double
Get the velocity of the integrated encoder.double
Get the voltage output of the motor controller.void
set
(double percentOutput) Set the percentage output.setAbsoluteEncoder
(SwerveAbsoluteEncoder encoder) 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.void
updateConfig
(SparkMaxConfig cfgGiven) Update the config for theSparkMax
boolean
Queries whether the absolute encoder is directly attached to the motor controller.
-
Field Details
-
absoluteEncoder
Absolute encoder attached to the SparkMax (if exists) -
encoder
Integrated encoder. -
pid
Closed-loop PID controller.
-
-
Constructor Details
-
SparkMaxBrushedMotorSwerve
public SparkMaxBrushedMotorSwerve(SparkMax motor, boolean isDriveMotor, SparkMaxBrushedMotorSwerve.Type encoderType, int countsPerRevolution, boolean useDataPortQuadEncoder, DCMotor motorType) Initialize the swerve motor.- Parameters:
motor
- The SwerveMotor as a SparkMax object.isDriveMotor
- Is the motor being initialized a drive motor?encoderType
-SparkMaxBrushedMotorSwerve.Type
of encoder to use for theSparkMax
device.countsPerRevolution
- The number of encoder pulses for theSparkMaxBrushedMotorSwerve.Type
encoder per revolution.useDataPortQuadEncoder
- Use the encoder attached to the data port of the spark max for a quadrature encoder.motorType
-DCMotor
which theSparkMax
is attached to.
-
SparkMaxBrushedMotorSwerve
public SparkMaxBrushedMotorSwerve(int id, boolean isDriveMotor, SparkMaxBrushedMotorSwerve.Type encoderType, int countsPerRevolution, boolean useDataPortQuadEncoder, DCMotor motorType) Initialize theSwerveMotor
as aSparkMax
connected to a Brushless Motor.- Parameters:
id
- CAN ID of the SparkMax.isDriveMotor
- Is the motor being initialized a drive motor?encoderType
-SparkMaxBrushedMotorSwerve.Type
of encoder to use for theSparkMax
device.countsPerRevolution
- The number of encoder pulses for theSparkMaxBrushedMotorSwerve.Type
encoder per revolution.useDataPortQuadEncoder
- Use the encoder attached to the data port of the spark max for a quadrature encoder.motorType
- Motor type controlled by theSparkMax
motor controller.
-
-
Method Details
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in classSwerveMotor
-
getConfig
Get the current configuration of theSparkMax
- Returns:
SparkMaxConfig
-
updateConfig
Update the config for theSparkMax
- Parameters:
cfgGiven
- GivenSparkMaxConfig
which should have minimal modifications.
-
setVoltageCompensation
public void setVoltageCompensation(double nominalVoltage) Set the voltage compensation for the swerve module motor.- Specified by:
setVoltageCompensation
in 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:
setCurrentLimit
in 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:
setLoopRampRate
in classSwerveMotor
- Parameters:
rampRate
- Time in seconds to go from 0 to full throttle.
-
getMotor
Get the motor object from the module.- Specified by:
getMotor
in classSwerveMotor
- Returns:
- Motor object.
-
getSimMotor
Get theDCMotor
of the motor class.- Specified by:
getSimMotor
in classSwerveMotor
- Returns:
DCMotor
of this type.
-
usingExternalFeedbackSensor
public boolean usingExternalFeedbackSensor()Queries whether the absolute encoder is directly attached to the motor controller.- Specified by:
usingExternalFeedbackSensor
in classSwerveMotor
- Returns:
- connected absolute encoder state.
-
factoryDefaults
public void factoryDefaults()Configure the factory defaults.- Specified by:
factoryDefaults
in classSwerveMotor
-
clearStickyFaults
public void clearStickyFaults()Clear the sticky faults on the motor controller.- Specified by:
clearStickyFaults
in classSwerveMotor
-
setAbsoluteEncoder
Set the absolute encoder to be a compatible absolute encoder.- Specified by:
setAbsoluteEncoder
in classSwerveMotor
- 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 classSwerveMotor
- Parameters:
positionConversionFactor
- The conversion factor to apply.
-
configurePIDF
Configure the PIDF values for the closed loop controller.- Specified by:
configurePIDF
in 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:
configurePIDWrapping
in classSwerveMotor
- Parameters:
minInput
- Minimum PID input.maxInput
- Maximum PID input.
-
disablePIDWrapping
public void disablePIDWrapping()Disable PID Wrapping on the motor.- Specified by:
disablePIDWrapping
in classSwerveMotor
-
setMotorBrake
public void setMotorBrake(boolean isBrakeMode) Set the idle mode.- Specified by:
setMotorBrake
in classSwerveMotor
- Parameters:
isBrakeMode
- Set the brake mode.
-
setInverted
public void setInverted(boolean inverted) Set the motor to be inverted.- Specified by:
setInverted
in classSwerveMotor
- Parameters:
inverted
- State of inversion.
-
burnFlash
public void burnFlash()Save the configurations from flash to EEPROM.- Specified by:
burnFlash
in classSwerveMotor
-
set
public void set(double percentOutput) Set the percentage output.- Specified by:
set
in 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:
setReference
in classSwerveMotor
- 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 classSwerveMotor
- 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 classSwerveMotor
- Returns:
- Voltage output.
-
setVoltage
public void setVoltage(double voltage) Set the voltage of the motor.- Specified by:
setVoltage
in classSwerveMotor
- Parameters:
voltage
- Voltage to set.
-
getAppliedOutput
public double getAppliedOutput()Get the applied dutycycle output.- Specified by:
getAppliedOutput
in classSwerveMotor
- Returns:
- Applied dutycycle output to the motor.
-
getVelocity
public double getVelocity()Get the velocity of the integrated encoder.- Specified by:
getVelocity
in classSwerveMotor
- Returns:
- velocity
-
getPosition
public double getPosition()Get the position of the integrated encoder.- Specified by:
getPosition
in classSwerveMotor
- Returns:
- Position
-
setPosition
public void setPosition(double position) Set the integrated encoder position.- Specified by:
setPosition
in classSwerveMotor
- Parameters:
position
- Integrated encoder position.
-