Skip to content

Commit

Permalink
Finishing logged practive
Browse files Browse the repository at this point in the history
  • Loading branch information
Rango813 committed Jan 27, 2024
1 parent 9b02c13 commit b4713a4
Show file tree
Hide file tree
Showing 14 changed files with 297 additions and 250 deletions.
112 changes: 27 additions & 85 deletions src/main/java/frc/lib/util/swerve/SwerveModule.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package frc.lib.util.swerve;

import com.ctre.phoenix6.configs.CANcoderConfiguration;
import com.ctre.phoenix6.configs.TalonFXConfiguration;
import org.littletonrobotics.junction.Logger;
import com.ctre.phoenix6.controls.DutyCycleOut;
import com.ctre.phoenix6.controls.PositionVoltage;
import com.ctre.phoenix6.controls.VelocityVoltage;
import com.ctre.phoenix6.hardware.CANcoder;
import com.ctre.phoenix6.hardware.TalonFX;
import edu.wpi.first.math.controller.SimpleMotorFeedforward;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.kinematics.SwerveModulePosition;
Expand All @@ -21,12 +18,8 @@ public class SwerveModule {
public int moduleNumber;
private Rotation2d angleOffset;

private TalonFX mAngleMotor;
private TalonFX mDriveMotor;
private CANcoder angleEncoder;
private TalonFXConfiguration swerveAngleFXConfig = new TalonFXConfiguration();
private TalonFXConfiguration swerveDriveFXConfig = new TalonFXConfiguration();
private CANcoderConfiguration swerveCANcoderConfig = new CANcoderConfiguration();
private SwerveModuleIO io;
private SwerveModuleInputsAutoLogged inputs = new SwerveModuleInputsAutoLogged();

private final SimpleMotorFeedforward driveFeedForward = new SimpleMotorFeedforward(
Constants.Swerve.driveKS, Constants.Swerve.driveKV, Constants.Swerve.driveKA);
Expand All @@ -48,87 +41,32 @@ public class SwerveModule {
* @param angleOffset Angle Offset of the CANCoder to align the wheels
*/
public SwerveModule(int moduleNumber, int driveMotorID, int angleMotorID, int cancoderID,
Rotation2d angleOffset) {
Rotation2d angleOffset, SwerveModuleIO io) {
this.io = io;
this.moduleNumber = moduleNumber;
this.angleOffset = angleOffset;

/* Angle Encoder Config */
swerveCANcoderConfig.MagnetSensor.SensorDirection = Constants.Swerve.cancoderInvert;

angleEncoder = new CANcoder(cancoderID);
angleEncoder.getConfigurator().apply(swerveCANcoderConfig);

/* Angle Motor Config */
/* Motor Inverts and Neutral Mode */
swerveAngleFXConfig.MotorOutput.Inverted = Constants.Swerve.angleMotorInvert;
swerveAngleFXConfig.MotorOutput.NeutralMode = Constants.Swerve.angleNeutralMode;

/* Gear Ratio and Wrapping Config */
swerveAngleFXConfig.Feedback.SensorToMechanismRatio = Constants.Swerve.angleGearRatio;
swerveAngleFXConfig.ClosedLoopGeneral.ContinuousWrap = true;

/* Current Limiting */
swerveAngleFXConfig.CurrentLimits.SupplyCurrentLimitEnable =
Constants.Swerve.angleEnableCurrentLimit;
swerveAngleFXConfig.CurrentLimits.SupplyCurrentLimit = Constants.Swerve.angleCurrentLimit;
swerveAngleFXConfig.CurrentLimits.SupplyCurrentThreshold =
Constants.Swerve.angleCurrentThreshold;
swerveAngleFXConfig.CurrentLimits.SupplyTimeThreshold =
Constants.Swerve.angleCurrentThresholdTime;

/* PID Config */
swerveAngleFXConfig.Slot0.kP = Constants.Swerve.angleKP;
swerveAngleFXConfig.Slot0.kI = Constants.Swerve.angleKI;
swerveAngleFXConfig.Slot0.kD = Constants.Swerve.angleKD;
mAngleMotor = new TalonFX(angleMotorID);
mAngleMotor.getConfigurator().apply(swerveAngleFXConfig);
// lastAngle = getState().angle.getDegrees();
io.updateInputs(inputs);
resetToAbsolute();
Logger.processInputs("SwerveModule" + moduleNumber, inputs);

/* Drive Motor Config */
/* Motor Inverts and Neutral Mode */
swerveDriveFXConfig.MotorOutput.Inverted = Constants.Swerve.driveMotorInvert;
swerveDriveFXConfig.MotorOutput.NeutralMode = Constants.Swerve.driveNeutralMode;

/* Gear Ratio Config */
swerveDriveFXConfig.Feedback.SensorToMechanismRatio = Constants.Swerve.driveGearRatio;

/* Current Limiting */
swerveDriveFXConfig.CurrentLimits.SupplyCurrentLimitEnable =
Constants.Swerve.driveEnableCurrentLimit;
swerveDriveFXConfig.CurrentLimits.SupplyCurrentLimit = Constants.Swerve.driveCurrentLimit;
swerveDriveFXConfig.CurrentLimits.SupplyCurrentThreshold =
Constants.Swerve.driveCurrentThreshold;
swerveDriveFXConfig.CurrentLimits.SupplyTimeThreshold =
Constants.Swerve.driveCurrentThresholdTime;

/* PID Config */
swerveDriveFXConfig.Slot0.kP = Constants.Swerve.driveKP;
swerveDriveFXConfig.Slot0.kI = Constants.Swerve.driveKI;
swerveDriveFXConfig.Slot0.kD = Constants.Swerve.driveKD;

/* Open and Closed Loop Ramping */
swerveDriveFXConfig.OpenLoopRamps.DutyCycleOpenLoopRampPeriod =
Constants.Swerve.openLoopRamp;
swerveDriveFXConfig.OpenLoopRamps.VoltageOpenLoopRampPeriod = Constants.Swerve.openLoopRamp;

swerveDriveFXConfig.ClosedLoopRamps.DutyCycleClosedLoopRampPeriod =
Constants.Swerve.closedLoopRamp;
swerveDriveFXConfig.ClosedLoopRamps.VoltageClosedLoopRampPeriod =
Constants.Swerve.closedLoopRamp;
mDriveMotor = new TalonFX(driveMotorID);
mDriveMotor.getConfigurator().apply(swerveDriveFXConfig);
mDriveMotor.getConfigurator().setPosition(0.0);
}

public void periodic() {
io.updateInputs(inputs);
Logger.processInputs("SwerveModule" + moduleNumber, inputs);
}

/**
* Set the desired state of the Swerve Module
* Set the desir ed state of the Swerve Module
*
* @param desiredState The desired {@link SwerveModuleState} for the module
* @param isOpenLoop Whether the state should be open or closed loop controlled
*/
public void setDesiredState(SwerveModuleState desiredState, boolean isOpenLoop) {
desiredState = SwerveModuleState.optimize(desiredState, getState().angle);
mAngleMotor.setControl(anglePosition.withPosition(desiredState.angle.getRotations()));
io.setAngleMotor(anglePosition.withPosition(desiredState.angle.getRotations()));
setSpeed(desiredState, isOpenLoop);
}

Expand All @@ -141,13 +79,17 @@ public void setDesiredState(SwerveModuleState desiredState, boolean isOpenLoop)
private void setSpeed(SwerveModuleState desiredState, boolean isOpenLoop) {
if (isOpenLoop) {
driveDutyCycle.Output = desiredState.speedMetersPerSecond / Constants.Swerve.maxSpeed;
mDriveMotor.setControl(driveDutyCycle);
io.setDriveMotor(driveDutyCycle);
inputs.driveMotorSelectedSensorVelocity = driveDutyCycle.Output;

} else {
driveVelocity.Velocity = Conversions.MPSToRPS(desiredState.speedMetersPerSecond,
Constants.Swerve.wheelCircumference);
driveVelocity.FeedForward =
driveFeedForward.calculate(desiredState.speedMetersPerSecond);
mDriveMotor.setControl(driveVelocity);
io.setDriveMotor(driveVelocity);
inputs.driveMotorSelectedSensorVelocity = driveDutyCycle.Output;

}
}

Expand All @@ -157,15 +99,15 @@ private void setSpeed(SwerveModuleState desiredState, boolean isOpenLoop) {
* @return The rotation of the CANCoder in {@link Rotation2d}
*/
public Rotation2d getCANcoder() {
return Rotation2d.fromRotations(angleEncoder.getAbsolutePosition().getValue());
return Rotation2d.fromRotations(inputs.absolutePositionAngleEncoder);
}

/**
* Reset the Swerve Module angle to face forward
*/
public void resetToAbsolute() {
double absolutePosition = getCANcoder().getRotations() - angleOffset.getRotations();
mAngleMotor.setPosition(absolutePosition);
io.setPositionAngleMotor(absolutePosition);
}

/**
Expand All @@ -175,9 +117,9 @@ public void resetToAbsolute() {
*/
public SwerveModuleState getState() {
return new SwerveModuleState(
Conversions.RPSToMPS(mDriveMotor.getVelocity().getValue(),
Conversions.RPSToMPS(inputs.driveMotorSelectedSensorVelocity,
Constants.Swerve.wheelCircumference),
Rotation2d.fromRotations(mAngleMotor.getPosition().getValue()));
Rotation2d.fromRotations(inputs.angleMotorSelectedPosition));
}

/**
Expand All @@ -187,8 +129,8 @@ public SwerveModuleState getState() {
*/
public SwerveModulePosition getPosition() {
return new SwerveModulePosition(
Conversions.rotationsToMeters(mDriveMotor.getPosition().getValue(),
Conversions.rotationsToMeters(inputs.driveMotorSelectedPosition,
Constants.Swerve.wheelCircumference),
Rotation2d.fromRotations(mAngleMotor.getPosition().getValue()));
Rotation2d.fromRotations(inputs.angleMotorSelectedPosition));
}
}
27 changes: 3 additions & 24 deletions src/main/java/frc/lib/util/swerve/SwerveModuleIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,22 @@ public interface SwerveModuleIO {

@AutoLog

Check warning on line 9 in src/main/java/frc/lib/util/swerve/SwerveModuleIO.java

View workflow job for this annotation

GitHub Actions / testtool

[testtool] src/main/java/frc/lib/util/swerve/SwerveModuleIO.java#L9 <com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocTypeCheck>

Missing a Javadoc comment.
Raw output
/github/workspace/./src/main/java/frc/lib/util/swerve/SwerveModuleIO.java:9:5: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocTypeCheck)
public static class SwerveModuleInputs {
public double angleEncoderValue;
public double driveMotorSelectedPosition;
public double driveMotorSelectedSensorVelocity;
public double angleMotorSelectedPosition;
public double driveMotorTemperature;
public double angleMotorTemperature;

public double absolutePositionAngleEncoder;
}

public default void updateInputs(SwerveModuleInputs inputs) {
int i = 0;
}
public default void updateInputs(SwerveModuleInputs inputs) {}

public default void setDriveMotor(ControlRequest request) {};

Check warning on line 19 in src/main/java/frc/lib/util/swerve/SwerveModuleIO.java

View workflow job for this annotation

GitHub Actions / testtool

[testtool] src/main/java/frc/lib/util/swerve/SwerveModuleIO.java#L19 <com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyLineSeparatorCheck>

';' should be separated from previous line.
Raw output
/github/workspace/./src/main/java/frc/lib/util/swerve/SwerveModuleIO.java:19:65: warning: ';' should be separated from previous line. (com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyLineSeparatorCheck)

public default void setAngleMotor(ControlRequest request) {};

Check warning on line 21 in src/main/java/frc/lib/util/swerve/SwerveModuleIO.java

View workflow job for this annotation

GitHub Actions / testtool

[testtool] src/main/java/frc/lib/util/swerve/SwerveModuleIO.java#L21 <com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyLineSeparatorCheck>

';' should be separated from previous line.
Raw output
/github/workspace/./src/main/java/frc/lib/util/swerve/SwerveModuleIO.java:21:65: warning: ';' should be separated from previous line. (com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyLineSeparatorCheck)

public default void setAngleSelectedSensorPosition(double angle) {};

Check warning on line 23 in src/main/java/frc/lib/util/swerve/SwerveModuleIO.java

View workflow job for this annotation

GitHub Actions / testtool

[testtool] src/main/java/frc/lib/util/swerve/SwerveModuleIO.java#L23 <com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyLineSeparatorCheck>

';' should be separated from previous line.
Raw output
/github/workspace/./src/main/java/frc/lib/util/swerve/SwerveModuleIO.java:23:72: warning: ';' should be separated from previous line. (com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyLineSeparatorCheck)

public default void setAngleMotorPosition(ControlRequest request) {};


public default double getEncoderPosition() {
return 0.0;
}

public default double getVelocityOfMotor() {
return 0.0;
}
public default void setPositionAngleMotor(double absolutePosition) {};

Check warning on line 25 in src/main/java/frc/lib/util/swerve/SwerveModuleIO.java

View workflow job for this annotation

GitHub Actions / testtool

[testtool] src/main/java/frc/lib/util/swerve/SwerveModuleIO.java#L25 <com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyLineSeparatorCheck>

';' should be separated from previous line.
Raw output
/github/workspace/./src/main/java/frc/lib/util/swerve/SwerveModuleIO.java:25:74: warning: ';' should be separated from previous line. (com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyLineSeparatorCheck)

public default double getAngleMotor() {
return 0.0;
}

public default double getPositionDriveMotor() {
return 0.0;
}


}
117 changes: 115 additions & 2 deletions src/main/java/frc/lib/util/swerve/SwerveModuleReal.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,128 @@
package frc.lib.util.swerve;

import com.ctre.phoenix6.configs.CANcoderConfiguration;
import com.ctre.phoenix6.configs.TalonFXConfiguration;
import com.ctre.phoenix6.controls.ControlRequest;
import com.ctre.phoenix6.hardware.CANcoder;
import com.ctre.phoenix6.hardware.TalonFX;
import edu.wpi.first.math.geometry.Rotation2d;
import frc.robot.Constants;

public class SwerveModuleReal implements SwerveModuleIO {

Check warning on line 11 in src/main/java/frc/lib/util/swerve/SwerveModuleReal.java

View workflow job for this annotation

GitHub Actions / testtool

[testtool] src/main/java/frc/lib/util/swerve/SwerveModuleReal.java#L11 <com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocTypeCheck>

Missing a Javadoc comment.
Raw output
/github/workspace/./src/main/java/frc/lib/util/swerve/SwerveModuleReal.java:11:1: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocTypeCheck)
private TalonFX mAngleMotor;
private TalonFX mDriveMotor;
private CANcoder angleEncoder;
private TalonFXConfiguration swerveAngleFXConfig = new TalonFXConfiguration();
private TalonFXConfiguration swerveDriveFXConfig = new TalonFXConfiguration();
private CANcoderConfiguration swerveCANcoderConfig = new CANcoderConfiguration();

public SwerveModuleReal(int moduleNumber, int driveMotorID, int angleMotorID, int angleCoderID,

Check warning on line 19 in src/main/java/frc/lib/util/swerve/SwerveModuleReal.java

View workflow job for this annotation

GitHub Actions / testtool

[testtool] src/main/java/frc/lib/util/swerve/SwerveModuleReal.java#L19 <com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck>

Missing a Javadoc comment.
Raw output
/github/workspace/./src/main/java/frc/lib/util/swerve/SwerveModuleReal.java:19:5: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck)
Rotation2d angleOffset) {

mAngleMotor = new TalonFX(angleMotorID, "canivore");
mDriveMotor = new TalonFX(driveMotorID, "canivore");
angleEncoder = new CANcoder(angleCoderID, "canivore");

configAngleEncoder();
configMotorDrive();
configAngleMotor();


}

