Skip to content

Commit

Permalink
finished remaining vision pose estimator tests and trying to fix phot…
Browse files Browse the repository at this point in the history
…on vision test errors
  • Loading branch information
PGgit08 committed Feb 19, 2025
1 parent 6d1a18b commit 2712820
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 21 deletions.
6 changes: 6 additions & 0 deletions src/main/java/frc/lib/FaultLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,18 @@ public static interface FaultReporter {
private static FaultsTable activeAlerts;
private static FaultsTable totalAlerts;

private static boolean _hasBeenSetup = false;

/** Must be called to setup the fault logger. */
public static void setup(NetworkTableInstance ntInst) {
if (_hasBeenSetup) return;

var base = ntInst.getTable("Faults");

activeAlerts = new FaultsTable(base, "Active Faults");
totalAlerts = new FaultsTable(base, "Total Faults");

_hasBeenSetup = true;
}

/** Must be called to setup the fault logger. */
Expand Down
23 changes: 10 additions & 13 deletions src/main/java/frc/lib/UnitTestingUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,23 @@ public static void setupTests() {
DogLog.setEnabled(false); // disabling doglog since it logs to the default nt instance

FaultLogger.setup(_ntInst);

FaultLogger.clear();
FaultLogger.unregisterAll();
FaultLogger.enableConsole(false);

// delay 100 ms to wait for CTRE device enable
Timer.delay(0.100);
}

/** Resets the CommandScheduler and the test NT instance. */
public static void reset() {
_ntInst.close();
_ntInst = null;

CommandScheduler.getInstance().unregisterAllSubsystems();
CommandScheduler.getInstance().cancelAll();
}

/**
* Resets CommandScheduler and NT and closes all closeables. Please call in an @AfterEach method!
*
* @param closeables All closeables that need to be closed.
*/
public static void reset(AutoCloseable... closeables) {
reset();
_ntInst.close();
_ntInst = null;

CommandScheduler.getInstance().unregisterAllSubsystems();
CommandScheduler.getInstance().cancelAll();

for (AutoCloseable closeable : closeables) {
try {
Expand All @@ -77,6 +69,11 @@ public static void reset(AutoCloseable... closeables) {
e.printStackTrace();
}
}

FaultLogger.clear();
FaultLogger.unregisterAll();

HAL.shutdown();
}

/**
Expand Down
42 changes: 42 additions & 0 deletions src/test/java/frc/robot/PhotonTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package frc.robot;

import static frc.lib.UnitTestingUtil.getNtInst;
import static frc.lib.UnitTestingUtil.reset;
import static frc.lib.UnitTestingUtil.setupTests;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.photonvision.PhotonCamera;
import org.photonvision.simulation.PhotonCameraSim;

// WHY IS THIS THROWING A PHOTON ERROR?
@Disabled
public class PhotonTest {
private PhotonCamera _cam;
private PhotonCameraSim _sim;

@BeforeEach
public void setup() {
setupTests();

_cam = new PhotonCamera(getNtInst(), "Cam");
_sim = new PhotonCameraSim(_cam);
}

@AfterEach
public void close() {
reset(_cam, _sim);
}

@Test
public void testOne() {
System.out.println("Test One");
}

@Test
public void testTwo() {
System.out.println("Test Two");
}
}
5 changes: 4 additions & 1 deletion src/test/java/frc/robot/SwerveTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import frc.robot.subsystems.Swerve;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

public class SwerveTest {
private Swerve _swerve;
Expand All @@ -25,7 +27,8 @@ public void close() {
reset(_swerve);
}

// @Test
@Disabled
@Test
public void driveTo() {
// TODO: ts don't work
var goal = new Pose2d(0.5, 0, Rotation2d.fromDegrees(5));
Expand Down
77 changes: 71 additions & 6 deletions src/test/java/frc/robot/VisionPoseEstimatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ public void setup() {
VisionPoseEstimator.buildFromConstants(
testCam, getNtInst(), _fieldLayout, this::dummyGyroHeading);

// specific corner noise for these tests
_testCam.getCameraSim().prop.setCalibError(0.01, 0.001);

_visionSystemSim = new VisionSystemSim("");
_visionSystemSim.addCamera(_testCam.getCameraSim(), _testCam.robotToCam);
}
Expand Down Expand Up @@ -121,7 +124,7 @@ public void singleTagResults() {
@Test
public void singleTagEstimate() {
_visionSystemSim.addVisionTargets(
new VisionTargetSim(_fieldLayout.getTagPose(1).get(), TargetModel.kAprilTag36h11, 1));
new VisionTargetSim(_fieldLayout.getTagPose(2).get(), TargetModel.kAprilTag36h11, 2));

_visionSystemSim.update(Pose2d.kZero);

Expand All @@ -132,6 +135,9 @@ public void singleTagEstimate() {

var estimate = estimates.get(0);

// this test sometimes fails for some unknown reason, so this is here in case
System.out.println(estimate);

assert estimate.isValid();

// pose validity
Expand All @@ -142,13 +148,13 @@ public void singleTagEstimate() {
assertEquals(0, estimate.pose().getRotation().getY(), 1e-2);
assertEquals(0, estimate.pose().getRotation().getZ(), 1e-2);

// should see only ID 1
assertArrayEquals(new int[] {1}, estimate.detectedTags());
// should see only ID 2
assertArrayEquals(new int[] {2}, estimate.detectedTags());

// distance validity
assertEquals(
_fieldLayout
.getTagPose(1)
.getTagPose(2)
.get()
.getTranslation()
.getDistance(Pose3d.kZero.getTranslation()),
Expand Down Expand Up @@ -239,7 +245,37 @@ public void boundsFilter() {

@Test
public void ambiguityFilter() {
// TODO
_visionSystemSim.addVisionTargets(
new VisionTargetSim(_fieldLayout.getTagPose(5).get(), TargetModel.kAprilTag36h11, 5));

_visionSystemSim.update(Pose2d.kZero);

_testCam.update();

var estimates = _testCam.getNewEstimates(); // 1 estimate
assertEquals(1, estimates.size());

var estimate = estimates.get(0);

assertFalse(estimate.isValid());

assert estimate.ambiguity() > _testCam.ambiguityThreshold;

// should see only ID 5
assertArrayEquals(new int[] {5}, estimate.detectedTags());

// distance validity (is still accurate in ambigious scenarios)
assertEquals(
_fieldLayout
.getTagPose(5)
.get()
.getTranslation()
.getDistance(Pose3d.kZero.getTranslation()),
estimate.avgTagDistance(),
1e-2);

// std devs validity
assertArrayEquals(new double[] {-1, -1, -1}, estimate.stdDevs());
}

@Test
Expand Down Expand Up @@ -385,6 +421,35 @@ public void estimateSort() {

@Test
public void disambiguation() {
// TODO
_visionSystemSim.addVisionTargets(
new VisionTargetSim(_fieldLayout.getTagPose(2).get(), TargetModel.kAprilTag36h11, 2));

_visionSystemSim.update(Pose2d.kZero);

_testCam.update();

var estimates = _testCam.getNewEstimates(); // 1 estimate
assertEquals(1, estimates.size());

var estimate = estimates.get(0);

// this test sometimes fails for some unknown reason, so this is here in case
System.out.println(estimate);

assert estimate.isValid();

// everything alr checked in singleTagEstimate, so just checking disambiguation here ⤵

var closerError =
estimate.pose().getRotation().toRotation2d().minus(dummyGyroHeading(estimate.timestamp()));
var furtherError =
estimate
.altPose()
.getRotation()
.toRotation2d()
.minus(dummyGyroHeading(estimate.timestamp()));

// pose's heading should be closer to the gyro than altPose's heading
assert Math.abs(closerError.getRadians()) < Math.abs(furtherError.getRadians());
}
}
5 changes: 4 additions & 1 deletion src/test/java/frc/robot/WheelRadiusCharacterizationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
import frc.robot.subsystems.Swerve;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

@Disabled
public class WheelRadiusCharacterizationTest {
// chassis omega during characterization (TODO: this would be tuneable later)
private final double velOmega = 1;
Expand All @@ -31,7 +34,7 @@ public void close() {
reset(_swerve);
}

// @Test
@Test
public void wheelRadiusCharacterization() {
// run enough ticks to complete 4pi radians at the given characterization omega
run(_characterization, (int) ((Math.PI * 4 / velOmega) / TICK_RATE.in(Seconds)));
Expand Down

0 comments on commit 2712820

Please sign in to comment.