Skip to content

Commit 46ba3a8

Browse files
committed
Organized variables, simpler deadband
1 parent bc5df67 commit 46ba3a8

3 files changed

Lines changed: 32 additions & 32 deletions

File tree

src/main/java/frc/robot/Constants.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public static final class DriveConstants {
3030
public static final double kMaxSpeedMetersPerSecond = 4.8;
3131
public static final double kMaxAngularSpeed = 2 * Math.PI; // radians per second
3232

33+
public static final double kDirectionSlewRate = 1.2; // radians per second
34+
public static final double kMagnitudeSlewRate = 1.8; // percent per second (1 = 100%)
35+
public static final double kRotationalSlewRate = 2.0; // percent per second (1 = 100%)
36+
3337
// Chassis configuration
3438
public static final double kTrackWidth = Units.inchesToMeters(26.5);
3539
// Distance between centers of right and left wheels on robot
@@ -115,10 +119,6 @@ public static final class ModuleConstants {
115119
public static final class OIConstants {
116120
public static final int kDriverControllerPort = 0;
117121
public static final double kDriveDeadband = 0.05;
118-
public static final double kMagnitudeDeadband = 0.05;
119-
public static final double kDirectionSlewRate = 1.2; // radians per second
120-
public static final double kMagnitudeSlewRate = 1.8; // percent per second (1 = 100%)
121-
public static final double kRotationalSlewRate = 2.0; // percent per second (1 = 100%)
122122
}
123123

124124
public static final class AutoConstants {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
package frc.robot;
66

7+
import edu.wpi.first.math.MathUtil;
78
import edu.wpi.first.math.controller.PIDController;
89
import edu.wpi.first.math.controller.ProfiledPIDController;
910
import edu.wpi.first.math.geometry.Pose2d;
@@ -50,9 +51,9 @@ public RobotContainer() {
5051
// Turning is controlled by the X axis of the right stick.
5152
new RunCommand(
5253
() -> m_robotDrive.drive(
53-
Math.max(0.0, (Math.abs(m_driverController.getLeftY())-OIConstants.kDriveDeadband)/(1.0-OIConstants.kDriveDeadband)) * Math.signum(-m_driverController.getLeftY()),
54-
Math.max(0.0, (Math.abs(m_driverController.getLeftX())-OIConstants.kDriveDeadband)/(1.0-OIConstants.kDriveDeadband)) * Math.signum(-m_driverController.getLeftX()),
55-
Math.max(0.0, (Math.abs(m_driverController.getRightX())-OIConstants.kDriveDeadband)/(1.0-OIConstants.kDriveDeadband)) * Math.signum(-m_driverController.getRightX()),
54+
MathUtil.applyDeadband(m_driverController.getLeftY(), OIConstants.kDriveDeadband),
55+
MathUtil.applyDeadband(m_driverController.getLeftX(), OIConstants.kDriveDeadband),
56+
MathUtil.applyDeadband(m_driverController.getRightX(), OIConstants.kDriveDeadband),
5657
true, true),
5758
m_robotDrive));
5859
}

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

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import edu.wpi.first.util.WPIUtilJNI;
1616
import edu.wpi.first.wpilibj.ADIS16470_IMU;
1717
import frc.robot.Constants.DriveConstants;
18-
import frc.robot.Constants.OIConstants;
1918
import frc.utils.SwerveUtils;
2019
import edu.wpi.first.wpilibj2.command.SubsystemBase;
2120

@@ -45,13 +44,13 @@ public class DriveSubsystem extends SubsystemBase {
4544
private final ADIS16470_IMU m_gyro = new ADIS16470_IMU();
4645

4746
// Slew rate filter variables for controlling lateral acceleration
48-
private double currentRotation = 0.0;
49-
private double currentTranslationDir = 0.0;
50-
private double currentTranslationMag = 0.0;
47+
private double m_currentRotation = 0.0;
48+
private double m_currentTranslationDir = 0.0;
49+
private double m_currentTranslationMag = 0.0;
5150

52-
private SlewRateLimiter magLimiter = new SlewRateLimiter(OIConstants.kMagnitudeSlewRate);
53-
private SlewRateLimiter rotLimiter = new SlewRateLimiter(OIConstants.kRotationalSlewRate);
54-
double prevTime = WPIUtilJNI.now() * 1e-6;
51+
private SlewRateLimiter m_magLimiter = new SlewRateLimiter(DriveConstants.kMagnitudeSlewRate);
52+
private SlewRateLimiter m_rotLimiter = new SlewRateLimiter(DriveConstants.kRotationalSlewRate);
53+
private double m_prevTime = WPIUtilJNI.now() * 1e-6;
5554

5655
// Odometry class for tracking robot pose
5756
SwerveDriveOdometry m_odometry = new SwerveDriveOdometry(
@@ -128,51 +127,51 @@ public void drive(double xSpeed, double ySpeed, double rot, boolean fieldRelativ
128127

129128
// Calculate the direction slew rate based on an estimate of the lateral acceleration
130129
double directionSlewRate;
131-
if (currentTranslationMag != 0.0) {
132-
directionSlewRate = Math.abs(OIConstants.kDirectionSlewRate / currentTranslationMag);
130+
if (m_currentTranslationMag != 0.0) {
131+
directionSlewRate = Math.abs(DriveConstants.kDirectionSlewRate / m_currentTranslationMag);
133132
} else {
134133
directionSlewRate = 500.0; //some high number that means the slew rate is effectively instantaneous
135134
}
136135

137136

138137
double currentTime = WPIUtilJNI.now() * 1e-6;
139-
double elapsedTime = currentTime - prevTime;
140-
double angleDif = SwerveUtils.AngleDifference(inputTranslationDir, currentTranslationDir);
138+
double elapsedTime = currentTime - m_prevTime;
139+
double angleDif = SwerveUtils.AngleDifference(inputTranslationDir, m_currentTranslationDir);
141140
if (angleDif < 0.45*Math.PI) {
142-
currentTranslationDir = SwerveUtils.StepTowardsCircular(currentTranslationDir, inputTranslationDir, directionSlewRate * elapsedTime);
143-
currentTranslationMag = magLimiter.calculate(inputTranslationMag);
141+
m_currentTranslationDir = SwerveUtils.StepTowardsCircular(m_currentTranslationDir, inputTranslationDir, directionSlewRate * elapsedTime);
142+
m_currentTranslationMag = m_magLimiter.calculate(inputTranslationMag);
144143
}
145144
else if (angleDif > 0.85*Math.PI) {
146-
if (currentTranslationMag > 1e-4) { //some small number to avoid floating-point errors with equality checking
145+
if (m_currentTranslationMag > 1e-4) { //some small number to avoid floating-point errors with equality checking
147146
// keep currentTranslationDir unchanged
148-
currentTranslationMag = magLimiter.calculate(0.0);
147+
m_currentTranslationMag = m_magLimiter.calculate(0.0);
149148
}
150149
else {
151-
currentTranslationDir = SwerveUtils.WrapAngle(currentTranslationDir + Math.PI);
152-
currentTranslationMag = magLimiter.calculate(inputTranslationMag);
150+
m_currentTranslationDir = SwerveUtils.WrapAngle(m_currentTranslationDir + Math.PI);
151+
m_currentTranslationMag = m_magLimiter.calculate(inputTranslationMag);
153152
}
154153
}
155154
else {
156-
currentTranslationDir = SwerveUtils.StepTowardsCircular(currentTranslationDir, inputTranslationDir, directionSlewRate * elapsedTime);
157-
currentTranslationMag = magLimiter.calculate(0.0);
155+
m_currentTranslationDir = SwerveUtils.StepTowardsCircular(m_currentTranslationDir, inputTranslationDir, directionSlewRate * elapsedTime);
156+
m_currentTranslationMag = m_magLimiter.calculate(0.0);
158157
}
159-
prevTime = currentTime;
158+
m_prevTime = currentTime;
160159

161-
xSpeedCommanded = currentTranslationMag * Math.cos(currentTranslationDir);
162-
ySpeedCommanded = currentTranslationMag * Math.sin(currentTranslationDir);
163-
currentRotation = rotLimiter.calculate(rot);
160+
xSpeedCommanded = m_currentTranslationMag * Math.cos(m_currentTranslationDir);
161+
ySpeedCommanded = m_currentTranslationMag * Math.sin(m_currentTranslationDir);
162+
m_currentRotation = m_rotLimiter.calculate(rot);
164163

165164

166165
} else {
167166
xSpeedCommanded = xSpeed;
168167
ySpeedCommanded = ySpeed;
169-
currentRotation = rot;
168+
m_currentRotation = rot;
170169
}
171170

172171
// Convert the commanded speeds into the correct units for the drivetrain
173172
double xSpeedDelivered = xSpeedCommanded * DriveConstants.kMaxSpeedMetersPerSecond;
174173
double ySpeedDelivered = ySpeedCommanded * DriveConstants.kMaxSpeedMetersPerSecond;
175-
double rotDelivered = currentRotation * DriveConstants.kMaxAngularSpeed;
174+
double rotDelivered = m_currentRotation * DriveConstants.kMaxAngularSpeed;
176175

177176
var swerveModuleStates = DriveConstants.kDriveKinematics.toSwerveModuleStates(
178177
fieldRelative

0 commit comments

Comments
 (0)