blob: 37b037b194600cad8a585caf81d2f53948e3beb5 [file] [log] [blame]
Milind Upadhyay7c205222022-11-16 18:20:58 -08001#include "frc971/vision/target_mapper.h"
2
3#include <random>
4
5#include "aos/events/simulated_event_loop.h"
6#include "aos/testing/random_seed.h"
7#include "glog/logging.h"
8#include "gtest/gtest.h"
9
10namespace frc971::vision {
11
12namespace {
13constexpr double kToleranceMeters = 0.05;
14constexpr double kToleranceRadians = 0.05;
15} // namespace
16
17#define EXPECT_POSE_NEAR(pose1, pose2) \
18 EXPECT_NEAR(pose1.x, pose2.x, kToleranceMeters); \
19 EXPECT_NEAR(pose1.y, pose2.y, kToleranceMeters); \
20 EXPECT_NEAR(pose1.yaw_radians, pose2.yaw_radians, kToleranceRadians);
21
22#define EXPECT_POSE_EQ(pose1, pose2) \
23 EXPECT_DOUBLE_EQ(pose1.x, pose2.x); \
24 EXPECT_DOUBLE_EQ(pose1.y, pose2.y); \
25 EXPECT_DOUBLE_EQ(pose1.yaw_radians, pose2.yaw_radians);
26
27#define EXPECT_BETWEEN_EXCLUSIVE(value, a, b) \
28 { \
29 auto low = std::min(a, b); \
30 auto high = std::max(a, b); \
31 EXPECT_GT(value, low); \
32 EXPECT_LT(value, high); \
33 }
34
35namespace {
36// Expects angles to be normalized
37double DeltaAngle(double a, double b) {
38 double delta = std::abs(a - b);
39 return std::min(delta, (2.0 * M_PI) - delta);
40}
41} // namespace
42
43// Expects angles to be normalized
44#define EXPECT_ANGLE_BETWEEN_EXCLUSIVE(theta, a, b) \
45 EXPECT_LT(DeltaAngle(a, theta), DeltaAngle(a, b)); \
46 EXPECT_LT(DeltaAngle(b, theta), DeltaAngle(a, b));
47
48#define EXPECT_POSE_IN_RANGE(interpolated_pose, pose_start, pose_end) \
49 EXPECT_BETWEEN_EXCLUSIVE(interpolated_pose.x, pose_start.x, pose_end.x); \
50 EXPECT_BETWEEN_EXCLUSIVE(interpolated_pose.y, pose_start.y, pose_end.y); \
51 EXPECT_ANGLE_BETWEEN_EXCLUSIVE(interpolated_pose.yaw_radians, \
52 pose_start.yaw_radians, \
53 pose_end.yaw_radians);
54
55// Both confidence matrixes should have the same dimensions and be square
56#define EXPECT_CONFIDENCE_GT(confidence1, confidence2) \
57 { \
58 ASSERT_EQ(confidence1.rows(), confidence2.rows()); \
59 ASSERT_EQ(confidence1.rows(), confidence1.cols()); \
60 ASSERT_EQ(confidence2.rows(), confidence2.cols()); \
61 for (size_t i = 0; i < confidence1.rows(); i++) { \
62 EXPECT_GT(confidence1(i, i), confidence2(i, i)); \
63 } \
64 }
65
66namespace {
67ceres::examples::Pose2d MakePose(double x, double y, double yaw_radians) {
68 return ceres::examples::Pose2d{x, y, yaw_radians};
69}
70
71bool TargetIsInView(TargetMapper::TargetPose target_detection) {
72 // And check if it is within the fov of the robot /
73 // camera, assuming camera is pointing in the
74 // positive x-direction of the robot
75 double angle_to_target =
76 atan2(target_detection.pose.y, target_detection.pose.x);
77
78 // Simulated camera field of view, in radians
79 constexpr double kCameraFov = M_PI_2;
80 if (fabs(angle_to_target) <= kCameraFov / 2.0) {
81 VLOG(2) << "Found target in view, based on T = " << target_detection.pose.x
82 << ", " << target_detection.pose.y << " with angle "
83 << angle_to_target;
84 return true;
85 } else {
86 return false;
87 }
88}
89
90aos::distributed_clock::time_point TimeInMs(size_t ms) {
91 return aos::distributed_clock::time_point(std::chrono::milliseconds(ms));
92}
93
94} // namespace
95
96TEST(DataAdapterTest, Interpolation) {
97 std::vector<DataAdapter::TimestampedPose> timestamped_robot_poses = {
98 {TimeInMs(0), ceres::examples::Pose2d{1.0, 2.0, 0.0}},
99 {TimeInMs(5), ceres::examples::Pose2d{1.0, 2.0, 0.0}},
100 {TimeInMs(10), ceres::examples::Pose2d{3.0, 1.0, M_PI_2}},
101 {TimeInMs(15), ceres::examples::Pose2d{5.0, -2.0, -M_PI}},
102 {TimeInMs(20), ceres::examples::Pose2d{5.0, -2.0, -M_PI}},
103 {TimeInMs(25), ceres::examples::Pose2d{10.0, -32.0, M_PI_2}},
104 {TimeInMs(30), ceres::examples::Pose2d{-15.0, 12.0, 0.0}},
105 {TimeInMs(35), ceres::examples::Pose2d{-15.0, 12.0, 0.0}}};
106 std::vector<DataAdapter::TimestampedDetection> timestamped_target_detections =
107 {{TimeInMs(5),
108 PoseUtils::Pose2dToAffine3d(ceres::examples::Pose2d{5.0, -4.0, 0.0}),
109 0},
110 {TimeInMs(9),
111 PoseUtils::Pose2dToAffine3d(ceres::examples::Pose2d{5.0, -4.0, 0.0}),
112 1},
113 {TimeInMs(9),
114 PoseUtils::Pose2dToAffine3d(ceres::examples::Pose2d{5.0, -4.0, 0.0}),
115 2},
116 {TimeInMs(15),
117 PoseUtils::Pose2dToAffine3d(ceres::examples::Pose2d{5.0, -4.0, 0.0}),
118 0},
119 {TimeInMs(16),
120 PoseUtils::Pose2dToAffine3d(ceres::examples::Pose2d{5.0, -4.0, 0.0}),
121 2},
122 {TimeInMs(27),
123 PoseUtils::Pose2dToAffine3d(ceres::examples::Pose2d{5.0, -4.0, 0.0}),
124 1}};
125 auto [target_constraints, robot_delta_poses] =
126 DataAdapter::MatchTargetDetections(timestamped_robot_poses,
127 timestamped_target_detections);
128
129 // Check that target constraints got inserted in the
130 // correct spots
131 EXPECT_EQ(target_constraints.size(),
132 timestamped_target_detections.size() - 1);
133 for (auto it = target_constraints.begin(); it < target_constraints.end();
134 it++) {
135 auto timestamped_it = timestamped_target_detections.begin() +
136 (it - target_constraints.begin());
137 EXPECT_EQ(it->id_begin, timestamped_it->id);
138 EXPECT_EQ(it->id_end, (timestamped_it + 1)->id);
139 }
140
141 // Check that poses were interpolated correctly.
142 // Keep track of the computed robot pose by adding the delta poses
143 auto computed_robot_pose = timestamped_robot_poses[1].pose;
144
145 computed_robot_pose =
146 PoseUtils::ComputeOffsetPose(computed_robot_pose, robot_delta_poses[0]);
147 EXPECT_POSE_IN_RANGE(computed_robot_pose, timestamped_robot_poses[1].pose,
148 timestamped_robot_poses[2].pose);
149
150 computed_robot_pose =
151 PoseUtils::ComputeOffsetPose(computed_robot_pose, robot_delta_poses[1]);
152 EXPECT_POSE_IN_RANGE(computed_robot_pose, timestamped_robot_poses[1].pose,
153 timestamped_robot_poses[2].pose);
154 EXPECT_POSE_EQ(robot_delta_poses[1], MakePose(0.0, 0.0, 0.0));
155
156 computed_robot_pose =
157 PoseUtils::ComputeOffsetPose(computed_robot_pose, robot_delta_poses[2]);
158 EXPECT_POSE_EQ(computed_robot_pose, timestamped_robot_poses[3].pose);
159
160 computed_robot_pose =
161 PoseUtils::ComputeOffsetPose(computed_robot_pose, robot_delta_poses[3]);
162 EXPECT_POSE_EQ(computed_robot_pose, timestamped_robot_poses[4].pose);
163
164 computed_robot_pose =
165 PoseUtils::ComputeOffsetPose(computed_robot_pose, robot_delta_poses[4]);
166 EXPECT_POSE_IN_RANGE(computed_robot_pose, timestamped_robot_poses[5].pose,
167 timestamped_robot_poses[6].pose);
168
169 // Check the confidence matrices. Don't check the actual values
170 // in case the constants change, just check the confidence of contraints
171 // relative to each other, as constraints over longer time periods should have
172 // lower confidence.
173 const auto confidence_0ms =
174 DataAdapter::ComputeConfidence(TimeInMs(0), TimeInMs(0));
175 const auto confidence_1ms =
176 DataAdapter::ComputeConfidence(TimeInMs(0), TimeInMs(1));
177 const auto confidence_4ms =
178 DataAdapter::ComputeConfidence(TimeInMs(0), TimeInMs(4));
179 const auto confidence_6ms =
180 DataAdapter::ComputeConfidence(TimeInMs(0), TimeInMs(6));
181 const auto confidence_11ms =
182 DataAdapter::ComputeConfidence(TimeInMs(0), TimeInMs(11));
183
184 // Check relative magnitude of different confidences.
185 // Confidences for 0-5ms, 5-10ms, and 10-15ms periods are equal
186 // because they fit within one control loop iteration.
187 EXPECT_EQ(confidence_0ms, confidence_1ms);
188 EXPECT_EQ(confidence_1ms, confidence_4ms);
189 EXPECT_CONFIDENCE_GT(confidence_4ms, confidence_6ms);
190 EXPECT_CONFIDENCE_GT(confidence_6ms, confidence_11ms);
191
192 // Check that confidences (information) of actual constraints are correct
193 EXPECT_EQ(target_constraints[0].information, confidence_4ms);
194 EXPECT_EQ(target_constraints[1].information, confidence_0ms);
195 EXPECT_EQ(target_constraints[2].information, confidence_6ms);
196 EXPECT_EQ(target_constraints[3].information, confidence_1ms);
197 EXPECT_EQ(target_constraints[4].information, confidence_11ms);
198}
199
200TEST(TargetMapperTest, TwoTargetsOneConstraint) {
201 std::map<TargetMapper::TargetId, ceres::examples::Pose2d> target_poses;
202 target_poses[0] = ceres::examples::Pose2d{5.0, 0.0, M_PI};
203 target_poses[1] = ceres::examples::Pose2d{-5.0, 0.0, 0.0};
204
205 std::vector<DataAdapter::TimestampedPose> timestamped_robot_poses = {
206 {TimeInMs(5), ceres::examples::Pose2d{2.0, 0.0, 0.0}},
207 {TimeInMs(10), ceres::examples::Pose2d{-1.0, 0.0, 0.0}},
208 {TimeInMs(15), ceres::examples::Pose2d{-1.0, 0.0, 0.0}}};
209 std::vector<DataAdapter::TimestampedDetection> timestamped_target_detections =
210 {{TimeInMs(5),
211 PoseUtils::Pose2dToAffine3d(ceres::examples::Pose2d{3.0, 0.0, M_PI}),
212 0},
213 {TimeInMs(10),
214 PoseUtils::Pose2dToAffine3d(ceres::examples::Pose2d{-4.0, 0.0, 0.0}),
215 1}};
216 auto target_constraints =
217 DataAdapter::MatchTargetDetections(timestamped_robot_poses,
218 timestamped_target_detections)
219 .first;
220
221 frc971::vision::TargetMapper mapper(target_poses, target_constraints);
222 mapper.Solve();
223
224 ASSERT_EQ(mapper.target_poses().size(), 2);
225 EXPECT_POSE_NEAR(mapper.target_poses()[0], MakePose(5.0, 0.0, M_PI));
226 EXPECT_POSE_NEAR(mapper.target_poses()[1], MakePose(-5.0, 0.0, 0.0));
227}
228
229TEST(TargetMapperTest, TwoTargetsTwoConstraints) {
230 std::map<TargetMapper::TargetId, ceres::examples::Pose2d> target_poses;
231 target_poses[0] = ceres::examples::Pose2d{5.0, 0.0, M_PI};
232 target_poses[1] = ceres::examples::Pose2d{-5.0, 0.0, -M_PI_2};
233
234 std::vector<DataAdapter::TimestampedPose> timestamped_robot_poses = {
235 {TimeInMs(5), ceres::examples::Pose2d{-1.0, 0.0, 0.0}},
236 {TimeInMs(10), ceres::examples::Pose2d{3.0, 0.0, 0.0}},
237 {TimeInMs(15), ceres::examples::Pose2d{4.0, 0.0, 0.0}},
238 {TimeInMs(20), ceres::examples::Pose2d{-1.0, 0.0, 0.0}}};
239 std::vector<DataAdapter::TimestampedDetection> timestamped_target_detections =
240 {{TimeInMs(5),
241 PoseUtils::Pose2dToAffine3d(ceres::examples::Pose2d{6.0, 0.0, M_PI}),
242 0},
243 {TimeInMs(10),
244 PoseUtils::Pose2dToAffine3d(
245 ceres::examples::Pose2d{-8.0, 0.0, -M_PI_2}),
246 1},
247 {TimeInMs(15),
248 PoseUtils::Pose2dToAffine3d(ceres::examples::Pose2d{1.0, 0.0, M_PI}),
249 0}};
250 auto target_constraints =
251 DataAdapter::MatchTargetDetections(timestamped_robot_poses,
252 timestamped_target_detections)
253 .first;
254
255 frc971::vision::TargetMapper mapper(target_poses, target_constraints);
256 mapper.Solve();
257
258 ASSERT_EQ(mapper.target_poses().size(), 2);
259 EXPECT_POSE_NEAR(mapper.target_poses()[0], MakePose(5.0, 0.0, M_PI));
260 EXPECT_POSE_NEAR(mapper.target_poses()[1], MakePose(-5.0, 0.0, -M_PI_2));
261}
262
263TEST(TargetMapperTest, TwoTargetsOneNoisyConstraint) {
264 std::map<TargetMapper::TargetId, ceres::examples::Pose2d> target_poses;
265 target_poses[0] = ceres::examples::Pose2d{5.0, 0.0, M_PI};
266 target_poses[1] = ceres::examples::Pose2d{-5.0, 0.0, 0.0};
267
268 std::vector<DataAdapter::TimestampedPose> timestamped_robot_poses = {
269 {TimeInMs(5), ceres::examples::Pose2d{1.99, 0.0, 0.0}},
270 {TimeInMs(10), ceres::examples::Pose2d{-1.0, 0.0, 0.0}},
271 {TimeInMs(15), ceres::examples::Pose2d{-1.01, -0.01, 0.004}}};
272 std::vector<DataAdapter::TimestampedDetection> timestamped_target_detections =
273 {{TimeInMs(5),
274 PoseUtils::Pose2dToAffine3d(
275 ceres::examples::Pose2d{3.01, 0.001, M_PI - 0.001}),
276 0},
277 {TimeInMs(10),
278 PoseUtils::Pose2dToAffine3d(ceres::examples::Pose2d{-4.01, 0.0, 0.0}),
279 1}};
280
281 auto target_constraints =
282 DataAdapter::MatchTargetDetections(timestamped_robot_poses,
283 timestamped_target_detections)
284 .first;
285
286 frc971::vision::TargetMapper mapper(target_poses, target_constraints);
287 mapper.Solve();
288
289 ASSERT_EQ(mapper.target_poses().size(), 2);
290 EXPECT_POSE_NEAR(mapper.target_poses()[0], MakePose(5.0, 0.0, M_PI));
291 EXPECT_POSE_NEAR(mapper.target_poses()[1], MakePose(-5.0, 0.0, 0.0));
292}
293
294TEST(TargetMapperTest, MultiTargetCircleMotion) {
295 // Build set of target locations wrt global origin
296 // For simplicity, do this on a grid of the field
297 double field_half_length = 7.5; // half length of the field
298 double field_half_width = 5.0; // half width of the field
299 std::map<TargetMapper::TargetId, ceres::examples::Pose2d> target_poses;
300 std::vector<TargetMapper::TargetPose> actual_target_poses;
301 for (int i = 0; i < 3; i++) {
302 for (int j = 0; j < 3; j++) {
303 TargetMapper::TargetId target_id = i * 3 + j;
304 TargetMapper::TargetPose target_pose{
305 target_id, ceres::examples::Pose2d{field_half_length * (1 - i),
306 field_half_width * (1 - j), 0.0}};
307 actual_target_poses.emplace_back(target_pose);
308 target_poses[target_id] = target_pose.pose;
309 VLOG(2) << "VERTEX_SE2 " << target_id << " " << target_pose.pose.x << " "
310 << target_pose.pose.y << " " << target_pose.pose.yaw_radians;
311 }
312 }
313
314 // Now, create a bunch of robot poses and target
315 // observations
316 size_t dt = 1;
317
318 std::vector<DataAdapter::TimestampedPose> timestamped_robot_poses;
319 std::vector<DataAdapter::TimestampedDetection> timestamped_target_detections;
320
321 constexpr size_t kTotalSteps = 100;
322 for (size_t step_count = 0; step_count < kTotalSteps; step_count++) {
323 size_t t = dt * step_count;
324 // Circle clockwise around the center of the field
325 double robot_theta = t;
326 double robot_x = (field_half_length / 2.0) * cos(robot_theta);
327 double robot_y = (-field_half_width / 2.0) * sin(robot_theta);
328
329 ceres::examples::Pose2d robot_pose{robot_x, robot_y, robot_theta};
330 for (TargetMapper::TargetPose target_pose : actual_target_poses) {
331 TargetMapper::TargetPose target_detection = {
332 .id = target_pose.id,
333 .pose = PoseUtils::ComputeRelativePose(robot_pose, target_pose.pose)};
334 if (TargetIsInView(target_detection)) {
335 // Define random generator with Gaussian
336 // distribution
337 const double mean = 0.0;
338 const double stddev = 1.0;
339 // Can play with this to see how it impacts
340 // randomness
341 constexpr double kNoiseScale = 0.01;
342 std::default_random_engine generator(aos::testing::RandomSeed());
343 std::normal_distribution<double> dist(mean, stddev);
344
345 target_detection.pose.x += dist(generator) * kNoiseScale;
346 target_detection.pose.y += dist(generator) * kNoiseScale;
347 robot_pose.x += dist(generator) * kNoiseScale;
348 robot_pose.y += dist(generator) * kNoiseScale;
349
350 auto time_point =
351 aos::distributed_clock::time_point(std::chrono::milliseconds(t));
352 timestamped_robot_poses.emplace_back(DataAdapter::TimestampedPose{
353 .time = time_point, .pose = robot_pose});
354 timestamped_target_detections.emplace_back(
355 DataAdapter::TimestampedDetection{
356 .time = time_point,
357 .H_robot_target =
358 PoseUtils::Pose2dToAffine3d(target_detection.pose),
359 .id = target_detection.id});
360 }
361 }
362 }
363
364 {
365 // Add in a robot pose after all target poses
366 auto final_robot_pose =
367 timestamped_robot_poses[timestamped_robot_poses.size() - 1];
368 timestamped_robot_poses.emplace_back(DataAdapter::TimestampedPose{
369 .time = final_robot_pose.time + std::chrono::milliseconds(dt),
370 .pose = final_robot_pose.pose});
371 }
372
373 auto target_constraints =
374 DataAdapter::MatchTargetDetections(timestamped_robot_poses,
375 timestamped_target_detections)
376 .first;
377 frc971::vision::TargetMapper mapper(target_poses, target_constraints);
378 mapper.Solve();
379
380 for (auto [target_pose_id, mapper_target_pose] : mapper.target_poses()) {
381 TargetMapper::TargetPose actual_target_pose =
382 TargetMapper::GetTargetPoseById(actual_target_poses, target_pose_id)
383 .value();
384 EXPECT_POSE_NEAR(mapper_target_pose, actual_target_pose.pose);
385 }
386
387 //
388 // See what happens when we don't start with the
389 // correct values
390 //
391 for (auto [target_id, target_pose] : target_poses) {
392 // Skip first pose, since that needs to be correct
393 // and is fixed in the solver
394 if (target_id != 0) {
395 ceres::examples::Pose2d bad_pose{0.0, 0.0, M_PI / 2.0};
396 target_poses[target_id] = bad_pose;
397 }
398 }
399
400 frc971::vision::TargetMapper mapper_bad_poses(target_poses,
401 target_constraints);
402 mapper_bad_poses.Solve();
403
404 for (auto [target_pose_id, mapper_target_pose] :
405 mapper_bad_poses.target_poses()) {
406 TargetMapper::TargetPose actual_target_pose =
407 TargetMapper::GetTargetPoseById(actual_target_poses, target_pose_id)
408 .value();
409 EXPECT_POSE_NEAR(mapper_target_pose, actual_target_pose.pose);
410 }
411}
412
413} // namespace frc971::vision