Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 1 | |
| 2 | #include <string> |
| 3 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 4 | #include "Eigen/Dense" |
| 5 | #include "opencv2/core/eigen.hpp" |
| 6 | #include "opencv2/imgproc.hpp" |
| 7 | #include "third_party/apriltag/apriltag.h" |
| 8 | #include "third_party/apriltag/apriltag_pose.h" |
| 9 | #include "third_party/apriltag/tag16h5.h" |
| 10 | |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 11 | #include "aos/events/event_loop.h" |
| 12 | #include "aos/events/shm_event_loop.h" |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 13 | #include "aos/network/team_number.h" |
| 14 | #include "aos/realtime.h" |
milind-u | f2a4e32 | 2023-02-01 19:33:10 -0800 | [diff] [blame] | 15 | #include "frc971/constants/constants_sender_lib.h" |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 16 | #include "frc971/vision/calibration_generated.h" |
| 17 | #include "frc971/vision/charuco_lib.h" |
| 18 | #include "frc971/vision/target_map_generated.h" |
| 19 | #include "frc971/vision/target_mapper.h" |
| 20 | #include "frc971/vision/vision_generated.h" |
Jim Ostrowski | 49be823 | 2023-03-23 01:00:14 -0700 | [diff] [blame] | 21 | #include "frc971/vision/visualize_robot.h" |
James Kuszmaul | d67f6d2 | 2023-02-05 17:37:25 -0800 | [diff] [blame] | 22 | #include "y2023/constants/constants_generated.h" |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 23 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame^] | 24 | namespace y2023::vision { |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 25 | |
| 26 | class AprilRoboticsDetector { |
| 27 | public: |
milind-u | fc8ab70 | 2023-02-26 14:14:39 -0800 | [diff] [blame] | 28 | // Aprilrobotics representation of a tag detection |
| 29 | struct Detection { |
| 30 | apriltag_detection_t det; |
| 31 | apriltag_pose_t pose; |
| 32 | double pose_error; |
milind-u | f5b3b4b | 2023-02-26 14:50:38 -0800 | [diff] [blame] | 33 | double distortion_factor; |
milind-u | de9045f | 2023-03-25 18:17:12 -0700 | [diff] [blame] | 34 | double pose_error_ratio; |
milind-u | fc8ab70 | 2023-02-26 14:14:39 -0800 | [diff] [blame] | 35 | }; |
| 36 | |
milind-u | 99b1a76 | 2023-03-12 16:48:32 -0700 | [diff] [blame] | 37 | struct DetectionResult { |
| 38 | std::vector<Detection> detections; |
| 39 | size_t rejections; |
| 40 | }; |
| 41 | |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 42 | AprilRoboticsDetector(aos::EventLoop *event_loop, |
milind-u | a30a4a1 | 2023-03-24 20:49:41 -0700 | [diff] [blame] | 43 | std::string_view channel_name, bool flip_image = true); |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 44 | ~AprilRoboticsDetector(); |
| 45 | |
| 46 | void SetWorkerpoolAffinities(); |
| 47 | |
milind-u | de9045f | 2023-03-25 18:17:12 -0700 | [diff] [blame] | 48 | // Deletes the heap-allocated rotation and translation pointers in the given |
| 49 | // pose |
| 50 | void DestroyPose(apriltag_pose_t *pose) const; |
| 51 | |
milind-u | f2a4e32 | 2023-02-01 19:33:10 -0800 | [diff] [blame] | 52 | // Undistorts the april tag corners using the camera calibration |
| 53 | void UndistortDetection(apriltag_detection_t *det) const; |
| 54 | |
Jim Ostrowski | 5e2c5e6 | 2023-02-26 12:52:56 -0800 | [diff] [blame] | 55 | // Helper function to store detection points in vector of Point2f's |
| 56 | std::vector<cv::Point2f> MakeCornerVector(const apriltag_detection_t *det); |
| 57 | |
milind-u | 99b1a76 | 2023-03-12 16:48:32 -0700 | [diff] [blame] | 58 | DetectionResult DetectTags(cv::Mat image, |
| 59 | aos::monotonic_clock::time_point eof); |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 60 | |
James Kuszmaul | 258e4ee | 2023-02-23 14:22:30 -0800 | [diff] [blame] | 61 | const std::optional<cv::Mat> extrinsics() const { return extrinsics_; } |
milind-u | f2a4e32 | 2023-02-01 19:33:10 -0800 | [diff] [blame] | 62 | const cv::Mat intrinsics() const { return intrinsics_; } |
| 63 | const cv::Mat dist_coeffs() const { return dist_coeffs_; } |
| 64 | |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 65 | private: |
milind-u | 09fb125 | 2023-01-28 19:21:41 -0800 | [diff] [blame] | 66 | void HandleImage(cv::Mat image, aos::monotonic_clock::time_point eof); |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 67 | |
| 68 | flatbuffers::Offset<frc971::vision::TargetPoseFbs> BuildTargetPose( |
milind-u | fc8ab70 | 2023-02-26 14:14:39 -0800 | [diff] [blame] | 69 | const Detection &detection, flatbuffers::FlatBufferBuilder *fbb); |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 70 | |
milind-u | f5b3b4b | 2023-02-26 14:50:38 -0800 | [diff] [blame] | 71 | // Computes the distortion effect on this detection taking the scaled average |
| 72 | // delta between orig_corners (distorted corners) and corners (undistorted |
| 73 | // corners) |
| 74 | double ComputeDistortionFactor(const std::vector<cv::Point2f> &orig_corners, |
| 75 | const std::vector<cv::Point2f> &corners); |
| 76 | |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 77 | apriltag_family_t *tag_family_; |
| 78 | apriltag_detector_t *tag_detector_; |
| 79 | |
James Kuszmaul | d67f6d2 | 2023-02-05 17:37:25 -0800 | [diff] [blame] | 80 | const frc971::constants::ConstantsFetcher<Constants> calibration_data_; |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 81 | const frc971::vision::calibration::CameraCalibration *calibration_; |
| 82 | cv::Mat intrinsics_; |
milind-u | f2a4e32 | 2023-02-01 19:33:10 -0800 | [diff] [blame] | 83 | cv::Mat projection_matrix_; |
James Kuszmaul | 258e4ee | 2023-02-23 14:22:30 -0800 | [diff] [blame] | 84 | std::optional<cv::Mat> extrinsics_; |
milind-u | f2a4e32 | 2023-02-01 19:33:10 -0800 | [diff] [blame] | 85 | cv::Mat dist_coeffs_; |
milind-u | f5b3b4b | 2023-02-26 14:50:38 -0800 | [diff] [blame] | 86 | cv::Size image_size_; |
milind-u | a30a4a1 | 2023-03-24 20:49:41 -0700 | [diff] [blame] | 87 | bool flip_image_; |
| 88 | std::string_view node_name_; |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 89 | |
| 90 | aos::Ftrace ftrace_; |
| 91 | |
| 92 | frc971::vision::ImageCallback image_callback_; |
| 93 | aos::Sender<frc971::vision::TargetMap> target_map_sender_; |
milind-u | 7aa29e2 | 2023-02-23 20:22:01 -0800 | [diff] [blame] | 94 | aos::Sender<foxglove::ImageAnnotations> image_annotations_sender_; |
milind-u | 99b1a76 | 2023-03-12 16:48:32 -0700 | [diff] [blame] | 95 | size_t rejections_; |
Jim Ostrowski | 49be823 | 2023-03-23 01:00:14 -0700 | [diff] [blame] | 96 | frc971::vision::VisualizeRobot vis_robot_; |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 97 | }; |
| 98 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame^] | 99 | } // namespace y2023::vision |