Skip to content

Commit 378abd6

Browse files
committed
Remove slew rate limiter
2.0 MAXSwerve wheels do not require this like the 1.0 wheels did
1 parent d5907c2 commit 378abd6

File tree

3 files changed

+8
-161
lines changed

3 files changed

+8
-161
lines changed

src/main/java/frc/robot/RobotContainer.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public RobotContainer() {
5454
-MathUtil.applyDeadband(m_driverController.getLeftY(), OIConstants.kDriveDeadband),
5555
-MathUtil.applyDeadband(m_driverController.getLeftX(), OIConstants.kDriveDeadband),
5656
-MathUtil.applyDeadband(m_driverController.getRightX(), OIConstants.kDriveDeadband),
57-
true, true),
57+
true),
5858
m_robotDrive));
5959
}
6060

@@ -117,6 +117,6 @@ public Command getAutonomousCommand() {
117117
m_robotDrive.resetOdometry(exampleTrajectory.getInitialPose());
118118

119119
// Run path following command, then stop at the end.
120-
return swerveControllerCommand.andThen(() -> m_robotDrive.drive(0, 0, 0, false, false));
120+
return swerveControllerCommand.andThen(() -> m_robotDrive.drive(0, 0, 0, false));
121121
}
122122
}

src/main/java/frc/robot/subsystems/DriveSubsystem.java

+6-70
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,16 @@
44

55
package frc.robot.subsystems;
66

7-
import edu.wpi.first.math.filter.SlewRateLimiter;
87
import edu.wpi.first.math.geometry.Pose2d;
98
import edu.wpi.first.math.geometry.Rotation2d;
109
import edu.wpi.first.math.kinematics.ChassisSpeeds;
1110
import edu.wpi.first.math.kinematics.SwerveDriveKinematics;
1211
import edu.wpi.first.math.kinematics.SwerveDriveOdometry;
1312
import edu.wpi.first.math.kinematics.SwerveModulePosition;
1413
import edu.wpi.first.math.kinematics.SwerveModuleState;
15-
import edu.wpi.first.util.WPIUtilJNI;
1614
import edu.wpi.first.wpilibj.ADIS16470_IMU;
1715
import edu.wpi.first.wpilibj.ADIS16470_IMU.IMUAxis;
1816
import frc.robot.Constants.DriveConstants;
19-
import frc.utils.SwerveUtils;
2017
import edu.wpi.first.wpilibj2.command.SubsystemBase;
2118

