blob: 568158067f2c55949ff52e36f7394209d8d00ced [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
James Kuszmaul9f5da5a2024-04-05 17:33:15 -0700173 reader_->MaybeRemapLoggedChannel<Constants>("/roborio/constants");
Maxwell Henderson123c8172024-03-01 22:54:16 -0800174 reader_->Register();
175
176 SendSimulationConstants(reader_->event_loop_factory(), FLAGS_team_number,
177 FLAGS_constants_path);
178
Jim Ostrowski67726282024-03-24 14:39:33 -0700179 if (FLAGS_visualize_solver) {
180 vis_robot_.ClearImage();
181 // Set focal length to zoomed in, to view extrinsics
182 const double kFocalLength = 1500.0;
183 vis_robot_.SetDefaultViewpoint(kImageWidth, kFocalLength);
184 }
Maxwell Henderson123c8172024-03-01 22:54:16 -0800185
Jim Ostrowski67726282024-03-24 14:39:33 -0700186 for (const CameraNode &camera_node : node_list) {
187 const aos::Node *node = aos::configuration::GetNode(
188 reader_->configuration(), camera_node.node_name.c_str());
Maxwell Henderson123c8172024-03-01 22:54:16 -0800189
190 mapping_event_loops_.emplace_back(
Jim Ostrowski67726282024-03-24 14:39:33 -0700191 reader_->event_loop_factory()->MakeEventLoop(
192 camera_node.node_name + "mapping", node));
193
Maxwell Henderson123c8172024-03-01 22:54:16 -0800194 frc971::constants::ConstantsFetcher<y2024::Constants> constants_fetcher(
195 mapping_event_loops_[mapping_event_loops_.size() - 1].get());
196 HandleNodeCaptures(
Maxwell Henderson123c8172024-03-01 22:54:16 -0800197 mapping_event_loops_[mapping_event_loops_.size() - 1].get(),
Jim Ostrowski67726282024-03-24 14:39:33 -0700198 &constants_fetcher, camera_node.camera_number);
199
200 if (FLAGS_visualize_solver) {
201 // Show the extrinsics calibration to start, for reference to confirm
202 const auto *calibration = FindCameraCalibration(
203 constants_fetcher.constants(),
204 mapping_event_loops_.back()->node()->name()->string_view(),
205 camera_node.camera_number);
206 cv::Mat extrinsics_cv =
207 frc971::vision::CameraExtrinsics(calibration).value();
208 Eigen::Matrix4d extrinsics_matrix;
209 cv::cv2eigen(extrinsics_cv, extrinsics_matrix);
210 const auto extrinsics = Eigen::Affine3d(extrinsics_matrix);
211
212 vis_robot_.DrawRobotOutline(extrinsics, camera_node.camera_name(),
213 kOrinColors.at(camera_node.camera_name()));
214 }
Maxwell Henderson123c8172024-03-01 22:54:16 -0800215 }
216
217 if (FLAGS_visualize_solver) {
Jim Ostrowski67726282024-03-24 14:39:33 -0700218 cv::imshow("Extrinsics", vis_robot_.image_);
219 cv::waitKey(0);
Maxwell Henderson123c8172024-03-01 22:54:16 -0800220 vis_robot_.ClearImage();
Jim Ostrowski67726282024-03-24 14:39:33 -0700221 // Reset focal length to more zoomed out view for field
Maxwell Henderson123c8172024-03-01 22:54:16 -0800222 const double kFocalLength = 500.0;
223 vis_robot_.SetDefaultViewpoint(kImageWidth, kFocalLength);
224 }
225}
226
227// Add detected apriltag poses relative to the robot to
228// timestamped_target_detections
229void TargetMapperReplay::HandleAprilTags(
230 const TargetMap &map,
231 aos::distributed_clock::time_point node_distributed_time,
232 std::string camera_name, Eigen::Affine3d extrinsics) {
233 bool drew = false;
234 std::stringstream label;
235 label << camera_name << " - ";
236
Jim Ostrowski5ff3c562024-03-20 20:35:11 -0700237 if (map.target_poses()->size() == 0) {
238 VLOG(2) << "Got 0 AprilTags for camera " << camera_name;
239 return;
240 }
241
Maxwell Henderson123c8172024-03-01 22:54:16 -0800242 for (const auto *target_pose_fbs : *map.target_poses()) {
243 // Skip detections with invalid ids
244 if (static_cast<TargetMapper::TargetId>(target_pose_fbs->id()) <
245 FLAGS_min_target_id ||
246 static_cast<TargetMapper::TargetId>(target_pose_fbs->id()) >
247 FLAGS_max_target_id) {
248 VLOG(1) << "Skipping tag with invalid id of " << target_pose_fbs->id();
249 continue;
250 }
251
252 // Skip detections with high pose errors
253 if (target_pose_fbs->pose_error() > FLAGS_max_pose_error) {
254 VLOG(1) << "Skipping tag " << target_pose_fbs->id()
255 << " due to pose error of " << target_pose_fbs->pose_error();
256 continue;
257 }
258 // Skip detections with high pose error ratios
259 if (target_pose_fbs->pose_error_ratio() > FLAGS_max_pose_error_ratio) {
260 VLOG(1) << "Skipping tag " << target_pose_fbs->id()
261 << " due to pose error ratio of "
262 << target_pose_fbs->pose_error_ratio();
263 continue;
264 }
265
266 const TargetMapper::TargetPose target_pose =
267 PoseUtils::TargetPoseFromFbs(*target_pose_fbs);
268
269 Eigen::Affine3d H_camera_target =
270 Eigen::Translation3d(target_pose.pose.p) * target_pose.pose.q;
271 Eigen::Affine3d H_robot_target =
272 CameraToRobotDetection(H_camera_target, extrinsics);
273
274 ceres::examples::Pose3d target_pose_camera =
275 PoseUtils::Affine3dToPose3d(H_camera_target);
276 double distance_from_camera = target_pose_camera.p.norm();
277 double distortion_factor = target_pose_fbs->distortion_factor();
278
Jim Ostrowski5ff3c562024-03-20 20:35:11 -0700279 double distance_threshold = 5.0;
280 if (distance_from_camera > distance_threshold) {
281 ignore_count_++;
282 LOG(INFO) << "Ignored " << ignore_count_ << " AprilTags with distance "
283 << distance_from_camera << " > " << distance_threshold;
Maxwell Henderson123c8172024-03-01 22:54:16 -0800284 continue;
285 }
286
287 CHECK(map.has_monotonic_timestamp_ns())
288 << "Need detection timestamps for mapping";
289
Jim Ostrowski5ff3c562024-03-20 20:35:11 -0700290 // Detection is usable, so store it
Maxwell Henderson123c8172024-03-01 22:54:16 -0800291 timestamped_target_detections_.emplace_back(
292 DataAdapter::TimestampedDetection{
293 .time = node_distributed_time,
294 .H_robot_target = H_robot_target,
295 .distance_from_camera = distance_from_camera,
296 .distortion_factor = distortion_factor,
297 .id = static_cast<TargetMapper::TargetId>(target_pose.id)});
298
299 if (FLAGS_visualize_solver) {
300 // If we've already drawn this camera_name in the current image,
301 // display the image before clearing and adding the new poses
Jim Ostrowski5ff3c562024-03-20 20:35:11 -0700302 if (drawn_cameras_.count(camera_name) != 0) {
Maxwell Henderson123c8172024-03-01 22:54:16 -0800303 display_count_++;
304 cv::putText(vis_robot_.image_,
305 "Poses #" + std::to_string(display_count_),
306 cv::Point(600, 10), cv::FONT_HERSHEY_PLAIN, 1.0,
307 cv::Scalar(255, 255, 255));
308
309 if (display_count_ >= FLAGS_skip_to) {
Jim Ostrowski5ff3c562024-03-20 20:35:11 -0700310 VLOG(1) << "Showing image for camera " << camera_name
Maxwell Henderson123c8172024-03-01 22:54:16 -0800311 << " since we've drawn it already";
312 cv::imshow("View", vis_robot_.image_);
313 // Pause if delta_T is too large, but only after first image (to make
Jim Ostrowski67726282024-03-24 14:39:33 -0700314 // sure the delta's are correct)
Maxwell Henderson123c8172024-03-01 22:54:16 -0800315 if (max_delta_T_world_robot_ > FLAGS_pause_on_distance &&
316 display_count_ > 1) {
317 LOG(INFO) << "Pausing since the delta between robot estimates is "
318 << max_delta_T_world_robot_ << " which is > threshold of "
319 << FLAGS_pause_on_distance;
320 cv::waitKey(0);
321 } else {
322 cv::waitKey(FLAGS_wait_key);
323 }
324 max_delta_T_world_robot_ = 0.0;
325 } else {
Jim Ostrowski5ff3c562024-03-20 20:35:11 -0700326 VLOG(2) << "At poses #" << std::to_string(display_count_);
Maxwell Henderson123c8172024-03-01 22:54:16 -0800327 }
328 vis_robot_.ClearImage();
Jim Ostrowski5ff3c562024-03-20 20:35:11 -0700329 drawn_cameras_.clear();
Maxwell Henderson123c8172024-03-01 22:54:16 -0800330 }
331
332 Eigen::Affine3d H_world_target = PoseUtils::Pose3dToAffine3d(
333 kFixedTargetMapper.GetTargetPoseById(target_pose_fbs->id())->pose);
334 Eigen::Affine3d H_world_robot = H_world_target * H_robot_target.inverse();
335 VLOG(2) << camera_name << ", id " << target_pose_fbs->id()
336 << ", t = " << node_distributed_time
337 << ", pose_error = " << target_pose_fbs->pose_error()
338 << ", pose_error_ratio = " << target_pose_fbs->pose_error_ratio()
339 << ", robot_pos (x,y,z) = "
340 << H_world_robot.translation().transpose();
341
Jim Ostrowski67726282024-03-24 14:39:33 -0700342 label << "id " << target_pose_fbs->id()
343 << ": err (% of max): " << target_pose_fbs->pose_error() << " ("
Maxwell Henderson123c8172024-03-01 22:54:16 -0800344 << (target_pose_fbs->pose_error() / FLAGS_max_pose_error)
Jim Ostrowski67726282024-03-24 14:39:33 -0700345 << ") err_ratio: " << target_pose_fbs->pose_error_ratio() << " ";
Maxwell Henderson123c8172024-03-01 22:54:16 -0800346
347 vis_robot_.DrawRobotOutline(H_world_robot, camera_name,
348 kOrinColors.at(camera_name));
349 vis_robot_.DrawFrameAxes(H_world_target,
350 std::to_string(target_pose_fbs->id()),
351 kOrinColors.at(camera_name));
352
353 double delta_T_world_robot =
354 (H_world_robot.translation() - last_H_world_robot_.translation())
355 .norm();
356 max_delta_T_world_robot_ =
357 std::max(delta_T_world_robot, max_delta_T_world_robot_);
358
Jim Ostrowski5ff3c562024-03-20 20:35:11 -0700359 VLOG(1) << "Drew in info for camera " << camera_name << " and target #"
Maxwell Henderson123c8172024-03-01 22:54:16 -0800360 << target_pose_fbs->id();
361 drew = true;
362 last_draw_time_ = node_distributed_time;
363 last_H_world_robot_ = H_world_robot;
364 }
365 }
Maxwell Henderson123c8172024-03-01 22:54:16 -0800366 if (FLAGS_visualize_solver) {
367 if (drew) {
Jim Ostrowski5ff3c562024-03-20 20:35:11 -0700368 // Collect all the labels from a given camera, and add the text
369 // TODO: Need to fix this one
Jim Ostrowski67726282024-03-24 14:39:33 -0700370 int position_number = camera_ordering_map[camera_name];
Maxwell Henderson123c8172024-03-01 22:54:16 -0800371 cv::putText(vis_robot_.image_, label.str(),
Jim Ostrowski67726282024-03-24 14:39:33 -0700372 cv::Point(10, 30 + 20 * position_number),
Jim Ostrowski5ff3c562024-03-20 20:35:11 -0700373 cv::FONT_HERSHEY_PLAIN, 1.0, kOrinColors.at(camera_name));
Jim Ostrowski5ff3c562024-03-20 20:35:11 -0700374 drawn_cameras_.emplace(camera_name);
Maxwell Henderson123c8172024-03-01 22:54:16 -0800375 } else if (node_distributed_time - last_draw_time_ >
376 std::chrono::milliseconds(30) &&
Jim Ostrowski5ff3c562024-03-20 20:35:11 -0700377 display_count_ >= FLAGS_skip_to && drew) {
378 // TODO: Check on 30ms value-- does this make sense?
379 double delta_t = (node_distributed_time - last_draw_time_).count() / 1e6;
380 VLOG(1) << "Last result was " << delta_t << "ms ago";
381 cv::putText(vis_robot_.image_, "No detections in last 30ms",
382 cv::Point(10, 0), cv::FONT_HERSHEY_PLAIN, 1.0,
383 kOrinColors.at(camera_name));
Maxwell Henderson123c8172024-03-01 22:54:16 -0800384 // Display and clear the image if we haven't draw in a while
385 VLOG(1) << "Displaying image due to time lapse";
386 cv::imshow("View", vis_robot_.image_);
387 cv::waitKey(FLAGS_wait_key);
Maxwell Henderson123c8172024-03-01 22:54:16 -0800388 max_delta_T_world_robot_ = 0.0;
Jim Ostrowski5ff3c562024-03-20 20:35:11 -0700389 drawn_cameras_.clear();
Maxwell Henderson123c8172024-03-01 22:54:16 -0800390 }
391 }
392}
393
394void TargetMapperReplay::HandleNodeCaptures(
395 aos::EventLoop *mapping_event_loop,
396 frc971::constants::ConstantsFetcher<y2024::Constants> *constants_fetcher,
397 int camera_number) {
398 // Get the camera extrinsics
Jim Ostrowski67726282024-03-24 14:39:33 -0700399 std::string node_name =
400 std::string(mapping_event_loop->node()->name()->string_view());
Maxwell Henderson123c8172024-03-01 22:54:16 -0800401 const auto *calibration = FindCameraCalibration(
Jim Ostrowski67726282024-03-24 14:39:33 -0700402 constants_fetcher->constants(), node_name, camera_number);
Maxwell Henderson123c8172024-03-01 22:54:16 -0800403 cv::Mat extrinsics_cv = frc971::vision::CameraExtrinsics(calibration).value();
404 Eigen::Matrix4d extrinsics_matrix;
405 cv::cv2eigen(extrinsics_cv, extrinsics_matrix);
406 const auto extrinsics = Eigen::Affine3d(extrinsics_matrix);
407 std::string camera_name = absl::StrFormat(
408 "/%s/camera%d", mapping_event_loop->node()->name()->str(), camera_number);
409
410 mapping_event_loop->MakeWatcher(
411 camera_name.c_str(), [this, mapping_event_loop, extrinsics,
412 camera_name](const TargetMap &map) {
413 aos::distributed_clock::time_point node_distributed_time =
414 reader_->event_loop_factory()
415 ->GetNodeEventLoopFactory(mapping_event_loop->node())
416 ->ToDistributedClock(aos::monotonic_clock::time_point(
417 aos::monotonic_clock::duration(
418 map.monotonic_timestamp_ns())));
419
420 HandleAprilTags(map, node_distributed_time, camera_name, extrinsics);
421 });
422}
423
424void TargetMapperReplay::MaybeSolve() {
425 if (FLAGS_solve) {
426 auto target_constraints =
427 DataAdapter::MatchTargetDetections(timestamped_target_detections_);
428
Jim Ostrowski5ff3c562024-03-20 20:35:11 -0700429 if (FLAGS_split_field) {
430 // Remove constraints between the two sides of the field - these are
431 // basically garbage because of how far the camera is. We will use seeding
432 // below to connect the two sides
433 target_constraints.erase(
434 std::remove_if(
435 target_constraints.begin(), target_constraints.end(),
436 [](const auto &constraint) {
437 return (
438 kIdAllianceMap[static_cast<uint>(constraint.id_begin)] !=
439 kIdAllianceMap[static_cast<uint>(constraint.id_end)]);
440 }),
441 target_constraints.end());
442 }
Maxwell Henderson123c8172024-03-01 22:54:16 -0800443
444 LOG(INFO) << "Solving for locations of tags with "
445 << target_constraints.size() << " constraints";
446 TargetMapper mapper(FLAGS_json_path, target_constraints);
447 mapper.Solve(FLAGS_field_name, FLAGS_output_dir);
448
449 if (!FLAGS_dump_constraints_to.empty()) {
450 mapper.DumpConstraints(FLAGS_dump_constraints_to);
451 }
452 if (!FLAGS_dump_stats_to.empty()) {
453 mapper.DumpStats(FLAGS_dump_stats_to);
454 }
Jim Ostrowskif41b0942024-03-24 18:05:02 -0700455 mapper.PrintDiffs();
Maxwell Henderson123c8172024-03-01 22:54:16 -0800456 }
457}
458
459void MappingMain(int argc, char *argv[]) {
460 std::vector<DataAdapter::TimestampedDetection> timestamped_target_detections;
461
462 std::optional<aos::FlatbufferDetachedBuffer<aos::Configuration>> config =
463 (FLAGS_config.empty()
464 ? std::nullopt
465 : std::make_optional(aos::configuration::ReadConfig(FLAGS_config)));
466
467 // Open logfiles
468 aos::logger::LogReader reader(
469 aos::logger::SortParts(aos::logger::FindLogs(argc, argv)),
470 config.has_value() ? &config->message() : nullptr);
471
472 TargetMapperReplay mapper_replay(&reader);
473 reader.event_loop_factory()->Run();
474 mapper_replay.MaybeSolve();
475}
476
477} // namespace y2024::vision
478
479int main(int argc, char **argv) {
480 aos::InitGoogle(&argc, &argv);
481 y2024::vision::MappingMain(argc, argv);
482}