Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replay Tool #125

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,5 @@ simgui-ds.json
simgui.json

!src/main/deploy/photon-configs/*.zip

/prev-log.txt
45 changes: 45 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,37 @@ task(checkAkitInstall, dependsOn: "classes", type: JavaExec) {
}
compileJava.finalizedBy checkAkitInstall

sourceSets {
tools {
compileClasspath += sourceSets.main.compileClasspath + sourceSets.main.output
runtimeClasspath += sourceSets.main.runtimeClasspath + sourceSets.main.output
}
}

import org.gradle.internal.os.OperatingSystem

project.ext.lwjglVersion = "3.3.3"

switch (OperatingSystem.current()) {
case OperatingSystem.LINUX:
project.ext.lwjglNatives = "natives-linux"
def osArch = System.getProperty("os.arch")
if (osArch.startsWith("arm") || osArch.startsWith("aarch64")) {
project.ext.lwjglNatives += osArch.contains("64") || osArch.startsWith("armv8") ? "-arm64" : "-arm32"
} else if (osArch.startsWith("ppc")) {
project.ext.lwjglNatives += "-ppc64le"
} else if (osArch.startsWith("riscv")) {
project.ext.lwjglNatives += "-riscv64"
}
break
case OperatingSystem.MAC_OS:
project.ext.lwjglNatives = "natives-macos"
break
case OperatingSystem.WINDOWS:
project.ext.lwjglNatives = "natives-windows"
break
}

dependencies {
def akitJson = new groovy.json.JsonSlurper().parseText(new File(projectDir.getAbsolutePath() + "/vendordeps/AdvantageKit.json").text)
annotationProcessor "org.littletonrobotics.akit.junction:junction-autolog:$akitJson.version"
Expand All @@ -104,6 +135,14 @@ dependencies {

implementation 'org.apache.httpcomponents:httpclient:4.5.14'
implementation 'org.apache.httpcomponents:httpmime:4.5.14'

toolsImplementation platform("org.lwjgl:lwjgl-bom:$lwjglVersion")
toolsImplementation "org.lwjgl:lwjgl"
toolsImplementation "org.lwjgl:lwjgl-nfd"
toolsRuntimeOnly "org.lwjgl:lwjgl::$lwjglNatives"
toolsRuntimeOnly "org.lwjgl:lwjgl-nfd::$lwjglNatives"
toolsImplementation wpi.java.deps.wpilibJniRelease(wpi.platforms.desktop)
toolsImplementation wpi.java.vendor.jniRelease(wpi.platforms.desktop)
}

// Simulation configuration (e.g. environment variables).
Expand Down Expand Up @@ -150,3 +189,9 @@ gversion {
timeZone = "America/New_York"
indent = " "
}

task(replay, dependsOn: "build", type: JavaExec) {
mainClass = "frc.robot.Replay"
classpath = sourceSets.tools.runtimeClasspath
systemProperties["java.library.path"] = new File(project.projectDir, "build/jni/release")
}
9 changes: 9 additions & 0 deletions src/main/java/frc/robot/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ public void testInit() {
@Override
public void testPeriodic() {}

private static String logFile = null;

public static void setReplayFile(String logFile) {
Robot.logFile = logFile;
}


private static final String environmentVariable = "AKIT_LOG_PATH";
private static final String advantageScopeFileName = "akit-log-path.txt";
Expand All @@ -192,6 +198,9 @@ public void testPeriodic() {}
* available 3. The result of the prompt displayed to the user
*/
public static String findReplayLog() {
if (Robot.logFile != null) {
return Robot.logFile;
}
// Read environment variables
String envPath = System.getenv(environmentVariable);
if (envPath != null) {
Expand Down
77 changes: 77 additions & 0 deletions src/tools/java/frc/robot/Replay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package frc.robot;

import static org.lwjgl.system.MemoryStack.stackPush;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.ByteBuffer;
import org.littletonrobotics.junction.LogFileUtil;
import org.lwjgl.PointerBuffer;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.util.nfd.NFDFilterItem;
import org.lwjgl.util.nfd.NativeFileDialog;
import edu.wpi.first.wpilibj.Filesystem;
import edu.wpi.first.wpilibj.RobotBase;

/** */
public class Replay {

/** */
public static void main(String[] argv) {
File prevPath = new File(Filesystem.getLaunchDirectory(), "prev-log.txt");
NativeFileDialog.NFD_Init();
String logPath;
try (MemoryStack stack = stackPush()) {
NFDFilterItem.Buffer filters = NFDFilterItem.malloc(1);
filters.get(0).name(stack.UTF8("WPILog Files")).spec(stack.UTF8("wpilog"));

ByteBuffer defaultPath = null;
if (prevPath.exists()) {
String prev = new File(readToString(prevPath).trim()).getParent();
defaultPath = stack.UTF8(prev);
}

PointerBuffer pp = stack.mallocPointer(1);
int res = NativeFileDialog.NFD_OpenDialog(pp, filters, defaultPath);
if (res != NativeFileDialog.NFD_OKAY) {
return;
}
logPath = pp.getStringUTF8();
}
NativeFileDialog.NFD_Quit();
try {
try (PrintWriter out = new PrintWriter(prevPath)) {
out.print(logPath);
}
} catch (IOException e) {
e.printStackTrace();
}

Robot.setReplayFile(logPath);
RobotBase.startRobot(Robot::new);
}

private static String readToString(File f) {
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(f));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
String ls = System.getProperty("line.separator");
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append(ls);
}
// delete the last new line separator
stringBuilder.deleteCharAt(stringBuilder.length() - 1);
reader.close();
return stringBuilder.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

}
57 changes: 57 additions & 0 deletions vendordeps/photonlib-json-1.0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"fileName": "photonlib.json",
"name": "photonlib",
"version": "v2024.3.1",
"uuid": "515fe07e-bfc6-11fa-b3de-0242ac130004",
"frcYear": "2024",
"mavenUrls": [
"https://maven.photonvision.org/repository/internal",
"https://maven.photonvision.org/repository/snapshots"
],
"jsonUrl": "https://maven.photonvision.org/repository/internal/org/photonvision/photonlib-json/1.0/photonlib-json-1.0.json",
"jniDependencies": [],
"cppDependencies": [
{
"groupId": "org.photonvision",
"artifactId": "photonlib-cpp",
"version": "v2024.3.1",
"libName": "photonlib",
"headerClassifier": "headers",
"sharedLibrary": true,
"skipInvalidPlatforms": true,
"binaryPlatforms": [
"windowsx86-64",
"linuxathena",
"linuxx86-64",
"osxuniversal"
]
},
{
"groupId": "org.photonvision",
"artifactId": "photontargeting-cpp",
"version": "v2024.3.1",
"libName": "photontargeting",
"headerClassifier": "headers",
"sharedLibrary": true,
"skipInvalidPlatforms": true,
"binaryPlatforms": [
"windowsx86-64",
"linuxathena",
"linuxx86-64",
"osxuniversal"
]
}
],
"javaDependencies": [
{
"groupId": "org.photonvision",
"artifactId": "photonlib-java",
"version": "v2024.3.1"
},
{
"groupId": "org.photonvision",
"artifactId": "photontargeting-java",
"version": "v2024.3.1"
}
]
}
57 changes: 0 additions & 57 deletions vendordeps/photonlib.json

This file was deleted.

Loading