James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 1 | #include "y2019/control_loops/drivetrain/localizer.h" |
| 2 | |
| 3 | #include <random> |
| 4 | #include <queue> |
| 5 | |
| 6 | #include "aos/testing/random_seed.h" |
| 7 | #include "aos/testing/test_shm.h" |
| 8 | #include "frc971/control_loops/drivetrain/trajectory.h" |
| 9 | #include "frc971/control_loops/drivetrain/splinedrivetrain.h" |
| 10 | #include "gflags/gflags.h" |
| 11 | #if defined(SUPPORT_PLOT) |
| 12 | #include "third_party/matplotlib-cpp/matplotlibcpp.h" |
| 13 | #endif |
| 14 | #include "gtest/gtest.h" |
| 15 | #include "y2019/control_loops/drivetrain/drivetrain_base.h" |
| 16 | #include "y2019/constants.h" |
| 17 | |
| 18 | DEFINE_bool(plot, false, "If true, plot"); |
| 19 | |
| 20 | namespace y2019 { |
| 21 | namespace control_loops { |
| 22 | namespace testing { |
| 23 | |
| 24 | using ::y2019::constants::Field; |
| 25 | |
| 26 | constexpr size_t kNumCameras = 4; |
| 27 | constexpr size_t kNumTargetsPerFrame = 3; |
| 28 | |
| 29 | typedef TypedLocalizer<kNumCameras, Field::kNumTargets, Field::kNumObstacles, |
| 30 | kNumTargetsPerFrame, double> TestLocalizer; |
| 31 | typedef typename TestLocalizer::Camera TestCamera; |
| 32 | typedef typename TestCamera::Pose Pose; |
| 33 | typedef typename TestCamera::LineSegment Obstacle; |
| 34 | |
| 35 | typedef TestLocalizer::StateIdx StateIdx; |
| 36 | |
| 37 | using ::frc971::control_loops::drivetrain::DrivetrainConfig; |
| 38 | |
| 39 | // When placing the cameras on the robot, set them all kCameraOffset out from |
| 40 | // the center, to test that we really can handle cameras that aren't at the |
| 41 | // center-of-mass. |
| 42 | constexpr double kCameraOffset = 0.1; |
| 43 | |
| 44 | #if defined(SUPPORT_PLOT) |
| 45 | // Plots a line from a vector of Pose's. |
| 46 | void PlotPlotPts(const ::std::vector<Pose> &poses, |
| 47 | const ::std::map<::std::string, ::std::string> &kwargs) { |
| 48 | ::std::vector<double> x; |
| 49 | ::std::vector<double> y; |
| 50 | for (const Pose & p : poses) { |
| 51 | x.push_back(p.abs_pos().x()); |
| 52 | y.push_back(p.abs_pos().y()); |
| 53 | } |
| 54 | matplotlibcpp::plot(x, y, kwargs); |
| 55 | } |
| 56 | #endif |
| 57 | |
| 58 | struct LocalizerTestParams { |
| 59 | // Control points for the spline to make the robot follow. |
| 60 | ::std::array<float, 6> control_pts_x; |
| 61 | ::std::array<float, 6> control_pts_y; |
| 62 | // The actual state to start the robot at. By setting voltage errors and the |
| 63 | // such you can introduce persistent disturbances. |
| 64 | TestLocalizer::State true_start_state; |
| 65 | // The initial state of the estimator. |
| 66 | TestLocalizer::State known_start_state; |
| 67 | // Whether or not to add Gaussian noise to the sensors and cameras. |
| 68 | bool noisify; |
| 69 | // Whether or not to add unmodelled disturbances. |
| 70 | bool disturb; |
| 71 | // The tolerances for the estimator and for the trajectory following at |
| 72 | // the end of the spline: |
| 73 | double estimate_tolerance; |
| 74 | double goal_tolerance; |
| 75 | }; |
| 76 | |
| 77 | class ParameterizedLocalizerTest |
| 78 | : public ::testing::TestWithParam<LocalizerTestParams> { |
| 79 | public: |
| 80 | ::aos::testing::TestSharedMemory shm_; |
| 81 | |
| 82 | // Set up three targets in a row, at (-1, 0), (0, 0), and (1, 0). |
| 83 | // Make the right-most target (1, 0) be facing away from the camera, and give |
| 84 | // the middle target some skew. |
| 85 | // Place one camera facing forward, the other facing backward, and set the |
| 86 | // robot at (0, -5) with the cameras each 0.1m from the center. |
| 87 | // Place one obstacle in a place where it can block the left-most target (-1, |
| 88 | // 0). |
| 89 | ParameterizedLocalizerTest() |
| 90 | : field_(), |
| 91 | targets_(field_.targets()), |
| 92 | modeled_obstacles_(field_.obstacles()), |
| 93 | true_obstacles_(field_.obstacles()), |
| 94 | dt_config_(drivetrain::GetDrivetrainConfig()), |
| 95 | // Pull the noise for the encoders/gyros from the R matrix: |
| 96 | encoder_noise_(::std::sqrt( |
| 97 | dt_config_.make_kf_drivetrain_loop().observer().coefficients().R( |
| 98 | 0, 0))), |
| 99 | gyro_noise_(::std::sqrt( |
| 100 | dt_config_.make_kf_drivetrain_loop().observer().coefficients().R( |
| 101 | 2, 2))), |
| 102 | // As per the comments in localizer.h, we set the field of view and |
| 103 | // noise parameters on the robot_cameras_ so that they see a bit more |
| 104 | // than the true_cameras_. The robot_cameras_ are what is passed to the |
| 105 | // localizer and used to generate "expected" targets. The true_cameras_ |
| 106 | // are what we actually use to generate targets to pass to the |
| 107 | // localizer. |
| 108 | robot_cameras_{ |
| 109 | {TestCamera({&robot_pose_, {0.0, kCameraOffset, 0.0}, M_PI_2}, |
| 110 | M_PI_2 * 1.1, robot_noise_parameters_, targets_, |
| 111 | modeled_obstacles_), |
| 112 | TestCamera({&robot_pose_, {kCameraOffset, 0.0, 0.0}, 0.0}, |
| 113 | M_PI_2 * 1.1, robot_noise_parameters_, targets_, |
| 114 | modeled_obstacles_), |
| 115 | TestCamera({&robot_pose_, {-kCameraOffset, 0.0, 0.0}, M_PI}, |
| 116 | M_PI_2 * 1.1, robot_noise_parameters_, targets_, |
| 117 | modeled_obstacles_), |
| 118 | TestCamera({&robot_pose_, {0.0, -kCameraOffset, 0.0}, -M_PI_2}, |
| 119 | M_PI_2 * 1.1, robot_noise_parameters_, targets_, |
| 120 | modeled_obstacles_)}}, |
| 121 | true_cameras_{ |
| 122 | {TestCamera({&true_robot_pose_, {0.0, kCameraOffset, 0.0}, M_PI_2}, |
| 123 | M_PI_2 * 0.9, true_noise_parameters_, targets_, |
| 124 | true_obstacles_), |
| 125 | TestCamera({&true_robot_pose_, {kCameraOffset, 0.0, 0.0}, 0.0}, |
| 126 | M_PI_2 * 0.9, true_noise_parameters_, targets_, |
| 127 | true_obstacles_), |
| 128 | TestCamera({&true_robot_pose_, {-kCameraOffset, 0.0, 0.0}, M_PI}, |
| 129 | M_PI_2 * 0.9, true_noise_parameters_, targets_, |
| 130 | true_obstacles_), |
| 131 | TestCamera( |
| 132 | {&true_robot_pose_, {-0.0, -kCameraOffset, 0.0}, -M_PI_2}, |
| 133 | M_PI_2 * 0.9, true_noise_parameters_, targets_, |
| 134 | true_obstacles_)}}, |
| 135 | localizer_(dt_config_, &robot_pose_), |
| 136 | spline_drivetrain_(dt_config_) { |
| 137 | // We use the default P() for initialization. |
| 138 | localizer_.ResetInitialState(t0_, GetParam().known_start_state, |
| 139 | localizer_.P()); |
| 140 | } |
| 141 | |
| 142 | void SetUp() { |
| 143 | ::frc971::control_loops::DrivetrainQueue::Goal goal; |
| 144 | goal.controller_type = 2; |
| 145 | goal.spline.spline_idx = 1; |
| 146 | goal.spline.spline_count = 1; |
| 147 | goal.spline_handle = 1; |
| 148 | ::std::copy(GetParam().control_pts_x.begin(), |
| 149 | GetParam().control_pts_x.end(), goal.spline.spline_x.begin()); |
| 150 | ::std::copy(GetParam().control_pts_y.begin(), |
| 151 | GetParam().control_pts_y.end(), goal.spline.spline_y.begin()); |
| 152 | spline_drivetrain_.SetGoal(goal); |
| 153 | } |
| 154 | |
| 155 | void TearDown() { |
| 156 | printf("Each iteration of the simulation took on average %f seconds.\n", |
| 157 | avg_time_.count()); |
| 158 | #if defined(SUPPORT_PLOT) |
| 159 | if (FLAGS_plot) { |
| 160 | matplotlibcpp::figure(); |
| 161 | matplotlibcpp::plot(simulation_t_, simulation_vl_, {{"label", "Vl"}}); |
| 162 | matplotlibcpp::plot(simulation_t_, simulation_vr_, {{"label", "Vr"}}); |
| 163 | matplotlibcpp::legend(); |
| 164 | |
| 165 | matplotlibcpp::figure(); |
| 166 | matplotlibcpp::plot(spline_x_, spline_y_, {{"label", "spline"}}); |
| 167 | matplotlibcpp::plot(simulation_x_, simulation_y_, {{"label", "robot"}}); |
| 168 | matplotlibcpp::plot(estimated_x_, estimated_y_, |
| 169 | {{"label", "estimation"}}); |
| 170 | for (const Target & target : targets_) { |
| 171 | PlotPlotPts(target.PlotPoints(), {{"c", "g"}}); |
| 172 | } |
| 173 | for (const Obstacle &obstacle : true_obstacles_) { |
| 174 | PlotPlotPts(obstacle.PlotPoints(), {{"c", "k"}}); |
| 175 | } |
| 176 | // Go through and plot true/expected camera targets for a few select |
| 177 | // time-steps. |
| 178 | ::std::vector<const char *> colors{"m", "y", "c"}; |
| 179 | int jj = 0; |
| 180 | for (size_t ii = 0; ii < simulation_x_.size(); ii += 400) { |
| 181 | *true_robot_pose_.mutable_pos() << simulation_x_[ii], simulation_y_[ii], |
| 182 | 0.0; |
| 183 | true_robot_pose_.set_theta(simulation_theta_[ii]); |
| 184 | for (const TestCamera &camera : true_cameras_) { |
| 185 | for (const auto &plot_pts : camera.PlotPoints()) { |
| 186 | PlotPlotPts(plot_pts, {{"c", colors[jj]}}); |
| 187 | } |
| 188 | } |
| 189 | for (const TestCamera &camera : robot_cameras_) { |
| 190 | *robot_pose_.mutable_pos() << estimated_x_[ii], estimated_y_[ii], 0.0; |
| 191 | robot_pose_.set_theta(estimated_theta_[ii]); |
| 192 | const auto &all_plot_pts = camera.PlotPoints(); |
| 193 | *robot_pose_.mutable_pos() = true_robot_pose_.rel_pos(); |
| 194 | robot_pose_.set_theta(true_robot_pose_.rel_theta()); |
| 195 | for (const auto &plot_pts : all_plot_pts) { |
| 196 | PlotPlotPts(plot_pts, {{"c", colors[jj]}, {"ls", "--"}}); |
| 197 | } |
| 198 | } |
| 199 | jj = (jj + 1) % colors.size(); |
| 200 | } |
| 201 | matplotlibcpp::legend(); |
| 202 | |
| 203 | matplotlibcpp::figure(); |
| 204 | matplotlibcpp::plot( |
| 205 | simulation_t_, spline_x_, |
| 206 | {{"label", "spline x"}, {"c", "g"}, {"ls", ""}, {"marker", "o"}}); |
| 207 | matplotlibcpp::plot(simulation_t_, simulation_x_, |
| 208 | {{"label", "simulated x"}, {"c", "g"}}); |
| 209 | matplotlibcpp::plot(simulation_t_, estimated_x_, |
| 210 | {{"label", "estimated x"}, {"c", "g"}, {"ls", "--"}}); |
| 211 | |
| 212 | matplotlibcpp::plot( |
| 213 | simulation_t_, spline_y_, |
| 214 | {{"label", "spline y"}, {"c", "b"}, {"ls", ""}, {"marker", "o"}}); |
| 215 | matplotlibcpp::plot(simulation_t_, simulation_y_, |
| 216 | {{"label", "simulated y"}, {"c", "b"}}); |
| 217 | matplotlibcpp::plot(simulation_t_, estimated_y_, |
| 218 | {{"label", "estimated y"}, {"c", "b"}, {"ls", "--"}}); |
| 219 | |
| 220 | matplotlibcpp::plot(simulation_t_, simulation_theta_, |
| 221 | {{"label", "simulated theta"}, {"c", "r"}}); |
| 222 | matplotlibcpp::plot( |
| 223 | simulation_t_, estimated_theta_, |
| 224 | {{"label", "estimated theta"}, {"c", "r"}, {"ls", "--"}}); |
| 225 | matplotlibcpp::legend(); |
| 226 | |
| 227 | matplotlibcpp::show(); |
| 228 | } |
| 229 | #endif |
| 230 | } |
| 231 | |
| 232 | protected: |
| 233 | // Returns a random number with a gaussian distribution with a mean of zero |
| 234 | // and a standard deviation of std, if noisify = true. |
| 235 | // If noisify is false, then returns 0.0. |
| 236 | double Normal(double std) { |
| 237 | if (GetParam().noisify) { |
| 238 | return normal_(gen_) * std; |
| 239 | } |
| 240 | return 0.0; |
| 241 | } |
| 242 | // Adds random noise to the given target view. |
| 243 | void Noisify(TestCamera::TargetView *view) { |
| 244 | view->reading.heading += Normal(view->noise.heading); |
| 245 | view->reading.distance += Normal(view->noise.distance); |
| 246 | view->reading.height += Normal(view->noise.height); |
| 247 | view->reading.skew += Normal(view->noise.skew); |
| 248 | } |
| 249 | // The differential equation for the dynamics of the system. |
| 250 | TestLocalizer::State DiffEq(const TestLocalizer::State &X, |
| 251 | const TestLocalizer::Input &U) { |
| 252 | return localizer_.DiffEq(X, U); |
| 253 | } |
| 254 | |
| 255 | Field field_; |
| 256 | ::std::array<Target, Field::kNumTargets> targets_; |
| 257 | // The obstacles that are passed to the camera objects for the localizer. |
| 258 | ::std::array<Obstacle, Field::kNumObstacles> modeled_obstacles_; |
| 259 | // The obstacles that are used for actually simulating the cameras. |
| 260 | ::std::array<Obstacle, Field::kNumObstacles> true_obstacles_; |
| 261 | |
| 262 | DrivetrainConfig<double> dt_config_; |
| 263 | |
| 264 | // Noise information for the actual simulated cameras (true_*) and the |
| 265 | // parameters that the localizer should use for modelling the cameras. Note |
| 266 | // how the max_viewable_distance is larger for the localizer, so that if |
| 267 | // there is any error in the estimation, it still thinks that it can see |
| 268 | // any targets that might actually be in range. |
| 269 | TestCamera::NoiseParameters true_noise_parameters_ = { |
| 270 | .max_viewable_distance = 10.0, |
| 271 | .heading_noise = 0.02, |
| 272 | .nominal_distance_noise = 0.06, |
| 273 | .nominal_skew_noise = 0.1, |
| 274 | .nominal_height_noise = 0.01}; |
| 275 | TestCamera::NoiseParameters robot_noise_parameters_ = { |
| 276 | .max_viewable_distance = 11.0, |
| 277 | .heading_noise = 0.02, |
| 278 | .nominal_distance_noise = 0.06, |
| 279 | .nominal_skew_noise = 0.1, |
| 280 | .nominal_height_noise = 0.01}; |
| 281 | |
| 282 | // Standard deviations of the noise for the encoders/gyro. |
| 283 | double encoder_noise_, gyro_noise_; |
| 284 | |
| 285 | Pose robot_pose_; |
| 286 | ::std::array<TestCamera, 4> robot_cameras_; |
| 287 | Pose true_robot_pose_; |
| 288 | ::std::array<TestCamera, 4> true_cameras_; |
| 289 | |
| 290 | TestLocalizer localizer_; |
| 291 | |
| 292 | ::frc971::control_loops::drivetrain::SplineDrivetrain spline_drivetrain_; |
| 293 | |
| 294 | // All the data we want to end up plotting. |
| 295 | ::std::vector<double> simulation_t_; |
| 296 | |
| 297 | ::std::vector<double> spline_x_; |
| 298 | ::std::vector<double> spline_y_; |
| 299 | ::std::vector<double> estimated_x_; |
| 300 | ::std::vector<double> estimated_y_; |
| 301 | ::std::vector<double> estimated_theta_; |
| 302 | ::std::vector<double> simulation_x_; |
| 303 | ::std::vector<double> simulation_y_; |
| 304 | ::std::vector<double> simulation_theta_; |
| 305 | |
| 306 | ::std::vector<double> simulation_vl_; |
| 307 | ::std::vector<double> simulation_vr_; |
| 308 | |
| 309 | // Simulation start time |
| 310 | ::aos::monotonic_clock::time_point t0_; |
| 311 | |
| 312 | // Average duration of each iteration; used for debugging and getting a |
| 313 | // sanity-check on what performance looks like--uses a real system clock. |
| 314 | ::std::chrono::duration<double> avg_time_; |
| 315 | |
| 316 | ::std::mt19937 gen_{static_cast<uint32_t>(::aos::testing::RandomSeed())}; |
| 317 | ::std::normal_distribution<> normal_; |
| 318 | }; |
| 319 | |
| 320 | // Tests that, when we attempt to follow a spline and use the localizer to |
| 321 | // perform the state estimation, we end up roughly where we should and that |
| 322 | // the localizer has a valid position estimate. |
| 323 | TEST_P(ParameterizedLocalizerTest, SplineTest) { |
| 324 | // state stores the true state of the robot throughout the simulation. |
| 325 | TestLocalizer::State state = GetParam().true_start_state; |
| 326 | |
| 327 | ::aos::monotonic_clock::time_point t = t0_; |
| 328 | |
| 329 | // The period with which we should take frames from the cameras. Currently, |
| 330 | // we just trigger all the cameras at once, rather than offsetting them or |
| 331 | // anything. |
| 332 | const int camera_period = 5; // cycles |
| 333 | // The amount of time to delay the camera images from when they are taken. |
| 334 | const ::std::chrono::milliseconds camera_latency(50); |
| 335 | |
| 336 | // A queue of camera frames so that we can add a time delay to the data |
| 337 | // coming from the cameras. |
| 338 | ::std::queue<::std::tuple< |
| 339 | ::aos::monotonic_clock::time_point, const TestCamera *, |
| 340 | ::aos::SizedArray<TestCamera::TargetView, kNumTargetsPerFrame>>> |
| 341 | camera_queue; |
| 342 | |
| 343 | // Start time, for debugging. |
| 344 | const auto begin = ::std::chrono::steady_clock::now(); |
| 345 | |
| 346 | size_t i; |
| 347 | for (i = 0; !spline_drivetrain_.IsAtEnd(); ++i) { |
| 348 | // Get the current state estimate into a matrix that works for the |
| 349 | // trajectory code. |
| 350 | ::Eigen::Matrix<double, 5, 1> known_state; |
| 351 | TestLocalizer::State X_hat = localizer_.X_hat(); |
| 352 | known_state << X_hat(StateIdx::kX, 0), X_hat(StateIdx::kY, 0), |
| 353 | X_hat(StateIdx::kTheta, 0), X_hat(StateIdx::kLeftVelocity, 0), |
| 354 | X_hat(StateIdx::kRightVelocity, 0); |
| 355 | |
| 356 | spline_drivetrain_.Update(true, known_state); |
| 357 | |
| 358 | ::frc971::control_loops::DrivetrainQueue::Output output; |
| 359 | output.left_voltage = 0; |
| 360 | output.right_voltage = 0; |
| 361 | spline_drivetrain_.SetOutput(&output); |
| 362 | TestLocalizer::Input U(output.left_voltage, output.right_voltage); |
| 363 | |
| 364 | const ::Eigen::Matrix<double, 5, 1> goal_state = |
| 365 | spline_drivetrain_.CurrentGoalState(); |
| 366 | simulation_t_.push_back( |
| 367 | ::std::chrono::duration_cast<::std::chrono::duration<double>>( |
| 368 | t.time_since_epoch()) |
| 369 | .count()); |
| 370 | spline_x_.push_back(goal_state(0, 0)); |
| 371 | spline_y_.push_back(goal_state(1, 0)); |
| 372 | simulation_x_.push_back(state(StateIdx::kX, 0)); |
| 373 | simulation_y_.push_back(state(StateIdx::kY, 0)); |
| 374 | simulation_theta_.push_back(state(StateIdx::kTheta, 0)); |
| 375 | estimated_x_.push_back(known_state(0, 0)); |
| 376 | estimated_y_.push_back(known_state(1, 0)); |
| 377 | estimated_theta_.push_back(known_state(StateIdx::kTheta, 0)); |
| 378 | |
| 379 | simulation_vl_.push_back(U(0)); |
| 380 | simulation_vr_.push_back(U(1)); |
| 381 | U(0, 0) = ::std::max(::std::min(U(0, 0), 12.0), -12.0); |
| 382 | U(1, 0) = ::std::max(::std::min(U(1, 0), 12.0), -12.0); |
| 383 | |
| 384 | state = ::frc971::control_loops::RungeKuttaU( |
| 385 | [this](const ::Eigen::Matrix<double, 10, 1> &X, |
| 386 | const ::Eigen::Matrix<double, 2, 1> &U) { return DiffEq(X, U); }, |
| 387 | state, U, |
| 388 | ::std::chrono::duration_cast<::std::chrono::duration<double>>( |
| 389 | dt_config_.dt) |
| 390 | .count()); |
| 391 | |
| 392 | // Add arbitrary disturbances at some arbitrary interval. The main points of |
| 393 | // interest here are that we (a) stop adding disturbances at the very end of |
| 394 | // the trajectory, to allow us to actually converge to the goal, and (b) |
| 395 | // scale disturbances by the corruent velocity. |
| 396 | if (GetParam().disturb && i % 50 == 0) { |
| 397 | // Scale the disturbance so that when we have near-zero velocity we don't |
| 398 | // have much disturbance. |
| 399 | double disturbance_scale = ::std::min( |
| 400 | 1.0, ::std::sqrt(::std::pow(state(StateIdx::kLeftVelocity, 0), 2) + |
| 401 | ::std::pow(state(StateIdx::kRightVelocity, 0), 2)) / |
| 402 | 3.0); |
| 403 | TestLocalizer::State disturbance; |
| 404 | disturbance << 0.02, 0.02, 0.001, 0.03, 0.02, 0.0, 0.0, 0.0, 0.0, 0.0; |
| 405 | disturbance *= disturbance_scale; |
| 406 | state += disturbance; |
| 407 | } |
| 408 | |
| 409 | t += dt_config_.dt; |
| 410 | *true_robot_pose_.mutable_pos() << state(StateIdx::kX, 0), |
| 411 | state(StateIdx::kY, 0), 0.0; |
| 412 | true_robot_pose_.set_theta(state(StateIdx::kTheta, 0)); |
| 413 | const double left_enc = state(StateIdx::kLeftEncoder, 0); |
| 414 | const double right_enc = state(StateIdx::kRightEncoder, 0); |
| 415 | |
| 416 | const double gyro = (state(StateIdx::kRightVelocity, 0) - |
| 417 | state(StateIdx::kLeftVelocity, 0)) / |
| 418 | dt_config_.robot_radius / 2.0; |
| 419 | |
| 420 | localizer_.UpdateEncodersAndGyro(left_enc + Normal(encoder_noise_), |
| 421 | right_enc + Normal(encoder_noise_), |
| 422 | gyro + Normal(gyro_noise_), U, t); |
| 423 | |
| 424 | // Clear out the camera frames that we are ready to pass to the localizer. |
| 425 | while (!camera_queue.empty() && |
| 426 | ::std::get<0>(camera_queue.front()) < t - camera_latency) { |
| 427 | const auto tuple = camera_queue.front(); |
| 428 | camera_queue.pop(); |
| 429 | ::aos::monotonic_clock::time_point t_obs = ::std::get<0>(tuple); |
| 430 | const TestCamera *camera = ::std::get<1>(tuple); |
| 431 | ::aos::SizedArray<TestCamera::TargetView, kNumTargetsPerFrame> views = |
| 432 | ::std::get<2>(tuple); |
| 433 | localizer_.UpdateTargets(*camera, views, t_obs); |
| 434 | } |
| 435 | |
| 436 | // Actually take all the images and store them in the queue. |
| 437 | if (i % camera_period == 0) { |
| 438 | for (size_t jj = 0; jj < true_cameras_.size(); ++jj) { |
| 439 | const auto target_views = true_cameras_[jj].target_views(); |
| 440 | ::aos::SizedArray<TestCamera::TargetView, kNumTargetsPerFrame> |
| 441 | pass_views; |
| 442 | for (size_t ii = 0; |
| 443 | ii < ::std::min(kNumTargetsPerFrame, target_views.size()); ++ii) { |
| 444 | TestCamera::TargetView view = target_views[ii]; |
| 445 | Noisify(&view); |
| 446 | pass_views.push_back(view); |
| 447 | } |
| 448 | camera_queue.emplace(t, &robot_cameras_[jj], pass_views); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | } |
| 453 | |
| 454 | const auto end = ::std::chrono::steady_clock::now(); |
| 455 | avg_time_ = (end - begin) / i; |
| 456 | TestLocalizer::State estimate_err = state - localizer_.X_hat(); |
| 457 | EXPECT_LT(estimate_err.template topRows<7>().norm(), |
| 458 | GetParam().estimate_tolerance); |
| 459 | // Check that none of the states that we actually care about (x/y/theta, and |
| 460 | // wheel positions/speeds) are too far off, individually: |
| 461 | EXPECT_LT(estimate_err.template topRows<7>().cwiseAbs().maxCoeff(), |
| 462 | GetParam().estimate_tolerance / 3.0) |
| 463 | << "Estimate error: " << estimate_err.transpose(); |
| 464 | Eigen::Matrix<double, 5, 1> final_trajectory_state; |
| 465 | final_trajectory_state << state(StateIdx::kX, 0), state(StateIdx::kY, 0), |
| 466 | state(StateIdx::kTheta, 0), state(StateIdx::kLeftVelocity, 0), |
| 467 | state(StateIdx::kRightVelocity, 0); |
| 468 | const Eigen::Matrix<double, 5, 1> final_trajectory_state_err = |
| 469 | final_trajectory_state - spline_drivetrain_.CurrentGoalState(); |
| 470 | EXPECT_LT(final_trajectory_state_err.norm(), GetParam().goal_tolerance) |
| 471 | << "Goal error: " << final_trajectory_state_err.transpose(); |
| 472 | } |
| 473 | |
| 474 | INSTANTIATE_TEST_CASE_P( |
| 475 | LocalizerTest, ParameterizedLocalizerTest, |
| 476 | ::testing::Values( |
| 477 | // Checks a "perfect" scenario, where we should track perfectly. |
| 478 | LocalizerTestParams({ |
| 479 | /*control_pts_x=*/{{0.0, 3.0, 3.0, 0.0, 1.0, 1.0}}, |
| 480 | /*control_pts_y=*/{{-5.0, -5.0, 2.0, 2.0, 2.0, 3.0}}, |
| 481 | (TestLocalizer::State() << 0.0, -5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, |
| 482 | 0.0, 0.0) |
| 483 | .finished(), |
| 484 | (TestLocalizer::State() << 0.0, -5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, |
| 485 | 0.0, 0.0) |
| 486 | .finished(), |
| 487 | /*noisify=*/false, |
| 488 | /*disturb=*/false, |
| 489 | /*estimate_tolerance=*/1e-5, |
| 490 | /*goal_tolerance=*/2e-2, |
| 491 | }), |
| 492 | // Checks "perfect" estimation, but start off the spline and check |
| 493 | // that we can still follow it. |
| 494 | LocalizerTestParams({ |
| 495 | /*control_pts_x=*/{{0.0, 3.0, 3.0, 0.0, 1.0, 1.0}}, |
| 496 | /*control_pts_y=*/{{-5.0, -5.0, 2.0, 2.0, 2.0, 3.0}}, |
| 497 | (TestLocalizer::State() << 0.0, -4.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, |
| 498 | 0.0, 0.0) |
| 499 | .finished(), |
| 500 | (TestLocalizer::State() << 0.0, -4.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, |
| 501 | 0.0, 0.0) |
| 502 | .finished(), |
| 503 | /*noisify=*/false, |
| 504 | /*disturb=*/false, |
| 505 | /*estimate_tolerance=*/1e-5, |
| 506 | /*goal_tolerance=*/2e-2, |
| 507 | }), |
| 508 | // Repeats perfect scenario, but add sensor noise. |
| 509 | LocalizerTestParams({ |
| 510 | /*control_pts_x=*/{{0.0, 3.0, 3.0, 0.0, 1.0, 1.0}}, |
| 511 | /*control_pts_y=*/{{-5.0, -5.0, 2.0, 2.0, 2.0, 3.0}}, |
| 512 | (TestLocalizer::State() << 0.0, -5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, |
| 513 | 0.0, 0.0) |
| 514 | .finished(), |
| 515 | (TestLocalizer::State() << 0.0, -5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, |
| 516 | 0.0, 0.0) |
| 517 | .finished(), |
| 518 | /*noisify=*/true, |
| 519 | /*disturb=*/false, |
| 520 | /*estimate_tolerance=*/0.2, |
| 521 | /*goal_tolerance=*/0.2, |
| 522 | }), |
| 523 | // Repeats perfect scenario, but add initial estimator error. |
| 524 | LocalizerTestParams({ |
| 525 | /*control_pts_x=*/{{0.0, 3.0, 3.0, 0.0, 1.0, 1.0}}, |
| 526 | /*control_pts_y=*/{{-5.0, -5.0, 2.0, 2.0, 2.0, 3.0}}, |
| 527 | (TestLocalizer::State() << 0.0, -5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, |
| 528 | 0.0, 0.0) |
| 529 | .finished(), |
| 530 | (TestLocalizer::State() << 0.1, -5.1, -0.01, 0.0, 0.0, 0.0, 0.0, |
| 531 | 0.0, 0.0, 0.0) |
| 532 | .finished(), |
| 533 | /*noisify=*/false, |
| 534 | /*disturb=*/false, |
| 535 | /*estimate_tolerance=*/1e-4, |
| 536 | /*goal_tolerance=*/2e-2, |
| 537 | }), |
| 538 | // Repeats perfect scenario, but add voltage + angular errors: |
| 539 | LocalizerTestParams({ |
| 540 | /*control_pts_x=*/{{0.0, 3.0, 3.0, 0.0, 1.0, 1.0}}, |
| 541 | /*control_pts_y=*/{{-5.0, -5.0, 2.0, 2.0, 2.0, 3.0}}, |
| 542 | (TestLocalizer::State() << 0.0, -5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, |
| 543 | 0.5, 0.02) |
| 544 | .finished(), |
| 545 | (TestLocalizer::State() << 0.1, -5.1, -0.01, 0.0, 0.0, 0.0, 0.0, |
| 546 | 0.0, 0.0, 0.0) |
| 547 | .finished(), |
| 548 | /*noisify=*/false, |
| 549 | /*disturb=*/false, |
| 550 | /*estimate_tolerance=*/1e-4, |
| 551 | /*goal_tolerance=*/2e-2, |
| 552 | }), |
| 553 | // Add disturbances while we are driving: |
| 554 | LocalizerTestParams({ |
| 555 | /*control_pts_x=*/{{0.0, 3.0, 3.0, 0.0, 1.0, 1.0}}, |
| 556 | /*control_pts_y=*/{{-5.0, -5.0, 2.0, 2.0, 2.0, 3.0}}, |
| 557 | (TestLocalizer::State() << 0.0, -5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, |
| 558 | 0.0, 0.0) |
| 559 | .finished(), |
| 560 | (TestLocalizer::State() << 0.0, -5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, |
| 561 | 0.0, 0.0) |
| 562 | .finished(), |
| 563 | /*noisify=*/false, |
| 564 | /*disturb=*/true, |
| 565 | /*estimate_tolerance=*/2e-2, |
| 566 | /*goal_tolerance=*/0.15, |
| 567 | }), |
| 568 | // Add noise and some initial error in addition: |
| 569 | LocalizerTestParams({ |
| 570 | /*control_pts_x=*/{{0.0, 3.0, 3.0, 0.0, 1.0, 1.0}}, |
| 571 | /*control_pts_y=*/{{-5.0, -5.0, 2.0, 2.0, 2.0, 3.0}}, |
| 572 | (TestLocalizer::State() << 0.0, -5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, |
| 573 | 0.0, 0.0) |
| 574 | .finished(), |
| 575 | (TestLocalizer::State() << 0.1, -5.1, 0.03, 0.0, 0.0, 0.0, 0.0, 0.0, |
| 576 | 0.0, 0.0) |
| 577 | .finished(), |
| 578 | /*noisify=*/true, |
| 579 | /*disturb=*/true, |
| 580 | /*estimate_tolerance=*/0.15, |
| 581 | /*goal_tolerance=*/0.5, |
| 582 | }), |
| 583 | // Try another spline, just in case the one I was using is special for |
| 584 | // some reason; this path will also go straight up to a target, to |
| 585 | // better simulate what might happen when trying to score: |
| 586 | LocalizerTestParams({ |
| 587 | /*control_pts_x=*/{{0.5, 3.5, 4.0, 8.0, 11.0, 10.2}}, |
| 588 | /*control_pts_y=*/{{1.0, 1.0, -3.0, -2.0, -3.5, -3.65}}, |
| 589 | (TestLocalizer::State() << 0.6, 1.01, 0.01, 0.0, 0.0, 0.0, 0.0, 0.0, |
| 590 | 0.0, 0.0) |
| 591 | .finished(), |
| 592 | (TestLocalizer::State() << 0.5, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, |
| 593 | 0.0, 0.0) |
| 594 | .finished(), |
| 595 | /*noisify=*/true, |
| 596 | /*disturb=*/false, |
| 597 | /*estimate_tolerance=*/0.1, |
| 598 | /*goal_tolerance=*/0.5, |
| 599 | }))); |
| 600 | |
| 601 | } // namespace testing |
| 602 | } // namespace control_loops |
| 603 | } // namespace y2019 |