Calibration and metrics clean up. (#68)

* Add more calibration metrics
* Cleanup metrics
* Rename PWM fields to match convention
This commit is contained in:
Xzibit
2020-08-04 16:19:40 -04:00
committed by GitHub
parent e2768eaee8
commit dea68041fb
11 changed files with 140 additions and 60 deletions

View File

@@ -35,6 +35,7 @@ public class HardwareConfig {
public final boolean ledsCanDim;
public final ArrayList<Integer> ledPWMRange;
public final String ledPWMSetRange;
public final int ledPWMFrequency;
public final String ledDimCommand;
public final String ledBlinkCommand;
@@ -54,6 +55,7 @@ public class HardwareConfig {
ledSetCommand = "";
ledsCanDim = false;
ledPWMRange = new ArrayList<>();
ledPWMFrequency = 0;
ledPWMSetRange = "";
ledDimCommand = "";
@@ -81,6 +83,7 @@ public class HardwareConfig {
this.ledsCanDim = (Boolean) hardware.get("ledsCanDim");
this.ledPWMRange = (ArrayList<Integer>) hardware.get("ledPWMRange");
this.ledPWMSetRange = (String) hardware.get("ledPWMSetRange");
this.ledPWMFrequency = (Integer) hardware.get("ledPWMFrequency");
this.ledDimCommand = (String) hardware.get("ledDimCommand");
this.ledBlinkCommand = (String) hardware.get("ledBlinkCommand");

View File

@@ -35,10 +35,10 @@ public class PiGPIO extends GPIOBase {
return Singleton.INSTANCE;
}
public PiGPIO(int address, int value, int range) {
public PiGPIO(int address, int frequency, int range) {
this.pin = address;
try {
getPigpioDaemon().setPWMFrequency(this.pin, value);
getPigpioDaemon().setPWMFrequency(this.pin, frequency);
getPigpioDaemon().setPWMRange(this.pin, range);
} catch (PigpioException e) {
logger.error("Could not set PWM settings on port " + this.pin);

View File

@@ -24,12 +24,9 @@ import org.photonvision.common.hardware.GPIO.GPIOBase;
import org.photonvision.common.hardware.GPIO.PiGPIO;
import org.photonvision.common.hardware.metrics.MetricsBase;
import org.photonvision.common.hardware.metrics.MetricsPublisher;
import org.photonvision.common.logging.LogGroup;
import org.photonvision.common.logging.Logger;
public class HardwareManager {
HardwareConfig hardwareConfig;
private static final Logger logger = new Logger(HardwareManager.class, LogGroup.General);
private static final HashMap<Integer, GPIOBase> LEDs = new HashMap<>();
public static HardwareManager getInstance() {
@@ -44,14 +41,16 @@ public class HardwareManager {
hardwareConfig.ledPins.forEach(
pin -> {
if (Platform.isRaspberryPi()) {
LEDs.put(pin, new PiGPIO(pin, 0, hardwareConfig.ledPWMRange.get(1)));
LEDs.put(
pin,
new PiGPIO(pin, hardwareConfig.ledPWMFrequency, hardwareConfig.ledPWMRange.get(1)));
} else {
LEDs.put(pin, new CustomGPIO(pin));
}
});
// Start hardware metrics thread
MetricsPublisher.getInstance().startThread();
MetricsPublisher.getInstance().startTask();
}
/** Example: HardwareManager.getInstance().getPWM(port).dimLEDs(int dimValue); */
public GPIOBase getGPIO(int pin) {

View File

@@ -17,7 +17,7 @@
package org.photonvision.common.hardware.metrics;
import java.util.Arrays;
import java.io.IOException;
import org.photonvision.common.configuration.HardwareConfig;
import org.photonvision.common.hardware.Platform;
import org.photonvision.common.logging.LogGroup;
@@ -58,9 +58,25 @@ public abstract class MetricsBase {
try {
runCommand.executeBashCommand(command);
return Double.parseDouble(runCommand.getOutput());
} catch (Exception e) {
logger.error(Arrays.toString(e.getStackTrace()));
} catch (NumberFormatException e) {
logger.error(
"Command: \""
+ command
+ "\" returned a non-double output!"
+ "\nOutput Received: "
+ runCommand.getOutput()
+ "\nStandard Error: "
+ runCommand.getError()
+ "\nCommand completed: "
+ runCommand.isOutputCompleted()
+ "\nError completed: "
+ runCommand.isErrorCompleted()
+ "\nExit code: "
+ runCommand.getExitCode());
return Double.NaN;
} catch (IOException e) {
MetricsPublisher.getInstance().stopTask();
return -1;
}
}
}

View File

@@ -18,54 +18,54 @@
package org.photonvision.common.hardware.metrics;
import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;
import org.photonvision.common.dataflow.DataChangeService;
import org.photonvision.common.dataflow.events.OutgoingUIEvent;
import org.photonvision.common.logging.LogGroup;
import org.photonvision.common.logging.Logger;
import org.photonvision.common.util.TimedTaskManager;
import org.photonvision.server.UIUpdateType;
public class MetricsPublisher {
private final HashMap<String, Double> metrics;
private final Thread metricsThread;
private static final Logger logger = new Logger(MetricsPublisher.class, LogGroup.General);
private static CPU cpu;
private static GPU gpu;
private static RAM ram;
public static MetricsPublisher getInstance() {
return Singleton.INSTANCE;
}
private MetricsPublisher() {
var cpu = CPU.getInstance();
var gpu = GPU.getInstance();
var ram = RAM.getInstance();
cpu = CPU.getInstance();
gpu = GPU.getInstance();
ram = RAM.getInstance();
metrics = new HashMap<>();
this.metricsThread =
new Thread(
() -> {
var timer = new Timer();
timer.schedule(
new TimerTask() {
public void run() {
metrics.put("cpuTemp", cpu.getTemp());
metrics.put("cpuUtil", cpu.getUtilization());
metrics.put("cpuMem", cpu.getMemory());
metrics.put("gpuTemp", gpu.getTemp());
metrics.put("gpuMem", gpu.getMemory());
metrics.put("ramUtil", ram.getUsedRam());
DataChangeService.getInstance()
.publishEvent(
new OutgoingUIEvent<>(
UIUpdateType.BROADCAST, "metrics", metrics, null));
}
},
0,
1000);
});
}
public void startThread() {
metricsThread.start();
public void startTask() {
TimedTaskManager.getInstance()
.addTask(
"Metrics",
() -> {
metrics.put("cpuTemp", cpu.getTemp());
metrics.put("cpuUtil", cpu.getUtilization());
metrics.put("cpuMem", cpu.getMemory());
metrics.put("gpuTemp", gpu.getTemp());
metrics.put("gpuMem", gpu.getMemory());
metrics.put("ramUtil", ram.getUsedRam());
DataChangeService.getInstance()
.publishEvent(
new OutgoingUIEvent<>(UIUpdateType.BROADCAST, "metrics", metrics, null));
},
1000);
}
public void stopTask() {
TimedTaskManager.getInstance().cancelTask("Metrics");
logger.info("This device does not support running bash commands. Stopped metrics thread.");
}
private static class Singleton {