mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-23 01:21:42 +00:00
[wpimath] Deduplicate angle modulus functions (#2998)
frc::NormalizeAngle(), units::math::NormalizeAngle(), and frc::GetModulusError() were replaced with frc::InputModulus() and frc::AngleModulus(). They were placed in wpimath/src/main/native/include/frc/MathUtil.h for C++ and wpimath/src/main/java/edu/wpi/first/wpiutil/math/MathUtil.java for Java.
This commit is contained in:
@@ -751,24 +751,4 @@ auto fma(const UnitTypeLhs x, const UnitMultiply y, const UnitAdd z) noexcept
|
||||
"Unit types are not compatible.");
|
||||
return resultType(std::fma(x(), y(), resultType(z)()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Constrains theta to within the range (-pi, pi].
|
||||
*
|
||||
* @param theta Angle to normalize.
|
||||
*/
|
||||
constexpr units::radian_t NormalizeAngle(units::radian_t theta) {
|
||||
units::radian_t pi(wpi::math::pi);
|
||||
|
||||
// Constrain theta to within (-3pi, pi)
|
||||
const int n_pi_pos = (theta + pi) / 2.0 / pi;
|
||||
theta = theta - units::radian_t{n_pi_pos * 2.0 * wpi::math::pi};
|
||||
|
||||
// Cut off the bottom half of the above range to constrain within
|
||||
// (-pi, pi]
|
||||
const int n_pi_neg = (theta - pi) / 2.0 / pi;
|
||||
theta = theta - units::radian_t{n_pi_neg * 2.0 * wpi::math::pi};
|
||||
|
||||
return theta;
|
||||
}
|
||||
} // namespace units::math
|
||||
|
||||
Reference in New Issue
Block a user