[wpimath] Revert Rotation2D change that limits angles (#4781)

Reverts "[wpimath] Constrain Rotation2d range to -pi to pi (#4611)"
This reverts commit d1d458db2b.

This broke multiple teams code in beta. It is also easier to limit the angle externally, then reconstruct a larger angle that got limited. This additionally adds comments to clarify the behavior and retains tests that were added in the reverted commit, and fixes a javadoc comment angle reference.
This commit is contained in:
sciencewhiz
2022-12-08 18:10:44 -08:00
committed by GitHub
parent 0f5b08ec69
commit 989c9fb29a
7 changed files with 54 additions and 56 deletions

View File

@@ -17,6 +17,11 @@ namespace frc {
/**
* A rotation in a 2D coordinate frame represented by a point on the unit circle
* (cosine and sine).
*
* The angle is continuous, that is if a Rotation2d is constructed with 361
* degrees, it will return 361 degrees. This allows algorithms that wouldn't
* want to see a discontinuity in the rotations as it sweeps past from 360 to 0
* on the second time around.
*/
class WPILIB_DLLEXPORT Rotation2d {
public:
@@ -124,18 +129,18 @@ class WPILIB_DLLEXPORT Rotation2d {
constexpr Rotation2d RotateBy(const Rotation2d& other) const;
/**
* Returns the radian value of the rotation within (-pi, pi].
* Returns the radian value of the rotation.
*
* @return The radian value of the rotation.
*/
constexpr units::radian_t Radians() const;
constexpr units::radian_t Radians() const { return m_value; }
/**
* Returns the degree value of the rotation within (-180, 180].
* Returns the degree value of the rotation.
*
* @return The degree value of the rotation.
*/
constexpr units::degree_t Degrees() const;
constexpr units::degree_t Degrees() const { return m_value; }
/**
* Returns the cosine of the rotation.
@@ -159,6 +164,7 @@ class WPILIB_DLLEXPORT Rotation2d {
constexpr double Tan() const { return Sin() / Cos(); }
private:
units::radian_t m_value = 0_rad;
double m_cos = 1;
double m_sin = 0;
};

View File

@@ -13,10 +13,11 @@
namespace frc {
constexpr Rotation2d::Rotation2d(units::radian_t value)
: Rotation2d(std::is_constant_evaluated() ? gcem::cos(value.to<double>())
: std::cos(value.to<double>()),
std::is_constant_evaluated() ? gcem::sin(value.to<double>())
: std::sin(value.to<double>())) {}
: m_value(value),
m_cos(std::is_constant_evaluated() ? gcem::cos(value.to<double>())
: std::cos(value.to<double>())),
m_sin(std::is_constant_evaluated() ? gcem::sin(value.to<double>())
: std::sin(value.to<double>())) {}
constexpr Rotation2d::Rotation2d(units::degree_t value)
: Rotation2d(units::radian_t{value}) {}
@@ -30,14 +31,17 @@ constexpr Rotation2d::Rotation2d(double x, double y) {
m_sin = 0.0;
m_cos = 1.0;
}
m_value =
units::radian_t{std::is_constant_evaluated() ? gcem::atan2(m_sin, m_cos)
: std::atan2(m_sin, m_cos)};
}
constexpr Rotation2d Rotation2d::operator-() const {
return Rotation2d{m_cos, -m_sin};
return Rotation2d{-m_value};
}
constexpr Rotation2d Rotation2d::operator*(double scalar) const {
return Rotation2d(Radians() * scalar);
return Rotation2d(m_value * scalar);
}
constexpr Rotation2d Rotation2d::operator+(const Rotation2d& other) const {
@@ -63,14 +67,4 @@ constexpr Rotation2d Rotation2d::RotateBy(const Rotation2d& other) const {
Cos() * other.Sin() + Sin() * other.Cos()};
}
constexpr units::radian_t Rotation2d::Radians() const {
return units::radian_t{std::is_constant_evaluated()
? gcem::atan2(m_sin, m_cos)
: std::atan2(m_sin, m_cos)};
}
constexpr units::degree_t Rotation2d::Degrees() const {
return Radians();
}
} // namespace frc