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" |
| 8 | #include "frc971/vision/calibration_generated.h" |
| 9 | #include "frc971/vision/charuco_lib.h" |
| 10 | #include "frc971/vision/target_map_generated.h" |
| 11 | #include "frc971/vision/target_mapper.h" |
| 12 | #include "frc971/vision/vision_generated.h" |
| 13 | #include "opencv2/core/eigen.hpp" |
| 14 | #include "opencv2/imgproc.hpp" |
| 15 | #include "third_party/apriltag/apriltag.h" |
| 16 | #include "third_party/apriltag/apriltag_pose.h" |
| 17 | #include "third_party/apriltag/tag16h5.h" |
Yash Chainani | 728ae22 | 2023-02-04 19:48:12 -0800 | [diff] [blame] | 18 | #include "y2023/vision/april_debug_generated.h" |
James Kuszmaul | d67f6d2 | 2023-02-05 17:37:25 -0800 | [diff] [blame^] | 19 | #include "y2023/constants/constants_generated.h" |
| 20 | #include "frc971/constants/constants_sender_lib.h" |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 21 | |
| 22 | DECLARE_int32(team_number); |
| 23 | |
| 24 | namespace y2023 { |
| 25 | namespace vision { |
| 26 | |
| 27 | class AprilRoboticsDetector { |
| 28 | public: |
| 29 | AprilRoboticsDetector(aos::EventLoop *event_loop, |
| 30 | std::string_view channel_name); |
| 31 | |
| 32 | ~AprilRoboticsDetector(); |
| 33 | |
| 34 | void SetWorkerpoolAffinities(); |
| 35 | |
| 36 | std::vector<std::pair<apriltag_detection_t, apriltag_pose_t>> DetectTags( |
| 37 | cv::Mat image); |
| 38 | |
| 39 | private: |
milind-u | 09fb125 | 2023-01-28 19:21:41 -0800 | [diff] [blame] | 40 | void HandleImage(cv::Mat image, aos::monotonic_clock::time_point eof); |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 41 | |
| 42 | flatbuffers::Offset<frc971::vision::TargetPoseFbs> BuildTargetPose( |
| 43 | const apriltag_pose_t &pose, |
| 44 | frc971::vision::TargetMapper::TargetId target_id, |
| 45 | flatbuffers::FlatBufferBuilder *fbb); |
| 46 | |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 47 | |
| 48 | static cv::Mat CameraIntrinsics( |
| 49 | const frc971::vision::calibration::CameraCalibration |
| 50 | *camera_calibration) { |
| 51 | cv::Mat result(3, 3, CV_32F, |
| 52 | const_cast<void *>(static_cast<const void *>( |
| 53 | camera_calibration->intrinsics()->data()))); |
| 54 | result.convertTo(result, CV_64F); |
| 55 | CHECK_EQ(result.total(), camera_calibration->intrinsics()->size()); |
| 56 | |
| 57 | return result; |
| 58 | } |
| 59 | |
| 60 | static cv::Mat CameraDistCoeffs( |
| 61 | const frc971::vision::calibration::CameraCalibration |
| 62 | *camera_calibration) { |
| 63 | const cv::Mat result(5, 1, CV_32F, |
| 64 | const_cast<void *>(static_cast<const void *>( |
| 65 | camera_calibration->dist_coeffs()->data()))); |
| 66 | CHECK_EQ(result.total(), camera_calibration->dist_coeffs()->size()); |
| 67 | return result; |
| 68 | } |
| 69 | |
| 70 | apriltag_family_t *tag_family_; |
| 71 | apriltag_detector_t *tag_detector_; |
| 72 | |
James Kuszmaul | d67f6d2 | 2023-02-05 17:37:25 -0800 | [diff] [blame^] | 73 | const frc971::constants::ConstantsFetcher<Constants> calibration_data_; |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 74 | const frc971::vision::calibration::CameraCalibration *calibration_; |
| 75 | cv::Mat intrinsics_; |
| 76 | cv::Mat camera_distortion_coeffs_; |
| 77 | |
| 78 | aos::Ftrace ftrace_; |
| 79 | |
| 80 | frc971::vision::ImageCallback image_callback_; |
| 81 | aos::Sender<frc971::vision::TargetMap> target_map_sender_; |
Yash Chainani | 728ae22 | 2023-02-04 19:48:12 -0800 | [diff] [blame] | 82 | aos::Sender<y2023::vision::AprilDebug> april_debug_sender_; |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | } // namespace vision |
| 86 | } // namespace y2023 |