Skip to content

Commit

Permalink
[wpical] Add support for new Apriltags (ID 16-22) (wpilibsuite#7619)
Browse files Browse the repository at this point in the history
  • Loading branch information
ElliotScher authored Jan 1, 2025
1 parent 468a3c6 commit ce60bd5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
35 changes: 10 additions & 25 deletions wpical/src/main/native/cpp/WPIcal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ static void DisplayGui() {
static int focusedTag = 1;
static int referenceTag = 1;

static int maxFRCTag = 22;

static Fieldmap currentCalibrationMap;
static Fieldmap currentReferenceMap;

Expand Down Expand Up @@ -187,17 +189,10 @@ static void DisplayGui() {
ImGui::SetNextItemWidth(ImGui::GetFontSize() * 12);
ImGui::InputInt("Pinned Tag", &pinnedTag);

if (pinnedTag < 1) {
pinnedTag = 1;
} else if (pinnedTag > 16) {
pinnedTag = 16;
}

// calibrate button
if (ImGui::Button("Calibrate!!!")) {
if (!selected_field_calibration_directory.empty() &&
!selected_camera_intrinsics.empty() && !selected_field_map.empty() &&
pinnedTag > 0 && pinnedTag <= 16) {
!selected_camera_intrinsics.empty() && !selected_field_map.empty()) {
download_directory_selector =
std::make_unique<pfd::select_folder>("Select Download Folder", "");
if (download_directory_selector) {
Expand All @@ -216,7 +211,7 @@ static void DisplayGui() {
showDebug);

if (calibrationOutput == 1) {
ImGui::OpenPopup("Fmap Conversion failed");
ImGui::OpenPopup("Field Calibration Error");
} else if (calibrationOutput == 0) {
std::ifstream caljsonpath(calibration_json_path);
try {
Expand All @@ -227,7 +222,7 @@ static void DisplayGui() {
ImGui::SetNextWindowSize(ImVec2(600, 400), ImGuiCond_Always);
ImGui::OpenPopup("Visualize Calibration");
} catch (...) {
ImGui::OpenPopup("Field Calibration Error");
ImGui::OpenPopup("Fmap Conversion Error");
}
}
}
Expand All @@ -241,8 +236,10 @@ static void DisplayGui() {
ImGui::TextWrapped(
"Some inputs are empty! please enter your camera calibration video, "
"field map, and field calibration directory");
} else if (!(pinnedTag > 0 && pinnedTag <= 16)) {
ImGui::TextWrapped("Make sure the pinned tag is a valid april tag (1-16)");
} else if (!(pinnedTag > 0 && pinnedTag <= maxFRCTag)) {
ImGui::TextWrapped(
"The pinned tag is not within the normal range for FRC fields (1-22), "
"If you proceed, You may experience a bad calibration.");
} else {
ImGui::TextWrapped("Calibration Ready");
}
Expand All @@ -269,7 +266,7 @@ static void DisplayGui() {
ImGui::EndPopup();
}

if (ImGui::BeginPopupModal("Fmap Conversion failed", NULL,
if (ImGui::BeginPopupModal("Fmap Conversion Error", NULL,
ImGuiWindowFlags_AlwaysAutoResize)) {
ImGui::TextWrapped(
"Fmap conversion failed - you can still use the calibration output on "
Expand Down Expand Up @@ -480,18 +477,6 @@ static void DisplayGui() {
ImGui::SetNextItemWidth(ImGui::GetFontSize() * 12);
ImGui::InputInt("Reference Tag", &referenceTag);

if (focusedTag < 1) {
focusedTag = 1;
} else if (focusedTag > 16) {
focusedTag = 16;
}

if (referenceTag < 1) {
referenceTag = 1;
} else if (referenceTag > 16) {
referenceTag = 16;
}

if (!calibration_json_path.empty() && !selected_field_map.empty()) {
std::ifstream calJson(calibration_json_path);
std::ifstream refJson(selected_field_map);
Expand Down
13 changes: 13 additions & 0 deletions wpical/src/main/native/cpp/fieldcalibration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,19 @@ int fieldcalibration::calibrate(std::string input_dir_path,
return 1;
}

bool pinned_tag_found = false;
// Check if pinned tag is in ideal map
for (const auto& [tag_id, tag_json] : ideal_map) {
if (tag_id == pinned_tag_id) {
pinned_tag_found = true;
break;
}
}

if (!pinned_tag_found) {
return 1;
}

// Apriltag detector
apriltag_detector_t* tag_detector = apriltag_detector_create();
tag_detector->nthreads = 8;
Expand Down
16 changes: 16 additions & 0 deletions wpical/src/test/native/cpp/test_calibrate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,19 @@ TEST(Field_CalibrationTest, Atypical_Bad_Input_Directory) {
projectRootPath + "/2024-crescendo.json", 3, false);
EXPECT_EQ(ret, 1);
}

TEST(Field_CalibrationTest, Atypical_Bad_Pinned_Tag) {
int ret = fieldcalibration::calibrate(
projectRootPath + videoLocation, calSavePath,
calSavePath + "/cameracalibration.json",
projectRootPath + "/2024-crescendo.json", 42, false);
EXPECT_EQ(ret, 1);
}

TEST(Field_CalibrationTest, Atypical_Bad_Pinned_Tag_Negative) {
int ret = fieldcalibration::calibrate(
projectRootPath + videoLocation, calSavePath,
calSavePath + "/cameracalibration.json",
projectRootPath + "/2024-crescendo.json", -1, false);
EXPECT_EQ(ret, 1);
}

0 comments on commit ce60bd5

Please sign in to comment.