[wpiutil] Add angle normalization method

This commit is contained in:
Prateek Machiraju
2020-07-13 15:53:16 -04:00
committed by Peter Johnson
parent 399684a58f
commit af588adce5
7 changed files with 128 additions and 30 deletions

View File

@@ -0,0 +1,22 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2020 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpiutil.math;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class MathUtilTest {
@Test
void testAngleNormalize() {
assertEquals(MathUtil.normalizeAngle(5 * Math.PI), Math.PI);
assertEquals(MathUtil.normalizeAngle(-5 * Math.PI), Math.PI);
assertEquals(MathUtil.normalizeAngle(Math.PI / 2), Math.PI / 2);
assertEquals(MathUtil.normalizeAngle(-Math.PI / 2), -Math.PI / 2);
}
}

View File

@@ -2979,11 +2979,15 @@ TEST_F(UnitMath, abs) {
EXPECT_EQ(meter_t(10.0), abs(meter_t(10.0)));
}
TEST_F(UnitMath, fma) {
meter_t x(2.0);
meter_t y(3.0);
square_meter_t z(1.0);
EXPECT_EQ(square_meter_t(7.0), fma(x, y, z));
TEST_F(UnitMath, normalize) {
EXPECT_EQ(NormalizeAngle(radian_t(5 * wpi::math::pi)),
radian_t(wpi::math::pi));
EXPECT_EQ(NormalizeAngle(radian_t(-5 * wpi::math::pi)),
radian_t(wpi::math::pi));
EXPECT_EQ(NormalizeAngle(radian_t(wpi::math::pi / 2)),
radian_t(wpi::math::pi / 2));
EXPECT_EQ(NormalizeAngle(radian_t(-wpi::math::pi / 2)),
radian_t(-wpi::math::pi / 2));
}
// Constexpr