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" |
| 10 | #include "opencv2/aruco.hpp" |
| 11 | #include "opencv2/calib3d.hpp" |
| 12 | #include "opencv2/core/eigen.hpp" |
| 13 | #include "opencv2/features2d.hpp" |
| 14 | #include "opencv2/highgui.hpp" |
| 15 | #include "opencv2/highgui/highgui.hpp" |
| 16 | #include "opencv2/imgproc.hpp" |
milind-u | c5a494f | 2023-02-24 15:39:22 -0800 | [diff] [blame] | 17 | #include "y2023/constants/simulated_constants_sender.h" |
milind-u | 09fb125 | 2023-01-28 19:21:41 -0800 | [diff] [blame] | 18 | #include "y2023/vision/aprilrobotics.h" |
James Kuszmaul | d67f6d2 | 2023-02-05 17:37:25 -0800 | [diff] [blame] | 19 | #include "y2023/vision/vision_util.h" |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 20 | |
milind-u | c5a494f | 2023-02-24 15:39:22 -0800 | [diff] [blame] | 21 | DEFINE_string(json_path, "y2023/vision/maps/target_map.json", |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 22 | "Specify path for json with initial pose guesses."); |
Jim Ostrowski | aec5dd2 | 2023-02-27 22:17:53 -0800 | [diff] [blame] | 23 | DEFINE_string(config, "y2023/aos_config.json", |
| 24 | "Path to the config file to use."); |
milind-u | c5a494f | 2023-02-24 15:39:22 -0800 | [diff] [blame] | 25 | DEFINE_string(constants_path, "y2023/constants/constants.json", |
| 26 | "Path to the constant file"); |
| 27 | DEFINE_string(output_dir, "y2023/vision/maps", |
| 28 | "Directory to write solved target map to"); |
| 29 | DEFINE_string(field_name, "charged_up", |
| 30 | "Field name, for the output json filename and flatbuffer field"); |
| 31 | DEFINE_int32(team_number, 7971, |
| 32 | "Use the calibration for a node with this team number"); |
Jim Ostrowski | 68965cd | 2023-03-01 20:32:51 -0800 | [diff] [blame] | 33 | DEFINE_string(mcap_output_path, "/tmp/log.mcap", "Log to output."); |
| 34 | 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^] | 35 | DEFINE_double(max_pose_error, 1e-6, |
| 36 | "Throw out target poses with a higher pose error than this"); |
Milind Upadhyay | c6e42ee | 2022-12-27 00:02:11 -0800 | [diff] [blame] | 37 | |
milind-u | 16e3a08 | 2023-01-21 13:53:43 -0800 | [diff] [blame] | 38 | namespace y2023 { |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 39 | namespace vision { |
| 40 | using frc971::vision::DataAdapter; |
milind-u | 16e3a08 | 2023-01-21 13:53:43 -0800 | [diff] [blame] | 41 | using frc971::vision::ImageCallback; |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 42 | using frc971::vision::PoseUtils; |
milind-u | 09fb125 | 2023-01-28 19:21:41 -0800 | [diff] [blame] | 43 | using frc971::vision::TargetMap; |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 44 | using frc971::vision::TargetMapper; |
milind-u | 16e3a08 | 2023-01-21 13:53:43 -0800 | [diff] [blame] | 45 | namespace calibration = frc971::vision::calibration; |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 46 | |
| 47 | // Change reference frame from camera to robot |
milind-u | ee67abd | 2023-02-23 18:26:55 -0800 | [diff] [blame] | 48 | Eigen::Affine3d CameraToRobotDetection(Eigen::Affine3d H_camera_target, |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 49 | Eigen::Affine3d extrinsics) { |
milind-u | ee67abd | 2023-02-23 18:26:55 -0800 | [diff] [blame] | 50 | const Eigen::Affine3d H_robot_camera = extrinsics; |
| 51 | const Eigen::Affine3d H_robot_target = H_robot_camera * H_camera_target; |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 52 | return H_robot_target; |
| 53 | } |
| 54 | |
| 55 | // Add detected apriltag poses relative to the robot to |
| 56 | // timestamped_target_detections |
milind-u | 5ddd515 | 2023-03-04 15:19:17 -0800 | [diff] [blame^] | 57 | void HandleAprilTags(const TargetMap &map, |
| 58 | aos::distributed_clock::time_point pi_distributed_time, |
| 59 | std::vector<DataAdapter::TimestampedDetection> |
| 60 | *timestamped_target_detections, |
| 61 | Eigen::Affine3d extrinsics) { |
milind-u | 3f5f83c | 2023-01-29 15:23:51 -0800 | [diff] [blame] | 62 | for (const auto *target_pose_fbs : *map.target_poses()) { |
milind-u | 5ddd515 | 2023-03-04 15:19:17 -0800 | [diff] [blame^] | 63 | // Skip detections with high pose errors |
| 64 | if (target_pose_fbs->pose_error() > FLAGS_max_pose_error) { |
| 65 | continue; |
| 66 | } |
| 67 | |
milind-u | 3f5f83c | 2023-01-29 15:23:51 -0800 | [diff] [blame] | 68 | const TargetMapper::TargetPose target_pose = |
| 69 | PoseUtils::TargetPoseFromFbs(*target_pose_fbs); |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 70 | |
milind-u | ee67abd | 2023-02-23 18:26:55 -0800 | [diff] [blame] | 71 | Eigen::Affine3d H_camera_target = |
milind-u | 3f5f83c | 2023-01-29 15:23:51 -0800 | [diff] [blame] | 72 | Eigen::Translation3d(target_pose.pose.p) * target_pose.pose.q; |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 73 | Eigen::Affine3d H_robot_target = |
milind-u | ee67abd | 2023-02-23 18:26:55 -0800 | [diff] [blame] | 74 | CameraToRobotDetection(H_camera_target, extrinsics); |
Milind Upadhyay | ebf93ee | 2023-01-05 14:12:58 -0800 | [diff] [blame] | 75 | |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 76 | ceres::examples::Pose3d target_pose_camera = |
milind-u | ee67abd | 2023-02-23 18:26:55 -0800 | [diff] [blame] | 77 | PoseUtils::Affine3dToPose3d(H_camera_target); |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 78 | double distance_from_camera = target_pose_camera.p.norm(); |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 79 | |
milind-u | 09fb125 | 2023-01-28 19:21:41 -0800 | [diff] [blame] | 80 | CHECK(map.has_monotonic_timestamp_ns()) |
| 81 | << "Need detection timestamps for mapping"; |
| 82 | |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 83 | timestamped_target_detections->emplace_back( |
Milind Upadhyay | ebf93ee | 2023-01-05 14:12:58 -0800 | [diff] [blame] | 84 | DataAdapter::TimestampedDetection{ |
| 85 | .time = pi_distributed_time, |
| 86 | .H_robot_target = H_robot_target, |
| 87 | .distance_from_camera = distance_from_camera, |
milind-u | 3f5f83c | 2023-01-29 15:23:51 -0800 | [diff] [blame] | 88 | .id = static_cast<TargetMapper::TargetId>(target_pose.id)}); |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
milind-u | 5ddd515 | 2023-03-04 15:19:17 -0800 | [diff] [blame^] | 92 | // Get images from pi and pass apriltag positions to HandleAprilTags() |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 93 | void HandlePiCaptures( |
milind-u | f3ab8ba | 2023-02-04 17:56:16 -0800 | [diff] [blame] | 94 | aos::EventLoop *detection_event_loop, aos::EventLoop *mapping_event_loop, |
| 95 | aos::logger::LogReader *reader, |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 96 | std::vector<DataAdapter::TimestampedDetection> |
| 97 | *timestamped_target_detections, |
milind-u | 09fb125 | 2023-01-28 19:21:41 -0800 | [diff] [blame] | 98 | std::vector<std::unique_ptr<AprilRoboticsDetector>> *detectors) { |
milind-u | f3ab8ba | 2023-02-04 17:56:16 -0800 | [diff] [blame] | 99 | static constexpr std::string_view kImageChannel = "/camera"; |
| 100 | auto detector_ptr = std::make_unique<AprilRoboticsDetector>( |
| 101 | detection_event_loop, kImageChannel); |
milind-u | f2a4e32 | 2023-02-01 19:33:10 -0800 | [diff] [blame] | 102 | // Get the camera extrinsics |
James Kuszmaul | 258e4ee | 2023-02-23 14:22:30 -0800 | [diff] [blame] | 103 | cv::Mat extrinsics_cv = detector_ptr->extrinsics().value(); |
milind-u | f2a4e32 | 2023-02-01 19:33:10 -0800 | [diff] [blame] | 104 | Eigen::Matrix4d extrinsics_matrix; |
| 105 | cv::cv2eigen(extrinsics_cv, extrinsics_matrix); |
| 106 | const auto extrinsics = Eigen::Affine3d(extrinsics_matrix); |
| 107 | |
| 108 | detectors->emplace_back(std::move(detector_ptr)); |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 109 | |
milind-u | f3ab8ba | 2023-02-04 17:56:16 -0800 | [diff] [blame] | 110 | mapping_event_loop->MakeWatcher("/camera", [=](const TargetMap &map) { |
milind-u | 09fb125 | 2023-01-28 19:21:41 -0800 | [diff] [blame] | 111 | aos::distributed_clock::time_point pi_distributed_time = |
| 112 | reader->event_loop_factory() |
milind-u | f3ab8ba | 2023-02-04 17:56:16 -0800 | [diff] [blame] | 113 | ->GetNodeEventLoopFactory(mapping_event_loop->node()) |
milind-u | 09fb125 | 2023-01-28 19:21:41 -0800 | [diff] [blame] | 114 | ->ToDistributedClock(aos::monotonic_clock::time_point( |
| 115 | aos::monotonic_clock::duration(map.monotonic_timestamp_ns()))); |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 116 | |
milind-u | 5ddd515 | 2023-03-04 15:19:17 -0800 | [diff] [blame^] | 117 | HandleAprilTags(map, pi_distributed_time, timestamped_target_detections, |
| 118 | extrinsics); |
milind-u | 09fb125 | 2023-01-28 19:21:41 -0800 | [diff] [blame] | 119 | }); |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void MappingMain(int argc, char *argv[]) { |
| 123 | std::vector<std::string> unsorted_logfiles = |
| 124 | aos::logger::FindLogs(argc, argv); |
| 125 | |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 126 | std::vector<DataAdapter::TimestampedDetection> timestamped_target_detections; |
| 127 | |
Jim Ostrowski | aec5dd2 | 2023-02-27 22:17:53 -0800 | [diff] [blame] | 128 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 129 | aos::configuration::ReadConfig(FLAGS_config); |
| 130 | |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 131 | // open logfiles |
Jim Ostrowski | aec5dd2 | 2023-02-27 22:17:53 -0800 | [diff] [blame] | 132 | aos::logger::LogReader reader(aos::logger::SortParts(unsorted_logfiles), |
| 133 | &config.message()); |
milind-u | f3ab8ba | 2023-02-04 17:56:16 -0800 | [diff] [blame] | 134 | // Send new april tag poses. This allows us to take a log of images, then play |
| 135 | // with the april detection code and see those changes take effect in mapping |
| 136 | constexpr size_t kNumPis = 4; |
| 137 | for (size_t i = 1; i <= kNumPis; i++) { |
| 138 | reader.RemapLoggedChannel(absl::StrFormat("/pi%u/camera", i), |
| 139 | "frc971.vision.TargetMap"); |
milind-u | c5a494f | 2023-02-24 15:39:22 -0800 | [diff] [blame] | 140 | reader.RemapLoggedChannel(absl::StrFormat("/pi%u/camera", i), |
| 141 | "foxglove.ImageAnnotations"); |
| 142 | reader.RemapLoggedChannel(absl::StrFormat("/pi%u/constants", i), |
| 143 | "y2023.Constants"); |
milind-u | f3ab8ba | 2023-02-04 17:56:16 -0800 | [diff] [blame] | 144 | } |
| 145 | |
milind-u | c5a494f | 2023-02-24 15:39:22 -0800 | [diff] [blame] | 146 | reader.RemapLoggedChannel("/imu/constants", "y2023.Constants"); |
| 147 | |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 148 | reader.Register(); |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 149 | |
milind-u | c5a494f | 2023-02-24 15:39:22 -0800 | [diff] [blame] | 150 | SendSimulationConstants(reader.event_loop_factory(), FLAGS_team_number, |
| 151 | FLAGS_constants_path); |
| 152 | |
milind-u | 09fb125 | 2023-01-28 19:21:41 -0800 | [diff] [blame] | 153 | std::vector<std::unique_ptr<AprilRoboticsDetector>> detectors; |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 154 | |
| 155 | const aos::Node *pi1 = |
| 156 | aos::configuration::GetNode(reader.configuration(), "pi1"); |
milind-u | f3ab8ba | 2023-02-04 17:56:16 -0800 | [diff] [blame] | 157 | std::unique_ptr<aos::EventLoop> pi1_detection_event_loop = |
| 158 | reader.event_loop_factory()->MakeEventLoop("pi1_detection", pi1); |
| 159 | std::unique_ptr<aos::EventLoop> pi1_mapping_event_loop = |
| 160 | reader.event_loop_factory()->MakeEventLoop("pi1_mapping", pi1); |
| 161 | HandlePiCaptures(pi1_detection_event_loop.get(), pi1_mapping_event_loop.get(), |
| 162 | &reader, ×tamped_target_detections, &detectors); |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 163 | |
| 164 | const aos::Node *pi2 = |
| 165 | aos::configuration::GetNode(reader.configuration(), "pi2"); |
milind-u | f3ab8ba | 2023-02-04 17:56:16 -0800 | [diff] [blame] | 166 | std::unique_ptr<aos::EventLoop> pi2_detection_event_loop = |
| 167 | reader.event_loop_factory()->MakeEventLoop("pi2_detection", pi2); |
| 168 | std::unique_ptr<aos::EventLoop> pi2_mapping_event_loop = |
| 169 | reader.event_loop_factory()->MakeEventLoop("pi2_mapping", pi2); |
| 170 | HandlePiCaptures(pi2_detection_event_loop.get(), pi2_mapping_event_loop.get(), |
| 171 | &reader, ×tamped_target_detections, &detectors); |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 172 | |
| 173 | const aos::Node *pi3 = |
| 174 | aos::configuration::GetNode(reader.configuration(), "pi3"); |
milind-u | f3ab8ba | 2023-02-04 17:56:16 -0800 | [diff] [blame] | 175 | std::unique_ptr<aos::EventLoop> pi3_detection_event_loop = |
| 176 | reader.event_loop_factory()->MakeEventLoop("pi3_detection", pi3); |
| 177 | std::unique_ptr<aos::EventLoop> pi3_mapping_event_loop = |
| 178 | reader.event_loop_factory()->MakeEventLoop("pi3_mapping", pi3); |
| 179 | HandlePiCaptures(pi3_detection_event_loop.get(), pi3_mapping_event_loop.get(), |
| 180 | &reader, ×tamped_target_detections, &detectors); |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 181 | |
| 182 | const aos::Node *pi4 = |
| 183 | aos::configuration::GetNode(reader.configuration(), "pi4"); |
milind-u | f3ab8ba | 2023-02-04 17:56:16 -0800 | [diff] [blame] | 184 | std::unique_ptr<aos::EventLoop> pi4_detection_event_loop = |
| 185 | reader.event_loop_factory()->MakeEventLoop("pi4_detection", pi4); |
| 186 | std::unique_ptr<aos::EventLoop> pi4_mapping_event_loop = |
| 187 | reader.event_loop_factory()->MakeEventLoop("pi4_mapping", pi4); |
| 188 | HandlePiCaptures(pi4_detection_event_loop.get(), pi4_mapping_event_loop.get(), |
| 189 | &reader, ×tamped_target_detections, &detectors); |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 190 | |
Jim Ostrowski | 68965cd | 2023-03-01 20:32:51 -0800 | [diff] [blame] | 191 | std::unique_ptr<aos::EventLoop> mcap_event_loop; |
| 192 | std::unique_ptr<aos::McapLogger> relogger; |
| 193 | if (!FLAGS_mcap_output_path.empty()) { |
| 194 | LOG(INFO) << "Writing out mcap file to " << FLAGS_mcap_output_path; |
| 195 | // TODO: Should make this work for any pi |
| 196 | const aos::Node *node = |
| 197 | aos::configuration::GetNode(reader.configuration(), FLAGS_pi); |
| 198 | reader.event_loop_factory()->GetNodeEventLoopFactory(node)->OnStartup( |
| 199 | [&relogger, &mcap_event_loop, &reader, node]() { |
| 200 | mcap_event_loop = |
| 201 | reader.event_loop_factory()->MakeEventLoop("mcap", node); |
| 202 | relogger = std::make_unique<aos::McapLogger>( |
| 203 | mcap_event_loop.get(), FLAGS_mcap_output_path, |
| 204 | aos::McapLogger::Serialization::kFlatbuffer, |
| 205 | aos::McapLogger::CanonicalChannelNames::kShortened, |
| 206 | aos::McapLogger::Compression::kLz4); |
| 207 | }); |
| 208 | } |
| 209 | |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 210 | reader.event_loop_factory()->Run(); |
| 211 | |
| 212 | auto target_constraints = |
Milind Upadhyay | c5beba1 | 2022-12-17 17:41:20 -0800 | [diff] [blame] | 213 | DataAdapter::MatchTargetDetections(timestamped_target_detections); |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 214 | |
| 215 | frc971::vision::TargetMapper mapper(FLAGS_json_path, target_constraints); |
milind-u | c5a494f | 2023-02-24 15:39:22 -0800 | [diff] [blame] | 216 | mapper.Solve(FLAGS_field_name, FLAGS_output_dir); |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 217 | |
milind-u | 09fb125 | 2023-01-28 19:21:41 -0800 | [diff] [blame] | 218 | // Clean up all the pointers |
| 219 | for (auto &detector_ptr : detectors) { |
| 220 | detector_ptr.reset(); |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
| 224 | } // namespace vision |
milind-u | 16e3a08 | 2023-01-21 13:53:43 -0800 | [diff] [blame] | 225 | } // namespace y2023 |
Yash Chainani | d5c7f0d | 2022-11-19 17:05:57 -0800 | [diff] [blame] | 226 | |
| 227 | int main(int argc, char **argv) { |
| 228 | aos::InitGoogle(&argc, &argv); |
milind-u | 16e3a08 | 2023-01-21 13:53:43 -0800 | [diff] [blame] | 229 | y2023::vision::MappingMain(argc, argv); |
Milind Upadhyay | 915d600 | 2022-12-26 20:37:43 -0800 | [diff] [blame] | 230 | } |