blob: 9a73ccf5f95f5fb52db4e65b2266bde3e84e4b3a [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) {
11 Target target({{1, 2, 3}, M_PI / 2.0});
12
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());
17
18 EXPECT_FALSE(target.occluded());
19 target.set_occluded(true);
20 EXPECT_TRUE(target.occluded());
21
22 ::std::vector<Target::Pose> plot_pts = target.PlotPoints();
23 ASSERT_EQ(4, plot_pts.size());
24 for (const Target::Pose &pt : plot_pts) {
25 EXPECT_EQ(3.0, pt.abs_pos().z());
26 EXPECT_EQ(M_PI / 2.0, pt.abs_theta());
27 // We don't particularly care about the plot point details, just check that
28 // they are all roughly in the right vicinity:
29 EXPECT_LT((pt.abs_pos() - target.pose().abs_pos()).norm(), 0.25);
30 }
31 EXPECT_EQ(plot_pts[0].abs_pos(), plot_pts[3].abs_pos());
32}
33
34typedef TypedCamera</*num_targets=*/3, /*num_obstacles=*/1, double> TestCamera;
35
36class CameraTest : public ::testing::Test {
37 public:
38 // Set up three targets in a row, at (-1, 0), (0, 0), and (1, 0).
39 // Make the right-most target (1, 0) be facing away from the camera, and give
40 // the middle target some skew.
41 // Place the camera at (0, -5) so the targets are a few meters away.
42 // Place one obstacle in a place where it blocks the left-most target (-1, 0).
43 CameraTest()
44 : targets_{{Target(Target::Pose({-1.0, 0.0, 0.0}, M_PI_2)),
45 Target(Target::Pose({0.0, 0.0, kMiddleHeight},
46 M_PI_2 + kMiddleSkew)),
47 Target(Target::Pose({1.0, 0.0, 0.0}, -M_PI_2))}},
48 obstacles_{{TestCamera::LineSegment({{-2.0, -0.5, 0.0}, 0.0},
49 {{-0.5, -0.5, 0.0}, 0.0})}},
50 base_pose_({0.0, -5.0, 0.0}, M_PI_2),
51 camera_({&base_pose_, {0.0, 0.0, 0.0}, 0.0}, M_PI_2, noise_parameters_,
52 targets_, obstacles_) {}
53
54 protected:
55 static constexpr double kMiddleSkew = 0.1;
56 static constexpr double kMiddleHeight = 0.5;
57 ::std::array<Target, 3> targets_;
58 ::std::array<TestCamera::LineSegment, 1> obstacles_;
59
60 TestCamera::NoiseParameters noise_parameters_ = {
61 .max_viewable_distance = 10.0,
62 .heading_noise = 0.03,
63 .nominal_distance_noise = 0.06,
64 .nominal_skew_noise = 0.1,
65 .nominal_height_noise = 0.01};
66
67 // Provide base_pose_ as the base for the Pose used in the camera, to make it
68 // so that we can easily move the camera around for testing.
69 TestCamera::Pose base_pose_;
70 TestCamera camera_;
71};
72
73constexpr double CameraTest::kMiddleSkew;
74constexpr double CameraTest::kMiddleHeight;
75
76constexpr double kEps = 1e-15;
77
78// Check that, in the default setup we have, the correct targets are visible in
79// the expected locations.
80TEST_F(CameraTest, BasicCameraTest) {
81 const auto views = camera_.target_views();
82 // We should only be able to see one target (the middle one).
83 ASSERT_EQ(1u, views.size());
84 // And, check the actual result for correctness.
85 EXPECT_NEAR(0.0, views[0].reading.heading, kEps);
86 EXPECT_NEAR(5.0, views[0].reading.distance, kEps);
87 EXPECT_NEAR(kMiddleSkew, views[0].reading.skew, kEps);
88 EXPECT_NEAR(kMiddleHeight, views[0].reading.height, kEps);
89 // Check that the noise outputs are sane; leave other tests to check the exact
90 // values of the noise outputs.
91 // All noise values should be strictly positive.
92 EXPECT_GT(views[0].noise.heading, 0.0);
93 EXPECT_GT(views[0].noise.distance, 0.0);
94 EXPECT_GT(views[0].noise.skew, 0.0);
95 EXPECT_GT(views[0].noise.height, 0.0);
96}
97
98// Check that occluding the middle target makes it invisible.
99TEST_F(CameraTest, OcclusionTest) {
100 auto views = camera_.target_views();
101 // We should only be able to see one target (the middle one).
102 ASSERT_EQ(1u, views.size());
103 targets_[1].set_occluded(true);
104 TestCamera occluded_camera(camera_.pose(), camera_.fov(), noise_parameters_,
105 targets_, obstacles_);
106 views = occluded_camera.target_views();
107 // We should no longer see any targets.
108 ASSERT_EQ(0u, views.size());
109}
110
111// Checks that targets outside of the field-of-view don't show up.
112TEST_F(CameraTest, FovTest) {
113 // Initially, we should still see just the middle target.
114 EXPECT_EQ(1u, camera_.target_views().size());
115 // Point camera so that the middle target is just barely in its field of view.
116 base_pose_.set_theta(3.0 * M_PI / 4.0 - 0.01);
117 EXPECT_EQ(1u, camera_.target_views().size());
118 // Point camera so that the middle target is just outside of its FoV.
119 base_pose_.set_theta(3.0 * M_PI / 4.0 + 0.01);
120 EXPECT_EQ(0u, camera_.target_views().size());
121 // Check the same things, but on the other edge of the FoV:
122 base_pose_.set_theta(M_PI / 4.0 + 0.01);
123 EXPECT_EQ(1u, camera_.target_views().size());
124 base_pose_.set_theta(M_PI / 4.0 - 0.01);
125 EXPECT_EQ(0u, camera_.target_views().size());
126}
127
128// Checks that targets don't show up when very far away.
129TEST_F(CameraTest, FarAwayTest) {
130 EXPECT_EQ(1u, camera_.target_views().size());
131 // If we move the camera really far away we can't see it any more:
132 base_pose_.mutable_pos()->y() = -1000.0;
133 EXPECT_EQ(0u, camera_.target_views().size());
134}
135
136// Checks that targets which are highly skewed only show up if we are
137// arbitrarily close.
138TEST_F(CameraTest, HighlySkewedTest) {
139 // Skew the target a bunch.
140 targets_[1] = Target({{0.0, 0.0, 0.0}, 0.01});
141 TestCamera occluded_camera(camera_.pose(), camera_.fov(), noise_parameters_,
142 targets_, obstacles_);
143 EXPECT_EQ(0u, occluded_camera.target_views().size());
144 // But if we get really close we should still see it...
145 base_pose_.mutable_pos()->y() = -0.1;
146 EXPECT_EQ(1u, camera_.target_views().size());
147}
148
149// Checks that reading noises have the expected characteristics (mostly, going
150// up linearly with distance):
151TEST_F(CameraTest, DistanceNoiseTest) {
152 using Reading = TestCamera::TargetView::Reading;
153 const Reading normal_noise = camera_.target_views()[0].noise;
154 // Get twice as close:
155 base_pose_.mutable_pos()->y() /= 2.0;
156 const Reading closer_noise = camera_.target_views()[0].noise;
157 EXPECT_EQ(normal_noise.distance / 2.0, closer_noise.distance);
158 EXPECT_EQ(normal_noise.skew / 2.0, closer_noise.skew);
159 EXPECT_EQ(normal_noise.height / 2.0, closer_noise.height);
160 // Heading reading should be equally good.
161 EXPECT_EQ(normal_noise.heading, closer_noise.heading);
162}
163
164} // namespace testing
165} // namespace control_loops
166} // namespace y2019