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