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

Commit

Permalink
refactor(swerve): Continue moving away from constants.
Browse files Browse the repository at this point in the history
  • Loading branch information
haydenheroux committed Apr 28, 2024
1 parent 7932cad commit f255838
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 86 deletions.
18 changes: 15 additions & 3 deletions src/main/java/frc/lib/config/MotionProfileConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package frc.lib.config;

import java.util.function.Function;

import edu.wpi.first.math.MathUtil;
import edu.wpi.first.math.filter.SlewRateLimiter;
import edu.wpi.first.math.trajectory.TrapezoidProfile;
import edu.wpi.first.math.trajectory.TrapezoidProfile.Constraints;
Expand Down Expand Up @@ -66,11 +69,20 @@ public double maximumAcceleration() {
}

/**
* Creates a new rate limiter using this motion profile config.
* Creates a new velocity clamper using this motion profile config.
*
* @return a new velocity clamper using this motion profile config.
*/
public Function<Double, Double> createVelocityClamper() {
return velocity -> MathUtil.clamp(velocity, -maximumVelocity, maximumVelocity);
}

/**
* Creates a new acceleration limiter using this motion profile config.
*
* @return a new rate limiter using this motion profile config.
* @return a new acceleration limiter using this motion profile config.
*/
public SlewRateLimiter createRateLimiter() {
public SlewRateLimiter createAccelerationLimiter() {
return new SlewRateLimiter(maximumAcceleration);
}

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/frc/robot/auto/Auto.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import frc.robot.odometry.Odometry;
import frc.robot.superstructure.Superstructure;
import frc.robot.swerve.Swerve;
import frc.robot.swerve.SwerveConstants;
import frc.robot.swerve.SwerveFactory;

/** Auto subsystem. */
Expand Down Expand Up @@ -49,7 +48,7 @@ private Auto() {
new HolonomicPathFollowerConfig(
new PIDConstants(1.0),
new PIDConstants(1.0),
SwerveConstants.MAXIMUM_SPEED,
swerve.maximumTranslationVelocity(),
SwerveFactory.createNorthEastModuleTranslation().getNorm(), // TODO
new ReplanningConfig()),
AllianceFlipHelper::shouldFlip,
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/frc/robot/shooter/Shooter.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ private Shooter() {

flywheelValues = new VelocityControllerIOValues();

flywheelAccelerationLimiter = flywheelConfig.motionProfileConfig().createRateLimiter();
flywheelAccelerationLimiter = flywheelConfig.motionProfileConfig().createAccelerationLimiter();

serializer = ShooterFactory.createSerializer(serializerConfig);
serializer.configure();

serializerValues = new VelocityControllerIOValues();

serializerAccelerationLimiter = serializerConfig.motionProfileConfig().createRateLimiter();
serializerAccelerationLimiter = serializerConfig.motionProfileConfig().createAccelerationLimiter();

setpoint = ShooterState.IDLING;
goal = ShooterState.IDLING;
Expand Down
53 changes: 22 additions & 31 deletions src/main/java/frc/robot/swerve/DriveCommand.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package frc.robot.swerve;

import edu.wpi.first.math.MathUtil;
import java.util.function.Function;

import edu.wpi.first.math.filter.SlewRateLimiter;
import edu.wpi.first.math.kinematics.ChassisSpeeds;
import edu.wpi.first.math.util.Units;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
import frc.robot.RobotConstants;
import frc.robot.odometry.Odometry;

/** Drives the swerve using driver input. */
Expand All @@ -18,8 +20,14 @@ public class DriveCommand extends Command {
/** Controller used to get driver input. */
private final CommandXboxController driverController;

/** Previous requested chassis speed. */
private ChassisSpeeds previousChassisSpeeds;
/** Translation acceleration limiter. */
private final SlewRateLimiter xAccelerationLimiter;

/** Translation acceleration limiter. */
private final SlewRateLimiter yAccelerationLimiter;

/** Rotation velocity clamper. */
private final Function<Double, Double> rotationVelocityClamper;

/** Request from the driver controller. */
private DriveRequest request;
Expand All @@ -37,7 +45,10 @@ public DriveCommand(CommandXboxController driverController) {

addRequirements(swerve);

previousChassisSpeeds = new ChassisSpeeds();
xAccelerationLimiter = swerve.translationMotionProfileConfig().createAccelerationLimiter();
yAccelerationLimiter = swerve.translationMotionProfileConfig().createAccelerationLimiter();

rotationVelocityClamper = swerve.rotationMotionProfileConfig().createVelocityClamper();

request = DriveRequest.fromController(driverController);
}
Expand All @@ -52,14 +63,12 @@ public void execute() {
final ChassisSpeeds chassisSpeeds =
clampChassisSpeeds(
ChassisSpeeds.fromFieldRelativeSpeeds(
request.velocity().getX(),
request.velocity().getY(),
request.omega().getRadians(),
request.translationAxis().getX() * swerve.maximumTranslationVelocity(),
request.translationAxis().getY() * swerve.maximumTranslationVelocity(),
swerve.maximumRotationVelocity().times(request.rotationVelocityAxis()).getRadians(),
odometry.getDriverRelativeHeading()));

swerve.setChassisSpeeds(chassisSpeeds);

previousChassisSpeeds = chassisSpeeds;
}

@Override
Expand All @@ -77,27 +86,9 @@ public boolean isFinished() {
* @return the clamped chassis speeds.
*/
private ChassisSpeeds clampChassisSpeeds(ChassisSpeeds desiredChassisSpeeds) {
double vxMetersPerSecond =
MathUtil.clamp(
desiredChassisSpeeds.vxMetersPerSecond,
previousChassisSpeeds.vxMetersPerSecond
- SwerveConstants.MAXIMUM_ACCELERATION * RobotConstants.PERIODIC_DURATION,
previousChassisSpeeds.vxMetersPerSecond
+ SwerveConstants.MAXIMUM_ACCELERATION * RobotConstants.PERIODIC_DURATION);

double vyMetersPerSecond =
MathUtil.clamp(
desiredChassisSpeeds.vyMetersPerSecond,
previousChassisSpeeds.vyMetersPerSecond
- SwerveConstants.MAXIMUM_ACCELERATION * RobotConstants.PERIODIC_DURATION,
previousChassisSpeeds.vyMetersPerSecond
+ SwerveConstants.MAXIMUM_ACCELERATION * RobotConstants.PERIODIC_DURATION);

double omegaRadiansPerSecond =
MathUtil.clamp(
desiredChassisSpeeds.omegaRadiansPerSecond,
-SwerveConstants.MAXIMUM_ROTATION_SPEED.getRadians(),
SwerveConstants.MAXIMUM_ROTATION_SPEED.getRadians());
double vxMetersPerSecond = xAccelerationLimiter.calculate(desiredChassisSpeeds.vxMetersPerSecond);
double vyMetersPerSecond = yAccelerationLimiter.calculate(desiredChassisSpeeds.vyMetersPerSecond);
double omegaRadiansPerSecond = rotationVelocityClamper.apply(Units.radiansToRotations(desiredChassisSpeeds.omegaRadiansPerSecond));

return new ChassisSpeeds(vxMetersPerSecond, vyMetersPerSecond, omegaRadiansPerSecond);
}
Expand Down
43 changes: 15 additions & 28 deletions src/main/java/frc/robot/swerve/DriveRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public record DriveRequest(
DriveRequest.TranslationMode translationMode,
DriveRequest.RotationMode rotationMode,
Translation2d translationAxis,
Translation2d rotationAxis) {
Translation2d headingAxis,
double rotationVelocityAxis) {

/** Translation mode. */
private enum TranslationMode {
Expand All @@ -33,16 +34,16 @@ private enum RotationMode {
/**
* Returns true if no rotation is requested.
*
* @param heading the heading axis.
* @param headingAxis the heading axis.
* @param aligning true if aligning is requested.
* @return
* @return true if no rotation is requested.
*/
private static boolean isDrifting(Translation2d heading, boolean aligning) {
private static boolean isDrifting(Translation2d headingAxis, boolean aligning) {
if (aligning) {
return heading.getNorm() < 0.7;
return headingAxis.getNorm() < 0.7;
}

return Math.abs(heading.getY()) < 0.1;
return Math.abs(headingAxis.getY()) < 0.1;
}

/**
Expand Down Expand Up @@ -70,43 +71,29 @@ public static DriveRequest fromController(CommandXboxController controller) {
translationMagnitude *= 0.25;
}

Translation2d translationVector = new Translation2d(translationMagnitude, translationDirection);
Translation2d translationAxis = new Translation2d(translationMagnitude, translationDirection);

TranslationMode translationMode = TranslationMode.FIELD_CENTRIC;

Translation2d rotationVector =
Translation2d headingAxis =
new Translation2d(-controller.getRightY(), -controller.getRightX());

RotationMode rotationMode;

if (isDrifting(rotationVector, aligningRequested)) {
if (isDrifting(headingAxis, aligningRequested)) {
rotationMode = RotationMode.DRIFTING;
} else if (aligningRequested) {
rotationMode = RotationMode.ALIGNING;
} else {
rotationMode = RotationMode.SPINNING;
}

return new DriveRequest(translationMode, rotationMode, translationVector, rotationVector);
}

/**
* Returns the requested translation velocity in meters per second.
*
* @return the requested translation velocity in meters per second.
*/
public Translation2d velocity() {
return translationAxis.times(SwerveConstants.MAXIMUM_SPEED);
}
double rotationVelocityAxis = 0.0;

/**
* Returns the requested rotation velocity.
*
* @return the requested rotation velocity.
*/
public Rotation2d omega() {
if (Math.abs(rotationAxis.getY()) < 0.1) return new Rotation2d();
if (rotationMode == RotationMode.SPINNING) {
rotationVelocityAxis = headingAxis.getY();
}

return SwerveConstants.MAXIMUM_ROTATION_SPEED.times(rotationAxis.getY());
return new DriveRequest(translationMode, rotationMode, translationAxis, headingAxis, rotationVelocityAxis);
}
}
60 changes: 56 additions & 4 deletions src/main/java/frc/robot/swerve/Swerve.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
import frc.lib.Subsystem;
import frc.lib.Telemetry;
import frc.lib.config.MotionProfileConfig;
import frc.lib.controller.SwerveModuleIO;
import frc.robot.RobotConstants;

Expand All @@ -26,6 +27,12 @@ public class Swerve extends Subsystem {
/** Swerve kinematics. */
private final SwerveDriveKinematics swerveKinematics;

/** Translation motion profile config. */
private final MotionProfileConfig translationMotionProfileConfig = new MotionProfileConfig().withMaximumVelocity(4.5).withMaximumAcceleration(18);

/** Rotation motion profile config. */
private final MotionProfileConfig rotationMotionProfileConfig = new MotionProfileConfig().withMaximumVelocity(0.25);

/** Initializes the swerve subsystem and configures swerve hardware. */
private Swerve() {
swerveModules[0] = SwerveFactory.createNorthWestModule();
Expand Down Expand Up @@ -61,15 +68,15 @@ public void periodic() {}
public void addToShuffleboard(ShuffleboardTab tab) {
ShuffleboardLayout translationConstants = Telemetry.addColumn(tab, "Translation Constants");

translationConstants.addDouble("Maximum Velocity (mps)", () -> SwerveConstants.MAXIMUM_SPEED);
translationConstants.addDouble("Maximum Velocity (mps)", this::maximumTranslationVelocity);
translationConstants.addDouble(
"Maximum Accleration (mpsps)", () -> SwerveConstants.MAXIMUM_ACCELERATION);
"Maximum Accleration (mpsps)", this::maximumTranslationAcceleration);
translationConstants.addDouble(
"Wheel Circumference (m)", () -> SwerveConstants.MK4iConstants.WHEEL_CIRCUMFERENCE);

ShuffleboardLayout rotationConstants = Telemetry.addColumn(tab, "Rotation Constants");
rotationConstants.addDouble(
"Maximum Velocity (rps)", () -> SwerveConstants.MAXIMUM_ROTATION_SPEED.getRotations());
"Maximum Velocity (rps)", () -> maximumRotationVelocity().getRotations());

Telemetry.addSwerveModuleStates(tab, "Swerve Module States", this::getModuleStates);
Telemetry.addSwerveModuleStates(tab, "Swerve Module Setpoints", this::getModuleSetpoints);
Expand Down Expand Up @@ -172,13 +179,58 @@ public void setChassisSpeeds(ChassisSpeeds speeds) {
* @param lazy if true, optimize the module setpoints.
*/
public void setSetpoints(SwerveModuleState[] setpoints, boolean lazy) {
SwerveDriveKinematics.desaturateWheelSpeeds(setpoints, SwerveConstants.MAXIMUM_SPEED);
SwerveDriveKinematics.desaturateWheelSpeeds(setpoints, maximumTranslationVelocity());

for (int i = 0; i < 4; i++) {
swerveModules[i].setSetpoint(setpoints[i], lazy);
}
}

/**
* Returns the motion profile config.
*
* @return the motion profile config.
*/
public MotionProfileConfig translationMotionProfileConfig() {
return translationMotionProfileConfig;
}

/**
* Returns the maximum translation velocity.
*
* @return the maximum translation velocity.
*/
public double maximumTranslationVelocity() {
return translationMotionProfileConfig.maximumVelocity();
}

/**
* Returns the maximum translation acceleration.
*
* @return the maximum translation acceleration.
*/
public double maximumTranslationAcceleration() {
return translationMotionProfileConfig.maximumAcceleration();
}

/**
* Returns the rotation motion profile config.
*
* @return the rotation motion profile config.
*/
public MotionProfileConfig rotationMotionProfileConfig() {
return rotationMotionProfileConfig;
}

/**
* Returns the maximum rotation velocity.
*
* @return the maximum rotation velocity.
*/
public Rotation2d maximumRotationVelocity() {
return Rotation2d.fromRotations(rotationMotionProfileConfig.maximumVelocity());
}

/**
* Drives the swerve using an Xbox controller.
*
Expand Down
16 changes: 0 additions & 16 deletions src/main/java/frc/robot/swerve/SwerveConstants.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package frc.robot.swerve;

import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.util.Units;
import frc.lib.config.FeedbackControllerConfig;
import frc.lib.config.FeedforwardControllerConfig;
Expand All @@ -25,21 +24,6 @@ public static class MK4iConstants {
public static final double WHEEL_CIRCUMFERENCE = WHEEL_DIAMETER * Math.PI;
}

/** Module X offset in meters. */
public static final double X_OFFSET = Units.inchesToMeters(10.375);

/** Module Y offset in meters. */
public static final double Y_OFFSET = Units.inchesToMeters(10.375);

/** Maximum speed in meters per second. */
public static final double MAXIMUM_SPEED = 4.5;

/** Maximum acceleration in meters per second per second. */
public static final double MAXIMUM_ACCELERATION = 18;

/** Maximum rotational speed. */
public static final Rotation2d MAXIMUM_ROTATION_SPEED = Rotation2d.fromRotations(0.25);

/** Drive motor config. */
public static final MechanismConfig DRIVE_CONFIG =
new MechanismConfig()
Expand Down

0 comments on commit f255838

Please sign in to comment.