Skip to content

Commit 9b98109

Browse files
committed
Working rate limiting solution
1 parent 05c308f commit 9b98109

3 files changed

Lines changed: 99 additions & 6 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ public static final class ModuleConstants {
114114

115115
public static final class OIConstants {
116116
public static final int kDriverControllerPort = 0;
117+
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%)
117122
}
118123

119124
public static final class AutoConstants {

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

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

55
package frc.robot;
66

7-
import edu.wpi.first.math.MathUtil;
87
import edu.wpi.first.math.controller.PIDController;
98
import edu.wpi.first.math.controller.ProfiledPIDController;
109
import edu.wpi.first.math.geometry.Pose2d;
@@ -51,10 +50,10 @@ public RobotContainer() {
5150
// Turning is controlled by the X axis of the right stick.
5251
new RunCommand(
5352
() -> m_robotDrive.drive(
54-
MathUtil.applyDeadband(-m_driverController.getLeftY(), 0.06),
55-
MathUtil.applyDeadband(-m_driverController.getLeftX(), 0.06),
56-
MathUtil.applyDeadband(-m_driverController.getRightX(), 0.06),
57-
true),
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()),
56+
true, true),
5857
m_robotDrive));
5958
}
6059

@@ -117,6 +116,6 @@ public Command getAutonomousCommand() {
117116
m_robotDrive.resetOdometry(exampleTrajectory.getInitialPose());
118117

119118
// Run path following command, then stop at the end.
120-
return swerveControllerCommand.andThen(() -> m_robotDrive.drive(0, 0, 0, false));
119+
return swerveControllerCommand.andThen(() -> m_robotDrive.drive(0, 0, 0, false, false));
121120
}
122121
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package frc.utils;
2+
3+
public class SwerveUtils {
4+
5+
/**
6+
* Steps a value towards a target with a specified step size.
7+
* @param _current The current or starting value. Can be positive or negative.
8+
* @param _target The target value the algorithm will step towards. Can be positive or negative.
9+
* @param _stepsize The maximum step size that can be taken.
10+
* @return The new value for {@code _current} after performing the specified step towards the specified target.
11+
*/
12+
public static double StepTowards(double _current, double _target, double _stepsize) {
13+
if (Math.abs(_current - _target) <= _stepsize) {
14+
return _target;
15+
}
16+
else if (_target < _current) {
17+
return _current - _stepsize;
18+
}
19+
else {
20+
return _current + _stepsize;
21+
}
22+
}
23+
24+
/**
25+
* Steps a value (angle) towards a target (angle) taking the shortest path with a specified step size.
26+
* @param _current The current or starting angle (in radians). Can lie outside the 0 to 2*PI range.
27+
* @param _target The target angle (in radians) the algorithm will step towards. Can lie outside the 0 to 2*PI range.
28+
* @param _stepsize The maximum step size that can be taken (in radians).
29+
* @return The new angle (in radians) for {@code _current} after performing the specified step towards the specified target.
30+
* This value will always lie in the range 0 to 2*PI (exclusive).
31+
*/
32+
public static double StepTowardsCircular(double _current, double _target, double _stepsize) {
33+
_current = WrapAngle(_current);
34+
_target = WrapAngle(_target);
35+
36+
double stepDirection = Math.signum(_target - _current);
37+
double difference = Math.abs(_current - _target);
38+
39+
if (difference <= _stepsize) {
40+
return _target;
41+
}
42+
else if (difference > Math.PI) { //does the system need to wrap over eventually?
43+
//handle the special case where you can reach the target in one step while also wrapping
44+
if (_current + 2*Math.PI - _target < _stepsize || _target + 2*Math.PI - _current < _stepsize) {
45+
return _target;
46+
}
47+
else {
48+
return WrapAngle(_current - stepDirection * _stepsize); //this will handle wrapping gracefully
49+
}
50+
51+
}
52+
else {
53+
return _current + stepDirection * _stepsize;
54+
}
55+
}
56+
57+
/**
58+
* Finds the (unsigned) minimum difference between two angles including calculating across 0.
59+
* @param _angleA An angle (in radians).
60+
* @param _angleB An angle (in radians).
61+
* @return The (unsigned) minimum difference between the two angles (in radians).
62+
*/
63+
public static double AngleDifference(double _angleA, double _angleB) {
64+
double difference = Math.abs(_angleA - _angleB);
65+
return difference > Math.PI? (2 * Math.PI) - difference : difference;
66+
}
67+
68+
/**
69+
* Wraps an angle until it lies within the range from 0 to 2*PI (exclusive).
70+
* @param _angle The angle (in radians) to wrap. Can be positive or negative and can lie multiple wraps outside the output range.
71+
* @return An angle (in radians) from 0 and 2*PI (exclusive).
72+
*/
73+
public static double WrapAngle(double _angle) {
74+
double twoPi = 2*Math.PI;
75+
76+
if (_angle == twoPi) { // Handle this case separately to avoid floating point errors with the floor after the division in the case below
77+
return 0.0;
78+
}
79+
else if (_angle > twoPi) {
80+
return _angle - twoPi*Math.floor(_angle / twoPi);
81+
}
82+
else if (_angle < 0.0) {
83+
return _angle + twoPi*(Math.floor((-_angle) / twoPi)+1);
84+
}
85+
else {
86+
return _angle;
87+
}
88+
}
89+
}

0 commit comments

Comments
 (0)