Skip to content
This repository was archived by the owner on May 19, 2024. It is now read-only.

Commit b8dd291

Browse files
committed
refactor(swerve): Remove swerve constants.
1 parent 4acf4b5 commit b8dd291

File tree

4 files changed

+125
-124
lines changed

4 files changed

+125
-124
lines changed

Diff for: src/main/java/frc/lib/controller/SwerveModuleIOCustom.java

+10-7
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import edu.wpi.first.math.kinematics.SwerveModuleState;
66
import edu.wpi.first.math.util.Units;
77
import frc.lib.CAN;
8+
import frc.lib.config.MechanismConfig;
89
import frc.lib.controller.PositionControllerIO.PositionControllerIOValues;
910
import frc.lib.controller.VelocityControllerIO.VelocityControllerIOValues;
10-
import frc.robot.swerve.SwerveConstants.MK4iConstants;
1111
import frc.robot.swerve.SwerveFactory;
1212

1313
/** Custom swerve module. */
@@ -25,14 +25,17 @@ public class SwerveModuleIOCustom implements SwerveModuleIO {
2525
/** Drive motor values. */
2626
private final VelocityControllerIOValues driveMotorValues = new VelocityControllerIOValues();
2727

28+
private final double wheelCircumference = Units.inchesToMeters(4.0) * Math.PI;
29+
2830
/** Module setpoint */
2931
private SwerveModuleState setpoint;
3032

31-
public SwerveModuleIOCustom(CAN steer, CAN azimuth, CAN drive, Rotation2d offset) {
32-
steerMotor = SwerveFactory.createSteerMotor(steer, azimuth, offset);
33+
public SwerveModuleIOCustom(
34+
CAN steer, CAN azimuth, CAN drive, MechanismConfig steerConfig, MechanismConfig driveConfig) {
35+
steerMotor = SwerveFactory.createSteerMotor(steer, azimuth, steerConfig);
3336
steerMotor.configure();
3437

35-
driveMotor = SwerveFactory.createDriveMotor(drive);
38+
driveMotor = SwerveFactory.createDriveMotor(drive, driveConfig);
3639
driveMotor.configure();
3740

3841
setpoint = new SwerveModuleState();
@@ -44,7 +47,7 @@ public SwerveModuleState getState() {
4447
driveMotor.update(driveMotorValues);
4548

4649
return new SwerveModuleState(
47-
driveMotorValues.velocityRotationsPerSecond * MK4iConstants.WHEEL_CIRCUMFERENCE,
50+
driveMotorValues.velocityRotationsPerSecond * wheelCircumference,
4851
Rotation2d.fromRotations(steerMotorValues.positionRotations));
4952
}
5053

@@ -58,7 +61,7 @@ public void setSetpoint(SwerveModuleState setpoint, boolean lazy) {
5861
setpoint = optimize(setpoint, getState(), lazy);
5962

6063
steerMotor.setSetpoint(setpoint.angle.getRotations(), 0);
61-
driveMotor.setSetpoint(setpoint.speedMetersPerSecond / MK4iConstants.WHEEL_CIRCUMFERENCE);
64+
driveMotor.setSetpoint(setpoint.speedMetersPerSecond / wheelCircumference);
6265

6366
this.setpoint = setpoint;
6467
}
@@ -106,7 +109,7 @@ public SwerveModulePosition getPosition() {
106109
driveMotor.update(driveMotorValues);
107110

108111
return new SwerveModulePosition(
109-
driveMotorValues.positionRotations * MK4iConstants.WHEEL_CIRCUMFERENCE,
112+
driveMotorValues.positionRotations * wheelCircumference,
110113
Rotation2d.fromRotations(steerMotorValues.positionRotations));
111114
}
112115
}

Diff for: src/main/java/frc/robot/swerve/Swerve.java

+82-35
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package frc.robot.swerve;
22

3-
import java.util.function.Function;
4-
53
import edu.wpi.first.math.filter.SlewRateLimiter;
64
import edu.wpi.first.math.geometry.Rotation2d;
75
import edu.wpi.first.math.kinematics.ChassisSpeeds;
@@ -16,10 +14,15 @@
1614
import frc.lib.DriveRequest;
1715
import frc.lib.Subsystem;
1816
import frc.lib.Telemetry;
17+
import frc.lib.config.FeedbackControllerConfig;
18+
import frc.lib.config.FeedforwardControllerConfig;
19+
import frc.lib.config.MechanismConfig;
1920
import frc.lib.config.MotionProfileConfig;
21+
import frc.lib.config.MotorConfig;
2022
import frc.lib.controller.SwerveModuleIO;
2123
import frc.robot.RobotConstants;
2224
import frc.robot.odometry.Odometry;
25+
import java.util.function.Function;
2326

2427
/** Swerve subsystem. */
2528
public class Swerve extends Subsystem {
@@ -33,23 +36,59 @@ public class Swerve extends Subsystem {
3336
/** Swerve kinematics. */
3437
private final SwerveDriveKinematics swerveKinematics;
3538

39+
/** Steer motor config. */
40+
private final MechanismConfig steerConfig =
41+
new MechanismConfig()
42+
.withMotorConfig(
43+
new MotorConfig()
44+
.withCCWPositive(false)
45+
.withCurrentLimit(20)
46+
.withMotorToMechanismRatio(150.0 / 7.0))
47+
.withFeedforwardConfig(
48+
new FeedforwardControllerConfig().withStaticFeedforward(0.205) // volts
49+
)
50+
.withFeedbackConfig(
51+
new FeedbackControllerConfig()
52+
.withContinuousInput(true)
53+
.withProportionalGain(54.0) // volts per rotation
54+
.withDerivativeGain(0.16) // volts per rotation per second
55+
.withPositionTolerance(Units.degreesToRotations(1)) // rotations
56+
);
57+
58+
/** Drive motor config. */
59+
private final MechanismConfig driveConfig =
60+
new MechanismConfig()
61+
.withMotorConfig(
62+
new MotorConfig()
63+
.withCCWPositive(false)
64+
.withCurrentLimit(40)
65+
.withMotorToMechanismRatio(6.75))
66+
.withFeedforwardConfig(
67+
new FeedforwardControllerConfig()
68+
.withStaticFeedforward(0.14) // volts
69+
.withVelocityFeedforward(0.725) // volts per rotation per second
70+
)
71+
.withFeedbackConfig(
72+
new FeedbackControllerConfig()
73+
.withProportionalGain(0.75) // volts per rotation per second
74+
);
75+
3676
/** Translation motion profile config. */
3777
private final MotionProfileConfig translationMotionProfileConfig =
3878
new MotionProfileConfig()
39-
.withMaximumVelocity(4.5) // meters per second
40-
.withMaximumAcceleration(18); // meters per second per second
79+
.withMaximumVelocity(4.5) // meters per second
80+
.withMaximumAcceleration(18); // meters per second per second
4181

4282
/** Rotation motion profile config. */
43-
// TODO Verify
4483
private final MotionProfileConfig rotationMotionProfileConfig =
4584
new MotionProfileConfig().withMaximumVelocity(1); // rotations per second
4685

4786
/** Initializes the swerve subsystem and configures swerve hardware. */
4887
private Swerve() {
49-
swerveModules[0] = SwerveFactory.createNorthWestModule();
50-
swerveModules[1] = SwerveFactory.createNorthEastModule();
51-
swerveModules[2] = SwerveFactory.createSouthEastModule();
52-
swerveModules[3] = SwerveFactory.createSouthWestModule();
88+
swerveModules[0] = SwerveFactory.createNorthWestModule(steerConfig, driveConfig);
89+
swerveModules[1] = SwerveFactory.createNorthEastModule(steerConfig, driveConfig);
90+
swerveModules[2] = SwerveFactory.createSouthEastModule(steerConfig, driveConfig);
91+
swerveModules[3] = SwerveFactory.createSouthWestModule(steerConfig, driveConfig);
5392

5493
swerveKinematics =
5594
new SwerveDriveKinematics(
@@ -82,8 +121,6 @@ public void addToShuffleboard(ShuffleboardTab tab) {
82121
translationConstants.addDouble("Maximum Velocity (mps)", this::maximumTranslationVelocity);
83122
translationConstants.addDouble(
84123
"Maximum Accleration (mpsps)", this::maximumTranslationAcceleration);
85-
translationConstants.addDouble(
86-
"Wheel Circumference (m)", () -> SwerveConstants.MK4iConstants.WHEEL_CIRCUMFERENCE);
87124

88125
ShuffleboardLayout rotationConstants = Telemetry.addColumn(tab, "Rotation Constants");
89126
rotationConstants.addDouble(
@@ -249,30 +286,40 @@ public Rotation2d maximumRotationVelocity() {
249286
* @return a command that drives the swerve using an Xbox controller.
250287
*/
251288
public Command teleopDrive(CommandXboxController controller) {
252-
final SlewRateLimiter xAccelerationLimiter = translationMotionProfileConfig.createAccelerationLimiter();
253-
final SlewRateLimiter yAccelerationLimiter = translationMotionProfileConfig.createAccelerationLimiter();
254-
255-
final Function<Double, Double> rotationVelocityLimiter = rotationMotionProfileConfig.createVelocityClamper();
256-
257-
final Function<ChassisSpeeds, ChassisSpeeds> chassisSpeedsLimiter = chassisSpeeds -> {
258-
return new ChassisSpeeds(
259-
xAccelerationLimiter.calculate(chassisSpeeds.vxMetersPerSecond),
260-
yAccelerationLimiter.calculate(chassisSpeeds.vyMetersPerSecond),
261-
Units.rotationsToRadians(rotationVelocityLimiter.apply(Units.radiansToRotations(chassisSpeeds.omegaRadiansPerSecond)))
262-
);
263-
};
264-
265-
final Function<DriveRequest, ChassisSpeeds> chassisSpeedsGetter = request -> {
266-
return ChassisSpeeds.fromFieldRelativeSpeeds(
267-
request.translationAxis().getX() * translationMotionProfileConfig.maximumVelocity(),
268-
request.translationAxis().getY() * translationMotionProfileConfig.maximumVelocity(),
269-
request.rotationVelocityAxis() * Units.rotationsToRadians(rotationMotionProfileConfig.maximumVelocity()),
270-
Odometry.getInstance().getDriverRelativeHeading());
271-
};
272-
273-
return run(() -> {
274-
setChassisSpeeds(chassisSpeedsLimiter.apply(chassisSpeedsGetter.apply(DriveRequest.fromController(controller))));
275-
});
289+
final SlewRateLimiter xAccelerationLimiter =
290+
translationMotionProfileConfig.createAccelerationLimiter();
291+
final SlewRateLimiter yAccelerationLimiter =
292+
translationMotionProfileConfig.createAccelerationLimiter();
293+
294+
final Function<Double, Double> rotationVelocityLimiter =
295+
rotationMotionProfileConfig.createVelocityClamper();
296+
297+
final Function<ChassisSpeeds, ChassisSpeeds> chassisSpeedsLimiter =
298+
chassisSpeeds -> {
299+
return new ChassisSpeeds(
300+
xAccelerationLimiter.calculate(chassisSpeeds.vxMetersPerSecond),
301+
yAccelerationLimiter.calculate(chassisSpeeds.vyMetersPerSecond),
302+
Units.rotationsToRadians(
303+
rotationVelocityLimiter.apply(
304+
Units.radiansToRotations(chassisSpeeds.omegaRadiansPerSecond))));
305+
};
306+
307+
final Function<DriveRequest, ChassisSpeeds> chassisSpeedsGetter =
308+
request -> {
309+
return ChassisSpeeds.fromFieldRelativeSpeeds(
310+
request.translationAxis().getX() * translationMotionProfileConfig.maximumVelocity(),
311+
request.translationAxis().getY() * translationMotionProfileConfig.maximumVelocity(),
312+
request.rotationVelocityAxis()
313+
* Units.rotationsToRadians(rotationMotionProfileConfig.maximumVelocity()),
314+
Odometry.getInstance().getDriverRelativeHeading());
315+
};
316+
317+
return run(
318+
() -> {
319+
setChassisSpeeds(
320+
chassisSpeedsLimiter.apply(
321+
chassisSpeedsGetter.apply(DriveRequest.fromController(controller))));
322+
});
276323
}
277324

278325
/**

Diff for: src/main/java/frc/robot/swerve/SwerveConstants.java

-63
This file was deleted.

0 commit comments

Comments
 (0)