James Kuszmaul | 57c7c9b | 2019-01-27 16:16:01 -0800 | [diff] [blame] | 1 | #include "y2019/control_loops/drivetrain/camera.h" |
| 2 | |
| 3 | #include "gtest/gtest.h" |
| 4 | |
| 5 | namespace y2019 { |
| 6 | namespace control_loops { |
| 7 | namespace testing { |
| 8 | |
| 9 | // Check that a Target's basic operations work. |
| 10 | TEST(TargetTest, BasicTargetTest) { |
James Kuszmaul | 7d1ef44 | 2019-03-23 20:20:50 -0700 | [diff] [blame^] | 11 | Target target({{1, 2, 3}, M_PI / 2.0}, 1.234, |
| 12 | Target::TargetType::kFaceCargoBay, Target::GoalType::kHatches); |
James Kuszmaul | 57c7c9b | 2019-01-27 16:16:01 -0800 | [diff] [blame] | 13 | |
| 14 | EXPECT_EQ(1.0, target.pose().abs_pos().x()); |
| 15 | EXPECT_EQ(2.0, target.pose().abs_pos().y()); |
| 16 | EXPECT_EQ(3.0, target.pose().abs_pos().z()); |
| 17 | EXPECT_EQ(M_PI / 2.0, target.pose().abs_theta()); |
James Kuszmaul | e093f51 | 2019-03-20 06:14:05 -0700 | [diff] [blame] | 18 | EXPECT_EQ(1.234, target.radius()); |
| 19 | EXPECT_EQ(Target::GoalType::kHatches, target.goal_type()); |
James Kuszmaul | 7d1ef44 | 2019-03-23 20:20:50 -0700 | [diff] [blame^] | 20 | EXPECT_EQ(Target::TargetType::kFaceCargoBay, target.target_type()); |
James Kuszmaul | 57c7c9b | 2019-01-27 16:16:01 -0800 | [diff] [blame] | 21 | |
| 22 | EXPECT_FALSE(target.occluded()); |
| 23 | target.set_occluded(true); |
| 24 | EXPECT_TRUE(target.occluded()); |
| 25 | |
| 26 | ::std::vector<Target::Pose> plot_pts = target.PlotPoints(); |
| 27 | ASSERT_EQ(4, plot_pts.size()); |
| 28 | for (const Target::Pose &pt : plot_pts) { |
| 29 | EXPECT_EQ(3.0, pt.abs_pos().z()); |
| 30 | EXPECT_EQ(M_PI / 2.0, pt.abs_theta()); |
| 31 | // We don't particularly care about the plot point details, just check that |
| 32 | // they are all roughly in the right vicinity: |
| 33 | EXPECT_LT((pt.abs_pos() - target.pose().abs_pos()).norm(), 0.25); |
| 34 | } |
| 35 | EXPECT_EQ(plot_pts[0].abs_pos(), plot_pts[3].abs_pos()); |
| 36 | } |
| 37 | |
| 38 | typedef TypedCamera</*num_targets=*/3, /*num_obstacles=*/1, double> TestCamera; |
| 39 | |
| 40 | class CameraTest : public ::testing::Test { |
| 41 | public: |
| 42 | // Set up three targets in a row, at (-1, 0), (0, 0), and (1, 0). |
| 43 | // Make the right-most target (1, 0) be facing away from the camera, and give |
| 44 | // the middle target some skew. |
| 45 | // Place the camera at (0, -5) so the targets are a few meters away. |
| 46 | // Place one obstacle in a place where it blocks the left-most target (-1, 0). |
| 47 | CameraTest() |
| 48 | : targets_{{Target(Target::Pose({-1.0, 0.0, 0.0}, M_PI_2)), |
| 49 | Target(Target::Pose({0.0, 0.0, kMiddleHeight}, |
| 50 | M_PI_2 + kMiddleSkew)), |
| 51 | Target(Target::Pose({1.0, 0.0, 0.0}, -M_PI_2))}}, |
| 52 | obstacles_{{TestCamera::LineSegment({{-2.0, -0.5, 0.0}, 0.0}, |
| 53 | {{-0.5, -0.5, 0.0}, 0.0})}}, |
| 54 | base_pose_({0.0, -5.0, 0.0}, M_PI_2), |
| 55 | camera_({&base_pose_, {0.0, 0.0, 0.0}, 0.0}, M_PI_2, noise_parameters_, |
| 56 | targets_, obstacles_) {} |
| 57 | |
| 58 | protected: |
| 59 | static constexpr double kMiddleSkew = 0.1; |
| 60 | static constexpr double kMiddleHeight = 0.5; |
| 61 | ::std::array<Target, 3> targets_; |
| 62 | ::std::array<TestCamera::LineSegment, 1> obstacles_; |
| 63 | |
| 64 | TestCamera::NoiseParameters noise_parameters_ = { |
| 65 | .max_viewable_distance = 10.0, |
| 66 | .heading_noise = 0.03, |
| 67 | .nominal_distance_noise = 0.06, |
| 68 | .nominal_skew_noise = 0.1, |
| 69 | .nominal_height_noise = 0.01}; |
| 70 | |
| 71 | // Provide base_pose_ as the base for the Pose used in the camera, to make it |
| 72 | // so that we can easily move the camera around for testing. |
| 73 | TestCamera::Pose base_pose_; |
| 74 | TestCamera camera_; |
| 75 | }; |
| 76 | |
| 77 | constexpr double CameraTest::kMiddleSkew; |
| 78 | constexpr double CameraTest::kMiddleHeight; |
| 79 | |
| 80 | constexpr double kEps = 1e-15; |
| 81 | |
| 82 | // Check that, in the default setup we have, the correct targets are visible in |
| 83 | // the expected locations. |
| 84 | TEST_F(CameraTest, BasicCameraTest) { |
| 85 | const auto views = camera_.target_views(); |
| 86 | // We should only be able to see one target (the middle one). |
| 87 | ASSERT_EQ(1u, views.size()); |
| 88 | // And, check the actual result for correctness. |
| 89 | EXPECT_NEAR(0.0, views[0].reading.heading, kEps); |
| 90 | EXPECT_NEAR(5.0, views[0].reading.distance, kEps); |
| 91 | EXPECT_NEAR(kMiddleSkew, views[0].reading.skew, kEps); |
| 92 | EXPECT_NEAR(kMiddleHeight, views[0].reading.height, kEps); |
| 93 | // Check that the noise outputs are sane; leave other tests to check the exact |
| 94 | // values of the noise outputs. |
| 95 | // All noise values should be strictly positive. |
| 96 | EXPECT_GT(views[0].noise.heading, 0.0); |
| 97 | EXPECT_GT(views[0].noise.distance, 0.0); |
| 98 | EXPECT_GT(views[0].noise.skew, 0.0); |
| 99 | EXPECT_GT(views[0].noise.height, 0.0); |
James Kuszmaul | 090563a | 2019-02-09 14:43:20 -0800 | [diff] [blame] | 100 | |
| 101 | // Check that the PlotPoints for debugging are as expected (should be a single |
| 102 | // line from the camera to the one visible target). |
| 103 | const auto plot_pts = camera_.PlotPoints(); |
| 104 | ASSERT_EQ(1u, plot_pts.size()); |
| 105 | ASSERT_EQ(2u, plot_pts[0].size()); |
| 106 | EXPECT_EQ(camera_.pose().abs_pos(), plot_pts[0][0].abs_pos()); |
| 107 | EXPECT_EQ(views[0].target->pose().abs_pos(), plot_pts[0][1].abs_pos()); |
James Kuszmaul | 57c7c9b | 2019-01-27 16:16:01 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | // Check that occluding the middle target makes it invisible. |
| 111 | TEST_F(CameraTest, OcclusionTest) { |
| 112 | auto views = camera_.target_views(); |
| 113 | // We should only be able to see one target (the middle one). |
| 114 | ASSERT_EQ(1u, views.size()); |
| 115 | targets_[1].set_occluded(true); |
| 116 | TestCamera occluded_camera(camera_.pose(), camera_.fov(), noise_parameters_, |
| 117 | targets_, obstacles_); |
| 118 | views = occluded_camera.target_views(); |
| 119 | // We should no longer see any targets. |
| 120 | ASSERT_EQ(0u, views.size()); |
| 121 | } |
| 122 | |
| 123 | // Checks that targets outside of the field-of-view don't show up. |
| 124 | TEST_F(CameraTest, FovTest) { |
| 125 | // Initially, we should still see just the middle target. |
| 126 | EXPECT_EQ(1u, camera_.target_views().size()); |
| 127 | // Point camera so that the middle target is just barely in its field of view. |
| 128 | base_pose_.set_theta(3.0 * M_PI / 4.0 - 0.01); |
| 129 | EXPECT_EQ(1u, camera_.target_views().size()); |
| 130 | // Point camera so that the middle target is just outside of its FoV. |
| 131 | base_pose_.set_theta(3.0 * M_PI / 4.0 + 0.01); |
| 132 | EXPECT_EQ(0u, camera_.target_views().size()); |
| 133 | // Check the same things, but on the other edge of the FoV: |
| 134 | base_pose_.set_theta(M_PI / 4.0 + 0.01); |
| 135 | EXPECT_EQ(1u, camera_.target_views().size()); |
| 136 | base_pose_.set_theta(M_PI / 4.0 - 0.01); |
| 137 | EXPECT_EQ(0u, camera_.target_views().size()); |
| 138 | } |
| 139 | |
| 140 | // Checks that targets don't show up when very far away. |
| 141 | TEST_F(CameraTest, FarAwayTest) { |
| 142 | EXPECT_EQ(1u, camera_.target_views().size()); |
| 143 | // If we move the camera really far away we can't see it any more: |
| 144 | base_pose_.mutable_pos()->y() = -1000.0; |
| 145 | EXPECT_EQ(0u, camera_.target_views().size()); |
| 146 | } |
| 147 | |
| 148 | // Checks that targets which are highly skewed only show up if we are |
| 149 | // arbitrarily close. |
| 150 | TEST_F(CameraTest, HighlySkewedTest) { |
| 151 | // Skew the target a bunch. |
| 152 | targets_[1] = Target({{0.0, 0.0, 0.0}, 0.01}); |
| 153 | TestCamera occluded_camera(camera_.pose(), camera_.fov(), noise_parameters_, |
| 154 | targets_, obstacles_); |
| 155 | EXPECT_EQ(0u, occluded_camera.target_views().size()); |
| 156 | // But if we get really close we should still see it... |
| 157 | base_pose_.mutable_pos()->y() = -0.1; |
| 158 | EXPECT_EQ(1u, camera_.target_views().size()); |
| 159 | } |
| 160 | |
James Kuszmaul | 6f941b7 | 2019-03-08 18:12:25 -0800 | [diff] [blame] | 161 | using Reading = TestCamera::TargetView::Reading; |
| 162 | |
James Kuszmaul | 57c7c9b | 2019-01-27 16:16:01 -0800 | [diff] [blame] | 163 | // Checks that reading noises have the expected characteristics (mostly, going |
| 164 | // up linearly with distance): |
| 165 | TEST_F(CameraTest, DistanceNoiseTest) { |
James Kuszmaul | 57c7c9b | 2019-01-27 16:16:01 -0800 | [diff] [blame] | 166 | const Reading normal_noise = camera_.target_views()[0].noise; |
| 167 | // Get twice as close: |
| 168 | base_pose_.mutable_pos()->y() /= 2.0; |
| 169 | const Reading closer_noise = camera_.target_views()[0].noise; |
| 170 | EXPECT_EQ(normal_noise.distance / 2.0, closer_noise.distance); |
| 171 | EXPECT_EQ(normal_noise.skew / 2.0, closer_noise.skew); |
| 172 | EXPECT_EQ(normal_noise.height / 2.0, closer_noise.height); |
| 173 | // Heading reading should be equally good. |
| 174 | EXPECT_EQ(normal_noise.heading, closer_noise.heading); |
| 175 | } |
| 176 | |
James Kuszmaul | 6f941b7 | 2019-03-08 18:12:25 -0800 | [diff] [blame] | 177 | class CameraViewParamTest : public CameraTest, |
| 178 | public ::testing::WithParamInterface<Reading> {}; |
| 179 | |
| 180 | // Checks that invalid or absurd measurements result in large but finite noises |
| 181 | // and non-visible targets. |
| 182 | TEST_P(CameraViewParamTest, InvalidReading) { |
| 183 | TestCamera::TargetView view; |
| 184 | view.reading = GetParam(); |
| 185 | bool visible = true; |
| 186 | camera_.PopulateNoise(&view, &visible); |
| 187 | // Target should not be visible |
| 188 | EXPECT_FALSE(visible); |
| 189 | // We should end up with finite but very large noises when things are invalid. |
| 190 | EXPECT_TRUE(::std::isfinite(view.noise.heading)); |
| 191 | EXPECT_TRUE(::std::isfinite(view.noise.distance)); |
| 192 | EXPECT_TRUE(::std::isfinite(view.noise.skew)); |
| 193 | EXPECT_TRUE(::std::isfinite(view.noise.height)); |
| 194 | // Don't check heading noise because it is always constant. |
| 195 | EXPECT_LT(10, view.noise.distance); |
| 196 | EXPECT_LT(10, view.noise.skew); |
| 197 | EXPECT_LT(5, view.noise.height); |
| 198 | } |
| 199 | |
| 200 | INSTANTIATE_TEST_CASE_P( |
| 201 | InvalidMeasurements, CameraViewParamTest, |
| 202 | ::testing::Values( |
| 203 | // heading, distance, height, skew |
| 204 | Reading({100.0, -10.0, -10.0, -3.0}), Reading({0.0, 1.0, 0.0, -3.0}), |
| 205 | Reading({0.0, 1.0, 0.0, 3.0}), Reading({0.0, 1.0, 0.0, 9.0}), |
| 206 | Reading({0.0, ::std::numeric_limits<double>::quiet_NaN(), 0.0, 0.0}), |
| 207 | Reading({0.0, ::std::numeric_limits<double>::infinity(), 0.0, 0.0}), |
| 208 | Reading({0.0, 1.0, 0.0, ::std::numeric_limits<double>::infinity()}), |
| 209 | Reading({0.0, 1.0, 0.0, ::std::numeric_limits<double>::quiet_NaN()}))); |
| 210 | |
James Kuszmaul | 57c7c9b | 2019-01-27 16:16:01 -0800 | [diff] [blame] | 211 | } // namespace testing |
| 212 | } // namespace control_loops |
| 213 | } // namespace y2019 |