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