Skip to content

Commit

Permalink
feat(logging): implement AdvantageKit support for Robot.java
Browse files Browse the repository at this point in the history
  • Loading branch information
TheGamer1002 committed Jan 27, 2024
1 parent f1629dc commit 2458969
Showing 1 changed file with 49 additions and 11 deletions.
60 changes: 49 additions & 11 deletions src/main/java/frc/robot/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,25 @@

package frc.robot;

import edu.wpi.first.wpilibj.PowerDistribution;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.CommandScheduler;
import org.littletonrobotics.junction.LogFileUtil;
import org.littletonrobotics.junction.LoggedRobot;
import org.littletonrobotics.junction.Logger;
import org.littletonrobotics.junction.networktables.NT4Publisher;
import org.littletonrobotics.junction.wpilog.WPILOGReader;
import org.littletonrobotics.junction.wpilog.WPILOGWriter;


/**
* The VM is configured to automatically run this class, and to call the functions corresponding to
* each mode, as described in the TimedRobot documentation. If you change the name of this class or
* the package after creating this project, you must also update the build.gradle file in the
* project.
*/
public class Robot extends TimedRobot {
public class Robot extends LoggedRobot {
private Command autonomousCommand;

private RobotContainer robotContainer;
Expand All @@ -25,6 +33,21 @@ public class Robot extends TimedRobot {
*/
@Override
public void robotInit() {
Logger.recordMetadata("ProjectName", "MyProject"); // Set a metadata value

if (isReal()) {
Logger.addDataReceiver(new WPILOGWriter()); // Log to a USB stick ("/U/logs")
Logger.addDataReceiver(new NT4Publisher()); // Publish data to NetworkTables
new PowerDistribution(1, PowerDistribution.ModuleType.kRev); // Enables power distribution logging
} 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
}

// 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.
// Instantiate our RobotContainer. This will perform all our button bindings, and put our
// autonomous chooser on the dashboard.
robotContainer = new RobotContainer();
Expand All @@ -46,14 +69,20 @@ public void robotPeriodic() {
CommandScheduler.getInstance().run();
}

/** This function is called once each time the robot enters Disabled mode. */
/**
* This function is called once each time the robot enters Disabled mode.
*/
@Override
public void disabledInit() {}
public void disabledInit() {
}

@Override
public void disabledPeriodic() {}
public void disabledPeriodic() {
}

/** This autonomous runs the autonomous command selected by your {@link RobotContainer} class. */
/**
* This autonomous runs the autonomous command selected by your {@link RobotContainer} class.
*/
@Override
public void autonomousInit() {
autonomousCommand = robotContainer.getAutonomousCommand();
Expand All @@ -64,9 +93,12 @@ public void autonomousInit() {
}
}

/** This function is called periodically during autonomous. */
/**
* This function is called periodically during autonomous.
*/
@Override
public void autonomousPeriodic() {}
public void autonomousPeriodic() {
}

@Override
public void teleopInit() {
Expand All @@ -79,17 +111,23 @@ public void teleopInit() {
}
}

/** This function is called periodically during operator control. */
/**
* This function is called periodically during operator control.
*/
@Override
public void teleopPeriodic() {}
public void teleopPeriodic() {
}

@Override
public void testInit() {
// Cancels all running commands at the start of test mode.
CommandScheduler.getInstance().cancelAll();
}

/** This function is called periodically during test mode. */
/**
* This function is called periodically during test mode.
*/
@Override
public void testPeriodic() {}
public void testPeriodic() {
}
}

0 comments on commit 2458969

Please sign in to comment.