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: |
| 26 | AprilRoboticsDetector(aos::EventLoop *event_loop, |
| 27 | std::string_view channel_name); |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 28 | ~AprilRoboticsDetector(); |
| 29 | |
| 30 | void SetWorkerpoolAffinities(); |
| 31 | |
milind-u | f2a4e32 | 2023-02-01 19:33:10 -0800 | [diff] [blame] | 32 | // Undistorts the april tag corners using the camera calibration |
| 33 | void UndistortDetection(apriltag_detection_t *det) const; |
| 34 | |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 35 | std::vector<std::pair<apriltag_detection_t, apriltag_pose_t>> DetectTags( |
milind-u | 7aa29e2 | 2023-02-23 20:22:01 -0800 | [diff] [blame] | 36 | cv::Mat image, aos::monotonic_clock::time_point eof); |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 37 | |
James Kuszmaul | 258e4ee | 2023-02-23 14:22:30 -0800 | [diff] [blame] | 38 | const std::optional<cv::Mat> extrinsics() const { return extrinsics_; } |
milind-u | f2a4e32 | 2023-02-01 19:33:10 -0800 | [diff] [blame] | 39 | const cv::Mat intrinsics() const { return intrinsics_; } |
| 40 | const cv::Mat dist_coeffs() const { return dist_coeffs_; } |
| 41 | |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 42 | private: |
milind-u | 09fb125 | 2023-01-28 19:21:41 -0800 | [diff] [blame] | 43 | void HandleImage(cv::Mat image, aos::monotonic_clock::time_point eof); |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 44 | |
| 45 | flatbuffers::Offset<frc971::vision::TargetPoseFbs> BuildTargetPose( |
milind-u | 681c471 | 2023-02-23 21:22:50 -0800 | [diff] [blame] | 46 | const apriltag_pose_t &pose, const apriltag_detection_t &det, |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 47 | flatbuffers::FlatBufferBuilder *fbb); |
| 48 | |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 49 | apriltag_family_t *tag_family_; |
| 50 | apriltag_detector_t *tag_detector_; |
| 51 | |
James Kuszmaul | d67f6d2 | 2023-02-05 17:37:25 -0800 | [diff] [blame] | 52 | const frc971::constants::ConstantsFetcher<Constants> calibration_data_; |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 53 | const frc971::vision::calibration::CameraCalibration *calibration_; |
| 54 | cv::Mat intrinsics_; |
milind-u | f2a4e32 | 2023-02-01 19:33:10 -0800 | [diff] [blame] | 55 | cv::Mat projection_matrix_; |
James Kuszmaul | 258e4ee | 2023-02-23 14:22:30 -0800 | [diff] [blame] | 56 | std::optional<cv::Mat> extrinsics_; |
milind-u | f2a4e32 | 2023-02-01 19:33:10 -0800 | [diff] [blame] | 57 | cv::Mat dist_coeffs_; |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 58 | |
| 59 | aos::Ftrace ftrace_; |
| 60 | |
| 61 | frc971::vision::ImageCallback image_callback_; |
| 62 | aos::Sender<frc971::vision::TargetMap> target_map_sender_; |
milind-u | 7aa29e2 | 2023-02-23 20:22:01 -0800 | [diff] [blame] | 63 | aos::Sender<foxglove::ImageAnnotations> image_annotations_sender_; |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | } // namespace vision |
| 67 | } // namespace y2023 |