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