blob: 269b1b95a082120c18a9be13035c3c2f94e140e3 [file] [log] [blame]
Maxwell Henderson123c8172024-03-01 22:54:16 -08001#include <string>
2
3#include "Eigen/Dense"
4#include "opencv2/aruco.hpp"
5#include "opencv2/calib3d.hpp"
6#include "opencv2/core/eigen.hpp"
7#include "opencv2/features2d.hpp"
8#include "opencv2/highgui.hpp"
9#include "opencv2/highgui/highgui.hpp"
10#include "opencv2/imgproc.hpp"
11
12#include "aos/configuration.h"
13#include "aos/events/logging/log_reader.h"
14#include "aos/events/simulated_event_loop.h"
15#include "aos/init.h"
16#include "aos/util/mcap_logger.h"
17#include "frc971/constants/constants_sender_lib.h"
18#include "frc971/control_loops/pose.h"
19#include "frc971/vision/calibration_generated.h"
20#include "frc971/vision/charuco_lib.h"
21#include "frc971/vision/target_mapper.h"
Jim Ostrowski67726282024-03-24 14:39:33 -070022#include "frc971/vision/vision_generated.h"
Maxwell Henderson123c8172024-03-01 22:54:16 -080023#include "frc971/vision/vision_util_lib.h"
24#include "frc971/vision/visualize_robot.h"
25#include "y2024/constants/simulated_constants_sender.h"
26#include "y2024/vision/vision_util.h"
27
28DEFINE_string(config, "",
29 "If set, override the log's config file with this one.");
30DEFINE_string(constants_path, "y2024/constants/constants.json",
31 "Path to the constant file");
32DEFINE_string(dump_constraints_to, "/tmp/mapping_constraints.txt",
33 "Write the target constraints to this path");
34DEFINE_string(dump_stats_to, "/tmp/mapping_stats.txt",
35 "Write the mapping stats to this path");
Jim Ostrowski5ff3c562024-03-20 20:35:11 -070036DEFINE_string(field_name, "crescendo",
Maxwell Henderson123c8172024-03-01 22:54:16 -080037 "Field name, for the output json filename and flatbuffer field");
38DEFINE_string(json_path, "y2024/vision/maps/target_map.json",
39 "Specify path for json with initial pose guesses.");
40DEFINE_double(max_pose_error, 1e-6,
41 "Throw out target poses with a higher pose error than this");
42DEFINE_double(
43 max_pose_error_ratio, 0.4,
44 "Throw out target poses with a higher pose error ratio than this");
45DEFINE_string(mcap_output_path, "", "Log to output.");
46DEFINE_string(output_dir, "y2024/vision/maps",
47 "Directory to write solved target map to");
Maxwell Henderson9d7b1862024-03-02 11:00:29 -080048DEFINE_double(pause_on_distance, 2.0,
Maxwell Henderson123c8172024-03-01 22:54:16 -080049 "Pause if two consecutive implied robot positions differ by more "
50 "than this many meters");
51DEFINE_string(orin, "orin1",
Jim Ostrowski67726282024-03-24 14:39:33 -070052 "Orin name to generate mcap log for; defaults to orin1.");
Maxwell Henderson123c8172024-03-01 22:54:16 -080053DEFINE_uint64(skip_to, 1,
54 "Start at combined image of this number (1 is the first image)");
55DEFINE_bool(solve, true, "Whether to solve for the field's target map.");
Jim Ostrowski5ff3c562024-03-20 20:35:11 -070056DEFINE_bool(split_field, false,
57 "Whether to break solve into two sides of field");
Maxwell Henderson123c8172024-03-01 22:54:16 -080058DEFINE_int32(team_number, 0,
59 "Required: Use the calibration for a node with this team number");
60DEFINE_uint64(wait_key, 1,
61 "Time in ms to wait between images, if no click (0 to wait "
62 "indefinitely until click).");
63
64DECLARE_int32(frozen_target_id);
65DECLARE_int32(min_target_id);
66DECLARE_int32(max_target_id);
67DECLARE_bool(visualize_solver);
68
69namespace y2024::vision {
70using frc971::vision::DataAdapter;
71using frc971::vision::ImageCallback;
72using frc971::vision::PoseUtils;
73using frc971::vision::TargetMap;
74using frc971::vision::TargetMapper;
75using frc971::vision::VisualizeRobot;
76namespace calibration = frc971::vision::calibration;
77
78// Class to handle reading target poses from a replayed log,
79// displaying various debug info, and passing the poses to
80// frc971::vision::TargetMapper for field mapping.
81class TargetMapperReplay {
82 public:
83 TargetMapperReplay(aos::logger::LogReader *reader);
84
85 // Solves for the target poses with the accumulated detections if FLAGS_solve.
86 void MaybeSolve();
87
88 private:
89 static constexpr int kImageWidth = 1280;
Jim Ostrowski67726282024-03-24 14:39:33 -070090
Maxwell Henderson123c8172024-03-01 22:54:16 -080091 // Contains fixed target poses without solving, for use with visualization
92 static const TargetMapper kFixedTargetMapper;
93
Jim Ostrowski5ff3c562024-03-20 20:35:11 -070094 // Map of TargetId to alliance "color" for splitting field
95 static std::map<uint, std::string> kIdAllianceMap;
96
Maxwell Henderson123c8172024-03-01 22:54:16 -080097 // Change reference frame from camera to robot
98 static Eigen::Affine3d CameraToRobotDetection(Eigen::Affine3d H_camera_target,
99 Eigen::Affine3d extrinsics);
100
101 // Adds april tag detections into the detection list, and handles
102 // visualization
103 void HandleAprilTags(const TargetMap &map,
104 aos::distributed_clock::time_point node_distributed_time,
105 std::string camera_name, Eigen::Affine3d extrinsics);
Maxwell Henderson123c8172024-03-01 22:54:16 -0800106 // Gets images from the given pi and passes apriltag positions to
107 // HandleAprilTags()
108 void HandleNodeCaptures(
109 aos::EventLoop *mapping_event_loop,
110 frc971::constants::ConstantsFetcher<y2024::Constants> *constants_fetcher,
111 int camera_number);
112
113 aos::logger::LogReader *reader_;
114 // April tag detections from all pis
115 std::vector<DataAdapter::TimestampedDetection> timestamped_target_detections_;
116
117 VisualizeRobot vis_robot_;
Jim Ostrowski5ff3c562024-03-20 20:35:11 -0700118 // Set of camera names which are currently drawn on the display
119 std::set<std::string> drawn_cameras_;
Maxwell Henderson123c8172024-03-01 22:54:16 -0800120 // Number of frames displayed
121 size_t display_count_;
122 // Last time we drew onto the display image.
123 // This is different from when we actually call imshow() to update
124 // the display window
125 aos::distributed_clock::time_point last_draw_time_;
126
127 Eigen::Affine3d last_H_world_robot_;
128 // Maximum distance between consecutive T_world_robot's in one display frame,
129 // used to determine if we need to pause for the user to see this frame
130 // clearly
131 double max_delta_T_world_robot_;
Jim Ostrowski5ff3c562024-03-20 20:35:11 -0700132 double ignore_count_;
133
Maxwell Henderson123c8172024-03-01 22:54:16 -0800134 std::vector<std::unique_ptr<aos::EventLoop>> mapping_event_loops_;
135
136 std::unique_ptr<aos::EventLoop> mcap_event_loop_;
137 std::unique_ptr<aos::McapLogger> relogger_;
138};
139
Jim Ostrowski67726282024-03-24 14:39:33 -0700140std::vector<CameraNode> node_list(y2024::vision::CreateNodeList());
141
142std::map<std::string, int> camera_ordering_map(
143 y2024::vision::CreateOrderingMap(node_list));
Maxwell Henderson123c8172024-03-01 22:54:16 -0800144
Jim Ostrowski5ff3c562024-03-20 20:35:11 -0700145std::map<uint, std::string> TargetMapperReplay::kIdAllianceMap = {
146 {1, "red"}, {2, "red"}, {3, "red"}, {4, "red"},
147 {5, "red"}, {6, "blue"}, {7, "blue"}, {8, "blue"},
148 {9, "blue"}, {10, "blue"}, {11, "red"}, {12, "red"},
149 {13, "red"}, {14, "blue"}, {15, "blue"}, {16, "blue"}};
150
Maxwell Henderson123c8172024-03-01 22:54:16 -0800151const auto TargetMapperReplay::kFixedTargetMapper =
152 TargetMapper(FLAGS_json_path, ceres::examples::VectorOfConstraints{});
153
154Eigen::Affine3d TargetMapperReplay::CameraToRobotDetection(
155 Eigen::Affine3d H_camera_target, Eigen::Affine3d extrinsics) {
156 const Eigen::Affine3d H_robot_camera = extrinsics;
157 const Eigen::Affine3d H_robot_target = H_robot_camera * H_camera_target;
158 return H_robot_target;
159}
160
161TargetMapperReplay::TargetMapperReplay(aos::logger::LogReader *reader)
162 : reader_(reader),
163 timestamped_target_detections_(),
164 vis_robot_(cv::Size(1280, 1000)),
Jim Ostrowski5ff3c562024-03-20 20:35:11 -0700165 drawn_cameras_(),
Maxwell Henderson123c8172024-03-01 22:54:16 -0800166 display_count_(0),
167 last_draw_time_(aos::distributed_clock::min_time),
168 last_H_world_robot_(Eigen::Matrix4d::Identity()),
169 max_delta_T_world_robot_(0.0) {
170 reader_->RemapLoggedChannel("/orin1/constants", "y2024.Constants");
171 reader_->RemapLoggedChannel("/imu/constants", "y2024.Constants");
Jim Ostrowski5ff3c562024-03-20 20:35:11 -0700172 // If it's Box of Orins, don't remap roborio constants
173 if (FLAGS_team_number == 7971) {
174 reader_->RemapLoggedChannel("/roborio/constants", "y2024.Constants");
175 }
Maxwell Henderson123c8172024-03-01 22:54:16 -0800176 reader_->Register();
177
178 SendSimulationConstants(reader_->event_loop_factory(), FLAGS_team_number,
179 FLAGS_constants_path);
180
Jim Ostrowski67726282024-03-24 14:39:33 -0700181 if (FLAGS_visualize_solver) {
182 vis_robot_.ClearImage();
183 // Set focal length to zoomed in, to view extrinsics
184 const double kFocalLength = 1500.0;
185 vis_robot_.SetDefaultViewpoint(kImageWidth, kFocalLength);
186 }
Maxwell Henderson123c8172024-03-01 22:54:16 -0800187
Jim Ostrowski67726282024-03-24 14:39:33 -0700188 for (const CameraNode &camera_node : node_list) {
189 const aos::Node *node = aos::configuration::GetNode(
190 reader_->configuration(), camera_node.node_name.c_str());
Maxwell Henderson123c8172024-03-01 22:54:16 -0800191
192 mapping_event_loops_.emplace_back(
Jim Ostrowski67726282024-03-24 14:39:33 -0700193 reader_->event_loop_factory()->MakeEventLoop(
194 camera_node.node_name + "mapping", node));
195
Maxwell Henderson123c8172024-03-01 22:54:16 -0800196 frc971::constants::ConstantsFetcher<y2024::Constants> constants_fetcher(
197 mapping_event_loops_[mapping_event_loops_.size() - 1].get());
198 HandleNodeCaptures(
Maxwell Henderson123c8172024-03-01 22:54:16 -0800199 mapping_event_loops_[mapping_event_loops_.size() - 1].get(),
Jim Ostrowski67726282024-03-24 14:39:33 -0700200 &constants_fetcher, camera_node.camera_number);
201
202 if (FLAGS_visualize_solver) {
203 // Show the extrinsics calibration to start, for reference to confirm
204 const auto *calibration = FindCameraCalibration(
205 constants_fetcher.constants(),
206 mapping_event_loops_.back()->node()->name()->string_view(),
207 camera_node.camera_number);
208 cv::Mat extrinsics_cv =
209 frc971::vision::CameraExtrinsics(calibration).value();
210 Eigen::Matrix4d extrinsics_matrix;
211 cv::cv2eigen(extrinsics_cv, extrinsics_matrix);
212 const auto extrinsics = Eigen::Affine3d(extrinsics_matrix);
213
214 vis_robot_.DrawRobotOutline(extrinsics, camera_node.camera_name(),
215 kOrinColors.at(camera_node.camera_name()));
216 }
Maxwell Henderson123c8172024-03-01 22:54:16 -0800217 }
218
219 if (FLAGS_visualize_solver) {
Jim Ostrowski67726282024-03-24 14:39:33 -0700220 cv::imshow("Extrinsics", vis_robot_.image_);
221 cv::waitKey(0);
Maxwell Henderson123c8172024-03-01 22:54:16 -0800222 vis_robot_.ClearImage();
Jim Ostrowski67726282024-03-24 14:39:33 -0700223 // Reset focal length to more zoomed out view for field
Maxwell Henderson123c8172024-03-01 22:54:16 -0800224 const double kFocalLength = 500.0;
225 vis_robot_.SetDefaultViewpoint(kImageWidth, kFocalLength);
226 }
227}
228
229// Add detected apriltag poses relative to the robot to
230// timestamped_target_detections
231void TargetMapperReplay::HandleAprilTags(
232 const TargetMap &map,
233 aos::distributed_clock::time_point node_distributed_time,
234 std::string camera_name, Eigen::Affine3d extrinsics) {
235 bool drew = false;
236 std::stringstream label;
237 label << camera_name << " - ";
238
Jim Ostrowski5ff3c562024-03-20 20:35:11 -0700239 if (map.target_poses()->size() == 0) {
240 VLOG(2) << "Got 0 AprilTags for camera " << camera_name;
241 return;
242 }
243
Maxwell Henderson123c8172024-03-01 22:54:16 -0800244 for (const auto *target_pose_fbs : *map.target_poses()) {
245 // Skip detections with invalid ids
246 if (static_cast<TargetMapper::TargetId>(target_pose_fbs->id()) <
247 FLAGS_min_target_id ||
248 static_cast<TargetMapper::TargetId>(target_pose_fbs->id()) >
249 FLAGS_max_target_id) {
250 VLOG(1) << "Skipping tag with invalid id of " << target_pose_fbs->id();
251 continue;
252 }
253
254 // Skip detections with high pose errors
255 if (target_pose_fbs->pose_error() > FLAGS_max_pose_error) {
256 VLOG(1) << "Skipping tag " << target_pose_fbs->id()
257 << " due to pose error of " << target_pose_fbs->pose_error();
258 continue;
259 }
260 // Skip detections with high pose error ratios
261 if (target_pose_fbs->pose_error_ratio() > FLAGS_max_pose_error_ratio) {
262 VLOG(1) << "Skipping tag " << target_pose_fbs->id()
263 << " due to pose error ratio of "
264 << target_pose_fbs->pose_error_ratio();
265 continue;
266 }
267
268 const TargetMapper::TargetPose target_pose =
269 PoseUtils::TargetPoseFromFbs(*target_pose_fbs);
270
271 Eigen::Affine3d H_camera_target =
272 Eigen::Translation3d(target_pose.pose.p) * target_pose.pose.q;
273 Eigen::Affine3d H_robot_target =
274 CameraToRobotDetection(H_camera_target, extrinsics);
275
276 ceres::examples::Pose3d target_pose_camera =
277 PoseUtils::Affine3dToPose3d(H_camera_target);
278 double distance_from_camera = target_pose_camera.p.norm();
279 double distortion_factor = target_pose_fbs->distortion_factor();
280
Jim Ostrowski5ff3c562024-03-20 20:35:11 -0700281 double distance_threshold = 5.0;
282 if (distance_from_camera > distance_threshold) {
283 ignore_count_++;
284 LOG(INFO) << "Ignored " << ignore_count_ << " AprilTags with distance "
285 << distance_from_camera << " > " << distance_threshold;
Maxwell Henderson123c8172024-03-01 22:54:16 -0800286 continue;
287 }
288
289 CHECK(map.has_monotonic_timestamp_ns())
290 << "Need detection timestamps for mapping";
291
Jim Ostrowski5ff3c562024-03-20 20:35:11 -0700292 // Detection is usable, so store it
Maxwell Henderson123c8172024-03-01 22:54:16 -0800293 timestamped_target_detections_.emplace_back(
294 DataAdapter::TimestampedDetection{
295 .time = node_distributed_time,
296 .H_robot_target = H_robot_target,
297 .distance_from_camera = distance_from_camera,
298 .distortion_factor = distortion_factor,
299 .id = static_cast<TargetMapper::TargetId>(target_pose.id)});
300
301 if (FLAGS_visualize_solver) {
302 // If we've already drawn this camera_name in the current image,
303 // display the image before clearing and adding the new poses
Jim Ostrowski5ff3c562024-03-20 20:35:11 -0700304 if (drawn_cameras_.count(camera_name) != 0) {
Maxwell Henderson123c8172024-03-01 22:54:16 -0800305 display_count_++;
306 cv::putText(vis_robot_.image_,
307 "Poses #" + std::to_string(display_count_),
308 cv::Point(600, 10), cv::FONT_HERSHEY_PLAIN, 1.0,
309 cv::Scalar(255, 255, 255));
310
311 if (display_count_ >= FLAGS_skip_to) {
Jim Ostrowski5ff3c562024-03-20 20:35:11 -0700312 VLOG(1) << "Showing image for camera " << camera_name
Maxwell Henderson123c8172024-03-01 22:54:16 -0800313 << " since we've drawn it already";
314 cv::imshow("View", vis_robot_.image_);
315 // Pause if delta_T is too large, but only after first image (to make
Jim Ostrowski67726282024-03-24 14:39:33 -0700316 // sure the delta's are correct)
Maxwell Henderson123c8172024-03-01 22:54:16 -0800317 if (max_delta_T_world_robot_ > FLAGS_pause_on_distance &&
318 display_count_ > 1) {
319 LOG(INFO) << "Pausing since the delta between robot estimates is "
320 << max_delta_T_world_robot_ << " which is > threshold of "
321 << FLAGS_pause_on_distance;
322 cv::waitKey(0);
323 } else {
324 cv::waitKey(FLAGS_wait_key);
325 }
326 max_delta_T_world_robot_ = 0.0;
327 } else {
Jim Ostrowski5ff3c562024-03-20 20:35:11 -0700328 VLOG(2) << "At poses #" << std::to_string(display_count_);
Maxwell Henderson123c8172024-03-01 22:54:16 -0800329 }
330 vis_robot_.ClearImage();
Jim Ostrowski5ff3c562024-03-20 20:35:11 -0700331 drawn_cameras_.clear();
Maxwell Henderson123c8172024-03-01 22:54:16 -0800332 }
333
334 Eigen::Affine3d H_world_target = PoseUtils::Pose3dToAffine3d(
335 kFixedTargetMapper.GetTargetPoseById(target_pose_fbs->id())->pose);
336 Eigen::Affine3d H_world_robot = H_world_target * H_robot_target.inverse();
337 VLOG(2) << camera_name << ", id " << target_pose_fbs->id()
338 << ", t = " << node_distributed_time
339 << ", pose_error = " << target_pose_fbs->pose_error()
340 << ", pose_error_ratio = " << target_pose_fbs->pose_error_ratio()
341 << ", robot_pos (x,y,z) = "
342 << H_world_robot.translation().transpose();
343
Jim Ostrowski67726282024-03-24 14:39:33 -0700344 label << "id " << target_pose_fbs->id()
345 << ": err (% of max): " << target_pose_fbs->pose_error() << " ("
Maxwell Henderson123c8172024-03-01 22:54:16 -0800346 << (target_pose_fbs->pose_error() / FLAGS_max_pose_error)
Jim Ostrowski67726282024-03-24 14:39:33 -0700347 << ") err_ratio: " << target_pose_fbs->pose_error_ratio() << " ";
Maxwell Henderson123c8172024-03-01 22:54:16 -0800348
349 vis_robot_.DrawRobotOutline(H_world_robot, camera_name,
350 kOrinColors.at(camera_name));
351 vis_robot_.DrawFrameAxes(H_world_target,
352 std::to_string(target_pose_fbs->id()),
353 kOrinColors.at(camera_name));
354
355 double delta_T_world_robot =
356 (H_world_robot.translation() - last_H_world_robot_.translation())
357 .norm();
358 max_delta_T_world_robot_ =
359 std::max(delta_T_world_robot, max_delta_T_world_robot_);
360
Jim Ostrowski5ff3c562024-03-20 20:35:11 -0700361 VLOG(1) << "Drew in info for camera " << camera_name << " and target #"
Maxwell Henderson123c8172024-03-01 22:54:16 -0800362 << target_pose_fbs->id();
363 drew = true;
364 last_draw_time_ = node_distributed_time;
365 last_H_world_robot_ = H_world_robot;
366 }
367 }
Maxwell Henderson123c8172024-03-01 22:54:16 -0800368 if (FLAGS_visualize_solver) {
369 if (drew) {
Jim Ostrowski5ff3c562024-03-20 20:35:11 -0700370 // Collect all the labels from a given camera, and add the text
371 // TODO: Need to fix this one
Jim Ostrowski67726282024-03-24 14:39:33 -0700372 int position_number = camera_ordering_map[camera_name];
Maxwell Henderson123c8172024-03-01 22:54:16 -0800373 cv::putText(vis_robot_.image_, label.str(),
Jim Ostrowski67726282024-03-24 14:39:33 -0700374 cv::Point(10, 30 + 20 * position_number),
Jim Ostrowski5ff3c562024-03-20 20:35:11 -0700375 cv::FONT_HERSHEY_PLAIN, 1.0, kOrinColors.at(camera_name));
Jim Ostrowski5ff3c562024-03-20 20:35:11 -0700376 drawn_cameras_.emplace(camera_name);
Maxwell Henderson123c8172024-03-01 22:54:16 -0800377 } else if (node_distributed_time - last_draw_time_ >
378 std::chrono::milliseconds(30) &&
Jim Ostrowski5ff3c562024-03-20 20:35:11 -0700379 display_count_ >= FLAGS_skip_to && drew) {
380 // TODO: Check on 30ms value-- does this make sense?
381 double delta_t = (node_distributed_time - last_draw_time_).count() / 1e6;
382 VLOG(1) << "Last result was " << delta_t << "ms ago";
383 cv::putText(vis_robot_.image_, "No detections in last 30ms",
384 cv::Point(10, 0), cv::FONT_HERSHEY_PLAIN, 1.0,
385 kOrinColors.at(camera_name));
Maxwell Henderson123c8172024-03-01 22:54:16 -0800386 // Display and clear the image if we haven't draw in a while
387 VLOG(1) << "Displaying image due to time lapse";
388 cv::imshow("View", vis_robot_.image_);
389 cv::waitKey(FLAGS_wait_key);
Maxwell Henderson123c8172024-03-01 22:54:16 -0800390 max_delta_T_world_robot_ = 0.0;
Jim Ostrowski5ff3c562024-03-20 20:35:11 -0700391 drawn_cameras_.clear();
Maxwell Henderson123c8172024-03-01 22:54:16 -0800392 }
393 }
394}
395
396void TargetMapperReplay::HandleNodeCaptures(
397 aos::EventLoop *mapping_event_loop,
398 frc971::constants::ConstantsFetcher<y2024::Constants> *constants_fetcher,
399 int camera_number) {
400 // Get the camera extrinsics
Jim Ostrowski67726282024-03-24 14:39:33 -0700401 std::string node_name =
402 std::string(mapping_event_loop->node()->name()->string_view());
Maxwell Henderson123c8172024-03-01 22:54:16 -0800403 const auto *calibration = FindCameraCalibration(
Jim Ostrowski67726282024-03-24 14:39:33 -0700404 constants_fetcher->constants(), node_name, camera_number);
Maxwell Henderson123c8172024-03-01 22:54:16 -0800405 cv::Mat extrinsics_cv = frc971::vision::CameraExtrinsics(calibration).value();
406 Eigen::Matrix4d extrinsics_matrix;
407 cv::cv2eigen(extrinsics_cv, extrinsics_matrix);
408 const auto extrinsics = Eigen::Affine3d(extrinsics_matrix);
409 std::string camera_name = absl::StrFormat(
410 "/%s/camera%d", mapping_event_loop->node()->name()->str(), camera_number);
411
412 mapping_event_loop->MakeWatcher(
413 camera_name.c_str(), [this, mapping_event_loop, extrinsics,
414 camera_name](const TargetMap &map) {
415 aos::distributed_clock::time_point node_distributed_time =
416 reader_->event_loop_factory()
417 ->GetNodeEventLoopFactory(mapping_event_loop->node())
418 ->ToDistributedClock(aos::monotonic_clock::time_point(
419 aos::monotonic_clock::duration(
420 map.monotonic_timestamp_ns())));
421
422 HandleAprilTags(map, node_distributed_time, camera_name, extrinsics);
423 });
424}
425
426void TargetMapperReplay::MaybeSolve() {
427 if (FLAGS_solve) {
428 auto target_constraints =
429 DataAdapter::MatchTargetDetections(timestamped_target_detections_);
430
Jim Ostrowski5ff3c562024-03-20 20:35:11 -0700431 if (FLAGS_split_field) {
432 // Remove constraints between the two sides of the field - these are
433 // basically garbage because of how far the camera is. We will use seeding
434 // below to connect the two sides
435 target_constraints.erase(
436 std::remove_if(
437 target_constraints.begin(), target_constraints.end(),
438 [](const auto &constraint) {
439 return (
440 kIdAllianceMap[static_cast<uint>(constraint.id_begin)] !=
441 kIdAllianceMap[static_cast<uint>(constraint.id_end)]);
442 }),
443 target_constraints.end());
444 }
Maxwell Henderson123c8172024-03-01 22:54:16 -0800445
446 LOG(INFO) << "Solving for locations of tags with "
447 << target_constraints.size() << " constraints";
448 TargetMapper mapper(FLAGS_json_path, target_constraints);
449 mapper.Solve(FLAGS_field_name, FLAGS_output_dir);
450
451 if (!FLAGS_dump_constraints_to.empty()) {
452 mapper.DumpConstraints(FLAGS_dump_constraints_to);
453 }
454 if (!FLAGS_dump_stats_to.empty()) {
455 mapper.DumpStats(FLAGS_dump_stats_to);
456 }
Jim Ostrowskif41b0942024-03-24 18:05:02 -0700457 mapper.PrintDiffs();
Maxwell Henderson123c8172024-03-01 22:54:16 -0800458 }
459}
460
461void MappingMain(int argc, char *argv[]) {
462 std::vector<DataAdapter::TimestampedDetection> timestamped_target_detections;
463
464 std::optional<aos::FlatbufferDetachedBuffer<aos::Configuration>> config =
465 (FLAGS_config.empty()
466 ? std::nullopt
467 : std::make_optional(aos::configuration::ReadConfig(FLAGS_config)));
468
469 // Open logfiles
470 aos::logger::LogReader reader(
471 aos::logger::SortParts(aos::logger::FindLogs(argc, argv)),
472 config.has_value() ? &config->message() : nullptr);
473
474 TargetMapperReplay mapper_replay(&reader);
475 reader.event_loop_factory()->Run();
476 mapper_replay.MaybeSolve();
477}
478
479} // namespace y2024::vision
480
481int main(int argc, char **argv) {
482 aos::InitGoogle(&argc, &argv);
483 y2024::vision::MappingMain(argc, argv);
484}