2219
public class DriveSubsystem extends SubsystemBase {
@@ -44,15 +41,6 @@ public class DriveSubsystem extends SubsystemBase {
4441
// The gyro sensor
4542
private final ADIS16470_IMU m_gyro = new ADIS16470_IMU();
4643

47-
// Slew rate filter variables for controlling lateral acceleration
48-
private double m_currentRotation = 0.0;
49-
private double m_currentTranslationDir = 0.0;
50-
private double m_currentTranslationMag = 0.0;
51-
52-
private SlewRateLimiter m_magLimiter = new SlewRateLimiter(DriveConstants.kMagnitudeSlewRate);
53-
private SlewRateLimiter m_rotLimiter = new SlewRateLimiter(DriveConstants.kRotationalSlewRate);
54-
private double m_prevTime = WPIUtilJNI.now() * 1e-6;
55-
5644
// Odometry class for tracking robot pose
5745
SwerveDriveOdometry m_odometry = new SwerveDriveOdometry(
5846
DriveConstants.kDriveKinematics,
@@ -115,69 +103,17 @@ public void resetOdometry(Pose2d pose) {
115103
* @param rot Angular rate of the robot.
116104
* @param fieldRelative Whether the provided x and y speeds are relative to the
117105
* field.
118-
* @param rateLimit Whether to enable rate limiting for smoother control.
119106
*/
120-
public void drive(double xSpeed, double ySpeed, double rot, boolean fieldRelative, boolean rateLimit) {
121-
122-
double xSpeedCommanded;
123-
double ySpeedCommanded;
124-
125-
if (rateLimit) {
126-
// Convert XY to polar for rate limiting
127-
double inputTranslationDir = Math.atan2(ySpeed, xSpeed);
128-
double inputTranslationMag = Math.sqrt(Math.pow(xSpeed, 2) + Math.pow(ySpeed, 2));
129-
130-
// Calculate the direction slew rate based on an estimate of the lateral acceleration
131-
double directionSlewRate;
132-
if (m_currentTranslationMag != 0.0) {
133-
directionSlewRate = Math.abs(DriveConstants.kDirectionSlewRate / m_currentTranslationMag);
134-
} else {
135-
directionSlewRate = 500.0; //some high number that means the slew rate is effectively instantaneous
136-
}
137-
138-
139-
double currentTime = WPIUtilJNI.now() * 1e-6;
140-
double elapsedTime = currentTime - m_prevTime;
141-
double angleDif = SwerveUtils.AngleDifference(inputTranslationDir, m_currentTranslationDir);
142-
if (angleDif < 0.45*Math.PI) {
143-
m_currentTranslationDir = SwerveUtils.StepTowardsCircular(m_currentTranslationDir, inputTranslationDir, directionSlewRate * elapsedTime);
144-
m_currentTranslationMag = m_magLimiter.calculate(inputTranslationMag);
145-
}
146-
else if (angleDif > 0.85*Math.PI) {
147-
if (m_currentTranslationMag > 1e-4) { //some small number to avoid floating-point errors with equality checking
148-
// keep currentTranslationDir unchanged
149-
m_currentTranslationMag = m_magLimiter.calculate(0.0);
150-
}
151-
else {
152-
m_currentTranslationDir = SwerveUtils.WrapAngle(m_currentTranslationDir + Math.PI);
153-
m_currentTranslationMag = m_magLimiter.calculate(inputTranslationMag);
154-
}
155-
}
156-
else {
157-
m_currentTranslationDir = SwerveUtils.StepTowardsCircular(m_currentTranslationDir, inputTranslationDir, directionSlewRate * elapsedTime);
158-
m_currentTranslationMag = m_magLimiter.calculate(0.0);
159-
}
160-
m_prevTime = currentTime;
161-
162-
xSpeedCommanded = m_currentTranslationMag * Math.cos(m_currentTranslationDir);
163-
ySpeedCommanded = m_currentTranslationMag * Math.sin(m_currentTranslationDir);
164-
m_currentRotation = m_rotLimiter.calculate(rot);
165-
166-
167-
} else {
168-
xSpeedCommanded = xSpeed;
169-
ySpeedCommanded = ySpeed;
170-
m_currentRotation = rot;
171-
}
172-
107+
public void drive(double xSpeed, double ySpeed, double rot, boolean fieldRelative) {
173108
// Convert the commanded speeds into the correct units for the drivetrain
174-
double xSpeedDelivered = xSpeedCommanded * DriveConstants.kMaxSpeedMetersPerSecond;
175-
double ySpeedDelivered = ySpeedCommanded * DriveConstants.kMaxSpeedMetersPerSecond;
176-
double rotDelivered = m_currentRotation * DriveConstants.kMaxAngularSpeed;
109+
double xSpeedDelivered = xSpeed * DriveConstants.kMaxSpeedMetersPerSecond;
110+
double ySpeedDelivered = ySpeed * DriveConstants.kMaxSpeedMetersPerSecond;
111+
double rotDelivered = rot * DriveConstants.kMaxAngularSpeed;
177112

178113
var swerveModuleStates = DriveConstants.kDriveKinematics.toSwerveModuleStates(
179114
fieldRelative
180-
? ChassisSpeeds.fromFieldRelativeSpeeds(xSpeedDelivered, ySpeedDelivered, rotDelivered, Rotation2d.fromDegrees(m_gyro.getAngle(IMUAxis.kZ)))
115+
? ChassisSpeeds.fromFieldRelativeSpeeds(xSpeedDelivered, ySpeedDelivered, rotDelivered,
116+
Rotation2d.fromDegrees(m_gyro.getAngle(IMUAxis.kZ)))
181117
: new ChassisSpeeds(xSpeedDelivered, ySpeedDelivered, rotDelivered));
182118
SwerveDriveKinematics.desaturateWheelSpeeds(
183119
swerveModuleStates, DriveConstants.kMaxSpeedMetersPerSecond);

src/main/java/frc/utils/SwerveUtils.java

-89
This file was deleted.

0 commit comments

Comments
 (0)