blob: ce1a2a4f250bb3bbcb87134beb410dadb5e9ad77 [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);
James Kuszmaul090563a2019-02-09 14:43:20 -080096
97 // Check that the PlotPoints for debugging are as expected (should be a single
98 // line from the camera to the one visible target).
99 const auto plot_pts = camera_.PlotPoints();
100 ASSERT_EQ(1u, plot_pts.size());
101 ASSERT_EQ(2u, plot_pts[0].size());
102 EXPECT_EQ(camera_.pose().abs_pos(), plot_pts[0][0].abs_pos());
103 EXPECT_EQ(views[0].target->pose().abs_pos(), plot_pts[0][1].abs_pos());
James Kuszmaul57c7c9b2019-01-27 16:16:01 -0800104}
105
106// Check that occluding the middle target makes it invisible.
107TEST_F(CameraTest, OcclusionTest) {
108 auto views = camera_.target_views();
109 // We should only be able to see one target (the middle one).
110 ASSERT_EQ(1u, views.size());
111 targets_[1].set_occluded(true);
112 TestCamera occluded_camera(camera_.pose(), camera_.fov(), noise_parameters_,
113 targets_, obstacles_);
114 views = occluded_camera.target_views();
115 // We should no longer see any targets.
116 ASSERT_EQ(0u, views.size());
117}
118
119// Checks that targets outside of the field-of-view don't show up.
120TEST_F(CameraTest, FovTest) {
121 // Initially, we should still see just the middle target.
122 EXPECT_EQ(1u, camera_.target_views().size());
123 // Point camera so that the middle target is just barely in its field of view.
124 base_pose_.set_theta(3.0 * M_PI / 4.0 - 0.01);
125 EXPECT_EQ(1u, camera_.target_views().size());
126 // Point camera so that the middle target is just outside of its FoV.
127 base_pose_.set_theta(3.0 * M_PI / 4.0 + 0.01);
128 EXPECT_EQ(0u, camera_.target_views().size());
129 // Check the same things, but on the other edge of the FoV:
130 base_pose_.set_theta(M_PI / 4.0 + 0.01);
131 EXPECT_EQ(1u, camera_.target_views().size());
132 base_pose_.set_theta(M_PI / 4.0 - 0.01);
133 EXPECT_EQ(0u, camera_.target_views().size());
134}
135
136// Checks that targets don't show up when very far away.
137TEST_F(CameraTest, FarAwayTest) {
138 EXPECT_EQ(1u, camera_.target_views().size());
139 // If we move the camera really far away we can't see it any more:
140 base_pose_.mutable_pos()->y() = -1000.0;
141 EXPECT_EQ(0u, camera_.target_views().size());
142}
143
144// Checks that targets which are highly skewed only show up if we are
145// arbitrarily close.
146TEST_F(CameraTest, HighlySkewedTest) {
147 // Skew the target a bunch.
148 targets_[1] = Target({{0.0, 0.0, 0.0}, 0.01});
149 TestCamera occluded_camera(camera_.pose(), camera_.fov(), noise_parameters_,
150 targets_, obstacles_);
151 EXPECT_EQ(0u, occluded_camera.target_views().size());
152 // But if we get really close we should still see it...
153 base_pose_.mutable_pos()->y() = -0.1;
154 EXPECT_EQ(1u, camera_.target_views().size());
155}
156
157// Checks that reading noises have the expected characteristics (mostly, going
158// up linearly with distance):
159TEST_F(CameraTest, DistanceNoiseTest) {
160 using Reading = TestCamera::TargetView::Reading;
161 const Reading normal_noise = camera_.target_views()[0].noise;
162 // Get twice as close:
163 base_pose_.mutable_pos()->y() /= 2.0;
164 const Reading closer_noise = camera_.target_views()[0].noise;
165 EXPECT_EQ(normal_noise.distance / 2.0, closer_noise.distance);
166 EXPECT_EQ(normal_noise.skew / 2.0, closer_noise.skew);
167 EXPECT_EQ(normal_noise.height / 2.0, closer_noise.height);
168 // Heading reading should be equally good.
169 EXPECT_EQ(normal_noise.heading, closer_noise.heading);
170}
171
172} // namespace testing
173} // namespace control_loops
174} // namespace y2019