Jim Ostrowski | aec5dd2 | 2023-02-27 22:17:53 -0800 | [diff] [blame] | 1 | #include "aos/configuration.h" |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 2 | #include "aos/events/logging/log_reader.h" |
| 3 | #include "aos/events/simulated_event_loop.h" |
| 4 | #include "aos/init.h" |
Jim Ostrowski | 68965cd | 2023-03-01 20:32:51 -0800 | [diff] [blame] | 5 | #include "aos/util/mcap_logger.h" |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 6 | #include "frc971/control_loops/pose.h" |
milind-u | 16e3a08 | 2023-01-21 13:53:43 -0800 | [diff] [blame] | 7 | #include "frc971/vision/calibration_generated.h" |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 8 | #include "frc971/vision/charuco_lib.h" |
| 9 | #include "frc971/vision/target_mapper.h" |
Jim Ostrowski | 49be823 | 2023-03-23 01:00:14 -0700 | [diff] [blame] | 10 | #include "frc971/vision/visualize_robot.h" |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 11 | #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-u | c5a494f | 2023-02-24 15:39:22 -0800 | [diff] [blame] | 18 | #include "y2023/constants/simulated_constants_sender.h" |
milind-u | 09fb125 | 2023-01-28 19:21:41 -0800 | [diff] [blame] | 19 | #include "y2023/vision/aprilrobotics.h" |
James Kuszmaul | d67f6d2 | 2023-02-05 17:37:25 -0800 | [diff] [blame] | 20 | #include "y2023/vision/vision_util.h" |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 21 | |
milind-u | c5a494f | 2023-02-24 15:39:22 -0800 | [diff] [blame] | 22 | DEFINE_string(json_path, "y2023/vision/maps/target_map.json", |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 23 | "Specify path for json with initial pose guesses."); |
milind-u | 4a5ef8a | 2023-03-05 14:10:23 -0800 | [diff] [blame] | 24 | DEFINE_string(config, "", |
| 25 | "If set, override the log's config file with this one."); |
milind-u | c5a494f | 2023-02-24 15:39:22 -0800 | [diff] [blame] | 26 | DEFINE_string(constants_path, "y2023/constants/constants.json", |
| 27 | "Path to the constant file"); |
| 28 | DEFINE_string(output_dir, "y2023/vision/maps", |
| 29 | "Directory to write solved target map to"); |
| 30 | DEFINE_string(field_name, "charged_up", |
| 31 | "Field name, for the output json filename and flatbuffer field"); |
Jim Ostrowski | 49be823 | 2023-03-23 01:00:14 -0700 | [diff] [blame] | 32 | DEFINE_int32(team_number, 0, |
| 33 | "Required: Use the calibration for a node with this team number"); |
Jim Ostrowski | 68965cd | 2023-03-01 20:32:51 -0800 | [diff] [blame] | 34 | DEFINE_string(mcap_output_path, "/tmp/log.mcap", "Log to output."); |
| 35 | DEFINE_string(pi, "pi1", "Pi name to generate mcap log for; defaults to pi1."); |
milind-u | 5ddd515 | 2023-03-04 15:19:17 -0800 | [diff] [blame] | 36 | DEFINE_double(max_pose_error, 1e-6, |
| 37 | "Throw out target poses with a higher pose error than this"); |
milind-u | de9045f | 2023-03-25 18:17:12 -0700 | [diff] [blame] | 38 | DEFINE_double( |
| 39 | max_pose_error_ratio, 0.4, |
| 40 | "Throw out target poses with a higher pose error ratio than this"); |
milind-u | a30a4a1 | 2023-03-24 20:49:41 -0700 | [diff] [blame] | 41 | DEFINE_uint64(wait_key, 0, |
| 42 | "Time in ms to wait between images, if no click (0 to wait " |
| 43 | "indefinitely until click)."); |
milind-u | 8cc03da | 2023-03-25 23:00:39 -0700 | [diff] [blame] | 44 | DEFINE_double(pause_on_distance, 1.0, |
| 45 | "Pause if two consecutive implied robot positions differ by more " |
| 46 | "than this many meters"); |
milind-u | de9045f | 2023-03-25 18:17:12 -0700 | [diff] [blame] | 47 | DEFINE_uint64(skip_to, 1, |
| 48 | "Start at combined image of this number (1 is the first image)"); |
milind-u | a30a4a1 | 2023-03-24 20:49:41 -0700 | [diff] [blame] | 49 | DEFINE_bool(solve, true, "Whether to solve for the field's target map."); |
Milind Upadhyay | c6e42ee | 2022-12-27 00:02:11 -0800 | [diff] [blame] | 50 | |
milind-u | 16e3a08 | 2023-01-21 13:53:43 -0800 | [diff] [blame] | 51 | namespace y2023 { |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 52 | namespace vision { |
| 53 | using frc971::vision::DataAdapter; |
milind-u | 16e3a08 | 2023-01-21 13:53:43 -0800 | [diff] [blame] | 54 | using frc971::vision::ImageCallback; |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 55 | using frc971::vision::PoseUtils; |
milind-u | 09fb125 | 2023-01-28 19:21:41 -0800 | [diff] [blame] | 56 | using frc971::vision::TargetMap; |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 57 | using frc971::vision::TargetMapper; |
milind-u | a30a4a1 | 2023-03-24 20:49:41 -0700 | [diff] [blame] | 58 | using frc971::vision::VisualizeRobot; |
milind-u | 16e3a08 | 2023-01-21 13:53:43 -0800 | [diff] [blame] | 59 | namespace calibration = frc971::vision::calibration; |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 60 | |
milind-u | 2ab4db1 | 2023-03-25 21:59:23 -0700 | [diff] [blame] | 61 | // Class to handle reading target poses from a replayed log, |
| 62 | // displaying various debug info, and passing the poses to |
| 63 | // frc971::vision::TargetMapper for field mapping. |
| 64 | class TargetMapperReplay { |
| 65 | public: |
| 66 | TargetMapperReplay(aos::logger::LogReader *reader); |
| 67 | ~TargetMapperReplay(); |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 68 | |
milind-u | 2ab4db1 | 2023-03-25 21:59:23 -0700 | [diff] [blame] | 69 | // Solves for the target poses with the accumulated detections if FLAGS_solve. |
| 70 | void MaybeSolve(); |
| 71 | |
| 72 | private: |
| 73 | static constexpr int kImageWidth = 1280; |
| 74 | // Map from pi node name to color for drawing |
| 75 | static const std::map<std::string, cv::Scalar> kPiColors; |
| 76 | // Contains fixed target poses without solving, for use with visualization |
| 77 | static const TargetMapper kFixedTargetMapper; |
| 78 | |
| 79 | // Change reference frame from camera to robot |
| 80 | static Eigen::Affine3d CameraToRobotDetection(Eigen::Affine3d H_camera_target, |
| 81 | Eigen::Affine3d extrinsics); |
| 82 | |
| 83 | // Adds april tag detections into the detection list, and handles |
| 84 | // visualization |
| 85 | void HandleAprilTags(const TargetMap &map, |
| 86 | aos::distributed_clock::time_point pi_distributed_time, |
| 87 | std::string node_name, Eigen::Affine3d extrinsics); |
| 88 | |
| 89 | // Gets images from the given pi and passes apriltag positions to |
| 90 | // HandleAprilTags() |
| 91 | void HandlePiCaptures(aos::EventLoop *detection_event_loop, |
| 92 | aos::EventLoop *mapping_event_loop); |
| 93 | |
| 94 | aos::logger::LogReader *reader_; |
| 95 | // April tag detections from all pis |
| 96 | std::vector<DataAdapter::TimestampedDetection> timestamped_target_detections_; |
| 97 | std::vector<std::unique_ptr<AprilRoboticsDetector>> detectors_; |
| 98 | |
| 99 | VisualizeRobot vis_robot_; |
| 100 | // Set of node names which are currently drawn on the display |
| 101 | std::set<std::string> drawn_nodes_; |
| 102 | // Number of frames displayed |
| 103 | size_t display_count_; |
| 104 | // Last time we drew onto the display image. |
| 105 | // This is different from when we actually call imshow() to update |
| 106 | // the display window |
| 107 | aos::distributed_clock::time_point last_draw_time_; |
| 108 | |
milind-u | 8cc03da | 2023-03-25 23:00:39 -0700 | [diff] [blame] | 109 | Eigen::Affine3d last_H_world_robot_; |
| 110 | // Maximum distance between consecutive T_world_robot's in one display frame, |
| 111 | // used to determine if we need to pause for the user to see this frame |
| 112 | // clearly |
| 113 | double max_delta_T_world_robot_; |
| 114 | |
milind-u | 2ab4db1 | 2023-03-25 21:59:23 -0700 | [diff] [blame] | 115 | std::vector<std::unique_ptr<aos::EventLoop>> detection_event_loops_; |
| 116 | std::vector<std::unique_ptr<aos::EventLoop>> mapping_event_loops_; |
| 117 | |
| 118 | std::unique_ptr<aos::EventLoop> mcap_event_loop_; |
| 119 | std::unique_ptr<aos::McapLogger> relogger_; |
| 120 | }; |
| 121 | |
| 122 | const auto TargetMapperReplay::kPiColors = std::map<std::string, cv::Scalar>{ |
milind-u | a30a4a1 | 2023-03-24 20:49:41 -0700 | [diff] [blame] | 123 | {"pi1", cv::Scalar(255, 0, 255)}, |
| 124 | {"pi2", cv::Scalar(255, 255, 0)}, |
| 125 | {"pi3", cv::Scalar(0, 255, 255)}, |
| 126 | {"pi4", cv::Scalar(255, 165, 0)}, |
| 127 | }; |
Jim Ostrowski | 49be823 | 2023-03-23 01:00:14 -0700 | [diff] [blame] | 128 | |
milind-u | 2ab4db1 | 2023-03-25 21:59:23 -0700 | [diff] [blame] | 129 | const auto TargetMapperReplay::kFixedTargetMapper = |
| 130 | TargetMapper(FLAGS_json_path, ceres::examples::VectorOfConstraints{}); |
| 131 | |
| 132 | Eigen::Affine3d TargetMapperReplay::CameraToRobotDetection( |
| 133 | Eigen::Affine3d H_camera_target, Eigen::Affine3d extrinsics) { |
| 134 | const Eigen::Affine3d H_robot_camera = extrinsics; |
| 135 | const Eigen::Affine3d H_robot_target = H_robot_camera * H_camera_target; |
| 136 | return H_robot_target; |
| 137 | } |
| 138 | |
| 139 | TargetMapperReplay::TargetMapperReplay(aos::logger::LogReader *reader) |
| 140 | : reader_(reader), |
| 141 | timestamped_target_detections_(), |
| 142 | detectors_(), |
| 143 | vis_robot_(cv::Size(1280, 1000)), |
| 144 | drawn_nodes_(), |
| 145 | display_count_(0), |
milind-u | 8cc03da | 2023-03-25 23:00:39 -0700 | [diff] [blame] | 146 | last_draw_time_(aos::distributed_clock::min_time), |
| 147 | last_H_world_robot_(Eigen::Matrix4d::Identity()), |
| 148 | max_delta_T_world_robot_(0.0) { |
milind-u | 2ab4db1 | 2023-03-25 21:59:23 -0700 | [diff] [blame] | 149 | // Send new april tag poses. This allows us to take a log of images, then |
| 150 | // play with the april detection code and see those changes take effect in |
| 151 | // mapping |
| 152 | constexpr size_t kNumPis = 4; |
| 153 | for (size_t i = 1; i <= kNumPis; i++) { |
| 154 | reader_->RemapLoggedChannel(absl::StrFormat("/pi%u/camera", i), |
| 155 | "frc971.vision.TargetMap"); |
| 156 | reader_->RemapLoggedChannel(absl::StrFormat("/pi%u/camera", i), |
| 157 | "foxglove.ImageAnnotations"); |
| 158 | reader_->RemapLoggedChannel(absl::StrFormat("/pi%u/constants", i), |
| 159 | "y2023.Constants"); |
| 160 | } |
| 161 | |
| 162 | reader_->RemapLoggedChannel("/imu/constants", "y2023.Constants"); |
| 163 | |
| 164 | reader_->Register(); |
| 165 | |
| 166 | SendSimulationConstants(reader_->event_loop_factory(), FLAGS_team_number, |
| 167 | FLAGS_constants_path); |
| 168 | |
| 169 | for (size_t i = 1; i < kNumPis; i++) { |
| 170 | std::string node_name = "pi" + std::to_string(i); |
| 171 | const aos::Node *pi = |
| 172 | aos::configuration::GetNode(reader_->configuration(), node_name); |
| 173 | detection_event_loops_.emplace_back( |
| 174 | reader_->event_loop_factory()->MakeEventLoop(node_name + "_detection", |
| 175 | pi)); |
| 176 | mapping_event_loops_.emplace_back( |
| 177 | reader_->event_loop_factory()->MakeEventLoop(node_name + "_mapping", |
| 178 | pi)); |
| 179 | HandlePiCaptures( |
| 180 | detection_event_loops_[detection_event_loops_.size() - 1].get(), |
| 181 | mapping_event_loops_[mapping_event_loops_.size() - 1].get()); |
| 182 | } |
| 183 | |
| 184 | if (!FLAGS_mcap_output_path.empty()) { |
| 185 | LOG(INFO) << "Writing out mcap file to " << FLAGS_mcap_output_path; |
| 186 | const aos::Node *node = |
| 187 | aos::configuration::GetNode(reader_->configuration(), FLAGS_pi); |
| 188 | reader_->event_loop_factory()->GetNodeEventLoopFactory(node)->OnStartup( |
| 189 | [this, node]() { |
| 190 | mcap_event_loop_ = |
| 191 | reader_->event_loop_factory()->MakeEventLoop("mcap", node); |
| 192 | relogger_ = std::make_unique<aos::McapLogger>( |
| 193 | mcap_event_loop_.get(), FLAGS_mcap_output_path, |
| 194 | aos::McapLogger::Serialization::kFlatbuffer, |
| 195 | aos::McapLogger::CanonicalChannelNames::kShortened, |
| 196 | aos::McapLogger::Compression::kLz4); |
| 197 | }); |
| 198 | } |
| 199 | |
| 200 | if (FLAGS_visualize) { |
| 201 | vis_robot_.ClearImage(); |
| 202 | const double kFocalLength = 500.0; |
| 203 | vis_robot_.SetDefaultViewpoint(kImageWidth, kFocalLength); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | TargetMapperReplay::~TargetMapperReplay() { |
| 208 | // Clean up all the pointers |
| 209 | for (auto &detector_ptr : detectors_) { |
| 210 | detector_ptr.reset(); |
| 211 | } |
| 212 | } |
| 213 | |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 214 | // Add detected apriltag poses relative to the robot to |
| 215 | // timestamped_target_detections |
milind-u | 2ab4db1 | 2023-03-25 21:59:23 -0700 | [diff] [blame] | 216 | void TargetMapperReplay::HandleAprilTags( |
| 217 | const TargetMap &map, |
| 218 | aos::distributed_clock::time_point pi_distributed_time, |
| 219 | std::string node_name, Eigen::Affine3d extrinsics) { |
milind-u | a30a4a1 | 2023-03-24 20:49:41 -0700 | [diff] [blame] | 220 | bool drew = false; |
| 221 | std::stringstream label; |
| 222 | label << node_name << " - "; |
| 223 | |
milind-u | 3f5f83c | 2023-01-29 15:23:51 -0800 | [diff] [blame] | 224 | for (const auto *target_pose_fbs : *map.target_poses()) { |
milind-u | 5ddd515 | 2023-03-04 15:19:17 -0800 | [diff] [blame] | 225 | // Skip detections with high pose errors |
| 226 | if (target_pose_fbs->pose_error() > FLAGS_max_pose_error) { |
milind-u | de9045f | 2023-03-25 18:17:12 -0700 | [diff] [blame] | 227 | VLOG(1) << " Skipping tag " << target_pose_fbs->id() |
| 228 | << " due to pose error of " << target_pose_fbs->pose_error(); |
| 229 | continue; |
| 230 | } |
| 231 | // Skip detections with high pose error ratios |
| 232 | if (target_pose_fbs->pose_error_ratio() > FLAGS_max_pose_error_ratio) { |
| 233 | VLOG(1) << " Skipping tag " << target_pose_fbs->id() |
| 234 | << " due to pose error ratio of " |
| 235 | << target_pose_fbs->pose_error_ratio(); |
milind-u | 5ddd515 | 2023-03-04 15:19:17 -0800 | [diff] [blame] | 236 | continue; |
| 237 | } |
| 238 | |
milind-u | 3f5f83c | 2023-01-29 15:23:51 -0800 | [diff] [blame] | 239 | const TargetMapper::TargetPose target_pose = |
| 240 | PoseUtils::TargetPoseFromFbs(*target_pose_fbs); |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 241 | |
milind-u | ee67abd | 2023-02-23 18:26:55 -0800 | [diff] [blame] | 242 | Eigen::Affine3d H_camera_target = |
milind-u | 3f5f83c | 2023-01-29 15:23:51 -0800 | [diff] [blame] | 243 | Eigen::Translation3d(target_pose.pose.p) * target_pose.pose.q; |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 244 | Eigen::Affine3d H_robot_target = |
milind-u | ee67abd | 2023-02-23 18:26:55 -0800 | [diff] [blame] | 245 | CameraToRobotDetection(H_camera_target, extrinsics); |
Milind Upadhyay | ebf93ee | 2023-01-05 14:12:58 -0800 | [diff] [blame] | 246 | |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 247 | ceres::examples::Pose3d target_pose_camera = |
milind-u | ee67abd | 2023-02-23 18:26:55 -0800 | [diff] [blame] | 248 | PoseUtils::Affine3dToPose3d(H_camera_target); |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 249 | double distance_from_camera = target_pose_camera.p.norm(); |
milind-u | d62f80a | 2023-03-04 16:37:09 -0800 | [diff] [blame] | 250 | double distortion_factor = target_pose_fbs->distortion_factor(); |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 251 | |
milind-u | 09fb125 | 2023-01-28 19:21:41 -0800 | [diff] [blame] | 252 | CHECK(map.has_monotonic_timestamp_ns()) |
| 253 | << "Need detection timestamps for mapping"; |
| 254 | |
milind-u | 2ab4db1 | 2023-03-25 21:59:23 -0700 | [diff] [blame] | 255 | timestamped_target_detections_.emplace_back( |
Milind Upadhyay | ebf93ee | 2023-01-05 14:12:58 -0800 | [diff] [blame] | 256 | DataAdapter::TimestampedDetection{ |
| 257 | .time = pi_distributed_time, |
| 258 | .H_robot_target = H_robot_target, |
| 259 | .distance_from_camera = distance_from_camera, |
milind-u | d62f80a | 2023-03-04 16:37:09 -0800 | [diff] [blame] | 260 | .distortion_factor = distortion_factor, |
milind-u | 3f5f83c | 2023-01-29 15:23:51 -0800 | [diff] [blame] | 261 | .id = static_cast<TargetMapper::TargetId>(target_pose.id)}); |
Jim Ostrowski | 49be823 | 2023-03-23 01:00:14 -0700 | [diff] [blame] | 262 | |
| 263 | if (FLAGS_visualize) { |
milind-u | a30a4a1 | 2023-03-24 20:49:41 -0700 | [diff] [blame] | 264 | // If we've already drawn in the current image, |
| 265 | // display it before clearing and adding the new poses |
milind-u | 2ab4db1 | 2023-03-25 21:59:23 -0700 | [diff] [blame] | 266 | if (drawn_nodes_.count(node_name) != 0) { |
| 267 | display_count_++; |
| 268 | cv::putText(vis_robot_.image_, |
| 269 | "Poses #" + std::to_string(display_count_), |
milind-u | a30a4a1 | 2023-03-24 20:49:41 -0700 | [diff] [blame] | 270 | cv::Point(600, 10), cv::FONT_HERSHEY_PLAIN, 1.0, |
| 271 | cv::Scalar(255, 255, 255)); |
| 272 | |
milind-u | 2ab4db1 | 2023-03-25 21:59:23 -0700 | [diff] [blame] | 273 | if (display_count_ >= FLAGS_skip_to) { |
| 274 | cv::imshow("View", vis_robot_.image_); |
milind-u | 8cc03da | 2023-03-25 23:00:39 -0700 | [diff] [blame] | 275 | cv::waitKey(max_delta_T_world_robot_ > FLAGS_pause_on_distance |
| 276 | ? 0 |
| 277 | : FLAGS_wait_key); |
| 278 | max_delta_T_world_robot_ = 0.0; |
milind-u | de9045f | 2023-03-25 18:17:12 -0700 | [diff] [blame] | 279 | } else { |
milind-u | 2ab4db1 | 2023-03-25 21:59:23 -0700 | [diff] [blame] | 280 | VLOG(1) << "At poses #" << std::to_string(display_count_); |
milind-u | de9045f | 2023-03-25 18:17:12 -0700 | [diff] [blame] | 281 | } |
milind-u | 2ab4db1 | 2023-03-25 21:59:23 -0700 | [diff] [blame] | 282 | vis_robot_.ClearImage(); |
| 283 | drawn_nodes_.clear(); |
milind-u | a30a4a1 | 2023-03-24 20:49:41 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Jim Ostrowski | 49be823 | 2023-03-23 01:00:14 -0700 | [diff] [blame] | 286 | Eigen::Affine3d H_world_target = PoseUtils::Pose3dToAffine3d( |
milind-u | 2ab4db1 | 2023-03-25 21:59:23 -0700 | [diff] [blame] | 287 | kFixedTargetMapper.GetTargetPoseById(target_pose_fbs->id())->pose); |
Jim Ostrowski | 49be823 | 2023-03-23 01:00:14 -0700 | [diff] [blame] | 288 | Eigen::Affine3d H_world_robot = H_world_target * H_robot_target.inverse(); |
milind-u | de9045f | 2023-03-25 18:17:12 -0700 | [diff] [blame] | 289 | VLOG(2) << node_name << ", " << target_pose_fbs->id() |
| 290 | << ", t = " << pi_distributed_time |
| 291 | << ", pose_error = " << target_pose_fbs->pose_error() |
| 292 | << ", pose_error_ratio = " << target_pose_fbs->pose_error_ratio() |
| 293 | << ", robot_pos (x,y,z) + " |
| 294 | << H_world_robot.translation().transpose(); |
milind-u | a30a4a1 | 2023-03-24 20:49:41 -0700 | [diff] [blame] | 295 | |
| 296 | label << "id " << target_pose_fbs->id() << ": err " |
milind-u | de9045f | 2023-03-25 18:17:12 -0700 | [diff] [blame] | 297 | << (target_pose_fbs->pose_error() / FLAGS_max_pose_error) |
| 298 | << " ratio " << target_pose_fbs->pose_error_ratio() << " "; |
milind-u | a30a4a1 | 2023-03-24 20:49:41 -0700 | [diff] [blame] | 299 | |
milind-u | 2ab4db1 | 2023-03-25 21:59:23 -0700 | [diff] [blame] | 300 | vis_robot_.DrawRobotOutline(H_world_robot, |
milind-u | a30a4a1 | 2023-03-24 20:49:41 -0700 | [diff] [blame] | 301 | std::to_string(target_pose_fbs->id()), |
| 302 | kPiColors.at(node_name)); |
| 303 | |
milind-u | 2ab4db1 | 2023-03-25 21:59:23 -0700 | [diff] [blame] | 304 | vis_robot_.DrawFrameAxes(H_world_target, |
milind-u | a30a4a1 | 2023-03-24 20:49:41 -0700 | [diff] [blame] | 305 | std::to_string(target_pose_fbs->id()), |
| 306 | kPiColors.at(node_name)); |
milind-u | 8cc03da | 2023-03-25 23:00:39 -0700 | [diff] [blame] | 307 | |
| 308 | double delta_T_world_robot = |
| 309 | (H_world_robot.translation() - last_H_world_robot_.translation()) |
| 310 | .norm(); |
| 311 | max_delta_T_world_robot_ = |
| 312 | std::max(delta_T_world_robot, max_delta_T_world_robot_); |
| 313 | |
milind-u | a30a4a1 | 2023-03-24 20:49:41 -0700 | [diff] [blame] | 314 | drew = true; |
milind-u | 2ab4db1 | 2023-03-25 21:59:23 -0700 | [diff] [blame] | 315 | last_draw_time_ = pi_distributed_time; |
milind-u | 8cc03da | 2023-03-25 23:00:39 -0700 | [diff] [blame] | 316 | last_H_world_robot_ = H_world_robot; |
Jim Ostrowski | 49be823 | 2023-03-23 01:00:14 -0700 | [diff] [blame] | 317 | } |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 318 | } |
milind-u | a30a4a1 | 2023-03-24 20:49:41 -0700 | [diff] [blame] | 319 | if (drew) { |
| 320 | size_t pi_number = |
| 321 | static_cast<size_t>(node_name[node_name.size() - 1] - '0'); |
milind-u | 2ab4db1 | 2023-03-25 21:59:23 -0700 | [diff] [blame] | 322 | cv::putText(vis_robot_.image_, label.str(), |
milind-u | a30a4a1 | 2023-03-24 20:49:41 -0700 | [diff] [blame] | 323 | cv::Point(10, 10 + 20 * pi_number), cv::FONT_HERSHEY_PLAIN, 1.0, |
| 324 | kPiColors.at(node_name)); |
| 325 | |
milind-u | 2ab4db1 | 2023-03-25 21:59:23 -0700 | [diff] [blame] | 326 | drawn_nodes_.emplace(node_name); |
| 327 | } else if (pi_distributed_time - last_draw_time_ > |
milind-u | 52db336 | 2023-03-25 20:09:22 -0700 | [diff] [blame] | 328 | std::chrono::milliseconds(30) && |
milind-u | 2ab4db1 | 2023-03-25 21:59:23 -0700 | [diff] [blame] | 329 | display_count_ >= FLAGS_skip_to) { |
milind-u | 52db336 | 2023-03-25 20:09:22 -0700 | [diff] [blame] | 330 | // Clear the image if we haven't draw in a while |
milind-u | 2ab4db1 | 2023-03-25 21:59:23 -0700 | [diff] [blame] | 331 | vis_robot_.ClearImage(); |
| 332 | cv::imshow("View", vis_robot_.image_); |
milind-u | 52db336 | 2023-03-25 20:09:22 -0700 | [diff] [blame] | 333 | cv::waitKey(FLAGS_wait_key); |
milind-u | 8cc03da | 2023-03-25 23:00:39 -0700 | [diff] [blame] | 334 | max_delta_T_world_robot_ = 0.0; |
milind-u | a30a4a1 | 2023-03-24 20:49:41 -0700 | [diff] [blame] | 335 | } |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 336 | } |
| 337 | |
milind-u | 2ab4db1 | 2023-03-25 21:59:23 -0700 | [diff] [blame] | 338 | void TargetMapperReplay::HandlePiCaptures(aos::EventLoop *detection_event_loop, |
| 339 | aos::EventLoop *mapping_event_loop) { |
milind-u | f3ab8ba | 2023-02-04 17:56:16 -0800 | [diff] [blame] | 340 | static constexpr std::string_view kImageChannel = "/camera"; |
milind-u | a30a4a1 | 2023-03-24 20:49:41 -0700 | [diff] [blame] | 341 | // For the robots, we need to flip the image since the cameras are rolled by |
| 342 | // 180 degrees |
| 343 | bool flip_image = (FLAGS_team_number != 7971); |
milind-u | f3ab8ba | 2023-02-04 17:56:16 -0800 | [diff] [blame] | 344 | auto detector_ptr = std::make_unique<AprilRoboticsDetector>( |
milind-u | a30a4a1 | 2023-03-24 20:49:41 -0700 | [diff] [blame] | 345 | detection_event_loop, kImageChannel, flip_image); |
milind-u | f2a4e32 | 2023-02-01 19:33:10 -0800 | [diff] [blame] | 346 | // Get the camera extrinsics |
James Kuszmaul | 258e4ee | 2023-02-23 14:22:30 -0800 | [diff] [blame] | 347 | cv::Mat extrinsics_cv = detector_ptr->extrinsics().value(); |
milind-u | f2a4e32 | 2023-02-01 19:33:10 -0800 | [diff] [blame] | 348 | Eigen::Matrix4d extrinsics_matrix; |
| 349 | cv::cv2eigen(extrinsics_cv, extrinsics_matrix); |
| 350 | const auto extrinsics = Eigen::Affine3d(extrinsics_matrix); |
| 351 | |
milind-u | 2ab4db1 | 2023-03-25 21:59:23 -0700 | [diff] [blame] | 352 | detectors_.emplace_back(std::move(detector_ptr)); |
Jim Ostrowski | 49be823 | 2023-03-23 01:00:14 -0700 | [diff] [blame] | 353 | |
milind-u | f3ab8ba | 2023-02-04 17:56:16 -0800 | [diff] [blame] | 354 | mapping_event_loop->MakeWatcher("/camera", [=](const TargetMap &map) { |
milind-u | 09fb125 | 2023-01-28 19:21:41 -0800 | [diff] [blame] | 355 | aos::distributed_clock::time_point pi_distributed_time = |
milind-u | 2ab4db1 | 2023-03-25 21:59:23 -0700 | [diff] [blame] | 356 | reader_->event_loop_factory() |
milind-u | f3ab8ba | 2023-02-04 17:56:16 -0800 | [diff] [blame] | 357 | ->GetNodeEventLoopFactory(mapping_event_loop->node()) |
milind-u | 09fb125 | 2023-01-28 19:21:41 -0800 | [diff] [blame] | 358 | ->ToDistributedClock(aos::monotonic_clock::time_point( |
| 359 | aos::monotonic_clock::duration(map.monotonic_timestamp_ns()))); |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 360 | |
Jim Ostrowski | 49be823 | 2023-03-23 01:00:14 -0700 | [diff] [blame] | 361 | std::string node_name = detection_event_loop->node()->name()->str(); |
milind-u | 2ab4db1 | 2023-03-25 21:59:23 -0700 | [diff] [blame] | 362 | HandleAprilTags(map, pi_distributed_time, node_name, extrinsics); |
milind-u | 09fb125 | 2023-01-28 19:21:41 -0800 | [diff] [blame] | 363 | }); |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 364 | } |
| 365 | |
milind-u | 2ab4db1 | 2023-03-25 21:59:23 -0700 | [diff] [blame] | 366 | void TargetMapperReplay::MaybeSolve() { |
| 367 | if (FLAGS_solve) { |
| 368 | auto target_constraints = |
| 369 | DataAdapter::MatchTargetDetections(timestamped_target_detections_); |
| 370 | |
| 371 | TargetMapper mapper(FLAGS_json_path, target_constraints); |
| 372 | mapper.Solve(FLAGS_field_name, FLAGS_output_dir); |
| 373 | } |
| 374 | } |
| 375 | |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 376 | void MappingMain(int argc, char *argv[]) { |
| 377 | std::vector<std::string> unsorted_logfiles = |
| 378 | aos::logger::FindLogs(argc, argv); |
| 379 | |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 380 | std::vector<DataAdapter::TimestampedDetection> timestamped_target_detections; |
| 381 | |
milind-u | 4a5ef8a | 2023-03-05 14:10:23 -0800 | [diff] [blame] | 382 | std::optional<aos::FlatbufferDetachedBuffer<aos::Configuration>> config = |
| 383 | (FLAGS_config.empty() |
| 384 | ? std::nullopt |
| 385 | : std::make_optional(aos::configuration::ReadConfig(FLAGS_config))); |
Jim Ostrowski | aec5dd2 | 2023-02-27 22:17:53 -0800 | [diff] [blame] | 386 | |
milind-u | 2ab4db1 | 2023-03-25 21:59:23 -0700 | [diff] [blame] | 387 | // Open logfiles |
milind-u | 4a5ef8a | 2023-03-05 14:10:23 -0800 | [diff] [blame] | 388 | aos::logger::LogReader reader( |
| 389 | aos::logger::SortParts(unsorted_logfiles), |
| 390 | config.has_value() ? &config->message() : nullptr); |
milind-u | f3ab8ba | 2023-02-04 17:56:16 -0800 | [diff] [blame] | 391 | |
milind-u | 2ab4db1 | 2023-03-25 21:59:23 -0700 | [diff] [blame] | 392 | TargetMapperReplay mapper_replay(&reader); |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 393 | reader.event_loop_factory()->Run(); |
milind-u | 2ab4db1 | 2023-03-25 21:59:23 -0700 | [diff] [blame] | 394 | mapper_replay.MaybeSolve(); |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | } // namespace vision |
milind-u | 16e3a08 | 2023-01-21 13:53:43 -0800 | [diff] [blame] | 398 | } // namespace y2023 |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 399 | |
| 400 | int main(int argc, char **argv) { |
| 401 | aos::InitGoogle(&argc, &argv); |
milind-u | 16e3a08 | 2023-01-21 13:53:43 -0800 | [diff] [blame] | 402 | y2023::vision::MappingMain(argc, argv); |
Milind Upadhyay | 915d600 | 2022-12-26 20:37:43 -0800 | [diff] [blame] | 403 | } |