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