blob: aa7f22f46187042dd0d1876563f3a6c7f484cbff [file] [log] [blame]
James Kuszmaul1057ce82019-02-09 17:58:24 -08001#include "y2019/control_loops/drivetrain/localizer.h"
2
James Kuszmaul1057ce82019-02-09 17:58:24 -08003#include <queue>
James Kuszmaul651fc3f2019-05-15 21:14:25 -07004#include <random>
James Kuszmaul1057ce82019-02-09 17:58:24 -08005
6#include "aos/testing/random_seed.h"
7#include "aos/testing/test_shm.h"
James Kuszmaul1057ce82019-02-09 17:58:24 -08008#include "frc971/control_loops/drivetrain/splinedrivetrain.h"
James Kuszmaul651fc3f2019-05-15 21:14:25 -07009#include "frc971/control_loops/drivetrain/trajectory.h"
James Kuszmaul1057ce82019-02-09 17:58:24 -080010#include "gflags/gflags.h"
11#if defined(SUPPORT_PLOT)
12#include "third_party/matplotlib-cpp/matplotlibcpp.h"
13#endif
14#include "gtest/gtest.h"
James Kuszmaul1057ce82019-02-09 17:58:24 -080015#include "y2019/constants.h"
James Kuszmaul651fc3f2019-05-15 21:14:25 -070016#include "y2019/control_loops/drivetrain/drivetrain_base.h"
James Kuszmaul1057ce82019-02-09 17:58:24 -080017
18DEFINE_bool(plot, false, "If true, plot");
19
20namespace y2019 {
21namespace control_loops {
22namespace testing {
23
24using ::y2019::constants::Field;
25
26constexpr size_t kNumCameras = 4;
27constexpr size_t kNumTargetsPerFrame = 3;
28
29typedef TypedLocalizer<kNumCameras, Field::kNumTargets, Field::kNumObstacles,
James Kuszmaul651fc3f2019-05-15 21:14:25 -070030 kNumTargetsPerFrame, double>
31 TestLocalizer;
James Kuszmaul1057ce82019-02-09 17:58:24 -080032typedef typename TestLocalizer::Camera TestCamera;
James Kuszmaulf4ede202020-02-14 08:47:40 -080033typedef typename TestLocalizer::Target Target;
James Kuszmaul1057ce82019-02-09 17:58:24 -080034typedef typename TestCamera::Pose Pose;
35typedef typename TestCamera::LineSegment Obstacle;
36
37typedef TestLocalizer::StateIdx StateIdx;
38
39using ::frc971::control_loops::drivetrain::DrivetrainConfig;
40
41// When placing the cameras on the robot, set them all kCameraOffset out from
42// the center, to test that we really can handle cameras that aren't at the
43// center-of-mass.
44constexpr double kCameraOffset = 0.1;
45
46#if defined(SUPPORT_PLOT)
47// Plots a line from a vector of Pose's.
48void PlotPlotPts(const ::std::vector<Pose> &poses,
49 const ::std::map<::std::string, ::std::string> &kwargs) {
50 ::std::vector<double> x;
51 ::std::vector<double> y;
James Kuszmaul651fc3f2019-05-15 21:14:25 -070052 for (const Pose &p : poses) {
James Kuszmaul1057ce82019-02-09 17:58:24 -080053 x.push_back(p.abs_pos().x());
54 y.push_back(p.abs_pos().y());
55 }
56 matplotlibcpp::plot(x, y, kwargs);
57}
58#endif
59
60struct LocalizerTestParams {
61 // Control points for the spline to make the robot follow.
62 ::std::array<float, 6> control_pts_x;
63 ::std::array<float, 6> control_pts_y;
64 // The actual state to start the robot at. By setting voltage errors and the
65 // such you can introduce persistent disturbances.
66 TestLocalizer::State true_start_state;
67 // The initial state of the estimator.
68 TestLocalizer::State known_start_state;
69 // Whether or not to add Gaussian noise to the sensors and cameras.
70 bool noisify;
71 // Whether or not to add unmodelled disturbances.
72 bool disturb;
73 // The tolerances for the estimator and for the trajectory following at
74 // the end of the spline:
75 double estimate_tolerance;
76 double goal_tolerance;
77};
78
79class ParameterizedLocalizerTest
80 : public ::testing::TestWithParam<LocalizerTestParams> {
81 public:
82 ::aos::testing::TestSharedMemory shm_;
83
84 // Set up three targets in a row, at (-1, 0), (0, 0), and (1, 0).
85 // Make the right-most target (1, 0) be facing away from the camera, and give
86 // the middle target some skew.
87 // Place one camera facing forward, the other facing backward, and set the
88 // robot at (0, -5) with the cameras each 0.1m from the center.
89 // Place one obstacle in a place where it can block the left-most target (-1,
90 // 0).
91 ParameterizedLocalizerTest()
92 : field_(),
93 targets_(field_.targets()),
94 modeled_obstacles_(field_.obstacles()),
95 true_obstacles_(field_.obstacles()),
96 dt_config_(drivetrain::GetDrivetrainConfig()),
97 // Pull the noise for the encoders/gyros from the R matrix:
98 encoder_noise_(::std::sqrt(
99 dt_config_.make_kf_drivetrain_loop().observer().coefficients().R(
100 0, 0))),
101 gyro_noise_(::std::sqrt(
102 dt_config_.make_kf_drivetrain_loop().observer().coefficients().R(
103 2, 2))),
104 // As per the comments in localizer.h, we set the field of view and
105 // noise parameters on the robot_cameras_ so that they see a bit more
106 // than the true_cameras_. The robot_cameras_ are what is passed to the
107 // localizer and used to generate "expected" targets. The true_cameras_
108 // are what we actually use to generate targets to pass to the
109 // localizer.
110 robot_cameras_{
111 {TestCamera({&robot_pose_, {0.0, kCameraOffset, 0.0}, M_PI_2},
112 M_PI_2 * 1.1, robot_noise_parameters_, targets_,
113 modeled_obstacles_),
114 TestCamera({&robot_pose_, {kCameraOffset, 0.0, 0.0}, 0.0},
115 M_PI_2 * 1.1, robot_noise_parameters_, targets_,
116 modeled_obstacles_),
117 TestCamera({&robot_pose_, {-kCameraOffset, 0.0, 0.0}, M_PI},
118 M_PI_2 * 1.1, robot_noise_parameters_, targets_,
119 modeled_obstacles_),
120 TestCamera({&robot_pose_, {0.0, -kCameraOffset, 0.0}, -M_PI_2},
121 M_PI_2 * 1.1, robot_noise_parameters_, targets_,
122 modeled_obstacles_)}},
123 true_cameras_{
124 {TestCamera({&true_robot_pose_, {0.0, kCameraOffset, 0.0}, M_PI_2},
125 M_PI_2 * 0.9, true_noise_parameters_, targets_,
126 true_obstacles_),
127 TestCamera({&true_robot_pose_, {kCameraOffset, 0.0, 0.0}, 0.0},
128 M_PI_2 * 0.9, true_noise_parameters_, targets_,
129 true_obstacles_),
130 TestCamera({&true_robot_pose_, {-kCameraOffset, 0.0, 0.0}, M_PI},
131 M_PI_2 * 0.9, true_noise_parameters_, targets_,
132 true_obstacles_),
133 TestCamera(
134 {&true_robot_pose_, {-0.0, -kCameraOffset, 0.0}, -M_PI_2},
135 M_PI_2 * 0.9, true_noise_parameters_, targets_,
136 true_obstacles_)}},
137 localizer_(dt_config_, &robot_pose_),
138 spline_drivetrain_(dt_config_) {
139 // We use the default P() for initialization.
140 localizer_.ResetInitialState(t0_, GetParam().known_start_state,
141 localizer_.P());
142 }
143
144 void SetUp() {
Austin Schuhb574fe42019-12-06 23:51:47 -0800145 // Turn on -v 1
146 FLAGS_v = std::max(FLAGS_v, 1);
147
Alex Perrycb7da4b2019-08-28 19:35:56 -0700148 flatbuffers::DetachedBuffer goal_buffer;
149 {
150 flatbuffers::FlatBufferBuilder fbb;
151
152 flatbuffers::Offset<flatbuffers::Vector<float>> spline_x_offset =
153 fbb.CreateVector<float>(GetParam().control_pts_x.begin(),
154 GetParam().control_pts_x.size());
155
156 flatbuffers::Offset<flatbuffers::Vector<float>> spline_y_offset =
157 fbb.CreateVector<float>(GetParam().control_pts_y.begin(),
158 GetParam().control_pts_y.size());
159
160 frc971::MultiSpline::Builder multispline_builder(fbb);
161
162 multispline_builder.add_spline_count(1);
163 multispline_builder.add_spline_x(spline_x_offset);
164 multispline_builder.add_spline_y(spline_y_offset);
165
166 flatbuffers::Offset<frc971::MultiSpline> multispline_offset =
167 multispline_builder.Finish();
168
169 frc971::control_loops::drivetrain::SplineGoal::Builder spline_builder(
170 fbb);
171
172 spline_builder.add_spline_idx(1);
173 spline_builder.add_spline(multispline_offset);
174
175 flatbuffers::Offset<frc971::control_loops::drivetrain::SplineGoal>
176 spline_offset = spline_builder.Finish();
177
178 frc971::control_loops::drivetrain::Goal::Builder goal_builder(fbb);
179
180 goal_builder.add_spline(spline_offset);
181 goal_builder.add_controller_type(
Austin Schuh872723c2019-12-25 14:38:09 -0800182 frc971::control_loops::drivetrain::ControllerType::SPLINE_FOLLOWER);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700183 goal_builder.add_spline_handle(1);
184
185 fbb.Finish(goal_builder.Finish());
186
187 goal_buffer = fbb.Release();
188 }
189 aos::FlatbufferDetachedBuffer<frc971::control_loops::drivetrain::Goal> goal(
190 std::move(goal_buffer));
191
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800192 // Let the spline drivetrain compute the spline.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700193 while (true) {
Austin Schuhb574fe42019-12-06 23:51:47 -0800194 // We need to keep sending the goal. There are conditions when the
195 // trajectory lock isn't grabbed the first time, and we want to keep
196 // banging on it to keep trying. Otherwise we deadlock.
197 spline_drivetrain_.SetGoal(&goal.message());
198
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800199 ::std::this_thread::sleep_for(::std::chrono::milliseconds(5));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700200
201 flatbuffers::FlatBufferBuilder fbb;
202
203 flatbuffers::Offset<frc971::control_loops::drivetrain::TrajectoryLogging>
204 trajectory_logging_offset =
205 spline_drivetrain_.MakeTrajectoryLogging(&fbb);
206
207 ::frc971::control_loops::drivetrain::Status::Builder status_builder(fbb);
208 status_builder.add_trajectory_logging(trajectory_logging_offset);
209 spline_drivetrain_.PopulateStatus(&status_builder);
210 fbb.Finish(status_builder.Finish());
211 aos::FlatbufferDetachedBuffer<::frc971::control_loops::drivetrain::Status>
212 status(fbb.Release());
213
214 if (status.message().trajectory_logging()->planning_state() ==
Austin Schuh872723c2019-12-25 14:38:09 -0800215 ::frc971::control_loops::drivetrain::PlanningState::PLANNED) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700216 break;
217 }
218 }
219 spline_drivetrain_.SetGoal(&goal.message());
James Kuszmaul1057ce82019-02-09 17:58:24 -0800220 }
221
222 void TearDown() {
223 printf("Each iteration of the simulation took on average %f seconds.\n",
224 avg_time_.count());
225#if defined(SUPPORT_PLOT)
226 if (FLAGS_plot) {
227 matplotlibcpp::figure();
228 matplotlibcpp::plot(simulation_t_, simulation_vl_, {{"label", "Vl"}});
229 matplotlibcpp::plot(simulation_t_, simulation_vr_, {{"label", "Vr"}});
230 matplotlibcpp::legend();
231
232 matplotlibcpp::figure();
233 matplotlibcpp::plot(spline_x_, spline_y_, {{"label", "spline"}});
234 matplotlibcpp::plot(simulation_x_, simulation_y_, {{"label", "robot"}});
235 matplotlibcpp::plot(estimated_x_, estimated_y_,
236 {{"label", "estimation"}});
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700237 for (const Target &target : targets_) {
James Kuszmaul1057ce82019-02-09 17:58:24 -0800238 PlotPlotPts(target.PlotPoints(), {{"c", "g"}});
239 }
240 for (const Obstacle &obstacle : true_obstacles_) {
241 PlotPlotPts(obstacle.PlotPoints(), {{"c", "k"}});
242 }
243 // Go through and plot true/expected camera targets for a few select
244 // time-steps.
245 ::std::vector<const char *> colors{"m", "y", "c"};
246 int jj = 0;
247 for (size_t ii = 0; ii < simulation_x_.size(); ii += 400) {
248 *true_robot_pose_.mutable_pos() << simulation_x_[ii], simulation_y_[ii],
249 0.0;
250 true_robot_pose_.set_theta(simulation_theta_[ii]);
251 for (const TestCamera &camera : true_cameras_) {
252 for (const auto &plot_pts : camera.PlotPoints()) {
253 PlotPlotPts(plot_pts, {{"c", colors[jj]}});
254 }
255 }
256 for (const TestCamera &camera : robot_cameras_) {
257 *robot_pose_.mutable_pos() << estimated_x_[ii], estimated_y_[ii], 0.0;
258 robot_pose_.set_theta(estimated_theta_[ii]);
259 const auto &all_plot_pts = camera.PlotPoints();
260 *robot_pose_.mutable_pos() = true_robot_pose_.rel_pos();
261 robot_pose_.set_theta(true_robot_pose_.rel_theta());
262 for (const auto &plot_pts : all_plot_pts) {
263 PlotPlotPts(plot_pts, {{"c", colors[jj]}, {"ls", "--"}});
264 }
265 }
266 jj = (jj + 1) % colors.size();
267 }
268 matplotlibcpp::legend();
269
270 matplotlibcpp::figure();
271 matplotlibcpp::plot(
272 simulation_t_, spline_x_,
273 {{"label", "spline x"}, {"c", "g"}, {"ls", ""}, {"marker", "o"}});
274 matplotlibcpp::plot(simulation_t_, simulation_x_,
275 {{"label", "simulated x"}, {"c", "g"}});
276 matplotlibcpp::plot(simulation_t_, estimated_x_,
277 {{"label", "estimated x"}, {"c", "g"}, {"ls", "--"}});
278
279 matplotlibcpp::plot(
280 simulation_t_, spline_y_,
281 {{"label", "spline y"}, {"c", "b"}, {"ls", ""}, {"marker", "o"}});
282 matplotlibcpp::plot(simulation_t_, simulation_y_,
283 {{"label", "simulated y"}, {"c", "b"}});
284 matplotlibcpp::plot(simulation_t_, estimated_y_,
285 {{"label", "estimated y"}, {"c", "b"}, {"ls", "--"}});
286
287 matplotlibcpp::plot(simulation_t_, simulation_theta_,
288 {{"label", "simulated theta"}, {"c", "r"}});
289 matplotlibcpp::plot(
290 simulation_t_, estimated_theta_,
291 {{"label", "estimated theta"}, {"c", "r"}, {"ls", "--"}});
292 matplotlibcpp::legend();
293
294 matplotlibcpp::show();
295 }
296#endif
297 }
298
299 protected:
300 // Returns a random number with a gaussian distribution with a mean of zero
301 // and a standard deviation of std, if noisify = true.
302 // If noisify is false, then returns 0.0.
303 double Normal(double std) {
304 if (GetParam().noisify) {
305 return normal_(gen_) * std;
306 }
307 return 0.0;
308 }
309 // Adds random noise to the given target view.
310 void Noisify(TestCamera::TargetView *view) {
311 view->reading.heading += Normal(view->noise.heading);
312 view->reading.distance += Normal(view->noise.distance);
313 view->reading.height += Normal(view->noise.height);
314 view->reading.skew += Normal(view->noise.skew);
315 }
316 // The differential equation for the dynamics of the system.
317 TestLocalizer::State DiffEq(const TestLocalizer::State &X,
318 const TestLocalizer::Input &U) {
319 return localizer_.DiffEq(X, U);
320 }
321
322 Field field_;
323 ::std::array<Target, Field::kNumTargets> targets_;
324 // The obstacles that are passed to the camera objects for the localizer.
325 ::std::array<Obstacle, Field::kNumObstacles> modeled_obstacles_;
326 // The obstacles that are used for actually simulating the cameras.
327 ::std::array<Obstacle, Field::kNumObstacles> true_obstacles_;
328
329 DrivetrainConfig<double> dt_config_;
330
331 // Noise information for the actual simulated cameras (true_*) and the
332 // parameters that the localizer should use for modelling the cameras. Note
333 // how the max_viewable_distance is larger for the localizer, so that if
334 // there is any error in the estimation, it still thinks that it can see
335 // any targets that might actually be in range.
336 TestCamera::NoiseParameters true_noise_parameters_ = {
337 .max_viewable_distance = 10.0,
338 .heading_noise = 0.02,
339 .nominal_distance_noise = 0.06,
340 .nominal_skew_noise = 0.1,
341 .nominal_height_noise = 0.01};
342 TestCamera::NoiseParameters robot_noise_parameters_ = {
343 .max_viewable_distance = 11.0,
344 .heading_noise = 0.02,
345 .nominal_distance_noise = 0.06,
346 .nominal_skew_noise = 0.1,
347 .nominal_height_noise = 0.01};
348
349 // Standard deviations of the noise for the encoders/gyro.
350 double encoder_noise_, gyro_noise_;
351
352 Pose robot_pose_;
353 ::std::array<TestCamera, 4> robot_cameras_;
354 Pose true_robot_pose_;
355 ::std::array<TestCamera, 4> true_cameras_;
356
357 TestLocalizer localizer_;
358
359 ::frc971::control_loops::drivetrain::SplineDrivetrain spline_drivetrain_;
360
361 // All the data we want to end up plotting.
362 ::std::vector<double> simulation_t_;
363
364 ::std::vector<double> spline_x_;
365 ::std::vector<double> spline_y_;
366 ::std::vector<double> estimated_x_;
367 ::std::vector<double> estimated_y_;
368 ::std::vector<double> estimated_theta_;
369 ::std::vector<double> simulation_x_;
370 ::std::vector<double> simulation_y_;
371 ::std::vector<double> simulation_theta_;
372
373 ::std::vector<double> simulation_vl_;
374 ::std::vector<double> simulation_vr_;
375
376 // Simulation start time
377 ::aos::monotonic_clock::time_point t0_;
378
379 // Average duration of each iteration; used for debugging and getting a
380 // sanity-check on what performance looks like--uses a real system clock.
381 ::std::chrono::duration<double> avg_time_;
382
383 ::std::mt19937 gen_{static_cast<uint32_t>(::aos::testing::RandomSeed())};
384 ::std::normal_distribution<> normal_;
385};
386
James Kuszmaul6f941b72019-03-08 18:12:25 -0800387using ::std::chrono::milliseconds;
388
James Kuszmaul1057ce82019-02-09 17:58:24 -0800389// Tests that, when we attempt to follow a spline and use the localizer to
390// perform the state estimation, we end up roughly where we should and that
391// the localizer has a valid position estimate.
392TEST_P(ParameterizedLocalizerTest, SplineTest) {
393 // state stores the true state of the robot throughout the simulation.
394 TestLocalizer::State state = GetParam().true_start_state;
395
396 ::aos::monotonic_clock::time_point t = t0_;
397
398 // The period with which we should take frames from the cameras. Currently,
399 // we just trigger all the cameras at once, rather than offsetting them or
400 // anything.
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700401 const int camera_period = 5; // cycles
James Kuszmaul6f941b72019-03-08 18:12:25 -0800402 // The amount of time to delay the camera images from when they are taken, for
403 // each camera.
404 const ::std::array<milliseconds, 4> camera_latencies{
405 {milliseconds(45), milliseconds(50), milliseconds(55),
406 milliseconds(100)}};
James Kuszmaul1057ce82019-02-09 17:58:24 -0800407
James Kuszmaul6f941b72019-03-08 18:12:25 -0800408 // A queue of camera frames for each camera so that we can add a time delay to
409 // the data coming from the cameras.
410 ::std::array<
411 ::std::queue<::std::tuple<
412 ::aos::monotonic_clock::time_point, const TestCamera *,
413 ::aos::SizedArray<TestCamera::TargetView, kNumTargetsPerFrame>>>,
414 4>
415 camera_queues;
James Kuszmaul1057ce82019-02-09 17:58:24 -0800416
417 // Start time, for debugging.
418 const auto begin = ::std::chrono::steady_clock::now();
419
420 size_t i;
421 for (i = 0; !spline_drivetrain_.IsAtEnd(); ++i) {
422 // Get the current state estimate into a matrix that works for the
423 // trajectory code.
424 ::Eigen::Matrix<double, 5, 1> known_state;
425 TestLocalizer::State X_hat = localizer_.X_hat();
426 known_state << X_hat(StateIdx::kX, 0), X_hat(StateIdx::kY, 0),
427 X_hat(StateIdx::kTheta, 0), X_hat(StateIdx::kLeftVelocity, 0),
428 X_hat(StateIdx::kRightVelocity, 0);
429
430 spline_drivetrain_.Update(true, known_state);
431
Alex Perrycb7da4b2019-08-28 19:35:56 -0700432 ::frc971::control_loops::drivetrain::OutputT output;
James Kuszmaul1057ce82019-02-09 17:58:24 -0800433 output.left_voltage = 0;
434 output.right_voltage = 0;
435 spline_drivetrain_.SetOutput(&output);
436 TestLocalizer::Input U(output.left_voltage, output.right_voltage);
437
438 const ::Eigen::Matrix<double, 5, 1> goal_state =
439 spline_drivetrain_.CurrentGoalState();
440 simulation_t_.push_back(
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700441 ::aos::time::DurationInSeconds(t.time_since_epoch()));
James Kuszmaul1057ce82019-02-09 17:58:24 -0800442 spline_x_.push_back(goal_state(0, 0));
443 spline_y_.push_back(goal_state(1, 0));
444 simulation_x_.push_back(state(StateIdx::kX, 0));
445 simulation_y_.push_back(state(StateIdx::kY, 0));
446 simulation_theta_.push_back(state(StateIdx::kTheta, 0));
447 estimated_x_.push_back(known_state(0, 0));
448 estimated_y_.push_back(known_state(1, 0));
449 estimated_theta_.push_back(known_state(StateIdx::kTheta, 0));
450
451 simulation_vl_.push_back(U(0));
452 simulation_vr_.push_back(U(1));
453 U(0, 0) = ::std::max(::std::min(U(0, 0), 12.0), -12.0);
454 U(1, 0) = ::std::max(::std::min(U(1, 0), 12.0), -12.0);
455
456 state = ::frc971::control_loops::RungeKuttaU(
James Kuszmaul074429e2019-03-23 16:01:49 -0700457 [this](const ::Eigen::Matrix<double, 10, 1> &X,
James Kuszmaul1057ce82019-02-09 17:58:24 -0800458 const ::Eigen::Matrix<double, 2, 1> &U) { return DiffEq(X, U); },
James Kuszmaul651fc3f2019-05-15 21:14:25 -0700459 state, U, ::aos::time::DurationInSeconds(dt_config_.dt));
James Kuszmaul1057ce82019-02-09 17:58:24 -0800460
461 // Add arbitrary disturbances at some arbitrary interval. The main points of
462 // interest here are that we (a) stop adding disturbances at the very end of
463 // the trajectory, to allow us to actually converge to the goal, and (b)
464 // scale disturbances by the corruent velocity.
James Kuszmaulc73bb222019-04-07 12:15:35 -0700465 if (GetParam().disturb && i % 75 == 0) {
James Kuszmaul1057ce82019-02-09 17:58:24 -0800466 // Scale the disturbance so that when we have near-zero velocity we don't
467 // have much disturbance.
468 double disturbance_scale = ::std::min(
469 1.0, ::std::sqrt(::std::pow(state(StateIdx::kLeftVelocity, 0), 2) +
470 ::std::pow(state(StateIdx::kRightVelocity, 0), 2)) /
471 3.0);
472 TestLocalizer::State disturbance;
James Kuszmaul074429e2019-03-23 16:01:49 -0700473 disturbance << 0.02, 0.02, 0.001, 0.03, 0.02, 0.0, 0.0, 0.0, 0.0, 0.0;
James Kuszmaul1057ce82019-02-09 17:58:24 -0800474 disturbance *= disturbance_scale;
475 state += disturbance;
476 }
477
478 t += dt_config_.dt;
479 *true_robot_pose_.mutable_pos() << state(StateIdx::kX, 0),
480 state(StateIdx::kY, 0), 0.0;
481 true_robot_pose_.set_theta(state(StateIdx::kTheta, 0));
482 const double left_enc = state(StateIdx::kLeftEncoder, 0);
483 const double right_enc = state(StateIdx::kRightEncoder, 0);
484
485 const double gyro = (state(StateIdx::kRightVelocity, 0) -
486 state(StateIdx::kLeftVelocity, 0)) /
487 dt_config_.robot_radius / 2.0;
488
489 localizer_.UpdateEncodersAndGyro(left_enc + Normal(encoder_noise_),
490 right_enc + Normal(encoder_noise_),
491 gyro + Normal(gyro_noise_), U, t);
492
James Kuszmaul6f941b72019-03-08 18:12:25 -0800493 for (size_t cam_idx = 0; cam_idx < camera_queues.size(); ++cam_idx) {
494 auto &camera_queue = camera_queues[cam_idx];
495 // Clear out the camera frames that we are ready to pass to the localizer.
496 while (!camera_queue.empty() && ::std::get<0>(camera_queue.front()) <
497 t - camera_latencies[cam_idx]) {
498 const auto tuple = camera_queue.front();
499 camera_queue.pop();
500 ::aos::monotonic_clock::time_point t_obs = ::std::get<0>(tuple);
501 const TestCamera *camera = ::std::get<1>(tuple);
502 ::aos::SizedArray<TestCamera::TargetView, kNumTargetsPerFrame> views =
503 ::std::get<2>(tuple);
504 localizer_.UpdateTargets(*camera, views, t_obs);
505 }
James Kuszmaul1057ce82019-02-09 17:58:24 -0800506
James Kuszmaul6f941b72019-03-08 18:12:25 -0800507 // Actually take all the images and store them in the queue.
508 if (i % camera_period == 0) {
509 for (size_t jj = 0; jj < true_cameras_.size(); ++jj) {
510 const auto target_views = true_cameras_[jj].target_views();
511 ::aos::SizedArray<TestCamera::TargetView, kNumTargetsPerFrame>
512 pass_views;
513 for (size_t ii = 0;
514 ii < ::std::min(kNumTargetsPerFrame, target_views.size());
515 ++ii) {
516 TestCamera::TargetView view = target_views[ii];
517 Noisify(&view);
518 pass_views.push_back(view);
519 }
520 camera_queue.emplace(t, &robot_cameras_[jj], pass_views);
James Kuszmaul1057ce82019-02-09 17:58:24 -0800521 }
James Kuszmaul1057ce82019-02-09 17:58:24 -0800522 }
523 }
James Kuszmaul1057ce82019-02-09 17:58:24 -0800524 }
525
526 const auto end = ::std::chrono::steady_clock::now();
527 avg_time_ = (end - begin) / i;
528 TestLocalizer::State estimate_err = state - localizer_.X_hat();
529 EXPECT_LT(estimate_err.template topRows<7>().norm(),
530 GetParam().estimate_tolerance);
531 // Check that none of the states that we actually care about (x/y/theta, and
532 // wheel positions/speeds) are too far off, individually:
James Kuszmaul7f1a4082019-04-14 10:50:44 -0700533 EXPECT_LT(estimate_err.template topRows<3>().cwiseAbs().maxCoeff(),
James Kuszmaul1057ce82019-02-09 17:58:24 -0800534 GetParam().estimate_tolerance / 3.0)
535 << "Estimate error: " << estimate_err.transpose();
536 Eigen::Matrix<double, 5, 1> final_trajectory_state;
537 final_trajectory_state << state(StateIdx::kX, 0), state(StateIdx::kY, 0),
538 state(StateIdx::kTheta, 0), state(StateIdx::kLeftVelocity, 0),
539 state(StateIdx::kRightVelocity, 0);
540 const Eigen::Matrix<double, 5, 1> final_trajectory_state_err =
541 final_trajectory_state - spline_drivetrain_.CurrentGoalState();
542 EXPECT_LT(final_trajectory_state_err.norm(), GetParam().goal_tolerance)
543 << "Goal error: " << final_trajectory_state_err.transpose();
544}
545
546INSTANTIATE_TEST_CASE_P(
547 LocalizerTest, ParameterizedLocalizerTest,
548 ::testing::Values(
549 // Checks a "perfect" scenario, where we should track perfectly.
550 LocalizerTestParams({
551 /*control_pts_x=*/{{0.0, 3.0, 3.0, 0.0, 1.0, 1.0}},
552 /*control_pts_y=*/{{-5.0, -5.0, 2.0, 2.0, 2.0, 3.0}},
James Kuszmaul074429e2019-03-23 16:01:49 -0700553 (TestLocalizer::State() << 0.0, -5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
554 0.0, 0.0)
James Kuszmaul1057ce82019-02-09 17:58:24 -0800555 .finished(),
James Kuszmaul074429e2019-03-23 16:01:49 -0700556 (TestLocalizer::State() << 0.0, -5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
557 0.0, 0.0)
James Kuszmaul1057ce82019-02-09 17:58:24 -0800558 .finished(),
559 /*noisify=*/false,
560 /*disturb=*/false,
561 /*estimate_tolerance=*/1e-5,
562 /*goal_tolerance=*/2e-2,
563 }),
564 // Checks "perfect" estimation, but start off the spline and check
565 // that we can still follow it.
566 LocalizerTestParams({
567 /*control_pts_x=*/{{0.0, 3.0, 3.0, 0.0, 1.0, 1.0}},
568 /*control_pts_y=*/{{-5.0, -5.0, 2.0, 2.0, 2.0, 3.0}},
James Kuszmaul074429e2019-03-23 16:01:49 -0700569 (TestLocalizer::State() << 0.0, -4.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
570 0.0, 0.0)
James Kuszmaul1057ce82019-02-09 17:58:24 -0800571 .finished(),
James Kuszmaul074429e2019-03-23 16:01:49 -0700572 (TestLocalizer::State() << 0.0, -4.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
573 0.0, 0.0)
James Kuszmaul1057ce82019-02-09 17:58:24 -0800574 .finished(),
575 /*noisify=*/false,
576 /*disturb=*/false,
577 /*estimate_tolerance=*/1e-5,
578 /*goal_tolerance=*/2e-2,
579 }),
580 // Repeats perfect scenario, but add sensor noise.
581 LocalizerTestParams({
582 /*control_pts_x=*/{{0.0, 3.0, 3.0, 0.0, 1.0, 1.0}},
583 /*control_pts_y=*/{{-5.0, -5.0, 2.0, 2.0, 2.0, 3.0}},
James Kuszmaul074429e2019-03-23 16:01:49 -0700584 (TestLocalizer::State() << 0.0, -5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
585 0.0, 0.0)
James Kuszmaul1057ce82019-02-09 17:58:24 -0800586 .finished(),
James Kuszmaul074429e2019-03-23 16:01:49 -0700587 (TestLocalizer::State() << 0.0, -5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
588 0.0, 0.0)
James Kuszmaul1057ce82019-02-09 17:58:24 -0800589 .finished(),
590 /*noisify=*/true,
591 /*disturb=*/false,
James Kuszmaulea314d92019-02-18 19:45:06 -0800592 /*estimate_tolerance=*/0.4,
James Kuszmaula5632fe2019-03-23 20:28:33 -0700593 /*goal_tolerance=*/0.4,
James Kuszmaul1057ce82019-02-09 17:58:24 -0800594 }),
595 // Repeats perfect scenario, but add initial estimator error.
596 LocalizerTestParams({
597 /*control_pts_x=*/{{0.0, 3.0, 3.0, 0.0, 1.0, 1.0}},
598 /*control_pts_y=*/{{-5.0, -5.0, 2.0, 2.0, 2.0, 3.0}},
James Kuszmaul074429e2019-03-23 16:01:49 -0700599 (TestLocalizer::State() << 0.0, -5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
600 0.0, 0.0)
James Kuszmaul1057ce82019-02-09 17:58:24 -0800601 .finished(),
James Kuszmaul074429e2019-03-23 16:01:49 -0700602 (TestLocalizer::State() << 0.1, -5.1, -0.01, 0.0, 0.0, 0.0, 0.0,
603 0.0, 0.0, 0.0)
604 .finished(),
605 /*noisify=*/false,
606 /*disturb=*/false,
607 /*estimate_tolerance=*/1e-4,
608 /*goal_tolerance=*/2e-2,
609 }),
610 // Repeats perfect scenario, but add voltage + angular errors:
611 LocalizerTestParams({
612 /*control_pts_x=*/{{0.0, 3.0, 3.0, 0.0, 1.0, 1.0}},
613 /*control_pts_y=*/{{-5.0, -5.0, 2.0, 2.0, 2.0, 3.0}},
614 (TestLocalizer::State() << 0.0, -5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0,
615 0.5, 0.02)
616 .finished(),
617 (TestLocalizer::State() << 0.1, -5.1, -0.01, 0.0, 0.0, 0.0, 0.0,
618 0.0, 0.0, 0.0)
James Kuszmaul1057ce82019-02-09 17:58:24 -0800619 .finished(),
620 /*noisify=*/false,
621 /*disturb=*/false,
622 /*estimate_tolerance=*/1e-4,
623 /*goal_tolerance=*/2e-2,
624 }),
625 // Add disturbances while we are driving:
626 LocalizerTestParams({
627 /*control_pts_x=*/{{0.0, 3.0, 3.0, 0.0, 1.0, 1.0}},
628 /*control_pts_y=*/{{-5.0, -5.0, 2.0, 2.0, 2.0, 3.0}},
James Kuszmaul074429e2019-03-23 16:01:49 -0700629 (TestLocalizer::State() << 0.0, -5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
630 0.0, 0.0)
James Kuszmaul1057ce82019-02-09 17:58:24 -0800631 .finished(),
James Kuszmaul074429e2019-03-23 16:01:49 -0700632 (TestLocalizer::State() << 0.0, -5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
633 0.0, 0.0)
James Kuszmaul1057ce82019-02-09 17:58:24 -0800634 .finished(),
635 /*noisify=*/false,
636 /*disturb=*/true,
James Kuszmaulea314d92019-02-18 19:45:06 -0800637 /*estimate_tolerance=*/3e-2,
James Kuszmaul1057ce82019-02-09 17:58:24 -0800638 /*goal_tolerance=*/0.15,
639 }),
640 // Add noise and some initial error in addition:
641 LocalizerTestParams({
642 /*control_pts_x=*/{{0.0, 3.0, 3.0, 0.0, 1.0, 1.0}},
643 /*control_pts_y=*/{{-5.0, -5.0, 2.0, 2.0, 2.0, 3.0}},
James Kuszmaul074429e2019-03-23 16:01:49 -0700644 (TestLocalizer::State() << 0.0, -5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
645 0.0, 0.0)
James Kuszmaul1057ce82019-02-09 17:58:24 -0800646 .finished(),
James Kuszmaul074429e2019-03-23 16:01:49 -0700647 (TestLocalizer::State() << 0.1, -5.1, 0.03, 0.0, 0.0, 0.0, 0.0, 0.0,
648 0.0, 0.0)
James Kuszmaul1057ce82019-02-09 17:58:24 -0800649 .finished(),
650 /*noisify=*/true,
651 /*disturb=*/true,
James Kuszmaul7f1a4082019-04-14 10:50:44 -0700652 /*estimate_tolerance=*/0.2,
James Kuszmaul074429e2019-03-23 16:01:49 -0700653 /*goal_tolerance=*/0.5,
James Kuszmaul1057ce82019-02-09 17:58:24 -0800654 }),
655 // Try another spline, just in case the one I was using is special for
656 // some reason; this path will also go straight up to a target, to
657 // better simulate what might happen when trying to score:
658 LocalizerTestParams({
659 /*control_pts_x=*/{{0.5, 3.5, 4.0, 8.0, 11.0, 10.2}},
660 /*control_pts_y=*/{{1.0, 1.0, -3.0, -2.0, -3.5, -3.65}},
James Kuszmaul074429e2019-03-23 16:01:49 -0700661 (TestLocalizer::State() << 0.6, 1.01, 0.01, 0.0, 0.0, 0.0, 0.0, 0.0,
662 0.0, 0.0)
James Kuszmaul1057ce82019-02-09 17:58:24 -0800663 .finished(),
James Kuszmaul074429e2019-03-23 16:01:49 -0700664 (TestLocalizer::State() << 0.5, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
665 0.0, 0.0)
James Kuszmaul1057ce82019-02-09 17:58:24 -0800666 .finished(),
667 /*noisify=*/true,
668 /*disturb=*/false,
James Kuszmaul7f1a4082019-04-14 10:50:44 -0700669 // TODO(james): Improve tests so that we aren't constantly
670 // readjusting the tolerances up.
James Kuszmaulc40c26e2019-03-24 16:26:43 -0700671 /*estimate_tolerance=*/0.3,
James Kuszmaul074429e2019-03-23 16:01:49 -0700672 /*goal_tolerance=*/0.7,
James Kuszmaul1057ce82019-02-09 17:58:24 -0800673 })));
674
675} // namespace testing
676} // namespace control_loops
677} // namespace y2019