private void configAngleMotor() {
/* Angle Motor Config */
/* Motor Inverts and Neutral Mode */
swerveAngleFXConfig.MotorOutput.Inverted = Constants.Swerve.angleMotorInvert;
swerveAngleFXConfig.MotorOutput.NeutralMode = Constants.Swerve.angleNeutralMode;

/* Gear Ratio and Wrapping Config */
swerveAngleFXConfig.Feedback.SensorToMechanismRatio = Constants.Swerve.angleGearRatio;
swerveAngleFXConfig.ClosedLoopGeneral.ContinuousWrap = true;

/* Current Limiting */
swerveAngleFXConfig.CurrentLimits.SupplyCurrentLimitEnable =
Constants.Swerve.angleEnableCurrentLimit;
swerveAngleFXConfig.CurrentLimits.SupplyCurrentLimit = Constants.Swerve.angleCurrentLimit;
swerveAngleFXConfig.CurrentLimits.SupplyCurrentThreshold =
Constants.Swerve.angleCurrentThreshold;
swerveAngleFXConfig.CurrentLimits.SupplyTimeThreshold =
Constants.Swerve.angleCurrentThresholdTime;

/* PID Config */
swerveAngleFXConfig.Slot0.kP = Constants.Swerve.angleKP;
swerveAngleFXConfig.Slot0.kI = Constants.Swerve.angleKI;
swerveAngleFXConfig.Slot0.kD = Constants.Swerve.angleKD;

mAngleMotor.getConfigurator().apply(swerveAngleFXConfig);

}

