Skip to content

Commit 4161a0a

Browse files
committed
Version 2.1.0: Add acceleration limiting
1 parent aa29f18 commit 4161a0a

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ plugins {
1414
}
1515

1616
group 'frc.team4373'
17-
version '2.0.1'
17+
version '2.1.0'
1818

1919
sourceCompatibility = 1.11
2020

src/main/java/frc/team4373/swerve/SwerveDrivetrain.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,18 @@ public BrakeMode getBrakeMode() {
291291
return this.brakeMode;
292292
}
293293

294+
295+
/**
296+
* Sets the max acceleration for each wheel.
297+
* @param maxAccel the maximum acceleration.
298+
*/
299+
public void setMaxWheelAcceleration(double maxAccel) {
300+
right1.setMaxWheelAcceleration(maxAccel);
301+
right2.setMaxWheelAcceleration(maxAccel);
302+
left1.setMaxWheelAcceleration(maxAccel);
303+
left2.setMaxWheelAcceleration(maxAccel);
304+
}
305+
294306
/**
295307
* Gets the swerve wheel with the specified ID.
296308
* @param wheelID the ID of the wheel to fetch.

src/main/java/frc/team4373/swerve/SwerveWheel.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ class SwerveWheel {
1717

1818
private boolean isInverted = false;
1919

20+
private double maxAcceleration = 2;
21+
22+
private double lastSetSpeed = 0;
23+
2024
/**
2125
* Constructs a new swerve wheel for the specified wheel.
2226
* @param driveMotorConfig the config for the drive motor.
@@ -90,6 +94,16 @@ class SwerveWheel {
9094
*/
9195
void set(double speed, double heading) {
9296

97+
double acceleration = speed - lastSetSpeed;
98+
99+
if (acceleration > maxAcceleration) {
100+
speed = lastSetSpeed + maxAcceleration;
101+
} else if (acceleration < -maxAcceleration) {
102+
speed = lastSetSpeed - maxAcceleration;
103+
}
104+
105+
lastSetSpeed = speed;
106+
93107
heading *= SwerveConstants.DEGREES_TO_ENCODER_UNITS;
94108

95109
double currentRotation = rotatorMotor.getSelectedSensorPosition();
@@ -235,4 +249,8 @@ void resetAbsoluteEncoder() {
235249
double getRotatorMotorVelocity() {
236250
return rotatorMotor.getSelectedSensorVelocity();
237251
}
252+
253+
void setMaxWheelAcceleration(double maxAccel) {
254+
maxAcceleration = maxAccel;
255+
}
238256
}

0 commit comments

Comments
 (0)