mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-28 02:11:43 +00:00
[wpimath] Fix swerve kinematics util classes equals function (#4907)
Co-authored-by: Tyler Veness <calcmogul@gmail.com>
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#include "frc/kinematics/SwerveModulePosition.h"
|
||||
|
||||
#include "frc/kinematics/SwerveModuleState.h"
|
||||
#include "units/math.h"
|
||||
|
||||
using namespace frc;
|
||||
|
||||
bool SwerveModulePosition::operator==(const SwerveModulePosition& other) const {
|
||||
return units::math::abs(distance - other.distance) < 1E-9_m &&
|
||||
angle == other.angle;
|
||||
}
|
||||
24
wpimath/src/main/native/cpp/kinematics/SwerveModuleState.cpp
Normal file
24
wpimath/src/main/native/cpp/kinematics/SwerveModuleState.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#include "frc/kinematics/SwerveModuleState.h"
|
||||
|
||||
#include "units/math.h"
|
||||
|
||||
using namespace frc;
|
||||
|
||||
bool SwerveModuleState::operator==(const SwerveModuleState& other) const {
|
||||
return units::math::abs(speed - other.speed) < 1E-9_mps &&
|
||||
angle == other.angle;
|
||||
}
|
||||
|
||||
SwerveModuleState SwerveModuleState::Optimize(
|
||||
const SwerveModuleState& desiredState, const Rotation2d& currentAngle) {
|
||||
auto delta = desiredState.angle - currentAngle;
|
||||
if (units::math::abs(delta.Degrees()) > 90_deg) {
|
||||
return {-desiredState.speed, desiredState.angle + Rotation2d{180_deg}};
|
||||
} else {
|
||||
return {desiredState.speed, desiredState.angle};
|
||||
}
|
||||
}
|
||||
@@ -25,5 +25,13 @@ struct WPILIB_DLLEXPORT SwerveModulePosition {
|
||||
* Angle of the module.
|
||||
*/
|
||||
Rotation2d angle;
|
||||
|
||||
/**
|
||||
* Checks equality between this SwerveModulePosition and another object.
|
||||
*
|
||||
* @param other The other object.
|
||||
* @return Whether the two objects are equal.
|
||||
*/
|
||||
bool operator==(const SwerveModulePosition& other) const;
|
||||
};
|
||||
} // namespace frc
|
||||
|
||||
@@ -26,6 +26,14 @@ struct WPILIB_DLLEXPORT SwerveModuleState {
|
||||
*/
|
||||
Rotation2d angle;
|
||||
|
||||
/**
|
||||
* Checks equality between this SwerveModuleState and another object.
|
||||
*
|
||||
* @param other The other object.
|
||||
* @return Whether the two objects are equal.
|
||||
*/
|
||||
bool operator==(const SwerveModuleState& other) const;
|
||||
|
||||
/**
|
||||
* Minimize the change in heading the desired swerve module state would
|
||||
* require by potentially reversing the direction the wheel spins. If this is
|
||||
@@ -36,13 +44,6 @@ struct WPILIB_DLLEXPORT SwerveModuleState {
|
||||
* @param currentAngle The current module angle.
|
||||
*/
|
||||
static SwerveModuleState Optimize(const SwerveModuleState& desiredState,
|
||||
const Rotation2d& currentAngle) {
|
||||
auto delta = desiredState.angle - currentAngle;
|
||||
if (units::math::abs(delta.Degrees()) > 90_deg) {
|
||||
return {-desiredState.speed, desiredState.angle + Rotation2d{180_deg}};
|
||||
} else {
|
||||
return {desiredState.speed, desiredState.angle};
|
||||
}
|
||||
}
|
||||
const Rotation2d& currentAngle);
|
||||
};
|
||||
} // namespace frc
|
||||
|
||||
Reference in New Issue
Block a user