Files
YAGSL/swervelib/SwerveModule.java

404 lines
12 KiB
Java
Raw Normal View History

2023-02-13 17:21:24 -06:00
package swervelib;
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;
import swervelib.parser.SwerveModuleConfiguration;
import swervelib.simulation.SwerveModuleSimulation;
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;
2023-02-01 16:48:31 -06:00
/**
2023-02-13 14:37:05 -06:00
* Angle offset from the absolute encoder.
2023-02-01 16:48:31 -06:00
*/
2023-02-13 14:37:05 -06:00
private final double angleOffset;
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;
2023-01-29 21:18:52 -06:00
/**
2023-02-13 14:37:05 -06:00
* Module number for kinematics, usually 0 to 3. front left -> front right -> back left -> back right.
2023-01-29 21:18:52 -06:00
*/
public int moduleNumber;
2023-01-29 21:18:52 -06:00
/**
2023-02-13 14:37:05 -06:00
* Feedforward for drive motor during closed loop control.
2023-01-29 21:18:52 -06:00
*/
public SimpleMotorFeedforward feedforward;
2023-11-09 17:32:48 -06:00
/**
* Maximum speed of the drive motors in meters per second.
*/
public double maxSpeed;
2023-01-29 21:18:52 -06:00
/**
* Last swerve module state applied.
2023-01-29 21:18:52 -06:00
*/
public SwerveModuleState lastState;
/**
* Enable {@link SwerveModuleState} optimizations so the angle is reversed and speed is reversed to ensure the module
* never turns more than 90deg.
*/
public boolean moduleStateOptimization = true;
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
/**
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;
// Initialize Feedforward for drive motor.
2023-11-09 17:32:48 -06:00
feedforward = 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);
angleMotor.setPosition(getAbsolutePosition());
}
2023-01-29 21:18:52 -06:00
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);
angleMotor.configurePIDWrapping(-180, 180);
angleMotor.setInverted(moduleConfiguration.angleMotorInverted);
angleMotor.setMotorBrake(false);
2023-01-29 21:18:52 -06:00
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
if (SwerveDriveTelemetry.isSimulation)
2023-01-29 21:18:52 -06:00
{
simModule = new SwerveModuleSimulation();
2023-01-29 21:18:52 -06:00
}
lastState = getState();
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
}
/**
* 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
{
if (moduleStateOptimization)
{
desiredState = SwerveModuleState.optimize(desiredState,
Rotation2d.fromDegrees(getAbsolutePosition()));
}
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
{
if (desiredState.speedMetersPerSecond != lastState.speedMetersPerSecond)
{
double velocity = desiredState.speedMetersPerSecond;
driveMotor.setReference(velocity, feedforward.calculate(velocity));
}
2023-01-29 21:18:52 -06:00
}
// If we are forcing the angle
if (!force)
{
// Prevents module rotation if speed is less than 1%
2023-11-09 17:32:48 -06:00
SwerveMath.antiJitter(desiredState, lastState, Math.min(maxSpeed, 4));
2023-03-21 22:18:24 -05:00
}
if (SwerveDriveTelemetry.verbosity == TelemetryVerbosity.HIGH)
{
2023-04-08 12:31:07 -05:00
SmartDashboard.putNumber("Module[" + configuration.name + "] Speed Setpoint:", desiredState.speedMetersPerSecond);
SmartDashboard.putNumber("Module[" + configuration.name + "] Angle Setpoint:", desiredState.angle.getDegrees());
}
// Prevent module rotation if angle is the same as the previous angle.
if (desiredState.angle != lastState.angle || synchronizeEncoderQueued)
{
// Synchronize encoders if queued and send in the current position as the value from the absolute encoder.
if (absoluteEncoder != null && synchronizeEncoderQueued)
{
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
}
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
{
2023-02-13 14:37:05 -06:00
velocity = driveMotor.getVelocity();
azimuth = Rotation2d.fromDegrees(angleMotor.getPosition());
} 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
{
2023-02-13 14:37:05 -06:00
position = driveMotor.getPosition();
azimuth = Rotation2d.fromDegrees(angleMotor.getPosition());
2023-01-29 21:18:52 -06:00
} else
{
return simModule.getPosition();
2023-01-29 21:18:52 -06:00
}
if (SwerveDriveTelemetry.verbosity == TelemetryVerbosity.HIGH)
{
2023-04-08 12:31:07 -05:00
SmartDashboard.putNumber("Module[" + configuration.name + "] Angle", azimuth.getDegrees());
}
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()
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;
}
/**
* Fetch the {@link SwerveModuleConfiguration} for the {@link SwerveModule} with the parsed configurations.
*
* @return {@link SwerveModuleConfiguration} for the {@link SwerveModule}.
*/
public SwerveModuleConfiguration getConfiguration()
{
return configuration;
}
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()
{
2023-11-29 17:36:08 -06:00
if(absoluteEncoder == null)
return true;
else
return absoluteEncoder.readingError;
}
2023-02-13 14:37:05 -06:00
}