blob: bf9265b8e59e80b76e7c995f66e2b5fe60706791 [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
Jim Ostrowski5e2c5e62023-02-26 12:52:56 -080043 // Helper function to store detection points in vector of Point2f's
44 std::vector<cv::Point2f> MakeCornerVector(const apriltag_detection_t *det);
45
milind-ufc8ab702023-02-26 14:14:39 -080046 std::vector<Detection> DetectTags(cv::Mat image,
47 aos::monotonic_clock::time_point eof);
Maxwell Hendersonfebee252023-01-28 16:53:52 -080048
James Kuszmaul258e4ee2023-02-23 14:22:30 -080049 const std::optional<cv::Mat> extrinsics() const { return extrinsics_; }
milind-uf2a4e322023-02-01 19:33:10 -080050 const cv::Mat intrinsics() const { return intrinsics_; }
51 const cv::Mat dist_coeffs() const { return dist_coeffs_; }
52
Maxwell Hendersonfebee252023-01-28 16:53:52 -080053 private:
milind-u09fb1252023-01-28 19:21:41 -080054 void HandleImage(cv::Mat image, aos::monotonic_clock::time_point eof);
Maxwell Hendersonfebee252023-01-28 16:53:52 -080055
56 flatbuffers::Offset<frc971::vision::TargetPoseFbs> BuildTargetPose(
milind-ufc8ab702023-02-26 14:14:39 -080057 const Detection &detection, flatbuffers::FlatBufferBuilder *fbb);
Maxwell Hendersonfebee252023-01-28 16:53:52 -080058
milind-uf5b3b4b2023-02-26 14:50:38 -080059 // Computes the distortion effect on this detection taking the scaled average
60 // delta between orig_corners (distorted corners) and corners (undistorted
61 // corners)
62 double ComputeDistortionFactor(const std::vector<cv::Point2f> &orig_corners,
63 const std::vector<cv::Point2f> &corners);
64
Maxwell Hendersonfebee252023-01-28 16:53:52 -080065 apriltag_family_t *tag_family_;
66 apriltag_detector_t *tag_detector_;
67
James Kuszmauld67f6d22023-02-05 17:37:25 -080068 const frc971::constants::ConstantsFetcher<Constants> calibration_data_;
Maxwell Hendersonfebee252023-01-28 16:53:52 -080069 const frc971::vision::calibration::CameraCalibration *calibration_;
70 cv::Mat intrinsics_;
milind-uf2a4e322023-02-01 19:33:10 -080071 cv::Mat projection_matrix_;
James Kuszmaul258e4ee2023-02-23 14:22:30 -080072 std::optional<cv::Mat> extrinsics_;
milind-uf2a4e322023-02-01 19:33:10 -080073 cv::Mat dist_coeffs_;
milind-uf5b3b4b2023-02-26 14:50:38 -080074 cv::Size image_size_;
Maxwell Hendersonfebee252023-01-28 16:53:52 -080075
76 aos::Ftrace ftrace_;
77
78 frc971::vision::ImageCallback image_callback_;
79 aos::Sender<frc971::vision::TargetMap> target_map_sender_;
milind-u7aa29e22023-02-23 20:22:01 -080080 aos::Sender<foxglove::ImageAnnotations> image_annotations_sender_;
Maxwell Hendersonfebee252023-01-28 16:53:52 -080081};
82
83} // namespace vision
84} // namespace y2023