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