Skip to content

Commit

Permalink
Loop on separate thread for PV
Browse files Browse the repository at this point in the history
  • Loading branch information
legoguy1000 committed Sep 25, 2024
1 parent f0cc09e commit 0db7fcb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
23 changes: 10 additions & 13 deletions src/main/java/frc/lib/util/photon/PhotonCameraWrapper.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package frc.lib.util.photon;

import java.io.File;
import java.io.IOException;
import java.util.Optional;
import org.littletonrobotics.junction.Logger;
import org.photonvision.EstimatedRobotPose;
Expand All @@ -16,7 +14,6 @@
import edu.wpi.first.math.geometry.Translation2d;
import edu.wpi.first.math.numbers.N1;
import edu.wpi.first.math.numbers.N3;
import edu.wpi.first.wpilibj.Filesystem;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import frc.lib.util.photon.PhotonIO.PhotonInputs;
Expand Down Expand Up @@ -63,16 +60,16 @@ public PhotonCameraWrapper(PhotonIO io, Transform3d robotToCam) {
public void periodic() {
this.io.updateInputs(this.inputs);
Logger.processInputs("PhotonVision/" + inputs.name, inputs);
if (this.inputs.distCoeffs.length == 0 && Timer.getFPGATimestamp() - resetTimer > 5) {
resetTimer = Timer.getFPGATimestamp();
try {
this.io.uploadSettings(this.io.ip + ":5800",
new File(Filesystem.getDeployDirectory().getAbsoluteFile(),
"photon-configs/" + inputs.name + ".zip"));
} catch (IOException e) {
e.printStackTrace();
}
}
// if (this.inputs.distCoeffs.length == 0 && Timer.getFPGATimestamp() - resetTimer > 5) {
// resetTimer = Timer.getFPGATimestamp();
// try {
// this.io.uploadSettings(this.io.ip + ":5800",
// new File(Filesystem.getDeployDirectory().getAbsoluteFile(),
// "photon-configs/" + inputs.name + ".zip"));
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
}

/**
Expand Down
23 changes: 17 additions & 6 deletions src/main/java/frc/lib/util/photon/PhotonReal.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
Expand Down Expand Up @@ -104,6 +105,8 @@ public boolean uploadSettings(String ip, File file) throws IOException {
}
}

private AtomicBoolean isPhotonOk = new AtomicBoolean(false);

/**
* Constructs a PhotonReal from a root table.
*
Expand All @@ -113,12 +116,18 @@ public boolean uploadSettings(String ip, File file) throws IOException {
public PhotonReal(NetworkTableInstance instance, String cameraName, String cameraIP) {
super(cameraName, cameraIP);
new Thread(() -> {
try {
uploadSettings(cameraIP + ":5800",
new File(Filesystem.getDeployDirectory().getAbsoluteFile(),
"photon-configs/" + cameraName + ".zip"));
} catch (IOException e) {
e.printStackTrace();
Timer timer = new Timer();
while (true) {
if (timer.advanceIfElapsed(5.0) && !isPhotonOk.get()) {
try {
uploadSettings(cameraIP + ":5800",
new File(Filesystem.getDeployDirectory().getAbsoluteFile(),
"photon-configs/" + cameraName + ".zip"));
} catch (IOException e) {
e.printStackTrace();
}
}
Thread.yield();
}
}).start();
var photonvision_root_table = instance.getTable(kTableName);
Expand Down Expand Up @@ -174,6 +183,8 @@ public void updateInputs(PhotonInputs inputs) {

inputs.cameraMatrix = cameraIntrinsicsSubscriber.get(EMPTY);
inputs.distCoeffs = cameraDistortionSubscriber.get(EMPTY);

isPhotonOk.set(inputs.distCoeffs.length != 0);
}

@Override
Expand Down

0 comments on commit 0db7fcb

Please sign in to comment.