Austin Schuh | c2b0877 | 2018-12-19 18:05:06 +1100 | [diff] [blame] | 1 | #include "frc971/control_loops/drivetrain/spline.h" |
| 2 | |
| 3 | #include <vector> |
| 4 | |
| 5 | #include "gflags/gflags.h" |
| 6 | #include "gtest/gtest.h" |
| 7 | #include "third_party/matplotlib-cpp/matplotlibcpp.h" |
| 8 | |
| 9 | DEFINE_bool(plot, false, "If true, plot"); |
| 10 | |
| 11 | namespace frc971 { |
| 12 | namespace control_loops { |
| 13 | namespace drivetrain { |
| 14 | namespace testing { |
| 15 | |
| 16 | // Test fixture with a spline from 0, 0 to 1, 1 |
| 17 | class SplineTest : public ::testing::Test { |
| 18 | protected: |
| 19 | SplineTest() |
Austin Schuh | b23f525 | 2019-01-13 21:16:23 -0800 | [diff] [blame] | 20 | : control_points_((::Eigen::Matrix<double, 2, 4>() << 0.0, 0.5, 0.5, 1.0, |
| 21 | 0.0, 0.0, 1.0, 1.0) |
| 22 | .finished()), |
| 23 | spline4_(control_points_), |
| 24 | spline6_(Spline4To6(control_points_)) {} |
| 25 | |
| 26 | ::Eigen::Matrix<double, 2, 4> control_points_; |
| 27 | NSpline<4> spline4_; |
| 28 | NSpline<6> spline6_; |
Austin Schuh | c2b0877 | 2018-12-19 18:05:06 +1100 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | // Tests that the derivitives of xy integrate back up to the position. |
| 32 | TEST_F(SplineTest, XYIntegral) { |
| 33 | ::std::vector<double> alphas_plot; |
| 34 | ::std::vector<double> x_plot; |
| 35 | ::std::vector<double> y_plot; |
| 36 | ::std::vector<double> ix_plot; |
| 37 | ::std::vector<double> iy_plot; |
| 38 | ::std::vector<double> dx_plot; |
| 39 | ::std::vector<double> dy_plot; |
| 40 | ::std::vector<double> idx_plot; |
| 41 | ::std::vector<double> idy_plot; |
| 42 | |
| 43 | const int num_points = 10000; |
Austin Schuh | b23f525 | 2019-01-13 21:16:23 -0800 | [diff] [blame] | 44 | ::Eigen::Matrix<double, 2, 1> point = spline6_.Point(0.0); |
| 45 | ::Eigen::Matrix<double, 2, 1> dpoint = spline6_.DPoint(0.0); |
| 46 | ::Eigen::Matrix<double, 2, 1> ddpoint = spline6_.DDPoint(0.0); |
Austin Schuh | c2b0877 | 2018-12-19 18:05:06 +1100 | [diff] [blame] | 47 | |
| 48 | const double dalpha = 1.0 / static_cast<double>(num_points - 1); |
| 49 | for (int i = 0; i < num_points; ++i) { |
| 50 | const double alpha = |
| 51 | 1.0 * static_cast<double>(i) / static_cast<double>(num_points - 1); |
Austin Schuh | b23f525 | 2019-01-13 21:16:23 -0800 | [diff] [blame] | 52 | const ::Eigen::Matrix<double, 2, 1> expected_point = spline6_.Point(alpha); |
| 53 | const ::Eigen::Matrix<double, 2, 1> expected_dpoint = spline6_.DPoint(alpha); |
Austin Schuh | c2b0877 | 2018-12-19 18:05:06 +1100 | [diff] [blame] | 54 | const ::Eigen::Matrix<double, 2, 1> expected_ddpoint = |
Austin Schuh | b23f525 | 2019-01-13 21:16:23 -0800 | [diff] [blame] | 55 | spline6_.DDPoint(alpha); |
Austin Schuh | c2b0877 | 2018-12-19 18:05:06 +1100 | [diff] [blame] | 56 | |
| 57 | alphas_plot.push_back(alpha); |
| 58 | x_plot.push_back(expected_point(0)); |
| 59 | y_plot.push_back(expected_point(1)); |
| 60 | ix_plot.push_back(point(0)); |
| 61 | iy_plot.push_back(point(1)); |
| 62 | dx_plot.push_back(expected_dpoint(0)); |
| 63 | dy_plot.push_back(expected_dpoint(1)); |
| 64 | idx_plot.push_back(dpoint(0)); |
| 65 | idy_plot.push_back(dpoint(1)); |
| 66 | |
| 67 | EXPECT_LT((point - expected_point).norm(), 1e-2) << ": At alpha " << alpha; |
| 68 | EXPECT_LT((dpoint - expected_dpoint).norm(), 1e-2) << ": At alpha " |
| 69 | << alpha; |
| 70 | EXPECT_LT((ddpoint - expected_ddpoint).norm(), 1e-2) << ": At alpha " |
| 71 | << alpha; |
| 72 | |
| 73 | // We need to record the starting state without integrating. |
| 74 | if (i == 0) { |
| 75 | continue; |
| 76 | } |
| 77 | |
| 78 | point += dpoint * dalpha; |
| 79 | dpoint += ddpoint * dalpha; |
Austin Schuh | b23f525 | 2019-01-13 21:16:23 -0800 | [diff] [blame] | 80 | ddpoint += spline6_.DDDPoint(alpha) * dalpha; |
Austin Schuh | c2b0877 | 2018-12-19 18:05:06 +1100 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | // Conditionally plot the functions and their integrals to aid debugging. |
| 84 | if (FLAGS_plot) { |
| 85 | matplotlibcpp::figure(); |
| 86 | matplotlibcpp::plot(alphas_plot, x_plot, {{"label", "x"}}); |
| 87 | matplotlibcpp::plot(alphas_plot, ix_plot, {{"label", "ix"}}); |
| 88 | matplotlibcpp::plot(alphas_plot, y_plot, {{"label", "y"}}); |
| 89 | matplotlibcpp::plot(alphas_plot, iy_plot, {{"label", "iy"}}); |
| 90 | matplotlibcpp::plot(alphas_plot, dx_plot, {{"label", "dx"}}); |
| 91 | matplotlibcpp::plot(alphas_plot, idx_plot, {{"label", "idx"}}); |
| 92 | matplotlibcpp::plot(alphas_plot, dy_plot, {{"label", "dy"}}); |
| 93 | matplotlibcpp::plot(alphas_plot, idy_plot, {{"label", "idy"}}); |
| 94 | matplotlibcpp::legend(); |
| 95 | |
Austin Schuh | b23f525 | 2019-01-13 21:16:23 -0800 | [diff] [blame] | 96 | matplotlibcpp::figure(); |
| 97 | matplotlibcpp::plot(x_plot, y_plot, {{"label", "spline"}}); |
| 98 | matplotlibcpp::legend(); |
| 99 | |
Austin Schuh | c2b0877 | 2018-12-19 18:05:06 +1100 | [diff] [blame] | 100 | matplotlibcpp::show(); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // Tests that the derivitives of theta integrate back up to the angle. |
| 105 | TEST_F(SplineTest, ThetaIntegral) { |
| 106 | ::std::vector<double> alphas_plot; |
| 107 | ::std::vector<double> theta_plot; |
| 108 | ::std::vector<double> itheta_plot; |
| 109 | ::std::vector<double> dtheta_plot; |
| 110 | ::std::vector<double> idtheta_plot; |
| 111 | |
| 112 | const int num_points = 10000; |
Austin Schuh | b23f525 | 2019-01-13 21:16:23 -0800 | [diff] [blame] | 113 | double theta = spline6_.Theta(0.0); |
| 114 | double dtheta = spline6_.DTheta(0.0); |
Austin Schuh | c2b0877 | 2018-12-19 18:05:06 +1100 | [diff] [blame] | 115 | |
| 116 | const double dalpha = 1.0 / static_cast<double>(num_points - 1); |
| 117 | for (int i = 0; i < num_points; ++i) { |
| 118 | const double alpha = |
| 119 | 1.0 * static_cast<double>(i) / static_cast<double>(num_points - 1); |
Austin Schuh | b23f525 | 2019-01-13 21:16:23 -0800 | [diff] [blame] | 120 | const double expected_theta = spline6_.Theta(alpha); |
| 121 | const double expected_dtheta = spline6_.DTheta(alpha); |
Austin Schuh | c2b0877 | 2018-12-19 18:05:06 +1100 | [diff] [blame] | 122 | |
| 123 | alphas_plot.push_back(alpha); |
| 124 | theta_plot.push_back(expected_theta); |
| 125 | itheta_plot.push_back(theta); |
| 126 | dtheta_plot.push_back(expected_dtheta); |
| 127 | idtheta_plot.push_back(dtheta); |
| 128 | |
| 129 | EXPECT_NEAR(expected_theta, theta, 1e-2) << ": At alpha " << alpha; |
| 130 | EXPECT_NEAR(expected_dtheta, dtheta, 1e-2) << ": At alpha " << alpha; |
| 131 | |
| 132 | // We need to record the starting state without integrating. |
| 133 | if (i == 0) { |
| 134 | continue; |
| 135 | } |
| 136 | |
| 137 | theta += dtheta * dalpha; |
Austin Schuh | b23f525 | 2019-01-13 21:16:23 -0800 | [diff] [blame] | 138 | dtheta += spline6_.DDTheta(alpha) * dalpha; |
Austin Schuh | c2b0877 | 2018-12-19 18:05:06 +1100 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | // Conditionally plot the functions and their integrals to aid debugging. |
| 142 | if (FLAGS_plot) { |
| 143 | matplotlibcpp::figure(); |
| 144 | matplotlibcpp::plot(alphas_plot, theta_plot, {{"label", "theta"}}); |
| 145 | matplotlibcpp::plot(alphas_plot, itheta_plot, {{"label", "itheta"}}); |
| 146 | matplotlibcpp::plot(alphas_plot, dtheta_plot, {{"label", "dtheta"}}); |
| 147 | matplotlibcpp::plot(alphas_plot, idtheta_plot, {{"label", "idtheta"}}); |
| 148 | matplotlibcpp::legend(); |
| 149 | |
| 150 | matplotlibcpp::show(); |
| 151 | } |
| 152 | } |
| 153 | |
Austin Schuh | b23f525 | 2019-01-13 21:16:23 -0800 | [diff] [blame] | 154 | // Tests that a 4 point spline has the same points as a 6 point spline built |
| 155 | // with Spline4To6. |
| 156 | TEST_F(SplineTest, FourToSixSpline) { |
| 157 | const int num_points = 10000; |
| 158 | |
| 159 | ::std::vector<double> alphas_plot; |
| 160 | ::std::vector<double> x_plot; |
| 161 | ::std::vector<double> y_plot; |
| 162 | |
| 163 | const double dalpha = 1.0 / static_cast<double>(num_points - 1); |
| 164 | for (int i = 0; i < num_points; ++i) { |
| 165 | const double alpha = dalpha * static_cast<double>(i); |
| 166 | |
| 167 | const ::Eigen::Matrix<double, 2, 1> expected_point = spline4_.Point(alpha); |
| 168 | const ::Eigen::Matrix<double, 2, 1> expected_dpoint = |
| 169 | spline4_.DPoint(alpha); |
| 170 | const ::Eigen::Matrix<double, 2, 1> expected_ddpoint = |
| 171 | spline4_.DDPoint(alpha); |
| 172 | const ::Eigen::Matrix<double, 2, 1> expected_dddpoint = |
| 173 | spline4_.DDDPoint(alpha); |
| 174 | |
| 175 | const ::Eigen::Matrix<double, 2, 1> point = spline6_.Point(alpha); |
| 176 | const ::Eigen::Matrix<double, 2, 1> dpoint = spline6_.DPoint(alpha); |
| 177 | const ::Eigen::Matrix<double, 2, 1> ddpoint = spline6_.DDPoint(alpha); |
| 178 | const ::Eigen::Matrix<double, 2, 1> dddpoint = spline6_.DDDPoint(alpha); |
| 179 | |
| 180 | alphas_plot.push_back(alpha); |
| 181 | x_plot.push_back(point(0)); |
| 182 | y_plot.push_back(point(1)); |
| 183 | |
| 184 | EXPECT_LT((point - expected_point).norm(), 1e-9) << ": At alpha " << alpha; |
| 185 | EXPECT_LT((dpoint - expected_dpoint).norm(), 1e-9) << ": At alpha " |
| 186 | << alpha; |
| 187 | EXPECT_LT((ddpoint - expected_ddpoint).norm(), 1e-9) << ": At alpha " |
| 188 | << alpha; |
| 189 | EXPECT_LT((dddpoint - expected_dddpoint).norm(), 1e-9) << ": At alpha " |
| 190 | << alpha; |
| 191 | } |
| 192 | |
| 193 | // Conditionally plot the functions and their integrals to aid debugging. |
| 194 | if (FLAGS_plot) { |
| 195 | matplotlibcpp::figure(); |
| 196 | matplotlibcpp::plot(alphas_plot, x_plot, {{"label", "x"}}); |
| 197 | matplotlibcpp::plot(alphas_plot, y_plot, {{"label", "y"}}); |
| 198 | matplotlibcpp::legend(); |
| 199 | |
| 200 | ::std::vector<double> control4x; |
| 201 | ::std::vector<double> control4y; |
| 202 | ::std::vector<double> control6x; |
| 203 | ::std::vector<double> control6y; |
| 204 | for (int i = 0; i < 4; ++i) { |
| 205 | control4x.push_back(spline4_.control_points()(0, i)); |
| 206 | control4y.push_back(spline4_.control_points()(1, i)); |
| 207 | } |
| 208 | for (int i = 0; i < 6; ++i) { |
| 209 | control6x.push_back(spline6_.control_points()(0, i)); |
| 210 | control6y.push_back(spline6_.control_points()(1, i)); |
| 211 | } |
| 212 | |
| 213 | matplotlibcpp::figure(); |
| 214 | matplotlibcpp::plot(x_plot, y_plot, {{"label", "spline"}}); |
| 215 | matplotlibcpp::plot(control4x, control4y, {{"label", "4 control points"}}); |
| 216 | matplotlibcpp::plot(control6x, control6y, {{"label", "6 control points"}}); |
| 217 | matplotlibcpp::legend(); |
| 218 | |
| 219 | matplotlibcpp::show(); |
| 220 | } |
| 221 | } |
| 222 | |
Austin Schuh | c2b0877 | 2018-12-19 18:05:06 +1100 | [diff] [blame] | 223 | } // namespace testing |
| 224 | } // namespace drivetrain |
| 225 | } // namespace control_loops |
| 226 | } // namespace frc971 |