public void configMotorDrive() {

Check warning on line 61 in src/main/java/frc/lib/util/swerve/SwerveModuleReal.java

View workflow job for this annotation

GitHub Actions / testtool

[testtool] src/main/java/frc/lib/util/swerve/SwerveModuleReal.java#L61 <com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck>

Missing a Javadoc comment.
Raw output
/github/workspace/./src/main/java/frc/lib/util/swerve/SwerveModuleReal.java:61:5: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck)
/* Drive Motor Config */
/* Motor Inverts and Neutral Mode */
swerveDriveFXConfig.MotorOutput.Inverted = Constants.Swerve.driveMotorInvert;
swerveDriveFXConfig.MotorOutput.NeutralMode = Constants.Swerve.driveNeutralMode;

/* Gear Ratio Config */
swerveDriveFXConfig.Feedback.SensorToMechanismRatio = Constants.Swerve.driveGearRatio;

/* Current Limiting */
swerveDriveFXConfig.CurrentLimits.SupplyCurrentLimitEnable =
Constants.Swerve.driveEnableCurrentLimit;
swerveDriveFXConfig.CurrentLimits.SupplyCurrentLimit = Constants.Swerve.driveCurrentLimit;
swerveDriveFXConfig.CurrentLimits.SupplyCurrentThreshold =
Constants.Swerve.driveCurrentThreshold;
swerveDriveFXConfig.CurrentLimits.SupplyTimeThreshold =
Constants.Swerve.driveCurrentThresholdTime;

