blob: 4fa200114b325004272380d4b400f1f77b52853e [file] [log] [blame]
Austin Schuhc2b08772018-12-19 18:05:06 +11001#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
9DEFINE_bool(plot, false, "If true, plot");
10
11namespace frc971 {
12namespace control_loops {
13namespace drivetrain {
14namespace testing {
15
16// Test fixture with a spline from 0, 0 to 1, 1
17class SplineTest : public ::testing::Test {
18 protected:
19 SplineTest()
Austin Schuhb23f5252019-01-13 21:16:23 -080020 : 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 Schuhc2b08772018-12-19 18:05:06 +110029};
30
31// Tests that the derivitives of xy integrate back up to the position.
32TEST_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 Schuhb23f5252019-01-13 21:16:23 -080044 ::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 Schuhc2b08772018-12-19 18:05:06 +110047
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 Schuhb23f5252019-01-13 21:16:23 -080052 const ::Eigen::Matrix<double, 2, 1> expected_point = spline6_.Point(alpha);
Austin Schuhd749d932020-12-30 21:38:40 -080053 const ::Eigen::Matrix<double, 2, 1> expected_dpoint =
54 spline6_.DPoint(alpha);
Austin Schuhc2b08772018-12-19 18:05:06 +110055 const ::Eigen::Matrix<double, 2, 1> expected_ddpoint =
Austin Schuhb23f5252019-01-13 21:16:23 -080056 spline6_.DDPoint(alpha);
Austin Schuhc2b08772018-12-19 18:05:06 +110057
58 alphas_plot.push_back(alpha);
59 x_plot.push_back(expected_point(0));
60 y_plot.push_back(expected_point(1));
61 ix_plot.push_back(point(0));
62 iy_plot.push_back(point(1));
63 dx_plot.push_back(expected_dpoint(0));
64 dy_plot.push_back(expected_dpoint(1));
65 idx_plot.push_back(dpoint(0));
66 idy_plot.push_back(dpoint(1));
67
68 EXPECT_LT((point - expected_point).norm(), 1e-2) << ": At alpha " << alpha;
Austin Schuhd749d932020-12-30 21:38:40 -080069 EXPECT_LT((dpoint - expected_dpoint).norm(), 1e-2)
70 << ": At alpha " << alpha;
71 EXPECT_LT((ddpoint - expected_ddpoint).norm(), 1e-2)
72 << ": At alpha " << alpha;
Austin Schuhc2b08772018-12-19 18:05:06 +110073
74 // We need to record the starting state without integrating.
75 if (i == 0) {
76 continue;
77 }
78
79 point += dpoint * dalpha;
80 dpoint += ddpoint * dalpha;
Austin Schuhb23f5252019-01-13 21:16:23 -080081 ddpoint += spline6_.DDDPoint(alpha) * dalpha;
Austin Schuhc2b08772018-12-19 18:05:06 +110082 }
83
84 // Conditionally plot the functions and their integrals to aid debugging.
85 if (FLAGS_plot) {
86 matplotlibcpp::figure();
87 matplotlibcpp::plot(alphas_plot, x_plot, {{"label", "x"}});
88 matplotlibcpp::plot(alphas_plot, ix_plot, {{"label", "ix"}});
89 matplotlibcpp::plot(alphas_plot, y_plot, {{"label", "y"}});
90 matplotlibcpp::plot(alphas_plot, iy_plot, {{"label", "iy"}});
91 matplotlibcpp::plot(alphas_plot, dx_plot, {{"label", "dx"}});
92 matplotlibcpp::plot(alphas_plot, idx_plot, {{"label", "idx"}});
93 matplotlibcpp::plot(alphas_plot, dy_plot, {{"label", "dy"}});
94 matplotlibcpp::plot(alphas_plot, idy_plot, {{"label", "idy"}});
95 matplotlibcpp::legend();
96
Austin Schuhb23f5252019-01-13 21:16:23 -080097 matplotlibcpp::figure();
98 matplotlibcpp::plot(x_plot, y_plot, {{"label", "spline"}});
99 matplotlibcpp::legend();
100
Austin Schuhc2b08772018-12-19 18:05:06 +1100101 matplotlibcpp::show();
102 }
103}
104
105// Tests that the derivitives of theta integrate back up to the angle.
106TEST_F(SplineTest, ThetaIntegral) {
107 ::std::vector<double> alphas_plot;
108 ::std::vector<double> theta_plot;
109 ::std::vector<double> itheta_plot;
110 ::std::vector<double> dtheta_plot;
111 ::std::vector<double> idtheta_plot;
112
113 const int num_points = 10000;
Austin Schuhb23f5252019-01-13 21:16:23 -0800114 double theta = spline6_.Theta(0.0);
115 double dtheta = spline6_.DTheta(0.0);
Austin Schuhc2b08772018-12-19 18:05:06 +1100116
117 const double dalpha = 1.0 / static_cast<double>(num_points - 1);
118 for (int i = 0; i < num_points; ++i) {
119 const double alpha =
120 1.0 * static_cast<double>(i) / static_cast<double>(num_points - 1);
Austin Schuhb23f5252019-01-13 21:16:23 -0800121 const double expected_theta = spline6_.Theta(alpha);
122 const double expected_dtheta = spline6_.DTheta(alpha);
Austin Schuhc2b08772018-12-19 18:05:06 +1100123
124 alphas_plot.push_back(alpha);
125 theta_plot.push_back(expected_theta);
126 itheta_plot.push_back(theta);
127 dtheta_plot.push_back(expected_dtheta);
128 idtheta_plot.push_back(dtheta);
129
130 EXPECT_NEAR(expected_theta, theta, 1e-2) << ": At alpha " << alpha;
131 EXPECT_NEAR(expected_dtheta, dtheta, 1e-2) << ": At alpha " << alpha;
132
133 // We need to record the starting state without integrating.
134 if (i == 0) {
135 continue;
136 }
137
138 theta += dtheta * dalpha;
Austin Schuhb23f5252019-01-13 21:16:23 -0800139 dtheta += spline6_.DDTheta(alpha) * dalpha;
Austin Schuhc2b08772018-12-19 18:05:06 +1100140 }
141
142 // Conditionally plot the functions and their integrals to aid debugging.
143 if (FLAGS_plot) {
144 matplotlibcpp::figure();
145 matplotlibcpp::plot(alphas_plot, theta_plot, {{"label", "theta"}});
146 matplotlibcpp::plot(alphas_plot, itheta_plot, {{"label", "itheta"}});
147 matplotlibcpp::plot(alphas_plot, dtheta_plot, {{"label", "dtheta"}});
148 matplotlibcpp::plot(alphas_plot, idtheta_plot, {{"label", "idtheta"}});
149 matplotlibcpp::legend();
150
151 matplotlibcpp::show();
152 }
153}
154
Austin Schuhb23f5252019-01-13 21:16:23 -0800155// Tests that a 4 point spline has the same points as a 6 point spline built
156// with Spline4To6.
157TEST_F(SplineTest, FourToSixSpline) {
158 const int num_points = 10000;
159
160 ::std::vector<double> alphas_plot;
161 ::std::vector<double> x_plot;
162 ::std::vector<double> y_plot;
163
164 const double dalpha = 1.0 / static_cast<double>(num_points - 1);
165 for (int i = 0; i < num_points; ++i) {
166 const double alpha = dalpha * static_cast<double>(i);
167
168 const ::Eigen::Matrix<double, 2, 1> expected_point = spline4_.Point(alpha);
169 const ::Eigen::Matrix<double, 2, 1> expected_dpoint =
170 spline4_.DPoint(alpha);
171 const ::Eigen::Matrix<double, 2, 1> expected_ddpoint =
172 spline4_.DDPoint(alpha);
173 const ::Eigen::Matrix<double, 2, 1> expected_dddpoint =
174 spline4_.DDDPoint(alpha);
175
176 const ::Eigen::Matrix<double, 2, 1> point = spline6_.Point(alpha);
177 const ::Eigen::Matrix<double, 2, 1> dpoint = spline6_.DPoint(alpha);
178 const ::Eigen::Matrix<double, 2, 1> ddpoint = spline6_.DDPoint(alpha);
179 const ::Eigen::Matrix<double, 2, 1> dddpoint = spline6_.DDDPoint(alpha);
180
181 alphas_plot.push_back(alpha);
182 x_plot.push_back(point(0));
183 y_plot.push_back(point(1));
184
185 EXPECT_LT((point - expected_point).norm(), 1e-9) << ": At alpha " << alpha;
Austin Schuhd749d932020-12-30 21:38:40 -0800186 EXPECT_LT((dpoint - expected_dpoint).norm(), 1e-9)
187 << ": At alpha " << alpha;
188 EXPECT_LT((ddpoint - expected_ddpoint).norm(), 1e-9)
189 << ": At alpha " << alpha;
190 EXPECT_LT((dddpoint - expected_dddpoint).norm(), 1e-9)
191 << ": At alpha " << alpha;
Austin Schuhb23f5252019-01-13 21:16:23 -0800192 }
193
194 // Conditionally plot the functions and their integrals to aid debugging.
195 if (FLAGS_plot) {
196 matplotlibcpp::figure();
197 matplotlibcpp::plot(alphas_plot, x_plot, {{"label", "x"}});
198 matplotlibcpp::plot(alphas_plot, y_plot, {{"label", "y"}});
199 matplotlibcpp::legend();
200
201 ::std::vector<double> control4x;
202 ::std::vector<double> control4y;
203 ::std::vector<double> control6x;
204 ::std::vector<double> control6y;
205 for (int i = 0; i < 4; ++i) {
206 control4x.push_back(spline4_.control_points()(0, i));
207 control4y.push_back(spline4_.control_points()(1, i));
208 }
209 for (int i = 0; i < 6; ++i) {
210 control6x.push_back(spline6_.control_points()(0, i));
211 control6y.push_back(spline6_.control_points()(1, i));
212 }
213
214 matplotlibcpp::figure();
215 matplotlibcpp::plot(x_plot, y_plot, {{"label", "spline"}});
216 matplotlibcpp::plot(control4x, control4y, {{"label", "4 control points"}});
217 matplotlibcpp::plot(control6x, control6y, {{"label", "6 control points"}});
218 matplotlibcpp::legend();
219
220 matplotlibcpp::show();
221 }
222}
223
Austin Schuhc2b08772018-12-19 18:05:06 +1100224} // namespace testing
225} // namespace drivetrain
226} // namespace control_loops
227} // namespace frc971