[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

@@ -13,6 +13,15 @@ import org.junit.jupiter.api.Test;
class Rotation2dTest {
private static final double kEpsilon = 1E-9;
@Test
void testInScope() {
var rot1 = Rotation2d.fromRadians(Math.PI * 5 / 2);
var rot2 = Rotation2d.fromRadians(Math.PI * 7 / 2);
assertEquals(Math.PI / 2, rot1.getRadians(), kEpsilon);
assertEquals(-Math.PI / 2, rot2.getRadians(), kEpsilon);
}
@Test
void testRadiansToDegrees() {
var rot1 = Rotation2d.fromRadians(Math.PI / 3);
@@ -59,6 +68,21 @@ class Rotation2dTest {
assertEquals(40.0, rot1.minus(rot2).getDegrees(), kEpsilon);
}
@Test
void testUnaryMinus() {
var rot = Rotation2d.fromDegrees(20.0);
assertEquals(-20.0, rot.unaryMinus().getDegrees(), kEpsilon);
}
@Test
void testMultiply() {
var rot = Rotation2d.fromDegrees(10.0);
assertEquals(30.0, rot.times(3.0).getDegrees(), kEpsilon);
assertEquals(50.0, rot.times(41.0).getDegrees(), kEpsilon);
}
@Test
void testEquality() {
var rot1 = Rotation2d.fromDegrees(43.0);

View File

@@ -10,6 +10,16 @@
using namespace frc;
TEST(Rotation2dTest, InScope) {
const auto rot1 = Rotation2d{units::radian_t{std::numbers::pi * 5 / 2}};
const auto rot2 = Rotation2d{units::radian_t{std::numbers::pi * 7 / 2}};
const auto rot3 = Rotation2d{270_deg};
EXPECT_DOUBLE_EQ(std::numbers::pi / 2, rot1.Radians().value());
EXPECT_DOUBLE_EQ(-std::numbers::pi / 2, rot2.Radians().value());
EXPECT_DOUBLE_EQ(-90, rot3.Degrees().value());
}
TEST(Rotation2dTest, RadiansToDegrees) {
const Rotation2d rot1{units::radian_t{std::numbers::pi / 3.0}};
const Rotation2d rot2{units::radian_t{std::numbers::pi / 4.0}};
@@ -66,8 +76,8 @@ TEST(Rotation2dTest, Inequality) {
TEST(Rotation2dTest, Constexpr) {
constexpr Rotation2d defaultCtor;
constexpr Rotation2d radianCtor{5_rad};
constexpr Rotation2d degreeCtor{270_deg};
constexpr Rotation2d radianCtor{0.25_rad};
constexpr Rotation2d degreeCtor{-90_deg};
constexpr Rotation2d rotation45{45_deg};
constexpr Rotation2d cartesianCtor{3.5, -3.5};
@@ -76,9 +86,9 @@ TEST(Rotation2dTest, Constexpr) {
constexpr auto subtracted = cartesianCtor - degreeCtor;
static_assert(defaultCtor.Radians() == 0_rad);
static_assert(degreeCtor.Degrees() == 270_deg);
static_assert(negated.Radians() == (-5_rad));
static_assert(multiplied.Radians() == 10_rad);
static_assert(degreeCtor.Degrees() == -90_deg);
static_assert(negated.Radians() == -0.25_rad);
static_assert(multiplied.Radians() == 0.5_rad);
static_assert(subtracted == rotation45);
static_assert(radianCtor != degreeCtor);
}