Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 1 | |
| 2 | #include <string> |
| 3 | |
| 4 | #include "aos/events/event_loop.h" |
| 5 | #include "aos/events/shm_event_loop.h" |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 6 | #include "aos/network/team_number.h" |
| 7 | #include "aos/realtime.h" |
| 8 | #include "frc971/vision/calibration_generated.h" |
| 9 | #include "frc971/vision/charuco_lib.h" |
| 10 | #include "frc971/vision/target_map_generated.h" |
| 11 | #include "frc971/vision/target_mapper.h" |
| 12 | #include "frc971/vision/vision_generated.h" |
| 13 | #include "opencv2/core/eigen.hpp" |
| 14 | #include "opencv2/imgproc.hpp" |
| 15 | #include "third_party/apriltag/apriltag.h" |
| 16 | #include "third_party/apriltag/apriltag_pose.h" |
| 17 | #include "third_party/apriltag/tag16h5.h" |
| 18 | #include "y2023/vision/calibration_data.h" |
| 19 | |
| 20 | DECLARE_int32(team_number); |
| 21 | |
| 22 | namespace y2023 { |
| 23 | namespace vision { |
| 24 | |
| 25 | class AprilRoboticsDetector { |
| 26 | public: |
| 27 | AprilRoboticsDetector(aos::EventLoop *event_loop, |
| 28 | std::string_view channel_name); |
| 29 | |
| 30 | ~AprilRoboticsDetector(); |
| 31 | |
| 32 | void SetWorkerpoolAffinities(); |
| 33 | |
| 34 | std::vector<std::pair<apriltag_detection_t, apriltag_pose_t>> DetectTags( |
| 35 | cv::Mat image); |
| 36 | |
| 37 | private: |
milind-u | 09fb125 | 2023-01-28 19:21:41 -0800 | [diff] [blame^] | 38 | void HandleImage(cv::Mat image, aos::monotonic_clock::time_point eof); |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 39 | |
| 40 | flatbuffers::Offset<frc971::vision::TargetPoseFbs> BuildTargetPose( |
| 41 | const apriltag_pose_t &pose, |
| 42 | frc971::vision::TargetMapper::TargetId target_id, |
| 43 | flatbuffers::FlatBufferBuilder *fbb); |
| 44 | |
| 45 | static const frc971::vision::calibration::CameraCalibration * |
| 46 | FindCameraCalibration( |
| 47 | const frc971::vision::calibration::CalibrationData *calibration_data, |
| 48 | std::string_view node_name) { |
| 49 | for (const frc971::vision::calibration::CameraCalibration *candidate : |
| 50 | *calibration_data->camera_calibrations()) { |
| 51 | if (candidate->node_name()->string_view() != node_name) { |
| 52 | continue; |
| 53 | } |
| 54 | if (candidate->team_number() != FLAGS_team_number) { |
| 55 | continue; |
| 56 | } |
| 57 | return candidate; |
| 58 | } |
| 59 | LOG(FATAL) << ": Failed to find camera calibration for " << node_name |
| 60 | << " on " << FLAGS_team_number; |
| 61 | } |
| 62 | |
| 63 | static cv::Mat CameraIntrinsics( |
| 64 | const frc971::vision::calibration::CameraCalibration |
| 65 | *camera_calibration) { |
| 66 | cv::Mat result(3, 3, CV_32F, |
| 67 | const_cast<void *>(static_cast<const void *>( |
| 68 | camera_calibration->intrinsics()->data()))); |
| 69 | result.convertTo(result, CV_64F); |
| 70 | CHECK_EQ(result.total(), camera_calibration->intrinsics()->size()); |
| 71 | |
| 72 | return result; |
| 73 | } |
| 74 | |
| 75 | static cv::Mat CameraDistCoeffs( |
| 76 | const frc971::vision::calibration::CameraCalibration |
| 77 | *camera_calibration) { |
| 78 | const cv::Mat result(5, 1, CV_32F, |
| 79 | const_cast<void *>(static_cast<const void *>( |
| 80 | camera_calibration->dist_coeffs()->data()))); |
| 81 | CHECK_EQ(result.total(), camera_calibration->dist_coeffs()->size()); |
| 82 | return result; |
| 83 | } |
| 84 | |
| 85 | apriltag_family_t *tag_family_; |
| 86 | apriltag_detector_t *tag_detector_; |
| 87 | |
| 88 | const aos::FlatbufferSpan<frc971::vision::calibration::CalibrationData> |
| 89 | calibration_data_; |
| 90 | const frc971::vision::calibration::CameraCalibration *calibration_; |
| 91 | cv::Mat intrinsics_; |
| 92 | cv::Mat camera_distortion_coeffs_; |
| 93 | |
| 94 | aos::Ftrace ftrace_; |
| 95 | |
| 96 | frc971::vision::ImageCallback image_callback_; |
| 97 | aos::Sender<frc971::vision::TargetMap> target_map_sender_; |
| 98 | }; |
| 99 | |
| 100 | } // namespace vision |
| 101 | } // namespace y2023 |