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