|
4 | 4 |
|
5 | 5 | package frc.robot.subsystems; |
6 | 6 |
|
7 | | -import edu.wpi.first.math.filter.SlewRateLimiter; |
8 | 7 | import edu.wpi.first.math.geometry.Pose2d; |
9 | 8 | import edu.wpi.first.math.geometry.Rotation2d; |
10 | 9 | import edu.wpi.first.math.kinematics.ChassisSpeeds; |
11 | 10 | import edu.wpi.first.math.kinematics.SwerveDriveKinematics; |
12 | 11 | import edu.wpi.first.math.kinematics.SwerveDriveOdometry; |
13 | 12 | import edu.wpi.first.math.kinematics.SwerveModulePosition; |
14 | 13 | import edu.wpi.first.math.kinematics.SwerveModuleState; |
15 | | -import edu.wpi.first.util.WPIUtilJNI; |
16 | 14 | import edu.wpi.first.wpilibj.ADIS16470_IMU; |
17 | 15 | import edu.wpi.first.wpilibj.ADIS16470_IMU.IMUAxis; |
18 | 16 | import frc.robot.Constants.DriveConstants; |
19 | | -import frc.utils.SwerveUtils; |
20 | 17 | import edu.wpi.first.wpilibj2.command.SubsystemBase; |
21 | 18 |
|
22 | 19 | public class DriveSubsystem extends SubsystemBase { |
@@ -44,15 +41,6 @@ public class DriveSubsystem extends SubsystemBase { |
44 | 41 | // The gyro sensor |
45 | 42 | private final ADIS16470_IMU m_gyro = new ADIS16470_IMU(); |
46 | 43 |
|
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 | | - |
56 | 44 | // Odometry class for tracking robot pose |
57 | 45 | SwerveDriveOdometry m_odometry = new SwerveDriveOdometry( |
58 | 46 | DriveConstants.kDriveKinematics, |
@@ -115,69 +103,17 @@ public void resetOdometry(Pose2d pose) { |
115 | 103 | * @param rot Angular rate of the robot. |
116 | 104 | * @param fieldRelative Whether the provided x and y speeds are relative to the |
117 | 105 | * field. |
118 | | - * @param rateLimit Whether to enable rate limiting for smoother control. |
119 | 106 | */ |
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) { |
173 | 108 | // 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; |
177 | 112 |
|
178 | 113 | var swerveModuleStates = DriveConstants.kDriveKinematics.toSwerveModuleStates( |
179 | 114 | 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))) |
181 | 117 | : new ChassisSpeeds(xSpeedDelivered, ySpeedDelivered, rotDelivered)); |
182 | 118 | SwerveDriveKinematics.desaturateWheelSpeeds( |
183 | 119 | swerveModuleStates, DriveConstants.kMaxSpeedMetersPerSecond); |
|
0 commit comments