Skip to content

Commit

Permalink
Started JNI cursed bs crashes for now
Browse files Browse the repository at this point in the history
Signed-off-by: Jade Turner <[email protected]>
  • Loading branch information
spacey-sooty committed Jan 19, 2025
1 parent 0b10ae0 commit 23d9819
Show file tree
Hide file tree
Showing 21 changed files with 162 additions and 341 deletions.
85 changes: 85 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,17 +1,55 @@
plugins {
id "java"
id "cpp"
id "edu.wpi.first.GradleRIO" version "2025.2.1"
id 'edu.wpi.first.GradleJni' version '1.1.0'
id "com.peterabeles.gversion" version "1.10"
id "com.diffplug.spotless" version "6.12.0"
}

import edu.wpi.first.gradlerio.wpi.java.*;
import org.gradle.internal.os.OperatingSystem;

def configureExecutableNatives(JavaForkOptions t, Provider<ExtractNativeJavaArtifacts> extract) {
Task tt = (Task) t
tt.dependsOn extract
Provider<DirectoryProperty> destDir = project.getProviders().provider({
return extract.get().getDestinationDirectory()
})
tt.getInputs().dir(destDir)
tt.doFirst(new TestTaskDoFirstAction(t, destDir))
}

def configureJavaExecTask(JavaExec t, boolean debug) {
def extract;

if (debug) {
extract = project.getTasks().register("extractDebugNative2", ExtractNativeJavaArtifacts.class)
} else {
extract = project.getTasks().register("extractReleaseNative2", ExtractNativeJavaArtifacts.class)
}

configureExecutableNatives(t, extract)
if (OperatingSystem.current().isMacOsX()) {
t.jvmArgs("-XstartOnFirstThread")
}
}

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

def ROBOT_MAIN_CLASS = "org.curtinfrc.frc2025.Main"

nativeUtils {
exportsConfigs {
// Only export explicit symbols from driver library
TrajgenJNI {
}
}
}

// Define my targets (RoboRIO) and artifacts (deployable files)
// This is added by GradleRIO's backing project DeployUtils.
deploy {
Expand Down Expand Up @@ -54,6 +92,46 @@ deploy {
}
}

task refreshCompileCommands(type: Exec) {
workingDir = rootDir
def args = [
"rm",
"-rf",
"$buildDir/TargetedCompileCommands"
]
commandLine args
}

model {
components {
TrajgenJNI(JniNativeLibrarySpec) {
targetPlatform wpi.platforms.desktop
enableCheckTask = true
javaCompileTasks << compileJava
jniCrossCompileOptions << JniCrossCompileOptions(wpi.platforms.desktop)

sources {
cpp {
source {
srcDirs 'src/main/native/cpp'
include '**/*.cpp'
}

exportedHeaders {
srcDir 'src/main/native/include'
}
}
}

binaries.all {
lib project: ':trajoptlib', library: 'TrajoptLib', linkage: 'static'
}

nativeUtils.useRequiredLibrary(it, "wpilib_shared")
}
}
}

def deployArtifact = deploy.targets.roborio.artifacts.frcJava

// Set to true to use debug for JNI.
Expand Down Expand Up @@ -88,6 +166,13 @@ task(regenerateTrajectories, dependsOn: "classes", type: Exec) {

project.compileJava.finalizedBy(regenerateTrajectories)

task(generateTrajectories, dependsOn: "classes", type: JavaExec) {
mainClass = "org.curtinfrc.frc2025.auto.TrajectoryGenerator"
classpath = sourceSets.main.runtimeClasspath
jvmArgs += "-Djava.library.path=${buildDir}/libs/trajgenJNI/shared/release"
}
configureJavaExecTask(generateTrajectories, project.frc.getDebugOrDefault(false))

// Defining my dependencies. In this case, WPILib (+ friends), and vendor libraries.
// Also defines JUnit 4.
dependencies {
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/curtinfrc/frc2025/BuildConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ public final class BuildConstants {
public static final String MAVEN_GROUP = "";
public static final String MAVEN_NAME = "2025-Reefscape";
public static final String VERSION = "unspecified";
public static final int GIT_REVISION = 33;
public static final String GIT_SHA = "c02ca801e6b1ef3a366b050ea65a8162bd71a85e";
public static final String GIT_DATE = "2025-01-19 21:06:05 AWST";
public static final int GIT_REVISION = 34;
public static final String GIT_SHA = "0b10ae048494bfc84b04e14e52241ce2efa3584b";
public static final String GIT_DATE = "2025-01-19 21:06:40 AWST";
public static final String GIT_BRANCH = "HEAD";
public static final String BUILD_DATE = "2025-01-19 21:06:18 AWST";
public static final long BUILD_UNIX_TIME = 1737291978786L;
public static final String BUILD_DATE = "2025-01-19 21:07:01 AWST";
public static final long BUILD_UNIX_TIME = 1737292021607L;
public static final int DIRTY = 1;

private BuildConstants() {}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.curtinfrc.frc2025.auto;

public class TrajectoryGenerator {
public static void main(String... args) {
TrajgenJNI.helloWorld();
}
}
17 changes: 17 additions & 0 deletions src/main/java/org/curtinfrc/frc2025/auto/TrajgenJNI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.curtinfrc.frc2025.auto;

import edu.wpi.first.util.RuntimeLoader;
import java.io.IOException;

public class TrajgenJNI {
static {
try {
RuntimeLoader.loadLibrary("TrajgenJNI");
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}

public static native void helloWorld();
}
12 changes: 12 additions & 0 deletions src/main/native/cpp/TrajectoryGeneration.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "TrajectoryGeneration.h"
#include "trajopt/SwerveTrajectoryGenerator.hpp"
#include "trajopt/path/Path.hpp"
#include <vector>

trajopt::SwerveSolution trajgen::CreateInitialGuess(std::vector<trajopt::Waypoint> trajectory) {
return trajopt::SwerveSolution{};
}

trajopt::SwerveTrajectory trajgen::Generate(InputTrajectory trajectory) {
return trajopt::SwerveTrajectory{};
}
20 changes: 20 additions & 0 deletions src/main/native/cpp/TrajgenJNI.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <jni.h>
#include <wpi/array.h>
#include <wpi/jni_util.h>

#include "fmt/base.h"
#include "jni_md.h"

extern "C" {
/*
* Class: frc_robot_jni_ShooterTrajoptJNI
* Method: calculateTrajectory
* Signature: ([D????)V
*/
JNIEXPORT void JNICALL
Java_org_curtinfrc_frc2025_auto_TrajgenJNI_helloWorld
(JNIEnv* env, jclass)
{
fmt::println("Hello world!");
}
} // extern "C"
16 changes: 16 additions & 0 deletions src/main/native/include/TrajectoryGeneration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include "trajopt/SwerveTrajectoryGenerator.hpp"
#include "trajopt/constraint/Constraint.hpp"
#include "trajopt/path/Path.hpp"
#include <vector>

namespace trajgen {
struct InputTrajectory {
std::vector<trajopt::Waypoint> waypoints;
std::vector<trajopt::Constraint> constraints;
};

trajopt::SwerveSolution CreateInitialGuess(std::vector<trajopt::Waypoint> waypoints);
trajopt::SwerveTrajectory Generate(InputTrajectory trajectory);
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 23d9819

Please sign in to comment.