blob: e11bfcc5767b976763dbf3fe6f4406c60550c3df [file] [log] [blame]
Maxwell Hendersonfebee252023-01-28 16:53:52 -08001
2#include <string>
3
Philipp Schrader790cb542023-07-05 21:06:52 -07004#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 Hendersonfebee252023-01-28 16:53:52 -080011#include "aos/events/event_loop.h"
12#include "aos/events/shm_event_loop.h"
Maxwell Hendersonfebee252023-01-28 16:53:52 -080013#include "aos/network/team_number.h"
14#include "aos/realtime.h"
milind-uf2a4e322023-02-01 19:33:10 -080015#include "frc971/constants/constants_sender_lib.h"
Maxwell Hendersonfebee252023-01-28 16:53:52 -080016#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 Ostrowski49be8232023-03-23 01:00:14 -070021#include "frc971/vision/visualize_robot.h"
James Kuszmauld67f6d22023-02-05 17:37:25 -080022#include "y2023/constants/constants_generated.h"
Maxwell Hendersonfebee252023-01-28 16:53:52 -080023
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080024namespace y2023::vision {
Maxwell Hendersonfebee252023-01-28 16:53:52 -080025
26class AprilRoboticsDetector {
27 public:
milind-ufc8ab702023-02-26 14:14:39 -080028 // Aprilrobotics representation of a tag detection
29 struct Detection {
30 apriltag_detection_t det;
31 apriltag_pose_t pose;
32 double pose_error;
milind-uf5b3b4b2023-02-26 14:50:38 -080033 double distortion_factor;
milind-ude9045f2023-03-25 18:17:12 -070034 double pose_error_ratio;
milind-ufc8ab702023-02-26 14:14:39 -080035 };
36
milind-u99b1a762023-03-12 16:48:32 -070037 struct DetectionResult {
38 std::vector<Detection> detections;
39 size_t rejections;
40 };
41
Maxwell Hendersonfebee252023-01-28 16:53:52 -080042 AprilRoboticsDetector(aos::EventLoop *event_loop,
milind-ua30a4a12023-03-24 20:49:41 -070043 std::string_view channel_name, bool flip_image = true);
Maxwell Hendersonfebee252023-01-28 16:53:52 -080044 ~AprilRoboticsDetector();
45
46 void SetWorkerpoolAffinities();
47
milind-ude9045f2023-03-25 18:17:12 -070048 // Deletes the heap-allocated rotation and translation pointers in the given
49 // pose
50 void DestroyPose(apriltag_pose_t *pose) const;
51
milind-uf2a4e322023-02-01 19:33:10 -080052 // Undistorts the april tag corners using the camera calibration
53 void UndistortDetection(apriltag_detection_t *det) const;
54
Jim Ostrowski5e2c5e62023-02-26 12:52:56 -080055 // Helper function to store detection points in vector of Point2f's
56 std::vector<cv::Point2f> MakeCornerVector(const apriltag_detection_t *det);
57
milind-u99b1a762023-03-12 16:48:32 -070058 DetectionResult DetectTags(cv::Mat image,
59 aos::monotonic_clock::time_point eof);
Maxwell Hendersonfebee252023-01-28 16:53:52 -080060
James Kuszmaul258e4ee2023-02-23 14:22:30 -080061 const std::optional<cv::Mat> extrinsics() const { return extrinsics_; }
milind-uf2a4e322023-02-01 19:33:10 -080062 const cv::Mat intrinsics() const { return intrinsics_; }
63 const cv::Mat dist_coeffs() const { return dist_coeffs_; }
64
Maxwell Hendersonfebee252023-01-28 16:53:52 -080065 private:
milind-u09fb1252023-01-28 19:21:41 -080066 void HandleImage(cv::Mat image, aos::monotonic_clock::time_point eof);
Maxwell Hendersonfebee252023-01-28 16:53:52 -080067
68 flatbuffers::Offset<frc971::vision::TargetPoseFbs> BuildTargetPose(
milind-ufc8ab702023-02-26 14:14:39 -080069 const Detection &detection, flatbuffers::FlatBufferBuilder *fbb);
Maxwell Hendersonfebee252023-01-28 16:53:52 -080070
milind-uf5b3b4b2023-02-26 14:50:38 -080071 // Computes the distortion effect on this detection taking the scaled average
72 // delta between orig_corners (distorted corners) and corners (undistorted
73 // corners)
74 double ComputeDistortionFactor(const std::vector<cv::Point2f> &orig_corners,
75 const std::vector<cv::Point2f> &corners);
76
Maxwell Hendersonfebee252023-01-28 16:53:52 -080077 apriltag_family_t *tag_family_;
78 apriltag_detector_t *tag_detector_;
79
James Kuszmauld67f6d22023-02-05 17:37:25 -080080 const frc971::constants::ConstantsFetcher<Constants> calibration_data_;
Maxwell Hendersonfebee252023-01-28 16:53:52 -080081 const frc971::vision::calibration::CameraCalibration *calibration_;
82 cv::Mat intrinsics_;
milind-uf2a4e322023-02-01 19:33:10 -080083 cv::Mat projection_matrix_;
James Kuszmaul258e4ee2023-02-23 14:22:30 -080084 std::optional<cv::Mat> extrinsics_;
milind-uf2a4e322023-02-01 19:33:10 -080085 cv::Mat dist_coeffs_;
milind-uf5b3b4b2023-02-26 14:50:38 -080086 cv::Size image_size_;
milind-ua30a4a12023-03-24 20:49:41 -070087 bool flip_image_;
88 std::string_view node_name_;
Maxwell Hendersonfebee252023-01-28 16:53:52 -080089
90 aos::Ftrace ftrace_;
91
92 frc971::vision::ImageCallback image_callback_;
93 aos::Sender<frc971::vision::TargetMap> target_map_sender_;
milind-u7aa29e22023-02-23 20:22:01 -080094 aos::Sender<foxglove::ImageAnnotations> image_annotations_sender_;
milind-u99b1a762023-03-12 16:48:32 -070095 size_t rejections_;
Jim Ostrowski49be8232023-03-23 01:00:14 -070096 frc971::vision::VisualizeRobot vis_robot_;
Maxwell Hendersonfebee252023-01-28 16:53:52 -080097};
98
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080099} // namespace y2023::vision