Skip to content

Commit ad9f932

Browse files
committed
Parity with other wheel speed classes
1 parent 8e91227 commit ad9f932

File tree

6 files changed

+25
-15
lines changed

6 files changed

+25
-15
lines changed

wpimath/src/main/java/org/wpilib/math/kinematics/DifferentialDriveWheelVelocities.java

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

77
import static org.wpilib.units.Units.MetersPerSecond;
88

9+
import org.wpilib.annotation.NoDiscard;
910
import org.wpilib.math.interpolation.Interpolatable;
1011
import org.wpilib.math.kinematics.proto.DifferentialDriveWheelVelocitiesProto;
1112
import org.wpilib.math.kinematics.struct.DifferentialDriveWheelVelocitiesStruct;
@@ -14,6 +15,7 @@
1415
import org.wpilib.util.struct.StructSerializable;
1516

1617
/** Represents the wheel velocities for a differential drive drivetrain. */
18+
@NoDiscard
1719
public class DifferentialDriveWheelVelocities
1820
implements Interpolatable<DifferentialDriveWheelVelocities>,
1921
ProtobufSerializable,
@@ -66,14 +68,17 @@ public DifferentialDriveWheelVelocities(LinearVelocity left, LinearVelocity righ
6668
*
6769
* @param attainableMaxVelocity The absolute max velocity in meters per second that a wheel can
6870
* reach.
71+
* @return The desaturated DifferentialDriveWheelVelocities.
6972
*/
70-
public void desaturate(double attainableMaxVelocity) {
73+
public DifferentialDriveWheelVelocities desaturate(double attainableMaxVelocity) {
7174
double realMaxVelocity = Math.max(Math.abs(left), Math.abs(right));
7275

7376
if (realMaxVelocity > attainableMaxVelocity) {
74-
left = left / realMaxVelocity * attainableMaxVelocity;
75-
right = right / realMaxVelocity * attainableMaxVelocity;
77+
return new DifferentialDriveWheelVelocities(
78+
left / realMaxVelocity * attainableMaxVelocity,
79+
right / realMaxVelocity * attainableMaxVelocity);
7680
}
81+
return new DifferentialDriveWheelVelocities(left, right);
7782
}
7883

7984
/**
@@ -86,9 +91,10 @@ public void desaturate(double attainableMaxVelocity) {
8691
*
8792
* @param attainableMaxVelocity The absolute max velocity in meters per second that a wheel can
8893
* reach.
94+
* @return The desaturated DifferentialDriveWheelVelocities.
8995
*/
90-
public void desaturate(LinearVelocity attainableMaxVelocity) {
91-
desaturate(attainableMaxVelocity.in(MetersPerSecond));
96+
public DifferentialDriveWheelVelocities desaturate(LinearVelocity attainableMaxVelocity) {
97+
return desaturate(attainableMaxVelocity.in(MetersPerSecond));
9298
}
9399

94100
/**

wpimath/src/main/java/org/wpilib/math/kinematics/MecanumDriveWheelVelocities.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import static org.wpilib.units.Units.MetersPerSecond;
88

9+
import org.wpilib.annotation.NoDiscard;
910
import org.wpilib.math.interpolation.Interpolatable;
1011
import org.wpilib.math.kinematics.proto.MecanumDriveWheelVelocitiesProto;
1112
import org.wpilib.math.kinematics.struct.MecanumDriveWheelVelocitiesStruct;
@@ -14,6 +15,7 @@
1415
import org.wpilib.util.struct.StructSerializable;
1516

1617
/** Represents the wheel velocities for a mecanum drive drivetrain. */
18+
@NoDiscard
1719
public class MecanumDriveWheelVelocities
1820
implements Interpolatable<MecanumDriveWheelVelocities>,
1921
ProtobufSerializable,
@@ -87,7 +89,7 @@ public MecanumDriveWheelVelocities(
8789
*
8890
* @param attainableMaxVelocity The absolute max velocity in meters per second that a wheel can
8991
* reach.
90-
* @return Desaturated MecanumDriveWheelVelocities.
92+
* @return The desaturated MecanumDriveWheelVelocities.
9193
*/
9294
public MecanumDriveWheelVelocities desaturate(double attainableMaxVelocity) {
9395
double realMaxVelocity = Math.max(Math.abs(frontLeft), Math.abs(frontRight));

wpimath/src/main/java/org/wpilib/math/trajectory/constraint/DifferentialDriveKinematicsConstraint.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ public double getMaxVelocity(Pose2d pose, double curvature, double velocity) {
4444
var chassisVelocities = new ChassisVelocities(velocity, 0, velocity * curvature);
4545

4646
// Get the wheel velocities and normalize them to within the max velocity.
47-
var wheelVelocities = m_kinematics.toWheelVelocities(chassisVelocities);
48-
wheelVelocities.desaturate(m_maxVelocity);
47+
var wheelVelocities =
48+
m_kinematics.toWheelVelocities(chassisVelocities).desaturate(m_maxVelocity);
4949

5050
// Return the new linear chassis velocity.
5151
return m_kinematics.toChassisVelocities(wheelVelocities).vx;

wpimath/src/main/native/include/wpi/math/kinematics/DifferentialDriveWheelVelocities.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,18 @@ struct WPILIB_DLLEXPORT DifferentialDriveWheelVelocities {
3636
*
3737
* @param attainableMaxVelocity The absolute max velocity that a wheel can
3838
* reach.
39+
* @return The desaturated DifferentialDriveWheelVelocities.
3940
*/
40-
constexpr void Desaturate(
41+
constexpr DifferentialDriveWheelVelocities Desaturate(
4142
wpi::units::meters_per_second_t attainableMaxVelocity) {
4243
auto realMaxVelocity = wpi::units::math::max(wpi::units::math::abs(left),
4344
wpi::units::math::abs(right));
4445

4546
if (realMaxVelocity > attainableMaxVelocity) {
46-
left = left / realMaxVelocity * attainableMaxVelocity;
47-
right = right / realMaxVelocity * attainableMaxVelocity;
47+
return {left / realMaxVelocity * attainableMaxVelocity,
48+
right / realMaxVelocity * attainableMaxVelocity};
4849
}
50+
return *this;
4951
}
5052

5153
/**

wpimath/src/main/native/include/wpi/math/trajectory/constraint/DifferentialDriveKinematicsConstraint.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class WPILIB_DLLEXPORT DifferentialDriveKinematicsConstraint
3030
const Pose2d& pose, wpi::units::curvature_t curvature,
3131
wpi::units::meters_per_second_t velocity) const override {
3232
auto wheelVelocities =
33-
m_kinematics.ToWheelVelocities({velocity, 0_mps, velocity * curvature});
34-
wheelVelocities.Desaturate(m_maxVelocity);
33+
m_kinematics.ToWheelVelocities({velocity, 0_mps, velocity * curvature})
34+
.Desaturate(m_maxVelocity);
3535

3636
return m_kinematics.ToChassisVelocities(wheelVelocities).vx;
3737
}

wpimath/src/main/native/include/wpi/math/trajectory/constraint/SwerveDriveKinematicsConstraint.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ class SwerveDriveKinematicsConstraint : public TrajectoryConstraint {
3232
auto wheelVelocities = m_kinematics.ToSwerveModuleVelocities(
3333
{xVelocity, yVelocity, velocity * curvature});
3434

35-
auto normSpeeds = m_kinematics.ToChassisVelocities(
35+
auto normVelocities = m_kinematics.ToChassisVelocities(
3636
m_kinematics.DesaturateWheelVelocities(wheelVelocities, m_maxVelocity));
3737

38-
return wpi::units::math::hypot(normSpeeds.vx, normSpeeds.vy);
38+
return wpi::units::math::hypot(normVelocities.vx, normVelocities.vy);
3939
}
4040

4141
MinMax MinMaxAcceleration(

0 commit comments

Comments
 (0)