James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame^] | 1 | #include "frc971/control_loops/polytope.h" |
Brian Silverman | aba7bf6 | 2016-01-31 18:03:59 -0500 | [diff] [blame] | 2 | |
| 3 | #include <vector> |
| 4 | |
| 5 | #include "Eigen/Dense" |
| 6 | #include "gtest/gtest.h" |
| 7 | #include "gmock/gmock.h" |
| 8 | |
| 9 | #include "aos/testing/test_logging.h" |
| 10 | |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame^] | 11 | namespace frc971 { |
Brian Silverman | aba7bf6 | 2016-01-31 18:03:59 -0500 | [diff] [blame] | 12 | namespace controls { |
| 13 | |
| 14 | class HPolytopeTest : public ::testing::Test { |
| 15 | protected: |
| 16 | HPolytope<2> Polytope1() { |
| 17 | return HPolytope<2>{ |
| 18 | (Eigen::Matrix<double, 4, 2>() << 1, 0, -1, 0, 0, 1, 0, -1).finished(), |
| 19 | (Eigen::Matrix<double, 4, 1>() << 12.0, 12.0, 12.0, 12.0).finished()}; |
| 20 | } |
| 21 | HPolytope<2> Polytope2() { |
| 22 | return HPolytope<2>{ |
| 23 | (Eigen::Matrix<double, 4, 2>() << 1, 1, -1, -1, 0, 1, 0, -1).finished(), |
| 24 | (Eigen::Matrix<double, 4, 1>() << 12.0, 12.0, 12.0, 12.0).finished()}; |
| 25 | } |
| 26 | HPolytope<1> Polytope3() { |
| 27 | return HPolytope<1>{(Eigen::Matrix<double, 2, 1>() << 1, -0.5).finished(), |
| 28 | (Eigen::Matrix<double, 2, 1>() << 5.0, 2.0).finished()}; |
| 29 | } |
| 30 | HPolytope<2> Polytope4() { |
| 31 | return HPolytope<2>{ |
| 32 | (Eigen::Matrix<double, 4, 2>() << 1, 1, -1, -1, 1, -1, -1, 1) |
| 33 | .finished(), |
| 34 | (Eigen::Matrix<double, 4, 1>() << 2, -1, 2, -1).finished()}; |
| 35 | } |
| 36 | HPolytope<2> Polytope5() { |
| 37 | return HPolytope<2>{ |
| 38 | (Eigen::Matrix<double, 4, 2>() << 1, 1, -1, -1, 1, -1, -1, 1) |
| 39 | .finished(), |
| 40 | (Eigen::Matrix<double, 4, 1>() << 1.5, -0.5, 1.5, -0.5).finished()}; |
| 41 | } |
| 42 | |
| 43 | void SetUp() override { |
| 44 | ::aos::testing::EnableTestLogging(); |
| 45 | ::aos::testing::ForcePrintLogsDuringTests(); |
| 46 | HPolytope<0>::Init(); |
| 47 | } |
| 48 | |
| 49 | template <typename T> |
| 50 | ::std::vector<::std::vector<double>> MatrixToVectors(const T &matrix) { |
| 51 | ::std::vector<::std::vector<double>> r; |
| 52 | for (int i = 0; i < matrix.cols(); ++i) { |
| 53 | ::std::vector<double> col; |
| 54 | for (int j = 0; j < matrix.rows(); ++j) { |
| 55 | col.emplace_back(matrix(j, i)); |
| 56 | } |
| 57 | r.emplace_back(col); |
| 58 | } |
| 59 | return r; |
| 60 | } |
| 61 | |
| 62 | template <typename T> |
| 63 | ::std::vector<::testing::Matcher<::std::vector<double>>> MatrixToMatchers( |
| 64 | const T &matrix) { |
| 65 | ::std::vector<::testing::Matcher<::std::vector<double>>> r; |
| 66 | for (int i = 0; i < matrix.cols(); ++i) { |
| 67 | ::std::vector<::testing::Matcher<double>> col; |
| 68 | for (int j = 0; j < matrix.rows(); ++j) { |
| 69 | col.emplace_back(::testing::DoubleNear(matrix(j, i), 0.000001)); |
| 70 | } |
| 71 | r.emplace_back(::testing::ElementsAreArray(col)); |
| 72 | } |
| 73 | return r; |
| 74 | } |
Brian Silverman | aba7bf6 | 2016-01-31 18:03:59 -0500 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | // Tests that the vertices for various polytopes calculated from H and k are |
| 78 | // correct. |
| 79 | TEST_F(HPolytopeTest, CalculatedVertices) { |
| 80 | EXPECT_THAT(MatrixToVectors(Polytope1().Vertices()), |
| 81 | ::testing::UnorderedElementsAreArray( |
| 82 | MatrixToVectors((Eigen::Matrix<double, 2, 4>() << -12, -12, |
| 83 | 12, 12, -12, 12, 12, -12).finished()))); |
| 84 | EXPECT_THAT(MatrixToVectors(Polytope2().Vertices()), |
| 85 | ::testing::UnorderedElementsAreArray( |
| 86 | MatrixToVectors((Eigen::Matrix<double, 2, 4>() << 24, 0, -24, |
| 87 | 0, -12, 12, 12, -12).finished()))); |
| 88 | EXPECT_THAT(MatrixToVectors(Polytope3().Vertices()), |
| 89 | ::testing::UnorderedElementsAreArray(MatrixToVectors( |
| 90 | (Eigen::Matrix<double, 1, 2>() << 5, -4).finished()))); |
| 91 | EXPECT_THAT(MatrixToVectors(Polytope4().Vertices()), |
| 92 | ::testing::UnorderedElementsAreArray( |
| 93 | MatrixToVectors((Eigen::Matrix<double, 2, 4>() << 1, 1.5, 1.5, |
| 94 | 2, 0, -0.5, 0.5, 0).finished()))); |
| 95 | EXPECT_THAT(MatrixToVectors(Polytope5().Vertices()), |
| 96 | ::testing::UnorderedElementsAreArray( |
| 97 | MatrixToVectors((Eigen::Matrix<double, 2, 4>() << 0.5, 1, 1.5, |
| 98 | 1, 0, 0.5, 0, -0.5).finished()))); |
| 99 | } |
| 100 | |
Brian Silverman | aba7bf6 | 2016-01-31 18:03:59 -0500 | [diff] [blame] | 101 | } // namespace controls |
James Kuszmaul | 6175066 | 2021-06-21 21:32:33 -0700 | [diff] [blame^] | 102 | } // namespace frc971 |