Files
YAGSL/swervelib/SwerveModule.java

661 lines
20 KiB
Java
Raw Normal View History

2023-02-13 17:21:24 -06:00
package swervelib;
2023-01-29 21:18:52 -06:00
2024-08-24 17:27:03 -05:00
import com.revrobotics.CANSparkMax;
import com.revrobotics.MotorFeedbackSensor;
2023-01-29 21:18:52 -06:00
import edu.wpi.first.math.controller.SimpleMotorFeedforward;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.kinematics.SwerveModulePosition;
import edu.wpi.first.math.kinematics.SwerveModuleState;
2023-02-13 14:37:05 -06:00
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
2023-02-13 17:21:24 -06:00
import swervelib.encoders.SwerveAbsoluteEncoder;
import swervelib.math.SwerveMath;
2023-02-13 17:21:24 -06:00
import swervelib.motors.SwerveMotor;
2024-02-02 18:55:29 -06:00
import swervelib.parser.Cache;
2024-02-28 13:25:59 -06:00
import swervelib.parser.PIDFConfig;
2023-02-13 17:21:24 -06:00
import swervelib.parser.SwerveModuleConfiguration;
import swervelib.simulation.SwerveModuleSimulation;
2024-01-17 09:17:39 -06:00
import swervelib.telemetry.Alert;
import swervelib.telemetry.SwerveDriveTelemetry;
import swervelib.telemetry.SwerveDriveTelemetry.TelemetryVerbosity;
2023-01-29 21:18:52 -06:00
/**
2023-02-13 14:37:05 -06:00
* The Swerve Module class which represents and controls Swerve Modules for the swerve drive.
2023-01-29 21:18:52 -06:00
*/
2023-02-13 14:37:05 -06:00
public class SwerveModule
2023-01-29 21:18:52 -06:00
{
/**
2023-02-13 14:37:05 -06:00
* Swerve module configuration options.
2023-01-29 21:18:52 -06:00
*/
2023-02-13 14:37:05 -06:00
public final SwerveModuleConfiguration configuration;
2024-02-02 18:55:29 -06:00
/**
* Absolute encoder position cache.
*/
2024-02-06 16:03:08 -06:00
public final Cache<Double> absolutePositionCache;
2024-02-02 18:55:29 -06:00
/**
* Drive motor position cache.
*/
2024-02-06 16:03:08 -06:00
public final Cache<Double> drivePositionCache;
2024-02-02 18:55:29 -06:00
/**
* Drive motor velocity cache.
*/
2024-02-06 16:03:08 -06:00
public final Cache<Double> driveVelocityCache;
2024-08-24 17:27:03 -05:00
/**
* Module number for kinematics, usually 0 to 3. front left -> front right -> back left -> back right.
*/
public final int moduleNumber;
2023-01-29 21:18:52 -06:00
/**
2023-02-13 14:37:05 -06:00
* Swerve Motors.
2023-01-29 21:18:52 -06:00
*/
2023-02-13 14:37:05 -06:00
private final SwerveMotor angleMotor, driveMotor;
2023-01-29 21:18:52 -06:00
/**
2023-02-13 14:37:05 -06:00
* Absolute encoder for swerve drive.
2023-01-29 21:18:52 -06:00
*/
2023-02-13 14:37:05 -06:00
private final SwerveAbsoluteEncoder absoluteEncoder;
2024-01-17 09:17:39 -06:00
/**
* An {@link Alert} for if pushing the Absolute Encoder offset to the encoder fails.
*/
private final Alert encoderOffsetWarning;
/**
* An {@link Alert} for if there is no Absolute Encoder on the module.
*/
private final Alert noEncoderWarning;
2024-02-12 18:59:40 -06:00
/**
* NT3 Raw Absolute Angle publisher for the absolute encoder.
*/
private final String rawAbsoluteAngleName;
/**
* NT3 Adjusted Absolute angle publisher for the absolute encoder.
*/
private final String adjAbsoluteAngleName;
/**
* NT3 Absolute encoder read issue.
*/
private final String absoluteEncoderIssueName;
/**
* NT3 raw angle motor.
*/
private final String rawAngleName;
/**
* NT3 Raw drive motor.
*/
private final String rawDriveName;
2024-06-12 15:10:29 -05:00
/**
* NT3 Raw drive motor.
*/
private final String rawDriveVelName;
2023-11-09 17:32:48 -06:00
/**
* Maximum speed of the drive motors in meters per second.
*/
public double maxSpeed;
2024-06-12 15:58:37 -05:00
/**
* Feedforward for the drive motor during closed loop control.
*/
private SimpleMotorFeedforward driveMotorFeedforward;
2024-02-28 13:25:59 -06:00
/**
* Anti-Jitter AKA auto-centering disabled.
*/
private boolean antiJitterEnabled = true;
2023-01-29 21:18:52 -06:00
/**
* Last swerve module state applied.
2023-01-29 21:18:52 -06:00
*/
2024-01-26 12:45:09 -06:00
private SwerveModuleState lastState;
/**
* Angle offset from the absolute encoder.
*/
2024-01-15 14:37:13 -06:00
private double angleOffset;
2023-01-29 21:18:52 -06:00
/**
* Simulated swerve module.
2023-01-29 21:18:52 -06:00
*/
private SwerveModuleSimulation simModule;
/**
* Encoder synchronization queued.
*/
private boolean synchronizeEncoderQueued = false;
2023-01-29 21:18:52 -06:00
2024-02-02 18:55:29 -06:00
2023-01-29 21:18:52 -06:00
/**
2023-02-13 14:37:05 -06:00
* Construct the swerve module and initialize the swerve module motors and absolute encoder.
2023-01-29 21:18:52 -06:00
*
2023-02-13 14:37:05 -06:00
* @param moduleNumber Module number for kinematics.
* @param moduleConfiguration Module constants containing CAN ID's and offsets.
2023-11-09 17:32:48 -06:00
* @param driveFeedforward Drive motor feedforward created by
* {@link SwerveMath#createDriveFeedforward(double, double, double)}.
2023-02-13 14:37:05 -06:00
*/
2023-11-09 17:32:48 -06:00
public SwerveModule(int moduleNumber, SwerveModuleConfiguration moduleConfiguration,
SimpleMotorFeedforward driveFeedforward)
2023-02-13 14:37:05 -06:00
{
// angle = 0;
// speed = 0;
// omega = 0;
// fakePos = 0;
2023-02-13 14:37:05 -06:00
this.moduleNumber = moduleNumber;
configuration = moduleConfiguration;
angleOffset = moduleConfiguration.angleOffset;
2024-02-28 13:25:59 -06:00
// Initialize Feedforwards.
driveMotorFeedforward = driveFeedforward;
2023-02-13 14:37:05 -06:00
// Create motors from configuration and reset them to defaults.
angleMotor = moduleConfiguration.angleMotor;
driveMotor = moduleConfiguration.driveMotor;
angleMotor.factoryDefaults();
driveMotor.factoryDefaults();
// Configure voltage comp, current limit, and ramp rate.
angleMotor.setVoltageCompensation(configuration.physicalCharacteristics.optimalVoltage);
driveMotor.setVoltageCompensation(configuration.physicalCharacteristics.optimalVoltage);
angleMotor.setCurrentLimit(configuration.physicalCharacteristics.angleMotorCurrentLimit);
driveMotor.setCurrentLimit(configuration.physicalCharacteristics.driveMotorCurrentLimit);
angleMotor.setLoopRampRate(configuration.physicalCharacteristics.angleMotorRampRate);
driveMotor.setLoopRampRate(configuration.physicalCharacteristics.driveMotorRampRate);
// Config angle encoders
absoluteEncoder = moduleConfiguration.absoluteEncoder;
if (absoluteEncoder != null)
{
absoluteEncoder.factoryDefault();
absoluteEncoder.configure(moduleConfiguration.absoluteEncoderInverted);
}
2023-01-29 21:18:52 -06:00
2024-02-02 18:55:29 -06:00
// Setup the cache for the absolute encoder position.
absolutePositionCache = new Cache<>(this::getRawAbsolutePosition, 15);
2023-02-13 14:37:05 -06:00
// Config angle motor/controller
2023-11-09 17:32:48 -06:00
angleMotor.configureIntegratedEncoder(moduleConfiguration.conversionFactors.angle);
2023-02-13 14:37:05 -06:00
angleMotor.configurePIDF(moduleConfiguration.anglePIDF);
2024-01-15 14:37:13 -06:00
angleMotor.configurePIDWrapping(0, 180);
2023-02-13 14:37:05 -06:00
angleMotor.setInverted(moduleConfiguration.angleMotorInverted);
angleMotor.setMotorBrake(false);
2023-01-29 21:18:52 -06:00
2024-01-26 11:29:15 -06:00
// Set the position AFTER settings the conversion factor.
if (absoluteEncoder != null)
{
angleMotor.setPosition(getAbsolutePosition());
}
2023-02-13 14:37:05 -06:00
// Config drive motor/controller
2023-11-09 17:32:48 -06:00
driveMotor.configureIntegratedEncoder(moduleConfiguration.conversionFactors.drive);
2023-02-13 14:37:05 -06:00
driveMotor.configurePIDF(moduleConfiguration.velocityPIDF);
driveMotor.setInverted(moduleConfiguration.driveMotorInverted);
driveMotor.setMotorBrake(true);
2023-01-29 21:18:52 -06:00
2023-02-13 14:37:05 -06:00
driveMotor.burnFlash();
angleMotor.burnFlash();
2023-01-29 21:18:52 -06:00
2024-02-02 18:55:29 -06:00
drivePositionCache = new Cache<>(driveMotor::getPosition, 15);
driveVelocityCache = new Cache<>(driveMotor::getVelocity, 15);
if (SwerveDriveTelemetry.isSimulation)
2023-01-29 21:18:52 -06:00
{
simModule = new SwerveModuleSimulation();
2023-01-29 21:18:52 -06:00
}
2024-02-02 18:55:29 -06:00
// Force a cache update on init.
driveVelocityCache.update();
drivePositionCache.update();
absolutePositionCache.update();
// Save the current state.
lastState = getState();
2024-01-17 09:17:39 -06:00
noEncoderWarning = new Alert("Motors",
"There is no Absolute Encoder on module #" +
moduleNumber,
Alert.AlertType.WARNING);
encoderOffsetWarning = new Alert("Motors",
"Pushing the Absolute Encoder offset to the encoder failed on module #" +
moduleNumber,
Alert.AlertType.WARNING);
2024-02-12 18:59:40 -06:00
2024-06-12 15:58:37 -05:00
rawAbsoluteAngleName = "swerve/modules/" + configuration.name + "/Raw Absolute Encoder";
adjAbsoluteAngleName = "swerve/modules/" + configuration.name + "/Adjusted Absolute Encoder";
absoluteEncoderIssueName = "swerve/modules/" + configuration.name + "/Absolute Encoder Read Issue";
rawAngleName = "swerve/modules/" + configuration.name + "/Raw Angle Encoder";
rawDriveName = "swerve/modules/" + configuration.name + "/Raw Drive Encoder";
rawDriveVelName = "swerve/modules/" + configuration.name + "/Raw Drive Velocity";
2023-01-29 21:18:52 -06:00
}
2023-11-09 17:32:48 -06:00
/**
* Set the voltage compensation for the swerve module motor.
*
* @param optimalVoltage Nominal voltage for operation to output to.
*/
public void setAngleMotorVoltageCompensation(double optimalVoltage)
{
angleMotor.setVoltageCompensation(optimalVoltage);
}
/**
* Set the voltage compensation for the swerve module motor.
*
* @param optimalVoltage Nominal voltage for operation to output to.
*/
public void setDriveMotorVoltageCompensation(double optimalVoltage)
{
driveMotor.setVoltageCompensation(optimalVoltage);
}
2023-01-29 21:18:52 -06:00
/**
* Queue synchronization of the integrated angle encoder with the absolute encoder.
2023-01-29 21:18:52 -06:00
*/
public void queueSynchronizeEncoders()
2023-01-29 21:18:52 -06:00
{
if (absoluteEncoder != null)
{
synchronizeEncoderQueued = true;
}
2023-01-29 21:18:52 -06:00
}
2024-02-28 13:25:59 -06:00
/**
* Set the antiJitter functionality, if true the modules will NOT auto center. Pushes the offsets to the angle motor
* controllers as well.
*
* @param antiJitter Anti-Jitter state desired.
*/
public void setAntiJitter(boolean antiJitter)
{
this.antiJitterEnabled = antiJitter;
if (antiJitter)
{
2024-06-12 15:10:29 -05:00
pushOffsetsToEncoders();
2024-02-28 13:25:59 -06:00
} else
{
restoreInternalOffset();
}
}
/**
* Set the feedforward attributes to the given parameters.
*
* @param drive Drive motor feedforward for the module.
*/
public void setFeedforward(SimpleMotorFeedforward drive)
{
this.driveMotorFeedforward = drive;
}
2024-06-12 15:58:37 -05:00
/**
* Get the current drive motor PIDF values.
*
* @return {@link PIDFConfig} of the drive motor.
*/
public PIDFConfig getDrivePIDF()
{
return configuration.velocityPIDF;
}
2024-02-28 13:25:59 -06:00
/**
* Set the drive PIDF values.
*
* @param config {@link PIDFConfig} of that should be set.
*/
public void setDrivePIDF(PIDFConfig config)
{
configuration.velocityPIDF = config;
driveMotor.configurePIDF(config);
}
/**
2024-06-12 15:58:37 -05:00
* Get the current angle/azimuth/steering motor PIDF values.
2024-02-28 13:25:59 -06:00
*
2024-06-12 15:58:37 -05:00
* @return {@link PIDFConfig} of the angle motor.
2024-02-28 13:25:59 -06:00
*/
2024-06-12 15:58:37 -05:00
public PIDFConfig getAnglePIDF()
2024-02-28 13:25:59 -06:00
{
2024-06-12 15:58:37 -05:00
return configuration.anglePIDF;
2024-02-28 13:25:59 -06:00
}
/**
* Set the angle/azimuth/steering motor PID
*
* @param config {@link PIDFConfig} of that should be set.
*/
public void setAnglePIDF(PIDFConfig config)
{
configuration.anglePIDF = config;
angleMotor.configurePIDF(config);
}
2023-01-29 21:18:52 -06:00
/**
* Set the desired state of the swerve module. <br /><b>WARNING: If you are not using one of the functions from
* {@link SwerveDrive} you may screw up {@link SwerveDrive#kinematics}</b>
2023-02-13 14:37:05 -06:00
*
* @param desiredState Desired swerve module state.
* @param isOpenLoop Whether to use open loop (direct percent) or direct velocity control.
* @param force Disables optimizations that prevent movement in the angle motor and forces the desired state
* onto the swerve module.
2023-01-29 21:18:52 -06:00
*/
public void setDesiredState(SwerveModuleState desiredState, boolean isOpenLoop, boolean force)
2023-01-29 21:18:52 -06:00
{
2024-01-15 14:37:13 -06:00
desiredState = SwerveModuleState.optimize(desiredState, Rotation2d.fromDegrees(getAbsolutePosition()));
2024-02-17 08:13:53 -06:00
// If we are forcing the angle
2024-02-28 13:25:59 -06:00
if (!force && antiJitterEnabled)
2024-02-17 08:13:53 -06:00
{
// Prevents module rotation if speed is less than 1%
SwerveMath.antiJitter(desiredState, lastState, Math.min(maxSpeed, 4));
}
2024-01-26 12:45:09 -06:00
// Cosine compensation.
2024-02-02 18:55:29 -06:00
double velocity = configuration.useCosineCompensator
? getCosineCompensatedVelocity(desiredState)
: desiredState.speedMetersPerSecond;
2023-04-08 12:31:07 -05:00
2023-02-13 14:37:05 -06:00
if (isOpenLoop)
2023-01-29 21:18:52 -06:00
{
2023-11-09 17:32:48 -06:00
double percentOutput = desiredState.speedMetersPerSecond / maxSpeed;
2023-02-13 14:37:05 -06:00
driveMotor.set(percentOutput);
2023-01-29 21:18:52 -06:00
} else
{
2024-02-28 13:25:59 -06:00
driveMotor.setReference(velocity, driveMotorFeedforward.calculate(velocity));
2024-02-06 16:03:08 -06:00
desiredState.speedMetersPerSecond = velocity;
2023-01-29 21:18:52 -06:00
}
// Prevent module rotation if angle is the same as the previous angle.
2024-01-15 14:37:13 -06:00
// Synchronize encoders if queued and send in the current position as the value from the absolute encoder.
if (absoluteEncoder != null && synchronizeEncoderQueued)
{
2024-01-15 14:37:13 -06:00
double absoluteEncoderPosition = getAbsolutePosition();
angleMotor.setPosition(absoluteEncoderPosition);
angleMotor.setReference(desiredState.angle.getDegrees(), 0, absoluteEncoderPosition);
synchronizeEncoderQueued = false;
} else
{
angleMotor.setReference(desiredState.angle.getDegrees(), 0);
}
2023-04-08 12:31:07 -05:00
lastState = desiredState;
2023-01-29 21:18:52 -06:00
if (SwerveDriveTelemetry.isSimulation)
2023-02-13 14:37:05 -06:00
{
simModule.updateStateAndPosition(desiredState);
2023-02-13 14:37:05 -06:00
}
2024-01-15 14:37:13 -06:00
2024-06-12 15:10:29 -05:00
if (SwerveDriveTelemetry.verbosity.ordinal() >= TelemetryVerbosity.INFO.ordinal())
2024-01-26 12:45:09 -06:00
{
SwerveDriveTelemetry.desiredStates[moduleNumber * 2] = desiredState.angle.getDegrees();
SwerveDriveTelemetry.desiredStates[(moduleNumber * 2) + 1] = velocity;
}
2024-01-15 14:37:13 -06:00
if (SwerveDriveTelemetry.verbosity == TelemetryVerbosity.HIGH)
{
2024-06-13 17:10:51 -05:00
SmartDashboard.putNumber("swerve/modules/" + configuration.name + "/Speed Setpoint",
desiredState.speedMetersPerSecond);
SmartDashboard.putNumber("swerve/modules/" + configuration.name + "/Angle Setpoint",
desiredState.angle.getDegrees());
2024-01-15 14:37:13 -06:00
}
2023-01-29 21:18:52 -06:00
}
2024-01-26 12:45:09 -06:00
/**
* Get the cosine compensated velocity to set the swerve module to.
*
* @param desiredState Desired {@link SwerveModuleState} to use.
* @return Cosine compensated velocity in meters/second.
*/
private double getCosineCompensatedVelocity(SwerveModuleState desiredState)
{
double cosineScalar = 1.0;
// Taken from the CTRE SwerveModule class.
// https://api.ctr-electronics.com/phoenix6/release/java/src-html/com/ctre/phoenix6/mechanisms/swerve/SwerveModule.html#line.46
/* From FRC 900's whitepaper, we add a cosine compensator to the applied drive velocity */
/* To reduce the "skew" that occurs when changing direction */
/* If error is close to 0 rotations, we're already there, so apply full power */
/* If the error is close to 0.25 rotations, then we're 90 degrees, so movement doesn't help us at all */
2024-02-06 16:03:08 -06:00
cosineScalar = Rotation2d.fromDegrees(desiredState.angle.getDegrees())
2024-02-12 18:59:40 -06:00
.minus(Rotation2d.fromDegrees(getAbsolutePosition()))
.getCos(); // TODO: Investigate angle modulus by 180.
2024-01-26 12:45:09 -06:00
/* Make sure we don't invert our drive, even though we shouldn't ever target over 90 degrees anyway */
if (cosineScalar < 0.0)
{
2024-02-06 16:03:08 -06:00
cosineScalar = 1;
2024-01-26 12:45:09 -06:00
}
return desiredState.speedMetersPerSecond * (cosineScalar);
}
2023-01-29 21:18:52 -06:00
/**
2023-02-13 14:37:05 -06:00
* Set the angle for the module.
2023-01-29 21:18:52 -06:00
*
* @param angle Angle in degrees.
*/
2023-02-13 14:37:05 -06:00
public void setAngle(double angle)
2023-01-29 21:18:52 -06:00
{
angleMotor.setReference(angle, 0);
lastState.angle = Rotation2d.fromDegrees(angle);
2023-01-29 21:18:52 -06:00
}
/**
2023-02-13 14:37:05 -06:00
* Get the Swerve Module state.
2023-01-29 21:18:52 -06:00
*
2023-02-13 14:37:05 -06:00
* @return Current SwerveModule state.
2023-01-29 21:18:52 -06:00
*/
public SwerveModuleState getState()
2023-01-29 21:18:52 -06:00
{
2023-02-13 14:37:05 -06:00
double velocity;
Rotation2d azimuth;
if (!SwerveDriveTelemetry.isSimulation)
2023-01-29 21:18:52 -06:00
{
2024-02-02 18:55:29 -06:00
velocity = driveVelocityCache.getValue();
2023-11-29 17:51:42 -06:00
azimuth = Rotation2d.fromDegrees(getAbsolutePosition());
2023-02-13 14:37:05 -06:00
} else
2023-01-29 21:18:52 -06:00
{
return simModule.getState();
2023-01-29 21:18:52 -06:00
}
return new SwerveModuleState(velocity, azimuth);
2023-01-29 21:18:52 -06:00
}
/**
* Get the position of the swerve module.
*
* @return {@link SwerveModulePosition} of the swerve module.
*/
2023-02-13 14:37:05 -06:00
public SwerveModulePosition getPosition()
2023-01-29 21:18:52 -06:00
{
2023-02-13 14:37:05 -06:00
double position;
Rotation2d azimuth;
if (!SwerveDriveTelemetry.isSimulation)
2023-01-29 21:18:52 -06:00
{
2024-02-02 18:55:29 -06:00
position = drivePositionCache.getValue();
2023-11-29 17:49:06 -06:00
azimuth = Rotation2d.fromDegrees(getAbsolutePosition());
2023-01-29 21:18:52 -06:00
} else
{
return simModule.getPosition();
2023-01-29 21:18:52 -06:00
}
2023-02-13 14:37:05 -06:00
return new SwerveModulePosition(position, azimuth);
2023-01-29 21:18:52 -06:00
}
/**
* Get the absolute position. Falls back to relative position on reading failure.
2023-01-29 21:18:52 -06:00
*
* @return Absolute encoder angle in degrees in the range [0, 360).
2023-01-29 21:18:52 -06:00
*/
public double getAbsolutePosition()
2024-02-02 18:55:29 -06:00
{
return absolutePositionCache.getValue();
}
/**
* Get the absolute position. Falls back to relative position on reading failure.
*
* @return Absolute encoder angle in degrees in the range [0, 360).
*/
public double getRawAbsolutePosition()
2023-01-29 21:18:52 -06:00
{
double angle;
if (absoluteEncoder != null)
{
angle = absoluteEncoder.getAbsolutePosition() - angleOffset;
if (absoluteEncoder.readingError)
{
angle = getRelativePosition();
}
} else
{
angle = getRelativePosition();
}
angle %= 360;
if (angle < 0.0)
{
angle += 360;
}
return angle;
2023-01-29 21:18:52 -06:00
}
/**
* Get the relative angle in degrees.
2023-01-29 21:18:52 -06:00
*
2023-02-13 14:37:05 -06:00
* @return Angle in degrees.
2023-01-29 21:18:52 -06:00
*/
public double getRelativePosition()
2023-01-29 21:18:52 -06:00
{
2023-02-13 14:37:05 -06:00
return angleMotor.getPosition();
2023-01-29 21:18:52 -06:00
}
/**
2023-02-13 14:37:05 -06:00
* Set the brake mode.
2023-01-29 21:18:52 -06:00
*
2023-02-13 14:37:05 -06:00
* @param brake Set the brake mode.
2023-01-29 21:18:52 -06:00
*/
2023-02-13 14:37:05 -06:00
public void setMotorBrake(boolean brake)
2023-01-29 21:18:52 -06:00
{
2023-02-13 14:37:05 -06:00
driveMotor.setMotorBrake(brake);
2023-01-29 21:18:52 -06:00
}
2023-11-09 17:32:48 -06:00
/**
* Set the conversion factor for the angle/azimuth motor controller.
*
* @param conversionFactor Angle motor conversion factor for PID, should be generated from
* {@link SwerveMath#calculateDegreesPerSteeringRotation(double, double)} or calculated.
*/
public void setAngleMotorConversionFactor(double conversionFactor)
{
angleMotor.configureIntegratedEncoder(conversionFactor);
}
/**
* Set the conversion factor for the drive motor controller.
*
* @param conversionFactor Drive motor conversion factor for PID, should be generated from
* {@link SwerveMath#calculateMetersPerRotation(double, double, double)} or calculated.
*/
public void setDriveMotorConversionFactor(double conversionFactor)
{
driveMotor.configureIntegratedEncoder(conversionFactor);
}
/**
* Get the angle {@link SwerveMotor} for the {@link SwerveModule}.
*
* @return {@link SwerveMotor} for the angle/steering motor of the module.
*/
public SwerveMotor getAngleMotor()
{
return angleMotor;
}
/**
* Get the drive {@link SwerveMotor} for the {@link SwerveModule}.
*
* @return {@link SwerveMotor} for the drive motor of the module.
*/
public SwerveMotor getDriveMotor()
{
return driveMotor;
}
/**
* Get the {@link SwerveAbsoluteEncoder} for the {@link SwerveModule}.
*
* @return {@link SwerveAbsoluteEncoder} for the swerve module.
*/
public SwerveAbsoluteEncoder getAbsoluteEncoder()
{
return absoluteEncoder;
}
/**
* Fetch the {@link SwerveModuleConfiguration} for the {@link SwerveModule} with the parsed configurations.
*
* @return {@link SwerveModuleConfiguration} for the {@link SwerveModule}.
*/
public SwerveModuleConfiguration getConfiguration()
{
return configuration;
}
/**
* Push absolute encoder offset in the memory of the encoder or controller. Also removes the internal angle offset.
*/
2024-06-12 15:10:29 -05:00
public void pushOffsetsToEncoders()
{
2024-02-28 13:25:59 -06:00
if (absoluteEncoder != null && angleOffset == configuration.angleOffset)
{
2024-08-24 17:27:03 -05:00
// If the absolute encoder is attached.
if (angleMotor.getMotor() instanceof CANSparkMax)
{
2024-08-24 17:27:03 -05:00
if (absoluteEncoder.getAbsoluteEncoder() instanceof MotorFeedbackSensor)
{
angleMotor.setAbsoluteEncoder(absoluteEncoder);
if (absoluteEncoder.setAbsoluteEncoderOffset(angleOffset))
{
angleOffset = 0;
} else
{
angleMotor.setAbsoluteEncoder(null);
encoderOffsetWarning.set(true);
}
}
}
2024-08-24 17:27:03 -05:00
} else
{
2024-01-17 09:17:39 -06:00
noEncoderWarning.set(true);
}
}
/**
* Restore internal offset in YAGSL and either sets absolute encoder offset to 0 or restores old value.
*/
public void restoreInternalOffset()
{
2024-08-24 17:31:26 -05:00
angleMotor.setAbsoluteEncoder(null);
absoluteEncoder.setAbsoluteEncoderOffset(0);
angleOffset = configuration.angleOffset;
}
2023-11-29 17:36:08 -06:00
/**
* Get if the last Absolute Encoder had a read issue, such as it does not exist.
*
* @return If the last Absolute Encoder had a read issue, or absolute encoder does not exist.
*/
public boolean getAbsoluteEncoderReadIssue()
{
if (absoluteEncoder == null)
{
2023-11-29 17:36:08 -06:00
return true;
} else
{
2023-11-29 17:36:08 -06:00
return absoluteEncoder.readingError;
}
}
2024-01-15 14:37:13 -06:00
/**
* Update data sent to {@link SmartDashboard}.
*/
public void updateTelemetry()
{
if (absoluteEncoder != null)
{
2024-02-12 18:59:40 -06:00
SmartDashboard.putNumber(rawAbsoluteAngleName, absoluteEncoder.getAbsolutePosition());
2024-01-15 14:37:13 -06:00
}
2024-02-12 18:59:40 -06:00
SmartDashboard.putNumber(rawAngleName, angleMotor.getPosition());
SmartDashboard.putNumber(rawDriveName, driveMotor.getPosition());
2024-06-12 15:58:37 -05:00
SmartDashboard.putNumber(rawDriveVelName, driveMotor.getVelocity());
SmartDashboard.putNumber(adjAbsoluteAngleName, getAbsolutePosition());
2024-02-12 18:59:40 -06:00
SmartDashboard.putNumber(absoluteEncoderIssueName, getAbsoluteEncoderReadIssue() ? 1 : 0);
2024-01-15 14:37:13 -06:00
}
2023-02-13 14:37:05 -06:00
}