/* PID Config */
swerveDriveFXConfig.Slot0.kP = Constants.Swerve.driveKP;
swerveDriveFXConfig.Slot0.kI = Constants.Swerve.driveKI;
swerveDriveFXConfig.Slot0.kD = Constants.Swerve.driveKD;

/* Open and Closed Loop Ramping */
swerveDriveFXConfig.OpenLoopRamps.DutyCycleOpenLoopRampPeriod =
Constants.Swerve.openLoopRamp;
swerveDriveFXConfig.OpenLoopRamps.VoltageOpenLoopRampPeriod = Constants.Swerve.openLoopRamp;

swerveDriveFXConfig.ClosedLoopRamps.DutyCycleClosedLoopRampPeriod =
Constants.Swerve.closedLoopRamp;
swerveDriveFXConfig.ClosedLoopRamps.VoltageClosedLoopRampPeriod =
Constants.Swerve.closedLoopRamp;

mDriveMotor.getConfigurator().apply(swerveDriveFXConfig);
mDriveMotor.getConfigurator().setPosition(0.0);
}

public void configAngleEncoder() {

Check warning on line 98 in src/main/java/frc/lib/util/swerve/SwerveModuleReal.java

View workflow job for this annotation

GitHub Actions / testtool

[testtool] src/main/java/frc/lib/util/swerve/SwerveModuleReal.java#L98 <com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck>

Missing a Javadoc comment.
Raw output
/github/workspace/./src/main/java/frc/lib/util/swerve/SwerveModuleReal.java:98:5: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck)
/* Angle Encoder Config */
swerveCANcoderConfig.MagnetSensor.SensorDirection = Constants.Swerve.cancoderInvert;

angleEncoder.getConfigurator().apply(swerveCANcoderConfig);
}

@Override
public void setAngleMotor(ControlRequest request) {
// TODO Auto-generated method stub
SwerveModuleIO.super.setAngleMotor(request);
mAngleMotor.setControl(request);
}

@Override
public void setDriveMotor(ControlRequest request) {
mDriveMotor.setControl(request);
}

@Override
public void updateInputs(SwerveModuleInputs inputs) {
inputs.driveMotorSelectedPosition = mDriveMotor.getPosition().getValueAsDouble();
inputs.driveMotorSelectedSensorVelocity = mDriveMotor.getVelocity().getValueAsDouble();
inputs.angleMotorSelectedPosition = mAngleMotor.getPosition().getValueAsDouble();
inputs.absolutePositionAngleEncoder = angleEncoder.getAbsolutePosition().getValueAsDouble();
}

@Override
public void setPositionAngleMotor(double absolutePosition) {
mAngleMotor.setPosition(absolutePosition);
}

}
Loading

0 comments on commit b4713a4

Please sign in to comment.