Skip to content

Commit 80c5438

Browse files
committed
Address review
1 parent 4450aef commit 80c5438

File tree

6 files changed

+39
-16
lines changed

6 files changed

+39
-16
lines changed

wpilibcExamples/src/main/cpp/examples/SwerveDrivePoseEstimator/cpp/SwerveModule.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ void SwerveModule::SetDesiredState(frc::SwerveModuleState& referenceState) {
5454
// then scale speed by cosine of angle error. This scales down movement
5555
// perpendicular to the desired direction of travel that can occur when
5656
// modules change directions. This results in smoother driving.
57-
// Optimize the reference state to avoid spinning further than 90 degrees
5857
auto state =
5958
referenceState.Optimize(encoderRotation).CosineScale(encoderRotation);
6059

wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/swervebot/Drivetrain.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ public void drive(
6363
}
6464
chassisSpeeds = chassisSpeeds.discretize(period);
6565

66-
var states = SwerveDriveKinematics.desaturateWheelSpeeds(m_kinematics.toWheelSpeeds(chassisSpeeds), kMaxSpeed);
66+
var states =
67+
SwerveDriveKinematics.desaturateWheelSpeeds(
68+
m_kinematics.toWheelSpeeds(chassisSpeeds), kMaxSpeed);
6769

6870
m_frontLeft.setDesiredState(states[0]);
6971
m_frontRight.setDesiredState(states[1]);

wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/swervedriveposeestimator/Drivetrain.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ public void drive(
7474

7575
chassisSpeeds = chassisSpeeds.discretize(period);
7676

77-
var states = SwerveDriveKinematics.desaturateWheelSpeeds(m_kinematics.toWheelSpeeds(chassisSpeeds), kMaxSpeed);
77+
var states =
78+
SwerveDriveKinematics.desaturateWheelSpeeds(
79+
m_kinematics.toWheelSpeeds(chassisSpeeds), kMaxSpeed);
7880

7981
m_frontLeft.setDesiredState(states[0]);
8082
m_frontRight.setDesiredState(states[1]);

wpimath/src/main/java/edu/wpi/first/math/kinematics/SwerveDriveKinematics.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,16 +271,21 @@ public static SwerveModuleState[] desaturateWheelSpeeds(
271271
for (SwerveModuleState moduleState : moduleStates) {
272272
realMaxSpeed = Math.max(realMaxSpeed, Math.abs(moduleState.speed));
273273
}
274+
var states = new SwerveModuleState[moduleStates.length];
274275
if (realMaxSpeed > attainableMaxSpeed) {
275-
var states = new SwerveModuleState[moduleStates.length];
276276
for (int i = 0; i < states.length; i++) {
277277
states[i] =
278278
new SwerveModuleState(
279279
moduleStates[i].speed / realMaxSpeed * attainableMaxSpeed, moduleStates[i].angle);
280280
}
281-
return states;
281+
} else {
282+
// Copy in the event someone wants to mutate the desaturated states but also wants the
283+
// original states
284+
for (int i = 0; i < states.length; i++) {
285+
states[i] = new SwerveModuleState(moduleStates[i].speed, moduleStates[i].angle);
286+
}
282287
}
283-
return moduleStates;
288+
return states;
284289
}
285290

286291
/**
@@ -338,18 +343,23 @@ public static SwerveModuleState[] desaturateWheelSpeeds(
338343
realMaxSpeed = Math.max(realMaxSpeed, Math.abs(moduleState.speed));
339344
}
340345

346+
var states = new SwerveModuleState[moduleStates.length];
341347
if (attainableMaxTranslationalSpeed == 0
342348
|| attainableMaxRotationalVelocity == 0
343349
|| realMaxSpeed == 0) {
344-
return moduleStates;
350+
// Copy in the event someone wants to mutate the desaturated states but also wants the
351+
// original states
352+
for (int i = 0; i < states.length; i++) {
353+
states[i] = new SwerveModuleState(moduleStates[i].speed, moduleStates[i].angle);
354+
}
355+
return states;
345356
}
346357
double translationalK =
347358
Math.hypot(desiredChassisSpeed.vx, desiredChassisSpeed.vy)
348359
/ attainableMaxTranslationalSpeed;
349360
double rotationalK = Math.abs(desiredChassisSpeed.omega) / attainableMaxRotationalVelocity;
350361
double k = Math.max(translationalK, rotationalK);
351362
double scale = Math.min(k * attainableMaxModuleSpeed / realMaxSpeed, 1);
352-
var states = new SwerveModuleState[moduleStates.length];
353363
for (int i = 0; i < states.length; i++) {
354364
states[i] = new SwerveModuleState(moduleStates[i].speed * scale, moduleStates[i].angle);
355365
}

wpimath/src/main/native/include/frc/kinematics/SwerveDriveKinematics.h

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class SwerveDriveKinematics
144144
* @return An array containing the module states. Use caution because these
145145
* module states are not normalized. Sometimes, a user input may cause one of
146146
* the module speeds to go above the attainable max velocity. Use the
147-
* DesaturateWheelSpeeds(wpi::array<SwerveModuleState, NumModules>&,
147+
* DesaturateWheelSpeeds(wpi::array<SwerveModuleState, NumModules>,
148148
* units::meters_per_second_t) function to rectify this issue. In addition,
149149
* you can leverage the power of C++17 to directly assign the module states to
150150
* variables:
@@ -353,15 +353,20 @@ class SwerveDriveKinematics
353353
})
354354
->speed);
355355

356+
wpi::array<SwerveModuleState, NumModules> states(wpi::empty_array);
356357
if (realMaxSpeed > attainableMaxSpeed) {
357-
wpi::array<SwerveModuleState, NumModules> states(wpi::empty_array);
358358
for (size_t i = 0; i < NumModules; ++i) {
359359
states[i] = {moduleStates[i].speed / realMaxSpeed * attainableMaxSpeed,
360360
moduleStates[i].angle};
361361
}
362-
return states;
362+
} else {
363+
// Copy in the event someone wants to mutate the desaturated states but
364+
// also wants the original states
365+
for (size_t i = 0; i < NumModules; ++i) {
366+
states[i] = moduleStates[i];
367+
}
363368
}
364-
return moduleStates;
369+
return states;
365370
}
366371

367372
/**
@@ -404,10 +409,16 @@ class SwerveDriveKinematics
404409
})
405410
->speed);
406411

412+
wpi::array<SwerveModuleState, NumModules> states(wpi::empty_array);
407413
if (attainableMaxRobotTranslationSpeed == 0_mps ||
408414
attainableMaxRobotRotationSpeed == 0_rad_per_s ||
409415
realMaxSpeed == 0_mps) {
410-
return moduleStates;
416+
// Copy in the event someone wants to mutate the desaturated states but
417+
// also wants the original states
418+
for (size_t i = 0; i < NumModules; ++i) {
419+
states[i] = moduleStates[i];
420+
}
421+
return states;
411422
}
412423

413424
auto translationalK =
@@ -421,7 +432,6 @@ class SwerveDriveKinematics
421432

422433
auto scale = units::math::min(k * attainableMaxModuleSpeed / realMaxSpeed,
423434
units::scalar_t{1});
424-
wpi::array<SwerveModuleState, NumModules> states(wpi::empty_array);
425435
for (size_t i = 0; i < NumModules; ++i) {
426436
states[i] = {moduleStates[i].speed * scale, moduleStates[i].angle};
427437
}

wpimath/src/test/java/edu/wpi/first/math/kinematics/SwerveModuleStateTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ void testCosineScale() {
8282
var optimizedD = refD.cosineScale(angleD);
8383

8484
assertAll(
85-
() -> assertEquals(0.0, refD.speed, kEpsilon),
86-
() -> assertEquals(45.0, refD.angle.getDegrees(), kEpsilon));
85+
() -> assertEquals(0.0, optimizedD.speed, kEpsilon),
86+
() -> assertEquals(45.0, optimizedD.angle.getDegrees(), kEpsilon));
8787

8888
var angleE = Rotation2d.fromDegrees(-135.0);
8989
var refE = new SwerveModuleState(2.0, Rotation2d.fromDegrees(45.0));

0 commit comments

Comments
 (0)