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

Commit

Permalink
refactor(swerve): Recalculate acceleration.
Browse files Browse the repository at this point in the history
  • Loading branch information
haydenheroux committed Jan 23, 2024
1 parent bb27c85 commit 5149ec2
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/main/java/frc/robot/auto/Auto.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private Auto() {
new HolonomicPathFollowerConfig(
new PIDConstants(1.0),
new PIDConstants(1.0),
SwerveConstants.MAXIMUM_SPEED,
SwerveConstants.MAXIMUM_ATTAINABLE_SPEED,
SwerveConstants.NORTH_EAST_MODULE_CONFIG.position().getNorm(),
new ReplanningConfig());

Expand Down
10 changes: 8 additions & 2 deletions src/main/java/frc/robot/swerve/Drive.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ public class Drive extends Command {
/* Heading feedback controller. */
private final ProfiledRotationPIDController headingFeedback =
new ProfiledRotationPIDController(
6, 0, 0.1, new Constraints(0.25 * SwerveConstants.MAXIMUM_ROTATION_SPEED, 8));
6,
0,
0.1,
new Constraints(
SwerveConstants.MAXIMUM_ROTATION_SPEED,
SwerveConstants.MAXIMUM_ROTATION_ACCELERATION));

private final DoubleEntry headingEntry,
headingVelocityEntry,
Expand Down Expand Up @@ -123,8 +128,9 @@ public void execute() {
}

double maxOmegaRadiansPerSecond =
Units.rotationsToRadians(SwerveConstants.MAXIMUM_ROTATION_SPEED);
Units.rotationsToRadians(SwerveConstants.MAXIMUM_ATTAINABLE_ROTATION_SPEED);

// Clamp angular velocity
chassisSpeeds.omegaRadiansPerSecond =
Math.signum(chassisSpeeds.omegaRadiansPerSecond)
* Math.min(maxOmegaRadiansPerSecond, Math.abs(chassisSpeeds.omegaRadiansPerSecond));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/frc/robot/swerve/DriveMotorIOTalonFXPID.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private VoltageOut calculateVelocityVoltage(double velocityMetersPerSecond, bool
* @return the voltage to apply.
*/
private VoltageOut calculateOpenLoopVelocityVoltage(double velocityMetersPerSecond) {
double velocityPercent = velocityMetersPerSecond / SwerveConstants.MAXIMUM_SPEED;
double velocityPercent = velocityMetersPerSecond / SwerveConstants.MAXIMUM_ATTAINABLE_SPEED;

double velocityVolts = velocityPercent * RobotConstants.BATTERY_VOLTAGE;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/frc/robot/swerve/DriveRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public Translation2d getTranslationVelocity() {
}

public double getRotationVelocity() {
return SwerveConstants.MAXIMUM_ROTATION_SPEED * 0.25 * this.rotationVector.getY();
return SwerveConstants.MAXIMUM_ROTATION_SPEED * this.rotationVector.getY();
}

private Rotation2d snapToNearest(Rotation2d angle, Rotation2d multiple) {
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/frc/robot/swerve/Swerve.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,16 @@ public void setChassisSpeeds(ChassisSpeeds speeds) {
* @param lazy if true, optimize the module setpoint.
*/
public void setSetpoints(SwerveModuleState[] setpoints, boolean lazy) {
SwerveDriveKinematics.desaturateWheelSpeeds(setpoints, SwerveConstants.MAXIMUM_SPEED);
SwerveDriveKinematics.desaturateWheelSpeeds(
setpoints, SwerveConstants.MAXIMUM_ATTAINABLE_SPEED);

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

/**
* Set the steer motor setpoints for each of the swerve modules.
* Set the steer motor setpoints for each of the swerve modules.
*
* @param steerSetpoints the steer motor setpoints for each swerve module.
*/
Expand Down Expand Up @@ -199,7 +200,10 @@ public void pointSideways() {
Rotation2d.fromDegrees(90.0));
}

/** Sets the steer motor setpoints to point the swerve modules inwards, like a cross or "X" pattern. */
/**
* Sets the steer motor setpoints to point the swerve modules inwards, like a cross or "X"
* pattern.
*/
public void pointInwards() {
setSteerSetpoints(
Rotation2d.fromDegrees(45.0),
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/frc/robot/swerve/SwerveConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,22 @@ public static class MK4iConstants {
Rotation2d.fromRotations(-0.954346));

/** Maximum attainable speed in meters per second. */
public static final double MAXIMUM_SPEED = Units.feetToMeters(18.38719);
public static final double MAXIMUM_ATTAINABLE_SPEED = Units.feetToMeters(18.38719);

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

/** Maximum attainable rotational speed in rotations per second. */
public static final double MAXIMUM_ROTATION_SPEED =
NORTH_WEST_MODULE_CONFIG.position().getNorm() * MAXIMUM_SPEED;
public static final double MAXIMUM_ATTAINABLE_ROTATION_SPEED =
NORTH_WEST_MODULE_CONFIG.position().getNorm() * MAXIMUM_ATTAINABLE_SPEED;

/** Maximum rotational speed while snapping to heading in rotations per second. */
public static final double MAXIMUM_ROTATION_SPEED = 0.5;

/**
* Maximum rotational acceleration while snapping to heading in rotations per second per second.
*/
public static final double MAXIMUM_ROTATION_ACCELERATION = 8.0;

public static class DriveMotorConstants {
/** If true, use open-loop control. */
Expand Down

0 comments on commit 5149ec2

Please sign in to comment.