blob: 4da972a33a42dcb36fb6600cf5d9d77a9b182d8c [file] [log] [blame]
Jim Ostrowskiaec5dd22023-02-27 22:17:53 -08001#include "aos/configuration.h"
Yash Chainanid5c7f0d2022-11-19 17:05:57 -08002#include "aos/events/logging/log_reader.h"
3#include "aos/events/simulated_event_loop.h"
4#include "aos/init.h"
Jim Ostrowski68965cd2023-03-01 20:32:51 -08005#include "aos/util/mcap_logger.h"
Yash Chainanid5c7f0d2022-11-19 17:05:57 -08006#include "frc971/control_loops/pose.h"
milind-u16e3a082023-01-21 13:53:43 -08007#include "frc971/vision/calibration_generated.h"
Yash Chainanid5c7f0d2022-11-19 17:05:57 -08008#include "frc971/vision/charuco_lib.h"
9#include "frc971/vision/target_mapper.h"
Jim Ostrowski49be8232023-03-23 01:00:14 -070010#include "frc971/vision/visualize_robot.h"
Yash Chainanid5c7f0d2022-11-19 17:05:57 -080011#include "opencv2/aruco.hpp"
12#include "opencv2/calib3d.hpp"
13#include "opencv2/core/eigen.hpp"
14#include "opencv2/features2d.hpp"
15#include "opencv2/highgui.hpp"
16#include "opencv2/highgui/highgui.hpp"
17#include "opencv2/imgproc.hpp"
milind-uc5a494f2023-02-24 15:39:22 -080018#include "y2023/constants/simulated_constants_sender.h"
milind-u09fb1252023-01-28 19:21:41 -080019#include "y2023/vision/aprilrobotics.h"
James Kuszmauld67f6d22023-02-05 17:37:25 -080020#include "y2023/vision/vision_util.h"
Yash Chainanid5c7f0d2022-11-19 17:05:57 -080021
milind-uc5a494f2023-02-24 15:39:22 -080022DEFINE_string(json_path, "y2023/vision/maps/target_map.json",
Yash Chainanid5c7f0d2022-11-19 17:05:57 -080023 "Specify path for json with initial pose guesses.");
milind-u4a5ef8a2023-03-05 14:10:23 -080024DEFINE_string(config, "",
25 "If set, override the log's config file with this one.");
milind-uc5a494f2023-02-24 15:39:22 -080026DEFINE_string(constants_path, "y2023/constants/constants.json",
27 "Path to the constant file");
28DEFINE_string(output_dir, "y2023/vision/maps",
29 "Directory to write solved target map to");
30DEFINE_string(field_name, "charged_up",
31 "Field name, for the output json filename and flatbuffer field");
Jim Ostrowski49be8232023-03-23 01:00:14 -070032DEFINE_int32(team_number, 0,
33 "Required: Use the calibration for a node with this team number");
Jim Ostrowski68965cd2023-03-01 20:32:51 -080034DEFINE_string(mcap_output_path, "/tmp/log.mcap", "Log to output.");
35DEFINE_string(pi, "pi1", "Pi name to generate mcap log for; defaults to pi1.");
milind-u5ddd5152023-03-04 15:19:17 -080036DEFINE_double(max_pose_error, 1e-6,
37 "Throw out target poses with a higher pose error than this");
milind-ude9045f2023-03-25 18:17:12 -070038DEFINE_double(
39 max_pose_error_ratio, 0.4,
40 "Throw out target poses with a higher pose error ratio than this");
milind-ua30a4a12023-03-24 20:49:41 -070041DEFINE_uint64(wait_key, 0,
42 "Time in ms to wait between images, if no click (0 to wait "
43 "indefinitely until click).");
milind-ude9045f2023-03-25 18:17:12 -070044DEFINE_uint64(skip_to, 1,
45 "Start at combined image of this number (1 is the first image)");
milind-ua30a4a12023-03-24 20:49:41 -070046DEFINE_bool(solve, true, "Whether to solve for the field's target map.");
Milind Upadhyayc6e42ee2022-12-27 00:02:11 -080047
milind-u16e3a082023-01-21 13:53:43 -080048namespace y2023 {
Yash Chainanid5c7f0d2022-11-19 17:05:57 -080049namespace vision {
50using frc971::vision::DataAdapter;
milind-u16e3a082023-01-21 13:53:43 -080051using frc971::vision::ImageCallback;
Yash Chainanid5c7f0d2022-11-19 17:05:57 -080052using frc971::vision::PoseUtils;
milind-u09fb1252023-01-28 19:21:41 -080053using frc971::vision::TargetMap;
Yash Chainanid5c7f0d2022-11-19 17:05:57 -080054using frc971::vision::TargetMapper;
milind-ua30a4a12023-03-24 20:49:41 -070055using frc971::vision::VisualizeRobot;
milind-u16e3a082023-01-21 13:53:43 -080056namespace calibration = frc971::vision::calibration;
Yash Chainanid5c7f0d2022-11-19 17:05:57 -080057
58// Change reference frame from camera to robot
milind-uee67abd2023-02-23 18:26:55 -080059Eigen::Affine3d CameraToRobotDetection(Eigen::Affine3d H_camera_target,
Milind Upadhyayc5beba12022-12-17 17:41:20 -080060 Eigen::Affine3d extrinsics) {
milind-uee67abd2023-02-23 18:26:55 -080061 const Eigen::Affine3d H_robot_camera = extrinsics;
62 const Eigen::Affine3d H_robot_target = H_robot_camera * H_camera_target;
Yash Chainanid5c7f0d2022-11-19 17:05:57 -080063 return H_robot_target;
64}
65
Jim Ostrowski49be8232023-03-23 01:00:14 -070066const int kImageWidth = 1000;
milind-ua30a4a12023-03-24 20:49:41 -070067// Map from pi node name to color for drawing
68const std::map<std::string, cv::Scalar> kPiColors = {
69 {"pi1", cv::Scalar(255, 0, 255)},
70 {"pi2", cv::Scalar(255, 255, 0)},
71 {"pi3", cv::Scalar(0, 255, 255)},
72 {"pi4", cv::Scalar(255, 165, 0)},
73};
Jim Ostrowski49be8232023-03-23 01:00:14 -070074
Yash Chainanid5c7f0d2022-11-19 17:05:57 -080075// Add detected apriltag poses relative to the robot to
76// timestamped_target_detections
milind-u5ddd5152023-03-04 15:19:17 -080077void HandleAprilTags(const TargetMap &map,
78 aos::distributed_clock::time_point pi_distributed_time,
79 std::vector<DataAdapter::TimestampedDetection>
80 *timestamped_target_detections,
Jim Ostrowski49be8232023-03-23 01:00:14 -070081 Eigen::Affine3d extrinsics, std::string node_name,
milind-ua30a4a12023-03-24 20:49:41 -070082 frc971::vision::TargetMapper target_loc_mapper,
83 std::set<std::string> *drawn_nodes,
milind-u52db3362023-03-25 20:09:22 -070084 VisualizeRobot *vis_robot, size_t *display_count,
85 aos::distributed_clock::time_point *last_draw_time) {
milind-ua30a4a12023-03-24 20:49:41 -070086 bool drew = false;
87 std::stringstream label;
88 label << node_name << " - ";
89
milind-u3f5f83c2023-01-29 15:23:51 -080090 for (const auto *target_pose_fbs : *map.target_poses()) {
milind-u5ddd5152023-03-04 15:19:17 -080091 // Skip detections with high pose errors
92 if (target_pose_fbs->pose_error() > FLAGS_max_pose_error) {
milind-ude9045f2023-03-25 18:17:12 -070093 VLOG(1) << " Skipping tag " << target_pose_fbs->id()
94 << " due to pose error of " << target_pose_fbs->pose_error();
95 continue;
96 }
97 // Skip detections with high pose error ratios
98 if (target_pose_fbs->pose_error_ratio() > FLAGS_max_pose_error_ratio) {
99 VLOG(1) << " Skipping tag " << target_pose_fbs->id()
100 << " due to pose error ratio of "
101 << target_pose_fbs->pose_error_ratio();
milind-u5ddd5152023-03-04 15:19:17 -0800102 continue;
103 }
104
milind-u3f5f83c2023-01-29 15:23:51 -0800105 const TargetMapper::TargetPose target_pose =
106 PoseUtils::TargetPoseFromFbs(*target_pose_fbs);
Yash Chainanid5c7f0d2022-11-19 17:05:57 -0800107
milind-uee67abd2023-02-23 18:26:55 -0800108 Eigen::Affine3d H_camera_target =
milind-u3f5f83c2023-01-29 15:23:51 -0800109 Eigen::Translation3d(target_pose.pose.p) * target_pose.pose.q;
Milind Upadhyayc5beba12022-12-17 17:41:20 -0800110 Eigen::Affine3d H_robot_target =
milind-uee67abd2023-02-23 18:26:55 -0800111 CameraToRobotDetection(H_camera_target, extrinsics);
Milind Upadhyayebf93ee2023-01-05 14:12:58 -0800112
Milind Upadhyayc5beba12022-12-17 17:41:20 -0800113 ceres::examples::Pose3d target_pose_camera =
milind-uee67abd2023-02-23 18:26:55 -0800114 PoseUtils::Affine3dToPose3d(H_camera_target);
Milind Upadhyayc5beba12022-12-17 17:41:20 -0800115 double distance_from_camera = target_pose_camera.p.norm();
milind-ud62f80a2023-03-04 16:37:09 -0800116 double distortion_factor = target_pose_fbs->distortion_factor();
Yash Chainanid5c7f0d2022-11-19 17:05:57 -0800117
milind-u09fb1252023-01-28 19:21:41 -0800118 CHECK(map.has_monotonic_timestamp_ns())
119 << "Need detection timestamps for mapping";
120
Yash Chainanid5c7f0d2022-11-19 17:05:57 -0800121 timestamped_target_detections->emplace_back(
Milind Upadhyayebf93ee2023-01-05 14:12:58 -0800122 DataAdapter::TimestampedDetection{
123 .time = pi_distributed_time,
124 .H_robot_target = H_robot_target,
125 .distance_from_camera = distance_from_camera,
milind-ud62f80a2023-03-04 16:37:09 -0800126 .distortion_factor = distortion_factor,
milind-u3f5f83c2023-01-29 15:23:51 -0800127 .id = static_cast<TargetMapper::TargetId>(target_pose.id)});
Jim Ostrowski49be8232023-03-23 01:00:14 -0700128
129 if (FLAGS_visualize) {
milind-ua30a4a12023-03-24 20:49:41 -0700130 // If we've already drawn in the current image,
131 // display it before clearing and adding the new poses
132 if (drawn_nodes->count(node_name) != 0) {
133 (*display_count)++;
134 cv::putText(vis_robot->image_,
135 "Poses #" + std::to_string(*display_count),
136 cv::Point(600, 10), cv::FONT_HERSHEY_PLAIN, 1.0,
137 cv::Scalar(255, 255, 255));
138
milind-ude9045f2023-03-25 18:17:12 -0700139 if (*display_count >= FLAGS_skip_to) {
140 cv::imshow("View", vis_robot->image_);
141 cv::waitKey(FLAGS_wait_key);
142 } else {
143 VLOG(1) << "At poses #" << std::to_string(*display_count);
144 }
milind-ua30a4a12023-03-24 20:49:41 -0700145 vis_robot->ClearImage();
146 drawn_nodes->clear();
147 }
148
Jim Ostrowski49be8232023-03-23 01:00:14 -0700149 Eigen::Affine3d H_world_target = PoseUtils::Pose3dToAffine3d(
milind-ua30a4a12023-03-24 20:49:41 -0700150 target_loc_mapper.GetTargetPoseById(target_pose_fbs->id())->pose);
Jim Ostrowski49be8232023-03-23 01:00:14 -0700151 Eigen::Affine3d H_world_robot = H_world_target * H_robot_target.inverse();
milind-ude9045f2023-03-25 18:17:12 -0700152 VLOG(2) << node_name << ", " << target_pose_fbs->id()
153 << ", t = " << pi_distributed_time
154 << ", pose_error = " << target_pose_fbs->pose_error()
155 << ", pose_error_ratio = " << target_pose_fbs->pose_error_ratio()
156 << ", robot_pos (x,y,z) + "
157 << H_world_robot.translation().transpose();
milind-ua30a4a12023-03-24 20:49:41 -0700158
159 label << "id " << target_pose_fbs->id() << ": err "
milind-ude9045f2023-03-25 18:17:12 -0700160 << (target_pose_fbs->pose_error() / FLAGS_max_pose_error)
161 << " ratio " << target_pose_fbs->pose_error_ratio() << " ";
milind-ua30a4a12023-03-24 20:49:41 -0700162
163 vis_robot->DrawRobotOutline(H_world_robot,
164 std::to_string(target_pose_fbs->id()),
165 kPiColors.at(node_name));
166
167 vis_robot->DrawFrameAxes(H_world_target,
168 std::to_string(target_pose_fbs->id()),
169 kPiColors.at(node_name));
170 drew = true;
milind-u52db3362023-03-25 20:09:22 -0700171 *last_draw_time = pi_distributed_time;
Jim Ostrowski49be8232023-03-23 01:00:14 -0700172 }
Yash Chainanid5c7f0d2022-11-19 17:05:57 -0800173 }
milind-ua30a4a12023-03-24 20:49:41 -0700174 if (drew) {
175 size_t pi_number =
176 static_cast<size_t>(node_name[node_name.size() - 1] - '0');
177 cv::putText(vis_robot->image_, label.str(),
178 cv::Point(10, 10 + 20 * pi_number), cv::FONT_HERSHEY_PLAIN, 1.0,
179 kPiColors.at(node_name));
180
181 drawn_nodes->emplace(node_name);
milind-u52db3362023-03-25 20:09:22 -0700182 } else if (pi_distributed_time - *last_draw_time >
183 std::chrono::milliseconds(30) &&
184 *display_count >= FLAGS_skip_to) {
185 // Clear the image if we haven't draw in a while
186 vis_robot->ClearImage();
187 cv::imshow("View", vis_robot->image_);
188 cv::waitKey(FLAGS_wait_key);
milind-ua30a4a12023-03-24 20:49:41 -0700189 }
Yash Chainanid5c7f0d2022-11-19 17:05:57 -0800190}
191
milind-u5ddd5152023-03-04 15:19:17 -0800192// Get images from pi and pass apriltag positions to HandleAprilTags()
Yash Chainanid5c7f0d2022-11-19 17:05:57 -0800193void HandlePiCaptures(
milind-uf3ab8ba2023-02-04 17:56:16 -0800194 aos::EventLoop *detection_event_loop, aos::EventLoop *mapping_event_loop,
195 aos::logger::LogReader *reader,
Yash Chainanid5c7f0d2022-11-19 17:05:57 -0800196 std::vector<DataAdapter::TimestampedDetection>
197 *timestamped_target_detections,
milind-ua30a4a12023-03-24 20:49:41 -0700198 std::vector<std::unique_ptr<AprilRoboticsDetector>> *detectors,
199 std::set<std::string> *drawn_nodes, VisualizeRobot *vis_robot,
milind-u52db3362023-03-25 20:09:22 -0700200 size_t *display_count, aos::distributed_clock::time_point *last_draw_time) {
milind-uf3ab8ba2023-02-04 17:56:16 -0800201 static constexpr std::string_view kImageChannel = "/camera";
milind-ua30a4a12023-03-24 20:49:41 -0700202 // For the robots, we need to flip the image since the cameras are rolled by
203 // 180 degrees
204 bool flip_image = (FLAGS_team_number != 7971);
milind-uf3ab8ba2023-02-04 17:56:16 -0800205 auto detector_ptr = std::make_unique<AprilRoboticsDetector>(
milind-ua30a4a12023-03-24 20:49:41 -0700206 detection_event_loop, kImageChannel, flip_image);
milind-uf2a4e322023-02-01 19:33:10 -0800207 // Get the camera extrinsics
James Kuszmaul258e4ee2023-02-23 14:22:30 -0800208 cv::Mat extrinsics_cv = detector_ptr->extrinsics().value();
milind-uf2a4e322023-02-01 19:33:10 -0800209 Eigen::Matrix4d extrinsics_matrix;
210 cv::cv2eigen(extrinsics_cv, extrinsics_matrix);
211 const auto extrinsics = Eigen::Affine3d(extrinsics_matrix);
212
213 detectors->emplace_back(std::move(detector_ptr));
Yash Chainanid5c7f0d2022-11-19 17:05:57 -0800214
Jim Ostrowski49be8232023-03-23 01:00:14 -0700215 ceres::examples::VectorOfConstraints target_constraints;
216 frc971::vision::TargetMapper target_loc_mapper(FLAGS_json_path,
217 target_constraints);
218
milind-uf3ab8ba2023-02-04 17:56:16 -0800219 mapping_event_loop->MakeWatcher("/camera", [=](const TargetMap &map) {
milind-u09fb1252023-01-28 19:21:41 -0800220 aos::distributed_clock::time_point pi_distributed_time =
221 reader->event_loop_factory()
milind-uf3ab8ba2023-02-04 17:56:16 -0800222 ->GetNodeEventLoopFactory(mapping_event_loop->node())
milind-u09fb1252023-01-28 19:21:41 -0800223 ->ToDistributedClock(aos::monotonic_clock::time_point(
224 aos::monotonic_clock::duration(map.monotonic_timestamp_ns())));
Yash Chainanid5c7f0d2022-11-19 17:05:57 -0800225
Jim Ostrowski49be8232023-03-23 01:00:14 -0700226 std::string node_name = detection_event_loop->node()->name()->str();
milind-u5ddd5152023-03-04 15:19:17 -0800227 HandleAprilTags(map, pi_distributed_time, timestamped_target_detections,
milind-ua30a4a12023-03-24 20:49:41 -0700228 extrinsics, node_name, target_loc_mapper, drawn_nodes,
milind-u52db3362023-03-25 20:09:22 -0700229 vis_robot, display_count, last_draw_time);
milind-u09fb1252023-01-28 19:21:41 -0800230 });
Yash Chainanid5c7f0d2022-11-19 17:05:57 -0800231}
232
233void MappingMain(int argc, char *argv[]) {
234 std::vector<std::string> unsorted_logfiles =
235 aos::logger::FindLogs(argc, argv);
236
Yash Chainanid5c7f0d2022-11-19 17:05:57 -0800237 std::vector<DataAdapter::TimestampedDetection> timestamped_target_detections;
238
milind-u4a5ef8a2023-03-05 14:10:23 -0800239 std::optional<aos::FlatbufferDetachedBuffer<aos::Configuration>> config =
240 (FLAGS_config.empty()
241 ? std::nullopt
242 : std::make_optional(aos::configuration::ReadConfig(FLAGS_config)));
Jim Ostrowskiaec5dd22023-02-27 22:17:53 -0800243
Yash Chainanid5c7f0d2022-11-19 17:05:57 -0800244 // open logfiles
milind-u4a5ef8a2023-03-05 14:10:23 -0800245 aos::logger::LogReader reader(
246 aos::logger::SortParts(unsorted_logfiles),
247 config.has_value() ? &config->message() : nullptr);
milind-ua30a4a12023-03-24 20:49:41 -0700248 // Send new april tag poses. This allows us to take a log of images, then
249 // play with the april detection code and see those changes take effect in
250 // mapping
milind-uf3ab8ba2023-02-04 17:56:16 -0800251 constexpr size_t kNumPis = 4;
252 for (size_t i = 1; i <= kNumPis; i++) {
253 reader.RemapLoggedChannel(absl::StrFormat("/pi%u/camera", i),
254 "frc971.vision.TargetMap");
milind-uc5a494f2023-02-24 15:39:22 -0800255 reader.RemapLoggedChannel(absl::StrFormat("/pi%u/camera", i),
256 "foxglove.ImageAnnotations");
257 reader.RemapLoggedChannel(absl::StrFormat("/pi%u/constants", i),
258 "y2023.Constants");
milind-uf3ab8ba2023-02-04 17:56:16 -0800259 }
260
milind-uc5a494f2023-02-24 15:39:22 -0800261 reader.RemapLoggedChannel("/imu/constants", "y2023.Constants");
262
Yash Chainanid5c7f0d2022-11-19 17:05:57 -0800263 reader.Register();
Yash Chainanid5c7f0d2022-11-19 17:05:57 -0800264
milind-uc5a494f2023-02-24 15:39:22 -0800265 SendSimulationConstants(reader.event_loop_factory(), FLAGS_team_number,
266 FLAGS_constants_path);
267
milind-u09fb1252023-01-28 19:21:41 -0800268 std::vector<std::unique_ptr<AprilRoboticsDetector>> detectors;
milind-ua30a4a12023-03-24 20:49:41 -0700269 VisualizeRobot vis_robot;
270 std::set<std::string> drawn_nodes;
271 size_t display_count = 0;
milind-u52db3362023-03-25 20:09:22 -0700272 aos::distributed_clock::time_point last_draw_time =
273 aos::distributed_clock::min_time;
Yash Chainanid5c7f0d2022-11-19 17:05:57 -0800274
275 const aos::Node *pi1 =
276 aos::configuration::GetNode(reader.configuration(), "pi1");
milind-uf3ab8ba2023-02-04 17:56:16 -0800277 std::unique_ptr<aos::EventLoop> pi1_detection_event_loop =
278 reader.event_loop_factory()->MakeEventLoop("pi1_detection", pi1);
279 std::unique_ptr<aos::EventLoop> pi1_mapping_event_loop =
280 reader.event_loop_factory()->MakeEventLoop("pi1_mapping", pi1);
281 HandlePiCaptures(pi1_detection_event_loop.get(), pi1_mapping_event_loop.get(),
milind-ua30a4a12023-03-24 20:49:41 -0700282 &reader, &timestamped_target_detections, &detectors,
milind-u52db3362023-03-25 20:09:22 -0700283 &drawn_nodes, &vis_robot, &display_count, &last_draw_time);
Yash Chainanid5c7f0d2022-11-19 17:05:57 -0800284
285 const aos::Node *pi2 =
286 aos::configuration::GetNode(reader.configuration(), "pi2");
milind-uf3ab8ba2023-02-04 17:56:16 -0800287 std::unique_ptr<aos::EventLoop> pi2_detection_event_loop =
288 reader.event_loop_factory()->MakeEventLoop("pi2_detection", pi2);
289 std::unique_ptr<aos::EventLoop> pi2_mapping_event_loop =
290 reader.event_loop_factory()->MakeEventLoop("pi2_mapping", pi2);
291 HandlePiCaptures(pi2_detection_event_loop.get(), pi2_mapping_event_loop.get(),
milind-ua30a4a12023-03-24 20:49:41 -0700292 &reader, &timestamped_target_detections, &detectors,
milind-u52db3362023-03-25 20:09:22 -0700293 &drawn_nodes, &vis_robot, &display_count, &last_draw_time);
Yash Chainanid5c7f0d2022-11-19 17:05:57 -0800294
295 const aos::Node *pi3 =
296 aos::configuration::GetNode(reader.configuration(), "pi3");
milind-uf3ab8ba2023-02-04 17:56:16 -0800297 std::unique_ptr<aos::EventLoop> pi3_detection_event_loop =
298 reader.event_loop_factory()->MakeEventLoop("pi3_detection", pi3);
299 std::unique_ptr<aos::EventLoop> pi3_mapping_event_loop =
300 reader.event_loop_factory()->MakeEventLoop("pi3_mapping", pi3);
301 HandlePiCaptures(pi3_detection_event_loop.get(), pi3_mapping_event_loop.get(),
milind-ua30a4a12023-03-24 20:49:41 -0700302 &reader, &timestamped_target_detections, &detectors,
milind-u52db3362023-03-25 20:09:22 -0700303 &drawn_nodes, &vis_robot, &display_count, &last_draw_time);
Yash Chainanid5c7f0d2022-11-19 17:05:57 -0800304
305 const aos::Node *pi4 =
306 aos::configuration::GetNode(reader.configuration(), "pi4");
milind-uf3ab8ba2023-02-04 17:56:16 -0800307 std::unique_ptr<aos::EventLoop> pi4_detection_event_loop =
308 reader.event_loop_factory()->MakeEventLoop("pi4_detection", pi4);
309 std::unique_ptr<aos::EventLoop> pi4_mapping_event_loop =
310 reader.event_loop_factory()->MakeEventLoop("pi4_mapping", pi4);
311 HandlePiCaptures(pi4_detection_event_loop.get(), pi4_mapping_event_loop.get(),
milind-ua30a4a12023-03-24 20:49:41 -0700312 &reader, &timestamped_target_detections, &detectors,
milind-u52db3362023-03-25 20:09:22 -0700313 &drawn_nodes, &vis_robot, &display_count, &last_draw_time);
Yash Chainanid5c7f0d2022-11-19 17:05:57 -0800314
Jim Ostrowski68965cd2023-03-01 20:32:51 -0800315 std::unique_ptr<aos::EventLoop> mcap_event_loop;
316 std::unique_ptr<aos::McapLogger> relogger;
317 if (!FLAGS_mcap_output_path.empty()) {
318 LOG(INFO) << "Writing out mcap file to " << FLAGS_mcap_output_path;
Jim Ostrowski68965cd2023-03-01 20:32:51 -0800319 const aos::Node *node =
320 aos::configuration::GetNode(reader.configuration(), FLAGS_pi);
321 reader.event_loop_factory()->GetNodeEventLoopFactory(node)->OnStartup(
322 [&relogger, &mcap_event_loop, &reader, node]() {
323 mcap_event_loop =
324 reader.event_loop_factory()->MakeEventLoop("mcap", node);
325 relogger = std::make_unique<aos::McapLogger>(
326 mcap_event_loop.get(), FLAGS_mcap_output_path,
327 aos::McapLogger::Serialization::kFlatbuffer,
328 aos::McapLogger::CanonicalChannelNames::kShortened,
329 aos::McapLogger::Compression::kLz4);
330 });
331 }
332
Jim Ostrowski49be8232023-03-23 01:00:14 -0700333 if (FLAGS_visualize) {
milind-ua30a4a12023-03-24 20:49:41 -0700334 vis_robot.ClearImage();
Jim Ostrowski49be8232023-03-23 01:00:14 -0700335 const double kFocalLength = 500.0;
milind-ua30a4a12023-03-24 20:49:41 -0700336 vis_robot.SetDefaultViewpoint(kImageWidth, kFocalLength);
Jim Ostrowski49be8232023-03-23 01:00:14 -0700337 }
338
Yash Chainanid5c7f0d2022-11-19 17:05:57 -0800339 reader.event_loop_factory()->Run();
340
milind-ua30a4a12023-03-24 20:49:41 -0700341 if (FLAGS_solve) {
342 auto target_constraints =
343 DataAdapter::MatchTargetDetections(timestamped_target_detections);
Yash Chainanid5c7f0d2022-11-19 17:05:57 -0800344
milind-ua30a4a12023-03-24 20:49:41 -0700345 frc971::vision::TargetMapper mapper(FLAGS_json_path, target_constraints);
346 mapper.Solve(FLAGS_field_name, FLAGS_output_dir);
347 }
Yash Chainanid5c7f0d2022-11-19 17:05:57 -0800348
milind-u09fb1252023-01-28 19:21:41 -0800349 // Clean up all the pointers
350 for (auto &detector_ptr : detectors) {
351 detector_ptr.reset();
Yash Chainanid5c7f0d2022-11-19 17:05:57 -0800352 }
353}
354
355} // namespace vision
milind-u16e3a082023-01-21 13:53:43 -0800356} // namespace y2023
Yash Chainanid5c7f0d2022-11-19 17:05:57 -0800357
358int main(int argc, char **argv) {
359 aos::InitGoogle(&argc, &argv);
milind-u16e3a082023-01-21 13:53:43 -0800360 y2023::vision::MappingMain(argc, argv);
Milind Upadhyay915d6002022-12-26 20:37:43 -0800361}