Skip to content

Commit

Permalink
CSI Camera Timeout & Auto-Recreate (#1102)
Browse files Browse the repository at this point in the history
Adds logic that will reset the CSI camera in the case that it doesn't receive any new frames from the camera in 3 seconds. This is very helpful for cases where the camera cable was bumped enough to cause a temporary disconnect. Most of the time (if not all the time) the camera needs to be recreated for it to start sending frames again.

Goes with PhotonVision/photon-libcamera-gl-driver#13
  • Loading branch information
BytingBulldogs3539 authored Jan 5, 2024
1 parent d85bafa commit 4d9f228
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ ext {
openCVversion = "4.8.0-2"
joglVersion = "2.4.0-rc-20200307"
javalinVersion = "5.6.2"
photonGlDriverLibVersion = "dev-v2023.1.0-8-g38bbe74"
photonGlDriverLibVersion = "dev-v2023.1.0-9-g75fc678"
frcYear = "2024"

pubVersion = versionString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package org.photonvision.vision.frame.provider;

import org.opencv.core.Mat;
import org.photonvision.common.logging.LogGroup;
import org.photonvision.common.logging.Logger;
import org.photonvision.common.util.math.MathUtils;
import org.photonvision.raspi.LibCameraJNI;
import org.photonvision.vision.camera.LibcameraGpuSettables;
Expand All @@ -31,6 +33,8 @@
public class LibcameraGpuFrameProvider implements FrameProvider {
private final LibcameraGpuSettables settables;

static final Logger logger = new Logger(LibcameraGpuFrameProvider.class, LogGroup.Camera);

public LibcameraGpuFrameProvider(LibcameraGpuSettables visionSettables) {
this.settables = visionSettables;

Expand All @@ -43,21 +47,30 @@ public String getName() {
return "AcceleratedPicamFrameProvider";
}

int i = 0;
int badFrameCounter = 0;

@Override
public Frame get() {
// We need to make sure that other threads don't try to change video modes while
// we're waiting
// for a frame
// we're waiting for a frame
// System.out.println("GET!");
synchronized (settables.CAMERA_LOCK) {
var p_ptr = LibCameraJNI.awaitNewFrame(settables.r_ptr);

if (p_ptr == 0) {
System.out.println("No new frame");
logger.error("No new frame from " + settables.getConfiguration().nickname);
badFrameCounter++;
if (badFrameCounter > 3) {
logger.error(
"No new frame from "
+ settables.getConfiguration().nickname
+ " for 3 seconds attempting recreate!");
settables.setVideoMode(settables.getCurrentVideoMode());
badFrameCounter = 0;
}
return new Frame();
}
badFrameCounter = 0;

var colorMat = new CVMat(new Mat(LibCameraJNI.takeColorFrame(p_ptr)));
var processedMat = new CVMat(new Mat(LibCameraJNI.takeProcessedFrame(p_ptr)));
Expand Down

0 comments on commit 4d9f228

Please sign in to comment.