Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 1 | #include "motors/math.h" |
| 2 | |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 3 | #include "gmock/gmock.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 4 | #include "gtest/gtest.h" |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 5 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame^] | 6 | namespace frc971::motors::testing { |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 7 | |
| 8 | class SinCosIntTest : public ::testing::Test { |
| 9 | public: |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 10 | void SetUp() override { MathInit(); } |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 11 | |
Brian Silverman | 9ccec6e | 2018-10-21 22:06:35 -0700 | [diff] [blame] | 12 | template <class Rotation, int kTableSize> |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 13 | void CheckSinCos() { |
| 14 | static constexpr float kTolerance = 0.004; |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 15 | SCOPED_TRACE("num=" + ::std::to_string(Rotation::num) + |
| 16 | " den=" + ::std::to_string(Rotation::den) + |
| 17 | " table_size=" + ::std::to_string(kTableSize)); |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 18 | for (uint32_t theta = 0; theta < Rotation::den; ++theta) { |
Brian Silverman | 9ccec6e | 2018-10-21 22:06:35 -0700 | [diff] [blame] | 19 | const float theta_float = ThetaToFloat<Rotation>(theta); |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 20 | SCOPED_TRACE("theta=" + ::std::to_string(theta) + |
| 21 | " theta_float=" + ::std::to_string(theta_float)); |
Brian Silverman | 9ccec6e | 2018-10-21 22:06:35 -0700 | [diff] [blame] | 22 | EXPECT_THAT((FastSinInt<Rotation, kTableSize>(theta)), |
| 23 | ::testing::FloatNear(sin(theta_float), kTolerance)); |
| 24 | EXPECT_THAT((FastCosInt<Rotation, kTableSize>(theta)), |
| 25 | ::testing::FloatNear(cos(theta_float), kTolerance)); |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 26 | } |
| 27 | } |
| 28 | |
| 29 | private: |
| 30 | template <class Rotation> |
| 31 | double ThetaToFloat(uint32_t theta) { |
| 32 | const double rotation_double = |
| 33 | static_cast<double>(Rotation::num) / static_cast<double>(Rotation::den); |
| 34 | return (static_cast<double>(theta) + 0.5) * rotation_double * 2.0 * M_PI; |
| 35 | } |
| 36 | }; |
| 37 | |
Brian Silverman | 9ccec6e | 2018-10-21 22:06:35 -0700 | [diff] [blame] | 38 | TEST_F(SinCosIntTest, Numerator1DefaultTable) { |
| 39 | CheckSinCos<::std::ratio<1, 2>, 2>(); |
| 40 | CheckSinCos<::std::ratio<1, 4>, 4>(); |
| 41 | CheckSinCos<::std::ratio<1, 8>, 8>(); |
| 42 | CheckSinCos<::std::ratio<1, 128>, 128>(); |
| 43 | CheckSinCos<::std::ratio<1, 1024>, 1024>(); |
| 44 | CheckSinCos<::std::ratio<1, 4096>, 4096>(); |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Brian Silverman | 9ccec6e | 2018-10-21 22:06:35 -0700 | [diff] [blame] | 47 | TEST_F(SinCosIntTest, Numerator1LargerTable) { |
| 48 | // Don't include silly things like a denominator of 2 and table size of 4 |
| 49 | // here. It will fail because the slope varies so much that linearly |
| 50 | // interpolating between 0.5pi/1.5pi vs 0.25pi/0.75pi/1.25pi/1.75pi gives very |
| 51 | // different results. |
| 52 | CheckSinCos<::std::ratio<1, 2>, 4096>(); |
| 53 | CheckSinCos<::std::ratio<1, 2>, 1024>(); |
| 54 | CheckSinCos<::std::ratio<1, 8>, 4096>(); |
| 55 | CheckSinCos<::std::ratio<1, 128>, 4096>(); |
| 56 | CheckSinCos<::std::ratio<1, 1024>, 2048>(); |
| 57 | } |
| 58 | |
| 59 | TEST_F(SinCosIntTest, Numerator5DefaultTable) { |
| 60 | CheckSinCos<::std::ratio<5, 8>, 8>(); |
| 61 | CheckSinCos<::std::ratio<5, 128>, 128>(); |
| 62 | CheckSinCos<::std::ratio<5, 1024>, 1024>(); |
| 63 | CheckSinCos<::std::ratio<5, 4096>, 4096>(); |
| 64 | } |
| 65 | |
| 66 | TEST_F(SinCosIntTest, Numerator5LargerTable) { |
| 67 | CheckSinCos<::std::ratio<5, 2>, 4096>(); |
| 68 | CheckSinCos<::std::ratio<5, 2>, 1024>(); |
| 69 | CheckSinCos<::std::ratio<5, 8>, 4096>(); |
| 70 | CheckSinCos<::std::ratio<5, 128>, 4096>(); |
| 71 | CheckSinCos<::std::ratio<5, 1024>, 2048>(); |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | class SinCosFloatTest : public ::testing::Test { |
| 75 | public: |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 76 | void SetUp() override { MathInit(); } |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 77 | |
| 78 | void CheckSinCos(float theta) { |
| 79 | ASSERT_GE(theta, -0.2f); |
| 80 | ASSERT_LE(theta, 0.2f); |
| 81 | |
| 82 | static constexpr float kTolerance = 0.002; |
| 83 | EXPECT_THAT(FastSinFloat(theta), |
| 84 | ::testing::FloatNear(sin(theta), kTolerance)); |
| 85 | EXPECT_THAT(FastCosFloat(theta), |
| 86 | ::testing::FloatNear(cos(theta), kTolerance)); |
| 87 | } |
| 88 | }; |
| 89 | |
| 90 | TEST_F(SinCosFloatTest, Endpoints) { |
| 91 | CheckSinCos(0); |
| 92 | CheckSinCos(-0.2); |
| 93 | CheckSinCos(0.2); |
| 94 | } |
| 95 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame^] | 96 | } // namespace frc971::motors::testing |