Skip to content

Commit

Permalink
Fix crash if replay log not found, add robot software metadata
Browse files Browse the repository at this point in the history
Fixed an issue where if not provided a log of a match during robot simulation, the software would crash. This now is handled with just running the simulation as normal without a replay. There is no current logic for how to feed information into the simulator at the moment.

Added metadata for robot software. This includes runtime type such as a simulation or a real robot, as well as Git information such as the commit SHA, the build date, the commit date, branch, and whether or not if there are files not committed to the repository.
  • Loading branch information
garrettsummerfi3ld committed Feb 17, 2024
1 parent 3d0d81d commit 007f9fb
Showing 1 changed file with 31 additions and 21 deletions.
52 changes: 31 additions & 21 deletions src/main/java/frc/robot/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package frc.robot;

import edu.wpi.first.wpilibj.Filesystem;
import edu.wpi.first.wpilibj.PowerDistribution;
import edu.wpi.first.wpilibj.PowerDistribution.ModuleType;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj2.command.Command;
Expand All @@ -16,6 +15,7 @@
import org.littletonrobotics.junction.LogFileUtil;
import org.littletonrobotics.junction.LoggedRobot;
import org.littletonrobotics.junction.Logger;
import org.littletonrobotics.junction.inputs.LoggedPowerDistribution;
import org.littletonrobotics.junction.networktables.NT4Publisher;
import org.littletonrobotics.junction.wpilog.WPILOGReader;
import org.littletonrobotics.junction.wpilog.WPILOGWriter;
Expand Down Expand Up @@ -51,34 +51,44 @@ public static Robot getInstance() {
*/
@Override
public void robotInit() {
// Set up logging
Logger.recordMetadata("ProjectName", "2024-Robot"); // Set a metadata value
// Record metadata about robot code
Logger.recordMetadata("ProjectName", "2024-Robot");
Logger.recordMetadata("RuntimeType", getRuntimeType().toString());
Logger.recordMetadata("BuildDate", BuildConstants.BUILD_DATE);
Logger.recordMetadata("GitSHA", null);
Logger.recordMetadata("GitDate", null);
Logger.recordMetadata("GitBranch", null);
// Determine if the git repo is dirty
switch (BuildConstants.DIRTY) {
case 0:

Check warning on line 63 in src/main/java/frc/robot/Robot.java

View workflow job for this annotation

GitHub Actions / qodana

Nullability and data flow problems

Switch label `0` is unreachable
Logger.recordMetadata("GitDirty", "Clean");
break;
case 1:

Check warning on line 66 in src/main/java/frc/robot/Robot.java

View workflow job for this annotation

GitHub Actions / qodana

Nullability and data flow problems

Switch label `1` is unreachable
Logger.recordMetadata("GitDirty", "Uncommitted changes");
break;
default:
Logger.recordMetadata("GitDirty", "Unknown");
break;
}

if (isReal()) {
Logger.addDataReceiver(new NT4Publisher()); // Publish data to NetworkTables
PowerDistribution pdh =
new PowerDistribution(
HardwareConstants.REV_PDH_ID, ModuleType.kRev); // Enables power distribution logging
pdh.setSwitchableChannel(true);
pdh.close();
Logger.addDataReceiver(new NT4Publisher());
LoggedPowerDistribution.getInstance(HardwareConstants.REV_PDH_ID, ModuleType.kRev);
Logger.registerURCL(URCL.startExternal());

} else {
setUseTiming(false); // Run as fast as possible
String logPath =
LogFileUtil
.findReplayLog(); // Pull the replay log from AdvantageScope (or prompt the user)
Logger.setReplaySource(new WPILOGReader(logPath)); // Read replay log
Logger.addDataReceiver(
new WPILOGWriter(
LogFileUtil.addPathSuffix(logPath, "_sim"))); // Save outputs to a new log
String logPath = LogFileUtil.findReplayLog();
if (logPath != null) {
Logger.setReplaySource(new WPILOGReader(logPath));
} else {
Logger.addDataReceiver(new NT4Publisher());
}
Logger.addDataReceiver(new WPILOGWriter(LogFileUtil.addPathSuffix(logPath, "_sim")));

Check warning on line 86 in src/main/java/frc/robot/Robot.java

View workflow job for this annotation

GitHub Actions / qodana

Nullability and data flow problems

Argument `logPath` might be null
}
Logger.recordMetadata("GitSHA", BuildConstants.GIT_SHA);

// Logger.disableDeterministicTimestamps() // See "Deterministic Timestamps" in the
// "Understanding Data Flow" page
Logger.start(); // Start logging! No more data receivers, replay sources, or metadata values may
// be added.
// Start logging
Logger.start();

// Instantiate our RobotContainer. This will perform all our button bindings, and put our
// autonomous chooser on the dashboard.
Expand Down

0 comments on commit 007f9fb

Please sign in to comment.