Add pi version check (#360)

Should prevent GPU acceleration on Pi 4
This commit is contained in:
Matt
2021-12-18 13:12:53 -05:00
committed by GitHub
parent 49048c3998
commit d6e1e28fc2
7 changed files with 79 additions and 8 deletions

View File

@@ -0,0 +1,42 @@
/*
* Copyright (C) Photon Vision.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.photonvision.common.hardware;
public enum PiVersion {
PI_B("Pi Model B"),
COMPUTE_MODULE("Compute Module Rev"),
ZERO_W("Pi Zero W Rev 1.1"),
PI_3("Pi 3"),
PI_4("Pi 4"),
COMPUTE_MODULE_3("Compute Module 3"),
UNKNOWN("UNKNOWN");
private final String identifier;
PiVersion(String s) {
this.identifier = s.toLowerCase();
}
public static PiVersion getPiVersion() {
if (!Platform.isRaspberryPi()) return PiVersion.UNKNOWN;
String piString = Platform.currentPiVersionStr;
for (PiVersion p : PiVersion.values()) {
if (piString.toLowerCase().contains(p.identifier)) return p;
}
return UNKNOWN;
}
}

View File

@@ -45,7 +45,11 @@ public enum Platform {
private static final String OS_NAME = System.getProperty("os.name");
private static final String OS_ARCH = System.getProperty("os.arch");
public static final Platform CurrentPlatform = getCurrentPlatform();
// These are querried on init and should never change after
public static final Platform currentPlatform = getCurrentPlatform();
protected static final String currentPiVersionStr = getPiVersionString();
public static final PiVersion currentPiVersion = PiVersion.getPiVersion();
private static String UnknownPlatformString =
String.format("Unknown Platform. OS: %s, Architecture: %s", OS_NAME, OS_ARCH);
@@ -61,7 +65,7 @@ public enum Platform {
}
public static boolean isRaspberryPi() {
return CurrentPlatform.equals(LINUX_RASPBIAN);
return currentPlatform.equals(LINUX_RASPBIAN);
}
@SuppressWarnings("StatementWithEmptyBody")
@@ -114,4 +118,22 @@ public enum Platform {
return this.value;
}
}
// Querry /proc/device-tree/model. This should return the model of the pi
// Versions here:
// https://github.com/raspberrypi/linux/blob/rpi-5.10.y/arch/arm/boot/dts/bcm2710-rpi-cm3.dts
private static String getPiVersionString() {
if (!isRaspberryPi()) return "";
try {
shell.executeBashCommand("cat /proc/device-tree/model");
} catch (IOException e) {
e.printStackTrace();
}
if (shell.getExitCode() == 0) {
// We expect it to be in the format "raspberry pi X model X"
return shell.getOutput();
}
return "";
}
}

View File

@@ -128,7 +128,7 @@ public class ScriptManager {
}
public static void queueEvent(ScriptEventType eventType) {
if (!Platform.CurrentPlatform.isWindows()) {
if (!Platform.currentPlatform.isWindows()) {
try {
queuedEvents.putLast(eventType);
logger.info("Queued event: " + eventType.name());

View File

@@ -76,7 +76,7 @@ public class FileUtils {
}
public static void setFilePerms(Path path) throws IOException {
if (!Platform.CurrentPlatform.isWindows()) {
if (!Platform.currentPlatform.isWindows()) {
File thisFile = path.toFile();
Set<PosixFilePermission> perms =
Files.readAttributes(path, PosixFileAttributes.class).permissions();
@@ -94,7 +94,7 @@ public class FileUtils {
}
public static void setAllPerms(Path path) {
if (!Platform.CurrentPlatform.isWindows()) {
if (!Platform.currentPlatform.isWindows()) {
String command = String.format("chmod 777 -R %s", path.toString());
try {
Process p = Runtime.getRuntime().exec(command);

View File

@@ -22,6 +22,7 @@ import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import org.photonvision.common.hardware.PiVersion;
import org.photonvision.common.hardware.Platform;
import org.photonvision.common.logging.LogGroup;
import org.photonvision.common.logging.Logger;
@@ -83,7 +84,12 @@ public class PicamJNI {
}
public static boolean isSupported() {
return libraryLoaded && !isVCSMSupported() && getSensorModel() != SensorModel.Disconnected;
return libraryLoaded
&& !isVCSMSupported()
&& getSensorModel() != SensorModel.Disconnected
&& Platform.isRaspberryPi()
&& (Platform.currentPiVersion == PiVersion.PI_3
|| Platform.currentPiVersion == PiVersion.COMPUTE_MODULE_3);
}
public static SensorModel getSensorModel() {

View File

@@ -36,7 +36,7 @@ public class HardwareTest {
if (!Platform.isRaspberryPi()) return;
System.out.println("Testing on platform: " + Platform.CurrentPlatform);
System.out.println("Testing on platform: " + Platform.currentPlatform);
System.out.println("Printing CPU Info:");
System.out.println("Memory: " + cpuMetrics.getMemory() + "MB");

View File

@@ -169,7 +169,8 @@ public class Main {
"Starting PhotonVision version "
+ PhotonVersion.versionString
+ " on "
+ Platform.CurrentPlatform.toString());
+ Platform.currentPlatform.toString()
+ (Platform.isRaspberryPi() ? (" (Pi " + Platform.currentPiVersion.name() + ")") : ""));
try {
CameraServerCvJNI.forceLoad();