Skip to content

Commit

Permalink
Pi startup fix (#923)
Browse files Browse the repository at this point in the history
  • Loading branch information
amquake committed Sep 28, 2023
1 parent 43eefcf commit 3c85291
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,23 @@ public String toString() {
}
}

public static ArrayList<NMDeviceInfo> getAllInterfaces() {
private static List<NMDeviceInfo> allInterfaces = new ArrayList<>();
private static long lastReadTimestamp = 0;

public static List<NMDeviceInfo> getAllInterfaces() {
long now = System.currentTimeMillis();
if (now - lastReadTimestamp < 5000) return allInterfaces;
else lastReadTimestamp = now;

var ret = new ArrayList<NMDeviceInfo>();

if (!Platform.isLinux()) {
// Can't determine interface name on Linux, give up
// Can only determine interface name on Linux, give up
return ret;
}

try {
var shell = new ShellExec(true, true);
var shell = new ShellExec(true, false);
shell.executeBashCommand(
"nmcli -t -f GENERAL.CONNECTION,GENERAL.DEVICE,GENERAL.TYPE device show");
String out = shell.getOutput();
Expand All @@ -103,6 +110,7 @@ public static ArrayList<NMDeviceInfo> getAllInterfaces() {

logger.debug("Found network interfaces:\n" + ret.toString());

allInterfaces = ret;
return ret;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,26 +199,27 @@ protected void setVideoModeInternal(VideoMode videoMode) {
// We need to make sure that other threads don't try to do anything funny while we're recreating
// the camera
synchronized (LibCameraJNI.CAMERA_LOCK) {
boolean success = false;
if (m_initialized) {
success |= LibCameraJNI.stopCamera();
success |= LibCameraJNI.destroyCamera();
logger.debug("Stopping libcamera");
if (!LibCameraJNI.stopCamera()) {
logger.error("Couldn't stop a zero copy Pi Camera while switching video modes");
}
logger.debug("Destroying libcamera");
if (!LibCameraJNI.destroyCamera()) {
logger.error("Couldn't destroy a zero copy Pi Camera while switching video modes");
}
}

// if (!success) {
// throw new RuntimeException(
// "Couldn't destroy a zero copy Pi Camera while switching video modes");
// }

System.out.println("Starting camera");
success |=
LibCameraJNI.createCamera(
mode.width, mode.height, (m_rotationMode == ImageRotationMode.DEG_180 ? 180 : 0));
success |= LibCameraJNI.startCamera();
if (!success) {
throw new RuntimeException(
"Couldn't create a zero copy Pi Camera while switching video modes");
logger.debug("Creating libcamera");
if (!LibCameraJNI.createCamera(
mode.width, mode.height, (m_rotationMode == ImageRotationMode.DEG_180 ? 180 : 0))) {
logger.error("Couldn't create a zero copy Pi Camera while switching video modes");
}
logger.debug("Starting libcamera");
if (!LibCameraJNI.startCamera()) {
logger.error("Couldn't start a zero copy Pi Camera while switching video modes");
}

m_initialized = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ private static List<VisionSource> loadVisionSourcesFromCamConfigs(
List<CameraConfiguration> camConfigs) {
var cameraSources = new ArrayList<VisionSource>();
for (var configuration : camConfigs) {
System.out.println("Creating VisionSource for " + configuration);
logger.debug("Creating VisionSource for " + configuration);

// Picams should have csi-video in the path
boolean is_picam =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.photonvision.vision.frame.FrameStaticProperties;

public abstract class VisionSourceSettables {
private static final Logger logger =
protected static final Logger logger =
new Logger(VisionSourceSettables.class, LogGroup.VisionModule);

private final CameraConfiguration configuration;
Expand Down
6 changes: 6 additions & 0 deletions photon-server/src/main/java/org/photonvision/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -344,18 +344,23 @@ public static void main(String[] args) {
+ Platform.getPlatformName()
+ (Platform.isRaspberryPi() ? (" (Pi " + PiVersion.getPiVersion() + ")") : ""));

logger.debug("Loading ConfigManager...");
ConfigManager.getInstance().load(); // init config manager
ConfigManager.getInstance().requestSave();

logger.debug("Loading HardwareManager...");
// Force load the hardware manager
HardwareManager.getInstance();

logger.debug("Loading NetworkManager...");
NetworkManager.getInstance().reinitialize();

logger.debug("Loading NetworkTablesManager...");
NetworkTablesManager.getInstance()
.setConfig(ConfigManager.getInstance().getConfig().getNetworkConfig());

if (!isTestMode) {
logger.debug("Loading VisionSourceManager...");
VisionSourceManager.getInstance()
.registerLoadedConfigs(
ConfigManager.getInstance().getConfig().getCameraConfigurations().values());
Expand All @@ -369,6 +374,7 @@ public static void main(String[] args) {
}
}

logger.info("Starting server...");
Server.start(DEFAULT_WEBPORT);
}
}

0 comments on commit 3c85291

Please sign in to comment.