blob: 7caa8480e51aaf4008b4ce754d64b6766e21842a [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-ufc8ab702023-02-26 14:14:39 -080033 };
34
milind-u99b1a762023-03-12 16:48:32 -070035 struct DetectionResult {
36 std::vector<Detection> detections;
37 size_t rejections;
38 };
39
Maxwell Hendersonfebee252023-01-28 16:53:52 -080040 AprilRoboticsDetector(aos::EventLoop *event_loop,
41 std::string_view channel_name);
Maxwell Hendersonfebee252023-01-28 16:53:52 -080042 ~AprilRoboticsDetector();
43
44 void SetWorkerpoolAffinities();
45
milind-uf2a4e322023-02-01 19:33:10 -080046 // Undistorts the april tag corners using the camera calibration
47 void UndistortDetection(apriltag_detection_t *det) const;
48
Jim Ostrowski5e2c5e62023-02-26 12:52:56 -080049 // Helper function to store detection points in vector of Point2f's
50 std::vector<cv::Point2f> MakeCornerVector(const apriltag_detection_t *det);
51
milind-u99b1a762023-03-12 16:48:32 -070052 DetectionResult DetectTags(cv::Mat image,
53 aos::monotonic_clock::time_point eof);
Maxwell Hendersonfebee252023-01-28 16:53:52 -080054
James Kuszmaul258e4ee2023-02-23 14:22:30 -080055 const std::optional<cv::Mat> extrinsics() const { return extrinsics_; }
milind-uf2a4e322023-02-01 19:33:10 -080056 const cv::Mat intrinsics() const { return intrinsics_; }
57 const cv::Mat dist_coeffs() const { return dist_coeffs_; }
58
Maxwell Hendersonfebee252023-01-28 16:53:52 -080059 private:
milind-u09fb1252023-01-28 19:21:41 -080060 void HandleImage(cv::Mat image, aos::monotonic_clock::time_point eof);
Maxwell Hendersonfebee252023-01-28 16:53:52 -080061
62 flatbuffers::Offset<frc971::vision::TargetPoseFbs> BuildTargetPose(
milind-ufc8ab702023-02-26 14:14:39 -080063 const Detection &detection, flatbuffers::FlatBufferBuilder *fbb);
Maxwell Hendersonfebee252023-01-28 16:53:52 -080064
milind-uf5b3b4b2023-02-26 14:50:38 -080065 // Computes the distortion effect on this detection taking the scaled average
66 // delta between orig_corners (distorted corners) and corners (undistorted
67 // corners)
68 double ComputeDistortionFactor(const std::vector<cv::Point2f> &orig_corners,
69 const std::vector<cv::Point2f> &corners);
70
Maxwell Hendersonfebee252023-01-28 16:53:52 -080071 apriltag_family_t *tag_family_;
72 apriltag_detector_t *tag_detector_;
73
James Kuszmauld67f6d22023-02-05 17:37:25 -080074 const frc971::constants::ConstantsFetcher<Constants> calibration_data_;
Maxwell Hendersonfebee252023-01-28 16:53:52 -080075 const frc971::vision::calibration::CameraCalibration *calibration_;
76 cv::Mat intrinsics_;
milind-uf2a4e322023-02-01 19:33:10 -080077 cv::Mat projection_matrix_;
James Kuszmaul258e4ee2023-02-23 14:22:30 -080078 std::optional<cv::Mat> extrinsics_;
milind-uf2a4e322023-02-01 19:33:10 -080079 cv::Mat dist_coeffs_;
milind-uf5b3b4b2023-02-26 14:50:38 -080080 cv::Size image_size_;
Maxwell Hendersonfebee252023-01-28 16:53:52 -080081
82 aos::Ftrace ftrace_;
83
84 frc971::vision::ImageCallback image_callback_;
85 aos::Sender<frc971::vision::TargetMap> target_map_sender_;
milind-u7aa29e22023-02-23 20:22:01 -080086 aos::Sender<foxglove::ImageAnnotations> image_annotations_sender_;
milind-u99b1a762023-03-12 16:48:32 -070087 size_t rejections_;
Jim Ostrowski49be8232023-03-23 01:00:14 -070088 frc971::vision::VisualizeRobot vis_robot_;
Maxwell Hendersonfebee252023-01-28 16:53:52 -080089};
90
91} // namespace vision
92} // namespace y2023