blob: fd371c780473885cf5cfc15ccf5b36ea1a1fc844 [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
Maxwell Hendersonfebee252023-01-28 16:53:52 -080034 AprilRoboticsDetector(aos::EventLoop *event_loop,
35 std::string_view channel_name);
Maxwell Hendersonfebee252023-01-28 16:53:52 -080036 ~AprilRoboticsDetector();
37
38 void SetWorkerpoolAffinities();
39
milind-uf2a4e322023-02-01 19:33:10 -080040 // Undistorts the april tag corners using the camera calibration
41 void UndistortDetection(apriltag_detection_t *det) const;
42
milind-ufc8ab702023-02-26 14:14:39 -080043 std::vector<Detection> DetectTags(cv::Mat image,
44 aos::monotonic_clock::time_point eof);
Maxwell Hendersonfebee252023-01-28 16:53:52 -080045
James Kuszmaul258e4ee2023-02-23 14:22:30 -080046 const std::optional<cv::Mat> extrinsics() const { return extrinsics_; }
milind-uf2a4e322023-02-01 19:33:10 -080047 const cv::Mat intrinsics() const { return intrinsics_; }
48 const cv::Mat dist_coeffs() const { return dist_coeffs_; }
49
Maxwell Hendersonfebee252023-01-28 16:53:52 -080050 private:
milind-u09fb1252023-01-28 19:21:41 -080051 void HandleImage(cv::Mat image, aos::monotonic_clock::time_point eof);
Maxwell Hendersonfebee252023-01-28 16:53:52 -080052
53 flatbuffers::Offset<frc971::vision::TargetPoseFbs> BuildTargetPose(
milind-ufc8ab702023-02-26 14:14:39 -080054 const Detection &detection, flatbuffers::FlatBufferBuilder *fbb);
Maxwell Hendersonfebee252023-01-28 16:53:52 -080055
milind-uf5b3b4b2023-02-26 14:50:38 -080056 // Computes the distortion effect on this detection taking the scaled average
57 // delta between orig_corners (distorted corners) and corners (undistorted
58 // corners)
59 double ComputeDistortionFactor(const std::vector<cv::Point2f> &orig_corners,
60 const std::vector<cv::Point2f> &corners);
61
Maxwell Hendersonfebee252023-01-28 16:53:52 -080062 apriltag_family_t *tag_family_;
63 apriltag_detector_t *tag_detector_;
64
James Kuszmauld67f6d22023-02-05 17:37:25 -080065 const frc971::constants::ConstantsFetcher<Constants> calibration_data_;
Maxwell Hendersonfebee252023-01-28 16:53:52 -080066 const frc971::vision::calibration::CameraCalibration *calibration_;
67 cv::Mat intrinsics_;
milind-uf2a4e322023-02-01 19:33:10 -080068 cv::Mat projection_matrix_;
James Kuszmaul258e4ee2023-02-23 14:22:30 -080069 std::optional<cv::Mat> extrinsics_;
milind-uf2a4e322023-02-01 19:33:10 -080070 cv::Mat dist_coeffs_;
milind-uf5b3b4b2023-02-26 14:50:38 -080071 cv::Size image_size_;
Maxwell Hendersonfebee252023-01-28 16:53:52 -080072
73 aos::Ftrace ftrace_;
74
75 frc971::vision::ImageCallback image_callback_;
76 aos::Sender<frc971::vision::TargetMap> target_map_sender_;
milind-u7aa29e22023-02-23 20:22:01 -080077 aos::Sender<foxglove::ImageAnnotations> image_annotations_sender_;
Maxwell Hendersonfebee252023-01-28 16:53:52 -080078};
79
80} // namespace vision
81} // namespace y2023