James Kuszmaul | 04a343c | 2023-02-20 16:38:22 -0800 | [diff] [blame] | 1 | #include "y2023/localizer/localizer.h" |
| 2 | |
| 3 | #include "aos/events/logging/log_writer.h" |
| 4 | #include "aos/events/simulated_event_loop.h" |
| 5 | #include "frc971/control_loops/drivetrain/drivetrain_test_lib.h" |
| 6 | #include "frc971/control_loops/drivetrain/localizer_generated.h" |
| 7 | #include "frc971/control_loops/pose.h" |
| 8 | #include "frc971/vision/target_map_generated.h" |
| 9 | #include "gtest/gtest.h" |
| 10 | #include "y2023/constants/simulated_constants_sender.h" |
| 11 | #include "y2023/control_loops/drivetrain/drivetrain_base.h" |
| 12 | #include "y2023/localizer/status_generated.h" |
James Kuszmaul | 18008f8 | 2023-02-23 20:52:50 -0800 | [diff] [blame] | 13 | #include "y2023/localizer/utils.h" |
James Kuszmaul | 04a343c | 2023-02-20 16:38:22 -0800 | [diff] [blame] | 14 | |
| 15 | DEFINE_string(output_folder, "", |
| 16 | "If set, logs all channels to the provided logfile."); |
| 17 | DECLARE_bool(die_on_malloc); |
| 18 | |
| 19 | namespace y2023::localizer::testing { |
| 20 | |
| 21 | using frc971::control_loops::drivetrain::Output; |
| 22 | |
| 23 | class LocalizerTest : public ::testing::Test { |
| 24 | protected: |
| 25 | static constexpr uint64_t kTargetId = 1; |
| 26 | LocalizerTest() |
| 27 | : configuration_(aos::configuration::ReadConfig("y2023/aos_config.json")), |
| 28 | event_loop_factory_(&configuration_.message()), |
| 29 | roborio_node_([this]() { |
| 30 | // Get the constants sent before anything else happens. |
| 31 | // It has nothing to do with the roborio node. |
| 32 | SendSimulationConstants(&event_loop_factory_, 7971, |
| 33 | "y2023/constants/test_constants.json"); |
| 34 | return aos::configuration::GetNode(&configuration_.message(), |
| 35 | "roborio"); |
| 36 | }()), |
| 37 | imu_node_( |
| 38 | aos::configuration::GetNode(&configuration_.message(), "imu")), |
| 39 | camera_node_( |
| 40 | aos::configuration::GetNode(&configuration_.message(), "pi1")), |
| 41 | dt_config_(frc971::control_loops::drivetrain::testing:: |
| 42 | GetTestDrivetrainConfig()), |
| 43 | localizer_event_loop_( |
| 44 | event_loop_factory_.MakeEventLoop("localizer", imu_node_)), |
| 45 | localizer_(localizer_event_loop_.get(), dt_config_), |
| 46 | drivetrain_plant_event_loop_(event_loop_factory_.MakeEventLoop( |
| 47 | "drivetrain_plant", roborio_node_)), |
| 48 | drivetrain_plant_imu_event_loop_( |
| 49 | event_loop_factory_.MakeEventLoop("drivetrain_plant", imu_node_)), |
| 50 | drivetrain_plant_(drivetrain_plant_event_loop_.get(), |
| 51 | drivetrain_plant_imu_event_loop_.get(), dt_config_, |
| 52 | std::chrono::microseconds(500)), |
| 53 | roborio_test_event_loop_( |
| 54 | event_loop_factory_.MakeEventLoop("test", roborio_node_)), |
| 55 | imu_test_event_loop_( |
| 56 | event_loop_factory_.MakeEventLoop("test", imu_node_)), |
| 57 | camera_test_event_loop_( |
| 58 | event_loop_factory_.MakeEventLoop("test", camera_node_)), |
| 59 | logger_test_event_loop_( |
| 60 | event_loop_factory_.GetNodeEventLoopFactory("logger") |
| 61 | ->MakeEventLoop("test")), |
| 62 | constants_fetcher_(imu_test_event_loop_.get()), |
| 63 | output_sender_( |
| 64 | roborio_test_event_loop_->MakeSender<Output>("/drivetrain")), |
| 65 | target_sender_( |
| 66 | camera_test_event_loop_->MakeSender<frc971::vision::TargetMap>( |
| 67 | "/camera")), |
| 68 | control_sender_(roborio_test_event_loop_->MakeSender< |
| 69 | frc971::control_loops::drivetrain::LocalizerControl>( |
| 70 | "/drivetrain")), |
| 71 | output_fetcher_( |
| 72 | roborio_test_event_loop_ |
| 73 | ->MakeFetcher<frc971::controls::LocalizerOutput>("/localizer")), |
| 74 | status_fetcher_( |
| 75 | imu_test_event_loop_->MakeFetcher<Status>("/localizer")) { |
| 76 | FLAGS_die_on_malloc = true; |
| 77 | { |
| 78 | aos::TimerHandler *timer = roborio_test_event_loop_->AddTimer([this]() { |
| 79 | { |
| 80 | auto builder = output_sender_.MakeBuilder(); |
| 81 | auto output_builder = builder.MakeBuilder<Output>(); |
| 82 | output_builder.add_left_voltage(output_voltages_(0)); |
| 83 | output_builder.add_right_voltage(output_voltages_(1)); |
| 84 | builder.CheckOk(builder.Send(output_builder.Finish())); |
| 85 | } |
| 86 | }); |
| 87 | roborio_test_event_loop_->OnRun([timer, this]() { |
| 88 | timer->Setup(roborio_test_event_loop_->monotonic_now(), |
| 89 | std::chrono::milliseconds(5)); |
| 90 | }); |
| 91 | } |
| 92 | { |
| 93 | // Sanity check that the test calibration files look like what we |
| 94 | // expect. |
| 95 | CHECK_EQ("pi1", constants_fetcher_.constants() |
| 96 | .cameras() |
| 97 | ->Get(0) |
| 98 | ->calibration() |
| 99 | ->node_name() |
| 100 | ->string_view()); |
| 101 | const Eigen::Matrix<double, 4, 4> H_robot_camera = |
| 102 | frc971::control_loops::drivetrain::FlatbufferToTransformationMatrix( |
| 103 | *constants_fetcher_.constants() |
| 104 | .cameras() |
| 105 | ->Get(0) |
| 106 | ->calibration() |
| 107 | ->fixed_extrinsics()); |
| 108 | |
| 109 | CHECK_EQ(kTargetId, constants_fetcher_.constants() |
| 110 | .target_map() |
| 111 | ->target_poses() |
| 112 | ->Get(0) |
| 113 | ->id()); |
| 114 | const Eigen::Matrix<double, 4, 4> H_field_target = PoseToTransform( |
| 115 | constants_fetcher_.constants().target_map()->target_poses()->Get(0)); |
| 116 | // For reference, the camera should pointed straight forwards on the |
| 117 | // robot, offset by 1 meter. |
| 118 | aos::TimerHandler *timer = camera_test_event_loop_->AddTimer( |
| 119 | [this, H_robot_camera, H_field_target]() { |
| 120 | if (!send_targets_) { |
| 121 | return; |
| 122 | } |
| 123 | const frc971::control_loops::Pose robot_pose( |
| 124 | {drivetrain_plant_.GetPosition().x(), |
| 125 | drivetrain_plant_.GetPosition().y(), 0.0}, |
| 126 | drivetrain_plant_.state()(2, 0)); |
| 127 | |
| 128 | const Eigen::Matrix<double, 4, 4> H_field_camera = |
| 129 | robot_pose.AsTransformationMatrix() * H_robot_camera; |
| 130 | const Eigen::Matrix<double, 4, 4> H_camera_target = |
| 131 | H_field_camera.inverse() * H_field_target; |
| 132 | |
| 133 | const Eigen::Quaterniond quat(H_camera_target.block<3, 3>(0, 0)); |
| 134 | const Eigen::Vector3d translation( |
| 135 | H_camera_target.block<3, 1>(0, 3)); |
| 136 | |
| 137 | auto builder = target_sender_.MakeBuilder(); |
| 138 | frc971::vision::Quaternion::Builder quat_builder(*builder.fbb()); |
| 139 | quat_builder.add_w(quat.w()); |
| 140 | quat_builder.add_x(quat.x()); |
| 141 | quat_builder.add_y(quat.y()); |
| 142 | quat_builder.add_z(quat.z()); |
| 143 | auto quat_offset = quat_builder.Finish(); |
| 144 | frc971::vision::Position::Builder position_builder(*builder.fbb()); |
| 145 | position_builder.add_x(translation.x()); |
| 146 | position_builder.add_y(translation.y()); |
| 147 | position_builder.add_z(translation.z()); |
| 148 | auto position_offset = position_builder.Finish(); |
| 149 | |
| 150 | frc971::vision::TargetPoseFbs::Builder target_builder( |
| 151 | *builder.fbb()); |
| 152 | target_builder.add_id(send_target_id_); |
| 153 | target_builder.add_position(position_offset); |
| 154 | target_builder.add_orientation(quat_offset); |
| 155 | auto target_offset = target_builder.Finish(); |
| 156 | |
| 157 | auto targets_offset = builder.fbb()->CreateVector({target_offset}); |
| 158 | frc971::vision::TargetMap::Builder map_builder(*builder.fbb()); |
| 159 | map_builder.add_target_poses(targets_offset); |
| 160 | map_builder.add_monotonic_timestamp_ns( |
| 161 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 162 | camera_test_event_loop_->monotonic_now().time_since_epoch()) |
| 163 | .count()); |
| 164 | |
| 165 | builder.CheckOk(builder.Send(map_builder.Finish())); |
| 166 | }); |
| 167 | camera_test_event_loop_->OnRun([timer, this]() { |
| 168 | timer->Setup(camera_test_event_loop_->monotonic_now(), |
| 169 | std::chrono::milliseconds(50)); |
| 170 | }); |
| 171 | } |
| 172 | |
| 173 | localizer_control_send_timer_ = |
| 174 | roborio_test_event_loop_->AddTimer([this]() { |
| 175 | auto builder = control_sender_.MakeBuilder(); |
| 176 | auto control_builder = builder.MakeBuilder< |
| 177 | frc971::control_loops::drivetrain::LocalizerControl>(); |
| 178 | control_builder.add_x(localizer_control_x_); |
| 179 | control_builder.add_y(localizer_control_y_); |
| 180 | control_builder.add_theta(localizer_control_theta_); |
| 181 | control_builder.add_theta_uncertainty(0.01); |
| 182 | control_builder.add_keep_current_theta(false); |
| 183 | builder.CheckOk(builder.Send(control_builder.Finish())); |
| 184 | }); |
| 185 | |
| 186 | // Get things zeroed. |
| 187 | event_loop_factory_.RunFor(std::chrono::seconds(10)); |
| 188 | CHECK(status_fetcher_.Fetch()); |
| 189 | CHECK(status_fetcher_->imu()->zeroed()); |
| 190 | |
| 191 | if (!FLAGS_output_folder.empty()) { |
| 192 | logger_event_loop_ = |
| 193 | event_loop_factory_.MakeEventLoop("logger", imu_node_); |
| 194 | logger_ = std::make_unique<aos::logger::Logger>(logger_event_loop_.get()); |
| 195 | logger_->StartLoggingOnRun(FLAGS_output_folder); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | void SendLocalizerControl(double x, double y, double theta) { |
| 200 | localizer_control_x_ = x; |
| 201 | localizer_control_y_ = y; |
| 202 | localizer_control_theta_ = theta; |
| 203 | localizer_control_send_timer_->Setup( |
| 204 | roborio_test_event_loop_->monotonic_now()); |
| 205 | } |
| 206 | ::testing::AssertionResult IsNear(double expected, double actual, |
| 207 | double epsilon) { |
| 208 | if (std::abs(expected - actual) < epsilon) { |
| 209 | return ::testing::AssertionSuccess(); |
| 210 | } else { |
| 211 | return ::testing::AssertionFailure() |
| 212 | << "Expected " << expected << " but got " << actual |
| 213 | << " with a max difference of " << epsilon |
| 214 | << " and an actual difference of " << std::abs(expected - actual); |
| 215 | } |
| 216 | } |
| 217 | ::testing::AssertionResult VerifyEstimatorAccurate(double eps) { |
| 218 | const Eigen::Matrix<double, 5, 1> true_state = drivetrain_plant_.state(); |
| 219 | ::testing::AssertionResult result(true); |
| 220 | status_fetcher_.Fetch(); |
| 221 | if (!(result = IsNear(status_fetcher_->state()->x(), true_state(0), eps))) { |
| 222 | return result; |
| 223 | } |
| 224 | if (!(result = IsNear(status_fetcher_->state()->y(), true_state(1), eps))) { |
| 225 | return result; |
| 226 | } |
| 227 | if (!(result = |
| 228 | IsNear(status_fetcher_->state()->theta(), true_state(2), eps))) { |
| 229 | return result; |
| 230 | } |
| 231 | return result; |
| 232 | } |
| 233 | |
| 234 | aos::FlatbufferDetachedBuffer<aos::Configuration> configuration_; |
| 235 | aos::SimulatedEventLoopFactory event_loop_factory_; |
| 236 | const aos::Node *const roborio_node_; |
| 237 | const aos::Node *const imu_node_; |
| 238 | const aos::Node *const camera_node_; |
| 239 | const frc971::control_loops::drivetrain::DrivetrainConfig<double> dt_config_; |
| 240 | std::unique_ptr<aos::EventLoop> localizer_event_loop_; |
| 241 | Localizer localizer_; |
| 242 | |
| 243 | std::unique_ptr<aos::EventLoop> drivetrain_plant_event_loop_; |
| 244 | std::unique_ptr<aos::EventLoop> drivetrain_plant_imu_event_loop_; |
| 245 | frc971::control_loops::drivetrain::testing::DrivetrainSimulation |
| 246 | drivetrain_plant_; |
| 247 | |
| 248 | std::unique_ptr<aos::EventLoop> roborio_test_event_loop_; |
| 249 | std::unique_ptr<aos::EventLoop> imu_test_event_loop_; |
| 250 | std::unique_ptr<aos::EventLoop> camera_test_event_loop_; |
| 251 | std::unique_ptr<aos::EventLoop> logger_test_event_loop_; |
| 252 | |
| 253 | frc971::constants::ConstantsFetcher<Constants> constants_fetcher_; |
| 254 | |
| 255 | aos::Sender<Output> output_sender_; |
| 256 | aos::Sender<frc971::vision::TargetMap> target_sender_; |
| 257 | aos::Sender<frc971::control_loops::drivetrain::LocalizerControl> |
| 258 | control_sender_; |
| 259 | aos::Fetcher<frc971::controls::LocalizerOutput> output_fetcher_; |
| 260 | aos::Fetcher<Status> status_fetcher_; |
| 261 | |
| 262 | Eigen::Vector2d output_voltages_ = Eigen::Vector2d::Zero(); |
| 263 | |
| 264 | aos::TimerHandler *localizer_control_send_timer_; |
| 265 | |
| 266 | bool send_targets_ = false; |
| 267 | |
| 268 | double localizer_control_x_ = 0.0; |
| 269 | double localizer_control_y_ = 0.0; |
| 270 | double localizer_control_theta_ = 0.0; |
| 271 | |
| 272 | std::unique_ptr<aos::EventLoop> logger_event_loop_; |
| 273 | std::unique_ptr<aos::logger::Logger> logger_; |
| 274 | |
| 275 | uint64_t send_target_id_ = kTargetId; |
| 276 | |
| 277 | gflags::FlagSaver flag_saver_; |
| 278 | }; |
| 279 | |
| 280 | // Test a simple scenario with no errors where the robot should just drive |
| 281 | // straight forwards. |
| 282 | TEST_F(LocalizerTest, Nominal) { |
| 283 | output_voltages_ << 1.0, 1.0; |
| 284 | event_loop_factory_.RunFor(std::chrono::seconds(2)); |
| 285 | CHECK(output_fetcher_.Fetch()); |
| 286 | CHECK(status_fetcher_.Fetch()); |
| 287 | // The two can be different because they may've been sent at different |
| 288 | // times. |
| 289 | EXPECT_NEAR(output_fetcher_->x(), status_fetcher_->state()->x(), 1e-2); |
| 290 | EXPECT_NEAR(output_fetcher_->y(), status_fetcher_->state()->y(), 1e-6); |
| 291 | EXPECT_NEAR(output_fetcher_->theta(), status_fetcher_->state()->theta(), |
| 292 | 1e-6); |
| 293 | // Confirm that we did indeed drive forwards (and straight), as expected. |
| 294 | EXPECT_LT(0.1, output_fetcher_->x()); |
| 295 | EXPECT_NEAR(0.0, output_fetcher_->y(), 1e-10); |
| 296 | EXPECT_NEAR(0.0, output_fetcher_->theta(), 1e-10); |
| 297 | EXPECT_NEAR(0.0, status_fetcher_->state()->left_voltage_error(), 1e-1); |
| 298 | EXPECT_NEAR(0.0, status_fetcher_->state()->right_voltage_error(), 1e-1); |
| 299 | |
| 300 | // And check that we actually think that we are near where the simulator |
| 301 | // says we are. |
| 302 | EXPECT_TRUE(VerifyEstimatorAccurate(1e-2)); |
| 303 | } |
| 304 | |
| 305 | // Confirm that when the robot drives backwards that we localize correctly. |
| 306 | TEST_F(LocalizerTest, NominalReverse) { |
| 307 | output_voltages_ << -1.0, -1.0; |
| 308 | event_loop_factory_.RunFor(std::chrono::seconds(2)); |
| 309 | CHECK(output_fetcher_.Fetch()); |
| 310 | CHECK(status_fetcher_.Fetch()); |
| 311 | // The two can be different because they may've been sent at different |
| 312 | // times. |
| 313 | EXPECT_NEAR(output_fetcher_->x(), status_fetcher_->state()->x(), 1e-2); |
| 314 | EXPECT_NEAR(output_fetcher_->y(), status_fetcher_->state()->y(), 1e-6); |
| 315 | EXPECT_NEAR(output_fetcher_->theta(), status_fetcher_->state()->theta(), |
| 316 | 1e-6); |
| 317 | // Confirm that we did indeed drive backwards (and straight), as expected. |
| 318 | EXPECT_GT(-0.1, output_fetcher_->x()); |
| 319 | EXPECT_NEAR(0.0, output_fetcher_->y(), 1e-10); |
| 320 | EXPECT_NEAR(0.0, output_fetcher_->theta(), 1e-10); |
| 321 | EXPECT_NEAR(0.0, status_fetcher_->state()->left_voltage_error(), 1e-1); |
| 322 | EXPECT_NEAR(0.0, status_fetcher_->state()->right_voltage_error(), 1e-1); |
| 323 | |
| 324 | // And check that we actually think that we are near where the simulator |
| 325 | // says we are. |
| 326 | EXPECT_TRUE(VerifyEstimatorAccurate(1e-2)); |
| 327 | } |
| 328 | |
| 329 | // Confirm that when the robot turns counter-clockwise that we localize |
| 330 | // correctly. |
| 331 | TEST_F(LocalizerTest, NominalSpinInPlace) { |
| 332 | output_voltages_ << -1.0, 1.0; |
| 333 | event_loop_factory_.RunFor(std::chrono::seconds(2)); |
| 334 | CHECK(output_fetcher_.Fetch()); |
| 335 | CHECK(status_fetcher_.Fetch()); |
| 336 | // The two can be different because they may've been sent at different |
| 337 | // times. |
| 338 | EXPECT_NEAR(output_fetcher_->x(), status_fetcher_->state()->x(), 1e-6); |
| 339 | EXPECT_NEAR(output_fetcher_->y(), status_fetcher_->state()->y(), 1e-6); |
| 340 | EXPECT_NEAR(output_fetcher_->theta(), status_fetcher_->state()->theta(), |
| 341 | 1e-2); |
| 342 | // Confirm that we did indeed turn counter-clockwise. |
| 343 | EXPECT_NEAR(0.0, output_fetcher_->x(), 1e-10); |
| 344 | EXPECT_NEAR(0.0, output_fetcher_->y(), 1e-10); |
| 345 | EXPECT_LT(0.1, output_fetcher_->theta()); |
| 346 | EXPECT_NEAR(0.0, status_fetcher_->state()->left_voltage_error(), 1e-1); |
| 347 | EXPECT_NEAR(0.0, status_fetcher_->state()->right_voltage_error(), 1e-1); |
| 348 | |
| 349 | // And check that we actually think that we are near where the simulator |
| 350 | // says we are. |
| 351 | EXPECT_TRUE(VerifyEstimatorAccurate(1e-2)); |
| 352 | } |
| 353 | |
| 354 | // Confirm that when the robot drives in a curve that we localize |
| 355 | // successfully. |
| 356 | TEST_F(LocalizerTest, NominalCurve) { |
| 357 | output_voltages_ << 2.0, 3.0; |
| 358 | event_loop_factory_.RunFor(std::chrono::seconds(4)); |
| 359 | CHECK(output_fetcher_.Fetch()); |
| 360 | CHECK(status_fetcher_.Fetch()); |
| 361 | // The two can be different because they may've been sent at different |
| 362 | // times. |
| 363 | EXPECT_NEAR(output_fetcher_->x(), status_fetcher_->state()->x(), 1e-2); |
| 364 | EXPECT_NEAR(output_fetcher_->y(), status_fetcher_->state()->y(), 1e-2); |
| 365 | EXPECT_NEAR(output_fetcher_->theta(), status_fetcher_->state()->theta(), |
| 366 | 1e-2); |
| 367 | // Confirm that we did indeed drive in a rough, counter-clockwise, curve. |
| 368 | EXPECT_LT(0.1, output_fetcher_->x()); |
| 369 | EXPECT_LT(0.1, output_fetcher_->y()); |
| 370 | EXPECT_LT(0.1, output_fetcher_->theta()); |
| 371 | |
| 372 | // And check that we actually think that we are near where the simulator |
| 373 | // says we are. |
| 374 | EXPECT_TRUE(VerifyEstimatorAccurate(1e-2)); |
| 375 | } |
| 376 | |
| 377 | // Tests that, in the presence of a non-zero voltage error, that we correct |
| 378 | // for it. |
| 379 | TEST_F(LocalizerTest, VoltageErrorDisabled) { |
| 380 | output_voltages_ << 0.0, 0.0; |
| 381 | drivetrain_plant_.set_left_voltage_offset(2.0); |
| 382 | drivetrain_plant_.set_right_voltage_offset(2.0); |
| 383 | |
| 384 | event_loop_factory_.RunFor(std::chrono::seconds(2)); |
| 385 | CHECK(output_fetcher_.Fetch()); |
| 386 | CHECK(status_fetcher_.Fetch()); |
| 387 | // We should've just ended up driving straight forwards. |
| 388 | EXPECT_LT(0.1, output_fetcher_->x()); |
| 389 | EXPECT_NEAR(0.0, output_fetcher_->y(), 1e-10); |
| 390 | EXPECT_NEAR(0.0, output_fetcher_->theta(), 1e-10); |
| 391 | EXPECT_NEAR(2.0, status_fetcher_->state()->left_voltage_error(), 1.0); |
| 392 | EXPECT_NEAR(2.0, status_fetcher_->state()->right_voltage_error(), 1.0); |
| 393 | |
| 394 | // And check that we actually think that we are near where the simulator |
| 395 | // says we are. |
| 396 | EXPECT_TRUE(VerifyEstimatorAccurate(0.05)); |
| 397 | } |
| 398 | |
| 399 | // Tests that image corrections in the nominal case (no errors) causes no |
| 400 | // issues. |
| 401 | TEST_F(LocalizerTest, NominalImageCorrections) { |
| 402 | output_voltages_ << 3.0, 2.0; |
| 403 | send_targets_ = true; |
| 404 | |
| 405 | event_loop_factory_.RunFor(std::chrono::seconds(4)); |
| 406 | CHECK(status_fetcher_.Fetch()); |
| 407 | EXPECT_TRUE(VerifyEstimatorAccurate(1e-2)); |
| 408 | ASSERT_TRUE(status_fetcher_->has_statistics()); |
| 409 | ASSERT_EQ(4u /* number of cameras */, status_fetcher_->statistics()->size()); |
| 410 | ASSERT_EQ(status_fetcher_->statistics()->Get(0)->total_candidates(), |
| 411 | status_fetcher_->statistics()->Get(0)->total_accepted()); |
| 412 | ASSERT_LT(10, status_fetcher_->statistics()->Get(0)->total_candidates()); |
| 413 | } |
| 414 | |
| 415 | // Tests that image corrections when there is an error at the start results |
| 416 | // in us actually getting corrected over time. |
| 417 | TEST_F(LocalizerTest, ImageCorrections) { |
| 418 | output_voltages_ << 0.0, 0.0; |
| 419 | drivetrain_plant_.mutable_state()->x() = 2.0; |
| 420 | drivetrain_plant_.mutable_state()->y() = 2.0; |
| 421 | SendLocalizerControl(5.0, 3.0, 0.0); |
| 422 | event_loop_factory_.RunFor(std::chrono::seconds(4)); |
| 423 | CHECK(output_fetcher_.Fetch()); |
| 424 | ASSERT_NEAR(5.0, output_fetcher_->x(), 1e-5); |
| 425 | ASSERT_NEAR(3.0, output_fetcher_->y(), 1e-5); |
| 426 | ASSERT_NEAR(0.0, output_fetcher_->theta(), 1e-5); |
| 427 | |
| 428 | send_targets_ = true; |
| 429 | |
| 430 | event_loop_factory_.RunFor(std::chrono::seconds(4)); |
| 431 | CHECK(status_fetcher_.Fetch()); |
| 432 | EXPECT_TRUE(VerifyEstimatorAccurate(0.1)); |
| 433 | ASSERT_TRUE(status_fetcher_->has_statistics()); |
| 434 | ASSERT_EQ(4u /* number of cameras */, status_fetcher_->statistics()->size()); |
| 435 | ASSERT_EQ(status_fetcher_->statistics()->Get(0)->total_candidates(), |
| 436 | status_fetcher_->statistics()->Get(0)->total_accepted()); |
| 437 | ASSERT_LT(10, status_fetcher_->statistics()->Get(0)->total_candidates()); |
| 438 | } |
| 439 | |
| 440 | // Tests that we correctly reject an invalid target. |
| 441 | TEST_F(LocalizerTest, InvalidTargetId) { |
| 442 | output_voltages_ << 0.0, 0.0; |
| 443 | send_targets_ = true; |
| 444 | send_target_id_ = 100; |
| 445 | |
| 446 | event_loop_factory_.RunFor(std::chrono::seconds(4)); |
| 447 | CHECK(status_fetcher_.Fetch()); |
| 448 | ASSERT_TRUE(status_fetcher_->has_statistics()); |
| 449 | ASSERT_EQ(4u /* number of cameras */, status_fetcher_->statistics()->size()); |
| 450 | ASSERT_EQ(0, status_fetcher_->statistics()->Get(0)->total_accepted()); |
| 451 | ASSERT_LT(10, status_fetcher_->statistics()->Get(0)->total_candidates()); |
| 452 | ASSERT_EQ(status_fetcher_->statistics() |
| 453 | ->Get(0) |
| 454 | ->rejection_reasons() |
| 455 | ->Get(static_cast<size_t>(RejectionReason::NO_SUCH_TARGET)) |
| 456 | ->count(), |
| 457 | status_fetcher_->statistics()->Get(0)->total_candidates()); |
| 458 | } |
| 459 | |
| 460 | } // namespace y2023::localizer::testing |