Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 1 | #include "y2023/vision/aprilrobotics.h" |
| 2 | |
James Kuszmaul | d67f6d2 | 2023-02-05 17:37:25 -0800 | [diff] [blame] | 3 | #include "y2023/vision/vision_util.h" |
| 4 | |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 5 | DEFINE_bool( |
| 6 | debug, false, |
| 7 | "If true, dump a ton of debug and crash on the first valid detection."); |
| 8 | |
milind-u | f2a4e32 | 2023-02-01 19:33:10 -0800 | [diff] [blame] | 9 | DEFINE_double(min_decision_margin, 30.0, |
| 10 | "Minimum decision margin (confidence) for an apriltag detection"); |
| 11 | |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 12 | namespace y2023 { |
| 13 | namespace vision { |
| 14 | |
Austin Schuh | d266793 | 2023-02-04 16:22:39 -0800 | [diff] [blame] | 15 | namespace chrono = std::chrono; |
| 16 | |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 17 | AprilRoboticsDetector::AprilRoboticsDetector(aos::EventLoop *event_loop, |
| 18 | std::string_view channel_name) |
James Kuszmaul | d67f6d2 | 2023-02-05 17:37:25 -0800 | [diff] [blame] | 19 | : calibration_data_(event_loop), |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 20 | ftrace_(), |
Austin Schuh | d266793 | 2023-02-04 16:22:39 -0800 | [diff] [blame] | 21 | image_callback_( |
| 22 | event_loop, channel_name, |
| 23 | [&](cv::Mat image_color_mat, |
| 24 | const aos::monotonic_clock::time_point eof) { |
| 25 | HandleImage(image_color_mat, eof); |
| 26 | }, |
| 27 | chrono::milliseconds(5)), |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 28 | target_map_sender_( |
Yash Chainani | 728ae22 | 2023-02-04 19:48:12 -0800 | [diff] [blame] | 29 | event_loop->MakeSender<frc971::vision::TargetMap>("/camera")), |
milind-u | 7aa29e2 | 2023-02-23 20:22:01 -0800 | [diff] [blame^] | 30 | image_annotations_sender_( |
| 31 | event_loop->MakeSender<foxglove::ImageAnnotations>("/camera")) { |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 32 | tag_family_ = tag16h5_create(); |
| 33 | tag_detector_ = apriltag_detector_create(); |
| 34 | |
| 35 | apriltag_detector_add_family_bits(tag_detector_, tag_family_, 1); |
| 36 | tag_detector_->nthreads = 6; |
| 37 | tag_detector_->wp = workerpool_create(tag_detector_->nthreads); |
| 38 | tag_detector_->qtp.min_white_black_diff = 5; |
| 39 | tag_detector_->debug = FLAGS_debug; |
| 40 | |
| 41 | std::string hostname = aos::network::GetHostname(); |
| 42 | |
| 43 | // Check team string is valid |
James Kuszmaul | d67f6d2 | 2023-02-05 17:37:25 -0800 | [diff] [blame] | 44 | calibration_ = FindCameraCalibration( |
| 45 | calibration_data_.constants(), event_loop->node()->name()->string_view()); |
milind-u | f2a4e32 | 2023-02-01 19:33:10 -0800 | [diff] [blame] | 46 | |
| 47 | extrinsics_ = CameraExtrinsics(calibration_); |
| 48 | |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 49 | intrinsics_ = CameraIntrinsics(calibration_); |
milind-u | f2a4e32 | 2023-02-01 19:33:10 -0800 | [diff] [blame] | 50 | // Create an undistort projection matrix using the intrinsics |
| 51 | projection_matrix_ = cv::Mat::zeros(3, 4, CV_64F); |
| 52 | intrinsics_.rowRange(0, 3).colRange(0, 3).copyTo( |
| 53 | projection_matrix_.rowRange(0, 3).colRange(0, 3)); |
| 54 | |
| 55 | dist_coeffs_ = CameraDistCoeffs(calibration_); |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 56 | |
| 57 | image_callback_.set_format(frc971::vision::ImageCallback::Format::GRAYSCALE); |
| 58 | } |
| 59 | |
| 60 | AprilRoboticsDetector::~AprilRoboticsDetector() { |
| 61 | apriltag_detector_destroy(tag_detector_); |
| 62 | free(tag_family_); |
| 63 | } |
| 64 | |
| 65 | void AprilRoboticsDetector::SetWorkerpoolAffinities() { |
| 66 | for (int i = 0; i < tag_detector_->wp->nthreads; i++) { |
| 67 | cpu_set_t affinity; |
| 68 | CPU_ZERO(&affinity); |
| 69 | CPU_SET(i, &affinity); |
| 70 | pthread_setaffinity_np(tag_detector_->wp->threads[i], sizeof(affinity), |
| 71 | &affinity); |
| 72 | struct sched_param param; |
| 73 | param.sched_priority = 20; |
| 74 | int res = pthread_setschedparam(tag_detector_->wp->threads[i], SCHED_FIFO, |
| 75 | ¶m); |
| 76 | PCHECK(res == 0) << "Failed to set priority of threadpool threads"; |
| 77 | } |
| 78 | } |
| 79 | |
milind-u | f2a4e32 | 2023-02-01 19:33:10 -0800 | [diff] [blame] | 80 | void AprilRoboticsDetector::HandleImage(cv::Mat image_grayscale, |
milind-u | 09fb125 | 2023-01-28 19:21:41 -0800 | [diff] [blame] | 81 | aos::monotonic_clock::time_point eof) { |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 82 | std::vector<std::pair<apriltag_detection_t, apriltag_pose_t>> detections = |
milind-u | 7aa29e2 | 2023-02-23 20:22:01 -0800 | [diff] [blame^] | 83 | DetectTags(image_grayscale, eof); |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 84 | |
| 85 | auto builder = target_map_sender_.MakeBuilder(); |
| 86 | std::vector<flatbuffers::Offset<frc971::vision::TargetPoseFbs>> target_poses; |
| 87 | for (const auto &[detection, pose] : detections) { |
| 88 | target_poses.emplace_back( |
| 89 | BuildTargetPose(pose, detection.id, builder.fbb())); |
| 90 | } |
| 91 | const auto target_poses_offset = builder.fbb()->CreateVector(target_poses); |
| 92 | auto target_map_builder = builder.MakeBuilder<frc971::vision::TargetMap>(); |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 93 | target_map_builder.add_target_poses(target_poses_offset); |
milind-u | 09fb125 | 2023-01-28 19:21:41 -0800 | [diff] [blame] | 94 | target_map_builder.add_monotonic_timestamp_ns(eof.time_since_epoch().count()); |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 95 | builder.CheckOk(builder.Send(target_map_builder.Finish())); |
| 96 | } |
| 97 | |
| 98 | flatbuffers::Offset<frc971::vision::TargetPoseFbs> |
| 99 | AprilRoboticsDetector::BuildTargetPose( |
| 100 | const apriltag_pose_t &pose, |
| 101 | frc971::vision::TargetMapper::TargetId target_id, |
| 102 | flatbuffers::FlatBufferBuilder *fbb) { |
| 103 | const auto T = |
| 104 | Eigen::Translation3d(pose.t->data[0], pose.t->data[1], pose.t->data[2]); |
milind-u | 3f5f83c | 2023-01-29 15:23:51 -0800 | [diff] [blame] | 105 | const auto position_offset = |
| 106 | frc971::vision::CreatePosition(*fbb, T.x(), T.y(), T.z()); |
| 107 | |
| 108 | const auto orientation = Eigen::Quaterniond(Eigen::Matrix3d(pose.R->data)); |
| 109 | const auto orientation_offset = frc971::vision::CreateQuaternion( |
| 110 | *fbb, orientation.w(), orientation.x(), orientation.y(), orientation.z()); |
| 111 | |
| 112 | return frc971::vision::CreateTargetPoseFbs(*fbb, target_id, position_offset, |
| 113 | orientation_offset); |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 114 | } |
| 115 | |
milind-u | f2a4e32 | 2023-02-01 19:33:10 -0800 | [diff] [blame] | 116 | void AprilRoboticsDetector::UndistortDetection( |
| 117 | apriltag_detection_t *det) const { |
| 118 | // 4 corners |
| 119 | constexpr size_t kRows = 4; |
| 120 | // 2d points |
| 121 | constexpr size_t kCols = 2; |
| 122 | |
| 123 | cv::Mat distorted_points(kRows, kCols, CV_64F, det->p); |
| 124 | cv::Mat undistorted_points = cv::Mat::zeros(kRows, kCols, CV_64F); |
| 125 | |
| 126 | // Undistort the april tag points |
| 127 | cv::undistortPoints(distorted_points, undistorted_points, intrinsics_, |
| 128 | dist_coeffs_, cv::noArray(), projection_matrix_); |
| 129 | |
| 130 | // Copy the undistorted points into det |
| 131 | for (size_t i = 0; i < kRows; i++) { |
| 132 | for (size_t j = 0; j < kCols; j++) { |
| 133 | det->p[i][j] = undistorted_points.at<double>(i, j); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 138 | std::vector<std::pair<apriltag_detection_t, apriltag_pose_t>> |
milind-u | 7aa29e2 | 2023-02-23 20:22:01 -0800 | [diff] [blame^] | 139 | AprilRoboticsDetector::DetectTags(cv::Mat image, |
| 140 | aos::monotonic_clock::time_point eof) { |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 141 | const aos::monotonic_clock::time_point start_time = |
| 142 | aos::monotonic_clock::now(); |
| 143 | |
| 144 | image_u8_t im = { |
milind-u | 09fb125 | 2023-01-28 19:21:41 -0800 | [diff] [blame] | 145 | .width = image.cols, |
| 146 | .height = image.rows, |
| 147 | .stride = image.cols, |
| 148 | .buf = image.data, |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | ftrace_.FormatMessage("Starting detect\n"); |
| 152 | zarray_t *detections = apriltag_detector_detect(tag_detector_, &im); |
| 153 | ftrace_.FormatMessage("Done detecting\n"); |
| 154 | |
| 155 | std::vector<std::pair<apriltag_detection_t, apriltag_pose_t>> results; |
| 156 | |
milind-u | 7aa29e2 | 2023-02-23 20:22:01 -0800 | [diff] [blame^] | 157 | std::vector<std::vector<cv::Point2f>> corners_vector; |
Yash Chainani | 728ae22 | 2023-02-04 19:48:12 -0800 | [diff] [blame] | 158 | |
milind-u | 7aa29e2 | 2023-02-23 20:22:01 -0800 | [diff] [blame^] | 159 | auto builder = image_annotations_sender_.MakeBuilder(); |
Yash Chainani | 728ae22 | 2023-02-04 19:48:12 -0800 | [diff] [blame] | 160 | |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 161 | for (int i = 0; i < zarray_size(detections); i++) { |
| 162 | apriltag_detection_t *det; |
| 163 | zarray_get(detections, i, &det); |
| 164 | |
milind-u | f2a4e32 | 2023-02-01 19:33:10 -0800 | [diff] [blame] | 165 | if (det->decision_margin > FLAGS_min_decision_margin) { |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 166 | VLOG(1) << "Found tag number " << det->id << " hamming: " << det->hamming |
| 167 | << " margin: " << det->decision_margin; |
| 168 | |
| 169 | const aos::monotonic_clock::time_point before_pose_estimation = |
| 170 | aos::monotonic_clock::now(); |
| 171 | // First create an apriltag_detection_info_t struct using your known |
| 172 | // parameters. |
| 173 | apriltag_detection_info_t info; |
| 174 | info.det = det; |
| 175 | info.tagsize = 0.1524; |
milind-u | f2a4e32 | 2023-02-01 19:33:10 -0800 | [diff] [blame] | 176 | |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 177 | info.fx = intrinsics_.at<double>(0, 0); |
| 178 | info.fy = intrinsics_.at<double>(1, 1); |
| 179 | info.cx = intrinsics_.at<double>(0, 2); |
| 180 | info.cy = intrinsics_.at<double>(1, 2); |
| 181 | |
| 182 | apriltag_pose_t pose; |
| 183 | double err = estimate_tag_pose(&info, &pose); |
| 184 | |
milind-u | f2a4e32 | 2023-02-01 19:33:10 -0800 | [diff] [blame] | 185 | UndistortDetection(det); |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 186 | VLOG(1) << "err: " << err; |
| 187 | |
| 188 | results.emplace_back(*det, pose); |
| 189 | |
| 190 | const aos::monotonic_clock::time_point after_pose_estimation = |
| 191 | aos::monotonic_clock::now(); |
| 192 | |
| 193 | VLOG(1) << "Took " |
Austin Schuh | d266793 | 2023-02-04 16:22:39 -0800 | [diff] [blame] | 194 | << chrono::duration<double>(after_pose_estimation - |
| 195 | before_pose_estimation) |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 196 | .count() |
| 197 | << " seconds for pose estimation"; |
Yash Chainani | 728ae22 | 2023-02-04 19:48:12 -0800 | [diff] [blame] | 198 | |
milind-u | 7aa29e2 | 2023-02-23 20:22:01 -0800 | [diff] [blame^] | 199 | std::vector<cv::Point2f> corner_points; |
Yash Chainani | 728ae22 | 2023-02-04 19:48:12 -0800 | [diff] [blame] | 200 | corner_points.emplace_back(det->p[0][0], det->p[0][1]); |
| 201 | corner_points.emplace_back(det->p[1][0], det->p[1][1]); |
| 202 | corner_points.emplace_back(det->p[2][0], det->p[2][1]); |
| 203 | corner_points.emplace_back(det->p[3][0], det->p[3][1]); |
| 204 | |
milind-u | 7aa29e2 | 2023-02-23 20:22:01 -0800 | [diff] [blame^] | 205 | corners_vector.emplace_back(corner_points); |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
milind-u | 7aa29e2 | 2023-02-23 20:22:01 -0800 | [diff] [blame^] | 209 | const auto annotations_offset = |
| 210 | frc971::vision::BuildAnnotations(eof, corners_vector, 5.0, builder.fbb()); |
| 211 | builder.CheckOk(builder.Send(annotations_offset)); |
Yash Chainani | 728ae22 | 2023-02-04 19:48:12 -0800 | [diff] [blame] | 212 | |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 213 | apriltag_detections_destroy(detections); |
| 214 | |
| 215 | const aos::monotonic_clock::time_point end_time = aos::monotonic_clock::now(); |
| 216 | |
milind-u | f3ab8ba | 2023-02-04 17:56:16 -0800 | [diff] [blame] | 217 | if (FLAGS_debug) { |
| 218 | timeprofile_display(tag_detector_->tp); |
| 219 | } |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 220 | |
Austin Schuh | d266793 | 2023-02-04 16:22:39 -0800 | [diff] [blame] | 221 | VLOG(1) << "Took " << chrono::duration<double>(end_time - start_time).count() |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 222 | << " seconds to detect overall"; |
| 223 | |
| 224 | return results; |
| 225 | } |
| 226 | |
Maxwell Henderson | febee25 | 2023-01-28 16:53:52 -0800 | [diff] [blame] | 227 | } // namespace vision |
| 228 | } // namespace y2023 |