blob: 01e31381447f1d2bb35a8645137f097a0e2d5a62 [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"
Jim Ostrowski49be8232023-03-23 01:00:14 -070014#include "frc971/vision/visualize_robot.h"
Maxwell Hendersonfebee252023-01-28 16:53:52 -080015#include "opencv2/core/eigen.hpp"
16#include "opencv2/imgproc.hpp"
17#include "third_party/apriltag/apriltag.h"
18#include "third_party/apriltag/apriltag_pose.h"
19#include "third_party/apriltag/tag16h5.h"
James Kuszmauld67f6d22023-02-05 17:37:25 -080020#include "y2023/constants/constants_generated.h"
Maxwell Hendersonfebee252023-01-28 16:53:52 -080021
Maxwell Hendersonfebee252023-01-28 16:53:52 -080022namespace y2023 {
23namespace vision {
24
25class AprilRoboticsDetector {
26 public:
milind-ufc8ab702023-02-26 14:14:39 -080027 // Aprilrobotics representation of a tag detection
28 struct Detection {
29 apriltag_detection_t det;
30 apriltag_pose_t pose;
31 double pose_error;
milind-uf5b3b4b2023-02-26 14:50:38 -080032 double distortion_factor;
milind-ude9045f2023-03-25 18:17:12 -070033 double pose_error_ratio;
milind-ufc8ab702023-02-26 14:14:39 -080034 };
35
milind-u99b1a762023-03-12 16:48:32 -070036 struct DetectionResult {
37 std::vector<Detection> detections;
38 size_t rejections;
39 };
40
Maxwell Hendersonfebee252023-01-28 16:53:52 -080041 AprilRoboticsDetector(aos::EventLoop *event_loop,
milind-ua30a4a12023-03-24 20:49:41 -070042 std::string_view channel_name, bool flip_image = true);
Maxwell Hendersonfebee252023-01-28 16:53:52 -080043 ~AprilRoboticsDetector();
44
45 void SetWorkerpoolAffinities();
46
milind-ude9045f2023-03-25 18:17:12 -070047 // Deletes the heap-allocated rotation and translation pointers in the given
48 // pose
49 void DestroyPose(apriltag_pose_t *pose) const;
50
milind-uf2a4e322023-02-01 19:33:10 -080051 // Undistorts the april tag corners using the camera calibration
52 void UndistortDetection(apriltag_detection_t *det) const;
53
Jim Ostrowski5e2c5e62023-02-26 12:52:56 -080054 // Helper function to store detection points in vector of Point2f's
55 std::vector<cv::Point2f> MakeCornerVector(const apriltag_detection_t *det);
56
milind-u99b1a762023-03-12 16:48:32 -070057 DetectionResult DetectTags(cv::Mat image,
58 aos::monotonic_clock::time_point eof);
Maxwell Hendersonfebee252023-01-28 16:53:52 -080059
James Kuszmaul258e4ee2023-02-23 14:22:30 -080060 const std::optional<cv::Mat> extrinsics() const { return extrinsics_; }
milind-uf2a4e322023-02-01 19:33:10 -080061 const cv::Mat intrinsics() const { return intrinsics_; }
62 const cv::Mat dist_coeffs() const { return dist_coeffs_; }
63
Maxwell Hendersonfebee252023-01-28 16:53:52 -080064 private:
milind-u09fb1252023-01-28 19:21:41 -080065 void HandleImage(cv::Mat image, aos::monotonic_clock::time_point eof);
Maxwell Hendersonfebee252023-01-28 16:53:52 -080066
67 flatbuffers::Offset<frc971::vision::TargetPoseFbs> BuildTargetPose(
milind-ufc8ab702023-02-26 14:14:39 -080068 const Detection &detection, flatbuffers::FlatBufferBuilder *fbb);
Maxwell Hendersonfebee252023-01-28 16:53:52 -080069
milind-uf5b3b4b2023-02-26 14:50:38 -080070 // Computes the distortion effect on this detection taking the scaled average
71 // delta between orig_corners (distorted corners) and corners (undistorted
72 // corners)
73 double ComputeDistortionFactor(const std::vector<cv::Point2f> &orig_corners,
74 const std::vector<cv::Point2f> &corners);
75
Maxwell Hendersonfebee252023-01-28 16:53:52 -080076 apriltag_family_t *tag_family_;
77 apriltag_detector_t *tag_detector_;
78
James Kuszmauld67f6d22023-02-05 17:37:25 -080079 const frc971::constants::ConstantsFetcher<Constants> calibration_data_;
Maxwell Hendersonfebee252023-01-28 16:53:52 -080080 const frc971::vision::calibration::CameraCalibration *calibration_;
81 cv::Mat intrinsics_;
milind-uf2a4e322023-02-01 19:33:10 -080082 cv::Mat projection_matrix_;
James Kuszmaul258e4ee2023-02-23 14:22:30 -080083 std::optional<cv::Mat> extrinsics_;
milind-uf2a4e322023-02-01 19:33:10 -080084 cv::Mat dist_coeffs_;
milind-uf5b3b4b2023-02-26 14:50:38 -080085 cv::Size image_size_;
milind-ua30a4a12023-03-24 20:49:41 -070086 bool flip_image_;
87 std::string_view node_name_;
Maxwell Hendersonfebee252023-01-28 16:53:52 -080088
89 aos::Ftrace ftrace_;
90
91 frc971::vision::ImageCallback image_callback_;
92 aos::Sender<frc971::vision::TargetMap> target_map_sender_;
milind-u7aa29e22023-02-23 20:22:01 -080093 aos::Sender<foxglove::ImageAnnotations> image_annotations_sender_;
milind-u99b1a762023-03-12 16:48:32 -070094 size_t rejections_;
Jim Ostrowski49be8232023-03-23 01:00:14 -070095 frc971::vision::VisualizeRobot vis_robot_;
Maxwell Hendersonfebee252023-01-28 16:53:52 -080096};
97
98} // namespace vision
99} // namespace y2023