blob: 123f3526cb52d191fe14191ed29f1018afbad12f [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
Maxwell Hendersonfebee252023-01-28 16:53:52 -080024namespace y2023 {
25namespace vision {
26
27class AprilRoboticsDetector {
28 public:
milind-ufc8ab702023-02-26 14:14:39 -080029 // Aprilrobotics representation of a tag detection
30 struct Detection {
31 apriltag_detection_t det;
32 apriltag_pose_t pose;
33 double pose_error;
milind-uf5b3b4b2023-02-26 14:50:38 -080034 double distortion_factor;
milind-ude9045f2023-03-25 18:17:12 -070035 double pose_error_ratio;
milind-ufc8ab702023-02-26 14:14:39 -080036 };
37
milind-u99b1a762023-03-12 16:48:32 -070038 struct DetectionResult {
39 std::vector<Detection> detections;
40 size_t rejections;
41 };
42
Maxwell Hendersonfebee252023-01-28 16:53:52 -080043 AprilRoboticsDetector(aos::EventLoop *event_loop,
milind-ua30a4a12023-03-24 20:49:41 -070044 std::string_view channel_name, bool flip_image = true);
Maxwell Hendersonfebee252023-01-28 16:53:52 -080045 ~AprilRoboticsDetector();
46
47 void SetWorkerpoolAffinities();
48
milind-ude9045f2023-03-25 18:17:12 -070049 // Deletes the heap-allocated rotation and translation pointers in the given
50 // pose
51 void DestroyPose(apriltag_pose_t *pose) const;
52
milind-uf2a4e322023-02-01 19:33:10 -080053 // Undistorts the april tag corners using the camera calibration
54 void UndistortDetection(apriltag_detection_t *det) const;
55
Jim Ostrowski5e2c5e62023-02-26 12:52:56 -080056 // Helper function to store detection points in vector of Point2f's
57 std::vector<cv::Point2f> MakeCornerVector(const apriltag_detection_t *det);
58
milind-u99b1a762023-03-12 16:48:32 -070059 DetectionResult DetectTags(cv::Mat image,
60 aos::monotonic_clock::time_point eof);
Maxwell Hendersonfebee252023-01-28 16:53:52 -080061
James Kuszmaul258e4ee2023-02-23 14:22:30 -080062 const std::optional<cv::Mat> extrinsics() const { return extrinsics_; }
milind-uf2a4e322023-02-01 19:33:10 -080063 const cv::Mat intrinsics() const { return intrinsics_; }
64 const cv::Mat dist_coeffs() const { return dist_coeffs_; }
65
Maxwell Hendersonfebee252023-01-28 16:53:52 -080066 private:
milind-u09fb1252023-01-28 19:21:41 -080067 void HandleImage(cv::Mat image, aos::monotonic_clock::time_point eof);
Maxwell Hendersonfebee252023-01-28 16:53:52 -080068
69 flatbuffers::Offset<frc971::vision::TargetPoseFbs> BuildTargetPose(
milind-ufc8ab702023-02-26 14:14:39 -080070 const Detection &detection, flatbuffers::FlatBufferBuilder *fbb);
Maxwell Hendersonfebee252023-01-28 16:53:52 -080071
milind-uf5b3b4b2023-02-26 14:50:38 -080072 // 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 Hendersonfebee252023-01-28 16:53:52 -080078 apriltag_family_t *tag_family_;
79 apriltag_detector_t *tag_detector_;
80
James Kuszmauld67f6d22023-02-05 17:37:25 -080081 const frc971::constants::ConstantsFetcher<Constants> calibration_data_;
Maxwell Hendersonfebee252023-01-28 16:53:52 -080082 const frc971::vision::calibration::CameraCalibration *calibration_;
83 cv::Mat intrinsics_;
milind-uf2a4e322023-02-01 19:33:10 -080084 cv::Mat projection_matrix_;
James Kuszmaul258e4ee2023-02-23 14:22:30 -080085 std::optional<cv::Mat> extrinsics_;
milind-uf2a4e322023-02-01 19:33:10 -080086 cv::Mat dist_coeffs_;
milind-uf5b3b4b2023-02-26 14:50:38 -080087 cv::Size image_size_;
milind-ua30a4a12023-03-24 20:49:41 -070088 bool flip_image_;
89 std::string_view node_name_;
Maxwell Hendersonfebee252023-01-28 16:53:52 -080090
91 aos::Ftrace ftrace_;
92
93 frc971::vision::ImageCallback image_callback_;
94 aos::Sender<frc971::vision::TargetMap> target_map_sender_;
milind-u7aa29e22023-02-23 20:22:01 -080095 aos::Sender<foxglove::ImageAnnotations> image_annotations_sender_;
milind-u99b1a762023-03-12 16:48:32 -070096 size_t rejections_;
Jim Ostrowski49be8232023-03-23 01:00:14 -070097 frc971::vision::VisualizeRobot vis_robot_;
Maxwell Hendersonfebee252023-01-28 16:53:52 -080098};
99
100} // namespace vision
101} // namespace y2023