blob: fab2d30182d9e886d595e97aec3026031fa82c3a [file] [log] [blame]
Maxwell Hendersonfebee252023-01-28 16:53:52 -08001
2#include <string>
3
4#include "aos/events/event_loop.h"
5#include "aos/events/shm_event_loop.h"
Maxwell Hendersonfebee252023-01-28 16:53:52 -08006#include "aos/network/team_number.h"
7#include "aos/realtime.h"
milind-uf2a4e322023-02-01 19:33:10 -08008#include "frc971/constants/constants_sender_lib.h"
Maxwell Hendersonfebee252023-01-28 16:53:52 -08009#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 Kuszmauld67f6d22023-02-05 17:37:25 -080019#include "y2023/constants/constants_generated.h"
Maxwell Hendersonfebee252023-01-28 16:53:52 -080020
Maxwell Hendersonfebee252023-01-28 16:53:52 -080021namespace y2023 {
22namespace vision {
23
24class AprilRoboticsDetector {
25 public:
milind-ufc8ab702023-02-26 14:14:39 -080026 // Aprilrobotics representation of a tag detection
27 struct Detection {
28 apriltag_detection_t det;
29 apriltag_pose_t pose;
30 double pose_error;
milind-uf5b3b4b2023-02-26 14:50:38 -080031 double distortion_factor;
milind-ufc8ab702023-02-26 14:14:39 -080032 };
33
milind-u99b1a762023-03-12 16:48:32 -070034 struct DetectionResult {
35 std::vector<Detection> detections;
36 size_t rejections;
37 };
38
Maxwell Hendersonfebee252023-01-28 16:53:52 -080039 AprilRoboticsDetector(aos::EventLoop *event_loop,
40 std::string_view channel_name);
Maxwell Hendersonfebee252023-01-28 16:53:52 -080041 ~AprilRoboticsDetector();
42
43 void SetWorkerpoolAffinities();
44
milind-uf2a4e322023-02-01 19:33:10 -080045 // Undistorts the april tag corners using the camera calibration
46 void UndistortDetection(apriltag_detection_t *det) const;
47
Jim Ostrowski5e2c5e62023-02-26 12:52:56 -080048 // Helper function to store detection points in vector of Point2f's
49 std::vector<cv::Point2f> MakeCornerVector(const apriltag_detection_t *det);
50
milind-u99b1a762023-03-12 16:48:32 -070051 DetectionResult DetectTags(cv::Mat image,
52 aos::monotonic_clock::time_point eof);
Maxwell Hendersonfebee252023-01-28 16:53:52 -080053
James Kuszmaul258e4ee2023-02-23 14:22:30 -080054 const std::optional<cv::Mat> extrinsics() const { return extrinsics_; }
milind-uf2a4e322023-02-01 19:33:10 -080055 const cv::Mat intrinsics() const { return intrinsics_; }
56 const cv::Mat dist_coeffs() const { return dist_coeffs_; }
57
Maxwell Hendersonfebee252023-01-28 16:53:52 -080058 private:
milind-u09fb1252023-01-28 19:21:41 -080059 void HandleImage(cv::Mat image, aos::monotonic_clock::time_point eof);
Maxwell Hendersonfebee252023-01-28 16:53:52 -080060
61 flatbuffers::Offset<frc971::vision::TargetPoseFbs> BuildTargetPose(
milind-ufc8ab702023-02-26 14:14:39 -080062 const Detection &detection, flatbuffers::FlatBufferBuilder *fbb);
Maxwell Hendersonfebee252023-01-28 16:53:52 -080063
milind-uf5b3b4b2023-02-26 14:50:38 -080064 // Computes the distortion effect on this detection taking the scaled average
65 // delta between orig_corners (distorted corners) and corners (undistorted
66 // corners)
67 double ComputeDistortionFactor(const std::vector<cv::Point2f> &orig_corners,
68 const std::vector<cv::Point2f> &corners);
69
Maxwell Hendersonfebee252023-01-28 16:53:52 -080070 apriltag_family_t *tag_family_;
71 apriltag_detector_t *tag_detector_;
72
James Kuszmauld67f6d22023-02-05 17:37:25 -080073 const frc971::constants::ConstantsFetcher<Constants> calibration_data_;
Maxwell Hendersonfebee252023-01-28 16:53:52 -080074 const frc971::vision::calibration::CameraCalibration *calibration_;
75 cv::Mat intrinsics_;
milind-uf2a4e322023-02-01 19:33:10 -080076 cv::Mat projection_matrix_;
James Kuszmaul258e4ee2023-02-23 14:22:30 -080077 std::optional<cv::Mat> extrinsics_;
milind-uf2a4e322023-02-01 19:33:10 -080078 cv::Mat dist_coeffs_;
milind-uf5b3b4b2023-02-26 14:50:38 -080079 cv::Size image_size_;
Maxwell Hendersonfebee252023-01-28 16:53:52 -080080
81 aos::Ftrace ftrace_;
82
83 frc971::vision::ImageCallback image_callback_;
84 aos::Sender<frc971::vision::TargetMap> target_map_sender_;
milind-u7aa29e22023-02-23 20:22:01 -080085 aos::Sender<foxglove::ImageAnnotations> image_annotations_sender_;
milind-u99b1a762023-03-12 16:48:32 -070086
87 size_t rejections_;
Maxwell Hendersonfebee252023-01-28 16:53:52 -080088};
89
90} // namespace vision
91} // namespace y2023