Skip to content

Commit

Permalink
[apriltag] AprilTagFieldLayout: Improve API shape for loading builtin…
Browse files Browse the repository at this point in the history
… JSONs (wpilibsuite#4949)
  • Loading branch information
brennenputh authored and Starlight220 committed Mar 2, 2023
1 parent 3958306 commit b033977
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,10 @@ public void serialize(Path path) throws IOException {
}

/**
* Deserializes a field layout from a resource within a jar file.
* Deserializes a field layout from a resource within a internal jar file.
*
* <p>Users should use {@link AprilTagFields#loadAprilTagLayoutField()} to load official layouts
* and {@link #AprilTagFieldLayout(String)} for custom layouts.
*
* @param resourcePath The absolute path of the resource
* @return The deserialized layout
Expand Down
12 changes: 12 additions & 0 deletions apriltag/src/main/java/edu/wpi/first/apriltag/AprilTagFields.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package edu.wpi.first.apriltag;

import java.io.IOException;

public enum AprilTagFields {
k2022RapidReact("2022-rapidreact.json"),
k2023ChargedUp("2023-chargedup.json");
Expand All @@ -18,4 +20,14 @@ public enum AprilTagFields {
AprilTagFields(String resourceFile) {
m_resourceFile = kBaseResourceDir + resourceFile;
}

/**
* Get a {@link AprilTagFieldLayout} from the resource JSON.
*
* @return AprilTagFieldLayout of the field
* @throws IOException If the layout does not exist
*/
public AprilTagFieldLayout loadAprilTagLayoutField() throws IOException {
return AprilTagFieldLayout.loadFromResource(m_resourceFile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@ class LoadConfigTest {
@ParameterizedTest
@EnumSource(AprilTagFields.class)
void testLoad(AprilTagFields field) {
AprilTagFieldLayout layout =
Assertions.assertDoesNotThrow(
() -> AprilTagFieldLayout.loadFromResource(field.m_resourceFile));
AprilTagFieldLayout layout = Assertions.assertDoesNotThrow(field::loadAprilTagLayoutField);
assertNotNull(layout);
}

@Test
void test2022RapidReact() throws IOException {
AprilTagFieldLayout layout =
AprilTagFieldLayout.loadFromResource(AprilTagFields.k2022RapidReact.m_resourceFile);
AprilTagFieldLayout layout = AprilTagFields.k2022RapidReact.loadAprilTagLayoutField();

// Blue Hangar Truss - Hub
Pose3d expectedPose =
Expand Down

0 comments on commit b033977

Please sign in to comment.