Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import edu.wpi.first.cscore.CameraServerJNI;
import edu.wpi.first.networktables.NetworkTable;
import edu.wpi.first.networktables.ProtobufPublisher;
import edu.wpi.first.wpilibj.Alert;
import edu.wpi.first.wpilibj.Alert.AlertType;
import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.Files;
Expand Down Expand Up @@ -62,6 +64,9 @@ private record NetworkTraffic(double sentBitRate, double recvBitRate) {}
.getProtobufTopic(CameraServerJNI.getHostname(), DeviceMetrics.proto)
.publish();

private final Alert thermalThrottlingAlert =
new Alert("PhotonAlerts", "Thermal throttling detected!", AlertType.kWarning);

private SystemInfo si;
private CentralProcessor cpu;
private OperatingSystem os;
Expand Down Expand Up @@ -140,11 +145,11 @@ protected SystemMonitor() {
}

/**
* Returns a comma-separated list of addtional thermal zone types that should be checked to get
* Returns a comma-separated list of additional thermal zone types that should be checked to get
* the CPU temperature on Unix systems. The temperature will be reported for the first temperature
* zone with a type that mateches an item of this list. If the CPU temperature isn't being
* zone with a type that matches an item of this list. If the CPU temperature isn't being
* reported correctly for a coprocessor, override this method to return a string with type
* associated with the thermal zone for that comprocessor.
* associated with the thermal zone for that coprocessor.
*
* @return String containing a comma-separated list of thermal zone types for reading CPU
* temperature.
Expand All @@ -159,7 +164,7 @@ protected String getThermalZoneTypes() {

/**
* Starts the periodic system monitor that publishes performance metrics. The metrics are
* published every millisUpdateInerval seconds after a millisStartDelay startup delay. Calling
* published every millisUpdateInterval seconds after a millisStartDelay startup delay. Calling
* this method when the monitor is running will stop it and restart it with the new delay and
* update interval.
*
Expand Down Expand Up @@ -210,6 +215,8 @@ private void publishMetrics() {

metricPublisher.set(metrics);

thermalThrottlingAlert.set(this.isThermallyThrottling());

if (writeMetricsToLog) {
logMetrics(metrics);
}
Expand All @@ -233,7 +240,7 @@ private void logMetrics(DeviceMetrics metrics) {
String.format("CPU Throttle: %s, ", metrics.cpuThr().isBlank() ? "N/A" : metrics.cpuThr()));
sb.append(
String.format(
"Data sent: %.0f Kbps, Data recieved: %.0f Kbps",
"Data sent: %.0f Kbps, Data received: %.0f Kbps",
metrics.sentBitRate() / 1000, metrics.recvBitRate() / 1000));
logger.debug(sb.toString());
}
Expand Down Expand Up @@ -446,10 +453,20 @@ public String getCpuThrottleReason() {
return "";
}

/**
* Returns true if the device is currently experiencing thermal throttling. Platforms that support
* thermal throttling detection will override this method.
*
* @return true if thermally throttling, false otherwise.
*/
public boolean isThermallyThrottling() {
return false;
}

/**
* Returns the total GPU memory in MiB.
*
* @return The total GPU memory in MiB, or -1.0 if not avaialable on this platform.
* @return The total GPU memory in MiB, or -1.0 if not available on this platform.
*/
public double getGpuMem() {
return -1.0;
Expand All @@ -475,7 +492,7 @@ public String getIpAddress() {
}

/**
* Returns a NetworkTraffic instance containing the average sent and recieved network traffic
* Returns a NetworkTraffic instance containing the average sent and received network traffic
* since the last time this was called.
*
* @return NetworkTraffic instance with data in bits/second. The traffic values will be -1 if the
Expand Down Expand Up @@ -537,7 +554,7 @@ private void testSM() {
() -> {
var nt = getNetworkTraffic();
return String.format(
"Data sent: %.0f Kbps, Data recieved: %.0f Kbps",
"Data sent: %.0f Kbps, Data received: %.0f Kbps",
nt.sentBitRate() / 1000, nt.recvBitRate() / 1000);
});

Expand All @@ -562,4 +579,4 @@ private double timeIt(StringBuilder sb, Supplier<String> source) {
sb.append(String.format(" %7.3f ms >> %s\n", delta, resp));
return delta;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,7 @@ public class SystemMonitorRaspberryPi extends SystemMonitor {

@Override
public String getCpuThrottleReason() {
int state = 0;
String output = vcgencmd("get_throttled");
try {
state = Integer.decode(output);
} catch (NumberFormatException e) {
logger.warn("Could not parse return value: " + output);
}
int state = getThrottleState();
if ((state & 0x01) != 0) {
return "LOW VOLTAGE";
} else if ((state & 0x08) != 0) {
Expand All @@ -44,6 +38,22 @@ public String getCpuThrottleReason() {
return "None";
}

@Override
public boolean isThermallyThrottling() {
return (getThrottleState() & 0x08) != 0;
}

private int getThrottleState() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we breaking this out into a helper function?

Copy link
Copy Markdown
Contributor Author

@Ruthie-FRC Ruthie-FRC Apr 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, scattered mess rn with regionals...
and also that it took me a good few hours to figure out that the cd post was an april fools joke...
:)

int state = 0;
String output = vcgencmd("get_throttled");
try {
state = Integer.decode(output);
} catch (NumberFormatException e) {
logger.warn("Could not parse return value: " + output);
}
return state;
}

@Override
public double getGpuMem() {
String output = vcgencmd("get_mem gpu");
Expand Down
Loading