Class SwerveMotor
- java.lang.Object
-
- frc.robot.subsystems.swervedrive.swerve.SwerveMotor
-
- Direct Known Subclasses:
CTRESwerveMotor,REVSwerveMotor
public abstract class SwerveMotor extends java.lang.Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classSwerveMotor.ModuleMotorTypeMotor type for the swerve drive module
-
Field Summary
Fields Modifier and Type Field Description doublekDPIDF Values.doublekFPIDF Values.doublekIPIDF Values.doublekIZPIDF Values.doublekPPIDF Values.SwerveMotor.ModuleMotorTypem_motorTypeModule motor type.doubletargetTarget value for PID.
-
Constructor Summary
Constructors Constructor Description SwerveMotor()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description static <MotorType extends edu.wpi.first.wpilibj.motorcontrol.MotorController>
SwerveMotorfromMotor(MotorType motor, SwerveEncoder<?> absoluteEncoder, SwerveMotor.ModuleMotorType type, double gearRatio, double wheelDiameter, double freeSpeedRPM, double powerLimit)Constructor for Swerve Motor, expecting CANSparkMax or TalonFX.abstract doubleget()Get the current value of the encoder corresponding to the PID.abstract doublegetAmps()Get the current output.abstract doublegetPosition()Get the current value of the encoder.abstract voidoptimizeCANFrames()Optimize the CAN status frames to reduce utilization.abstract booleanreachable()Check that the link is good on the swerve module and CAN bus is able to retrieve data.abstract booleanremoteIntegratedEncoder()Check if the absolute encoder is used inplace of the integrated encoder.abstract voidsaveConfig()Save configuration data to the motor controller so it is persistent on reboot.abstract voidset(double speed)Set the speed of the drive motor from -1 to 1.abstract voidsetConversionFactor(double conversionFactor)Configures the conversion factor based upon which motor.abstract voidsetCurrentLimit(int currentLimit)Set the current limit for the swerve drive motor, remember this may cause jumping if used in conjunction with voltage compensation.abstract voidsetEncoder(double value)Set the encoder value.abstract voidsetInverted(boolean inverted)Invert the motor.abstract voidsetPIDF(double P, double I, double D, double F, double integralZone)Set the PIDF coefficients for the closed loop PID onboard the SparkMax.abstract voidsetPIDOutputRange(double min, double max)Configure the maximum power (-1 to 1) the PID can output.abstract voidsetTarget(double target, double feedforward)Set the target for the PID loop.abstract voidsetVoltage(double voltage)Set the voltage of the motor.abstract voidsetVoltageCompensation(double nominalVoltage)Set the voltage compensation for the swerve module motor.abstract voidstop()Stop the motor by sending 0 volts to it.
-
-
-
Field Detail
-
m_motorType
public SwerveMotor.ModuleMotorType m_motorType
Module motor type.
-
kP
public double kP
PIDF Values.
-
kI
public double kI
PIDF Values.
-
kD
public double kD
PIDF Values.
-
kF
public double kF
PIDF Values.
-
kIZ
public double kIZ
PIDF Values.
-
target
public double target
Target value for PID.
-
-
Method Detail
-
fromMotor
public static <MotorType extends edu.wpi.first.wpilibj.motorcontrol.MotorController> SwerveMotor fromMotor(MotorType motor, SwerveEncoder<?> absoluteEncoder, SwerveMotor.ModuleMotorType type, double gearRatio, double wheelDiameter, double freeSpeedRPM, double powerLimit)
Constructor for Swerve Motor, expecting CANSparkMax or TalonFX. Clears sticky faults and restores factory defaults.- Type Parameters:
MotorType- Motor type to use.- Parameters:
motor- 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.
-
setPIDOutputRange
public abstract 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.- Parameters:
min- Minimum output.max- Maximum output.
-
setPIDF
public abstract void setPIDF(double P, double I, double D, double F, double integralZone)Set the PIDF coefficients for the closed loop PID onboard the SparkMax.- 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 abstract void setConversionFactor(double conversionFactor)
Configures the conversion factor based upon which motor.- Parameters:
conversionFactor- Conversion from RPM to MPS for drive motor, and rotations to degrees for the turning motor.
-
setTarget
public abstract void setTarget(double target, double feedforward)Set the target for the PID loop.- Parameters:
target- The PID target to aim for.feedforward- The feedforward for this target.
-
stop
public abstract void stop()
Stop the motor by sending 0 volts to it.
-
set
public abstract void set(double speed)
Set the speed of the drive motor from -1 to 1.- Parameters:
speed- Speed from -1 to 1.
-
setVoltage
public abstract void setVoltage(double voltage)
Set the voltage of the motor.- Parameters:
voltage- Voltage to output.
-
get
public abstract double get()
Get the current value of the encoder corresponding to the PID.- Returns:
- Current value of the encoder either in velocity or position..
-
getAmps
public abstract double getAmps()
Get the current output.- Returns:
- Output amps.
-
getPosition
public abstract double getPosition()
Get the current value of the encoder.- Returns:
- Current value of the encoder.
-
setVoltageCompensation
public abstract void setVoltageCompensation(double nominalVoltage)
Set the voltage compensation for the swerve module motor.- Parameters:
nominalVoltage- Nominal voltage for operation to output to.
-
setCurrentLimit
public abstract 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.- Parameters:
currentLimit- Current limit in AMPS at free speed.
-
setEncoder
public abstract void setEncoder(double value)
Set the encoder value.- Parameters:
value- Value to set the encoder to.
-
reachable
public abstract boolean reachable()
Check that the link is good on the swerve module and CAN bus is able to retrieve data.- Returns:
- true on all devices are accessible over CAN.
-
remoteIntegratedEncoder
public abstract boolean remoteIntegratedEncoder()
Check if the absolute encoder is used inplace of the integrated encoder.- Returns:
- true, if the absolute encoder is being used as the integrated controller.
-
optimizeCANFrames
public abstract void optimizeCANFrames()
Optimize the CAN status frames to reduce utilization.
-
saveConfig
public abstract void saveConfig()
Save configuration data to the motor controller so it is persistent on reboot.
-
setInverted
public abstract void setInverted(boolean inverted)
Invert the motor.- Parameters:
inverted- Set the motor as inverted.
-
-