Difficulty Loading Apriltag Layouts File

We are trying to use PhotonVision to get a field relative position but we seem to be stuck on loading the apriltags layout. As far as I know we are not doing anything wrong but the code just throws an IOException saying it cannot read the file however it’s a file that comes with WPILib so it’s clearly there. I’ve completely ran out of ideas so I would gladly appreciate any help.

package frc.robot.subsystems;

import java.util.List;
import org.photonvision.PhotonCamera;
import org.photonvision.targeting.PhotonPipelineResult;
import org.photonvision.targeting.PhotonTrackedTarget;
import edu.wpi.first.apriltag.AprilTagFieldLayout;
import edu.wpi.first.apriltag.AprilTagFields;
import edu.wpi.first.math.geometry.Rotation3d;
import edu.wpi.first.math.geometry.Transform3d;
import edu.wpi.first.math.geometry.Translation3d;
import edu.wpi.first.wpilibj2.command.CommandBase;
import edu.wpi.first.wpilibj2.command.SubsystemBase;


public class VisionSubsystem extends SubsystemBase {

  //Telemetry
  public PhotonCamera lifecam = new PhotonCamera("lifecam");
  public PhotonPipelineResult pipelineResult = lifecam.getLatestResult();
  public boolean hasTargets = pipelineResult.hasTargets();
  public List<PhotonTrackedTarget> targets = pipelineResult.getTargets();
  public PhotonTrackedTarget target = pipelineResult.getBestTarget();

  // Cam position
  public Transform3d robotToCam = new Transform3d(new Translation3d(0.5, 0.0, 0.5), new Rotation3d(0,0,0));
  public AprilTagFieldLayout aprilTagFieldLayout = AprilTagFieldLayout.loadFromResource(AprilTagFields.k2023ChargedUp.m_resourceFile);

  public VisionSubsystem() {
  }

  public CommandBase exampleMethodCommand() {
    // Inline construction of command goes here.
    // Subsystem::RunOnce implicitly requires `this` subsystem.
    return runOnce(
        () -> {
          /* one-time action goes here */
        });
  }


  @Override
  public void periodic() {
    // This method will be called once per scheduler run
  }

  @Override
  public void simulationPeriodic() {
    // This method will be called once per scheduler run during simulation
  }
}

It looks right to me. It fails randomly for us when we try to run simulation, but the solution is always to just try it again.

Wrap the loading in a try/catch statement in your constructor, java hates explicit declaration issues.

1 Like

That worked, but now when I’m trying to pass everything I need to the PhotonVision field relative position estimator, I get this weird error

The method estimateFieldToRobotAprilTag(Transform3d, Pose3d, Transform3d) in the type PhotonUtils is not applicable for the arguments (Transform3d, Optional, Transform3d)Java(67108979)

However as it can be seen in the code, I am not trying to pass invalid arguments;

public Transform3d robotToCam = new Transform3d(new Translation3d(0.5, 0.0, 0.5), new Rotation3d(0,0,0));
  public AprilTagFieldLayout aprilTagFieldLayout;
  
  public VisionSubsystem() {
    try {
        aprilTagFieldLayout = AprilTagFieldLayout.loadFromResource(AprilTagFields.k2023ChargedUp.m_resourceFile);
      } catch (IOException e) {
        e.printStackTrace();
    }
    Pose3d robotPose = PhotonUtils.estimateFieldToRobotAprilTag(target.getBestCameraToTarget(), 
    aprilTagFieldLayout.getTagPose(target.getFiducialId()), robotToCam);
  }

What could possibly be the reason behind this?

It tells you the issue, you’re passing a Optional not a Pose3d, grab the Pose3d from the optional if it is there.

1 Like

Finally managed to get the Pose3d out of the optional, thanks a lot!

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.