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