[wpimath] Constrain Rotation2d range to -pi to pi (#4611)

Co-authored-by: Ryan Blue <ryanzblue@gmail.com>
This commit is contained in:
ohowe
2022-11-14 15:25:15 -07:00
committed by GitHub
parent f656e99245
commit d1d458db2b
7 changed files with 69 additions and 43 deletions

View File

@@ -132,18 +132,18 @@ class WPILIB_DLLEXPORT Rotation2d {
constexpr Rotation2d RotateBy(const Rotation2d& other) const;
/**
* Returns the radian value of the rotation.
* Returns the radian value of the rotation within (-pi, pi].
*
* @return The radian value of the rotation.
*/
constexpr units::radian_t Radians() const { return m_value; }
constexpr units::radian_t Radians() const;
/**
* Returns the degree value of the rotation.
* Returns the degree value of the rotation within (-180, 180].
*
* @return The degree value of the rotation.
*/
constexpr units::degree_t Degrees() const { return m_value; }
constexpr units::degree_t Degrees() const;
/**
* Returns the cosine of the rotation.
@@ -167,7 +167,6 @@ 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,11 +13,10 @@
namespace frc {
constexpr Rotation2d::Rotation2d(units::radian_t value)
: 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>())) {}
: 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>())) {}
constexpr Rotation2d::Rotation2d(units::degree_t value)
: Rotation2d(units::radian_t{value}) {}
@@ -31,17 +30,14 @@ 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_value};
return Rotation2d{m_cos, -m_sin};
}
constexpr Rotation2d Rotation2d::operator*(double scalar) const {
return Rotation2d(m_value * scalar);
return Rotation2d(Radians() * scalar);
}
constexpr Rotation2d Rotation2d::operator+(const Rotation2d& other) const {
@@ -71,4 +67,14 @@ 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