From 3dfc14fe4f0152d5eeccfe988d65dee975edc060 Mon Sep 17 00:00:00 2001 From: Matt Morley Date: Thu, 19 Sep 2024 20:01:03 -0700 Subject: [PATCH] Fix things --- photon-lib/nanobind | 1 - photon-targeting/build.gradle | 7 +- ...brationHelper.java => TimeSyncClient.java} | 23 ++-- .../org/photonvision/jni/TimeSyncServer.java} | 33 +----- .../native/cpp/net/TimeSyncClientServer.cpp | 35 +++--- .../native/include/net/TimeSyncClientServer.h | 35 ++++-- .../src/main/native/jni/TimeSyncClientJNI.cpp | 104 ++++++++++++++++++ .../src/main/native/jni/TimeSyncServerJNI.cpp | 71 ++++++++++++ 8 files changed, 242 insertions(+), 67 deletions(-) delete mode 160000 photon-lib/nanobind rename photon-targeting/src/main/java/org/photonvision/jni/{CalibrationHelper.java => TimeSyncClient.java} (57%) rename photon-targeting/src/main/{native/jni/CalibrationHelperJNI.cpp => java/org/photonvision/jni/TimeSyncServer.java} (51%) create mode 100644 photon-targeting/src/main/native/jni/TimeSyncClientJNI.cpp create mode 100644 photon-targeting/src/main/native/jni/TimeSyncServerJNI.cpp diff --git a/photon-lib/nanobind b/photon-lib/nanobind deleted file mode 160000 index 9641bb7151..0000000000 --- a/photon-lib/nanobind +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 9641bb7151f04120013b812789b3ebdfa7e7324f diff --git a/photon-targeting/build.gradle b/photon-targeting/build.gradle index 54680ee0e5..361f91eae9 100644 --- a/photon-targeting/build.gradle +++ b/photon-targeting/build.gradle @@ -55,13 +55,13 @@ model { nativeUtils.useRequiredLibrary(it, "opencv_shared") } "${nativeName}JNI"(JniNativeLibrarySpec) { - + enableCheckTask project.hasProperty('doJniCheck') javaCompileTasks << compileJava jniCrossCompileOptions << JniCrossCompileOptions(nativeUtils.wpi.platforms.roborio) jniCrossCompileOptions << JniCrossCompileOptions(nativeUtils.wpi.platforms.linuxarm32) jniCrossCompileOptions << JniCrossCompileOptions(nativeUtils.wpi.platforms.linuxarm64) - + sources { cpp { source { @@ -83,7 +83,7 @@ model { binaries.withType(SharedLibraryBinarySpec) { binary -> // check that we're building for the platform (per PArchOverride/wpilib plat detection) if (binary.targetPlatform.name == jniPlatform) { - + // only include release binaries (hard coded for now) def isDebug = binary.buildType.name.contains('debug') if (!isDebug) { @@ -95,7 +95,6 @@ model { // And (not sure if this is a hack) make the jar task depend on the build task dependsOn binary.identifier.projectScopedName } - } } } diff --git a/photon-targeting/src/main/java/org/photonvision/jni/CalibrationHelper.java b/photon-targeting/src/main/java/org/photonvision/jni/TimeSyncClient.java similarity index 57% rename from photon-targeting/src/main/java/org/photonvision/jni/CalibrationHelper.java rename to photon-targeting/src/main/java/org/photonvision/jni/TimeSyncClient.java index 230ca72b8e..b2b7f90443 100644 --- a/photon-targeting/src/main/java/org/photonvision/jni/CalibrationHelper.java +++ b/photon-targeting/src/main/java/org/photonvision/jni/TimeSyncClient.java @@ -17,18 +17,21 @@ package org.photonvision.jni; -public class CalibrationHelper { - public static class CalResult {} +public class TimeSyncClient { + public static class PingMetadata { + long offset; + long pingsSent; + long pingsRecieved; + long lastPongTime; + } - public static native long Create(int width, int height, long overlayMatPtr, double tolerance); + public static native long create(String serverIP, int serverPort, double pingIntervalSeconds); - public static native long Destroy(); + public static native void start(long handle); - public static native CalResult Detect(long inputImg, long outputImg); + public static native void stop(long handle); - public static void main(String[] args) { - System.load( - "/home/matt/Documents/GitHub/photonvision/photon-core/build/libs/photoncoreJNI/shared/linuxx86-64/release/libphotoncorejni.so"); - System.out.println(Create(1, 2, 3, 4)); - } + public static native long getOffset(long handle); + + public static native PingMetadata getLatestMetadata(); } diff --git a/photon-targeting/src/main/native/jni/CalibrationHelperJNI.cpp b/photon-targeting/src/main/java/org/photonvision/jni/TimeSyncServer.java similarity index 51% rename from photon-targeting/src/main/native/jni/CalibrationHelperJNI.cpp rename to photon-targeting/src/main/java/org/photonvision/jni/TimeSyncServer.java index dfe208ed07..b6830b3efa 100644 --- a/photon-targeting/src/main/native/jni/CalibrationHelperJNI.cpp +++ b/photon-targeting/src/main/java/org/photonvision/jni/TimeSyncServer.java @@ -15,35 +15,12 @@ * along with this program. If not, see . */ -#include +package org.photonvision.jni; -#include +public class TimeSyncServer { + public static native long create(int port); -#include -#include -#include + public static native void start(long handle); - - -extern "C" { - -/* - * Class: org_photonvision_jni_CalibrationHelper - * Method: Create - * Signature: (IIJD)J - */ -JNIEXPORT jlong JNICALL -Java_org_photonvision_jni_CalibrationHelper_Create - (JNIEnv*, jclass, jint, jint, jlong, jdouble) -{ - - cv::Mat mat = cv::imread( - "/home/matt/Documents/GitHub/photonvision/test-resources/testimages/2022/" - "WPI/FarLaunchpad13ft10in.png"); - - std::printf("mat size %i %i\n", mat.rows, mat.cols); - - return 0; + public static native void stop(long handle); } - -} // extern "C" diff --git a/photon-targeting/src/main/native/cpp/net/TimeSyncClientServer.cpp b/photon-targeting/src/main/native/cpp/net/TimeSyncClientServer.cpp index 6ab736e758..1592502795 100644 --- a/photon-targeting/src/main/native/cpp/net/TimeSyncClientServer.cpp +++ b/photon-targeting/src/main/native/cpp/net/TimeSyncClientServer.cpp @@ -1,9 +1,26 @@ -// Copyright (c) FIRST and other WPILib contributors. -// Open Source Software; you can modify and/or share it under the terms of -// the WPILib BSD license file in the root directory of this project. +/* + * Copyright (C) Photon Vision. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ #include "net/TimeSyncClientServer.h" +#include +#include +#include + #include #include #include @@ -14,11 +31,7 @@ #include #include -// #include -#include #include -#include -#include #include "ntcore_cpp.h" @@ -169,9 +182,7 @@ void wpi::TimeSyncServer::Start() { m_udp->StartRecv(); } -void wpi::TimeSyncServer::Stop() { - m_loopRunner.Stop(); -} +void wpi::TimeSyncServer::Stop() { m_loopRunner.Stop(); } void wpi::TimeSyncClient::Tick() { fmt::println("wpi::TimeSyncClient::Tick"); @@ -286,9 +297,7 @@ void wpi::TimeSyncClient::Start() { [this](uv::Loop&) { m_pingTimer->Start(m_loopDelay, m_loopDelay); }); } -void wpi::TimeSyncClient::Stop() { - m_loopRunner.Stop(); -} +void wpi::TimeSyncClient::Stop() { m_loopRunner.Stop(); } int64_t wpi::TimeSyncClient::GetOffset() { std::lock_guard lock{m_offsetMutex}; diff --git a/photon-targeting/src/main/native/include/net/TimeSyncClientServer.h b/photon-targeting/src/main/native/include/net/TimeSyncClientServer.h index be45e56876..d13bee8947 100644 --- a/photon-targeting/src/main/native/include/net/TimeSyncClientServer.h +++ b/photon-targeting/src/main/native/include/net/TimeSyncClientServer.h @@ -1,9 +1,29 @@ -// Copyright (c) FIRST and other WPILib contributors. -// Open Source Software; you can modify and/or share it under the terms of -// the WPILib BSD license file in the root directory of this project. +/* + * Copyright (C) Photon Vision. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ #pragma once +#include +#include +#include +#include +#include +#include + #include #include #include @@ -17,14 +37,7 @@ #include #include -// #include -#include #include -#include -#include -#include -#include -#include #include "ntcore_cpp.h" @@ -59,7 +72,7 @@ class TimeSyncServer { public: explicit TimeSyncServer(int port = 5810, - std::function timeProvider = nt::Now); + std::function timeProvider = nt::Now); /** * Start listening for pings diff --git a/photon-targeting/src/main/native/jni/TimeSyncClientJNI.cpp b/photon-targeting/src/main/native/jni/TimeSyncClientJNI.cpp new file mode 100644 index 0000000000..61a50775f7 --- /dev/null +++ b/photon-targeting/src/main/native/jni/TimeSyncClientJNI.cpp @@ -0,0 +1,104 @@ +/* + * Copyright (C) Photon Vision. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include + +#include + +#include +#include +#include + +#include "net/TimeSyncClientServer.h" + +using namespace wpi; + +extern "C" { + +/* + * Class: org_photonvision_jni_TimeSyncClient + * Method: create + * Signature: (Ljava/lang/String;ID)J + */ +JNIEXPORT jlong JNICALL +Java_org_photonvision_jni_TimeSyncClient_create + (JNIEnv* env, jclass, jstring name, jint port, jdouble interval) +{ + using namespace std::chrono_literals; + + const char* c_name{env->GetStringUTFChars(name, 0)}; + std::string cpp_name{c_name}; + jlong ret{reinterpret_cast( + new TimeSyncClient(cpp_name, static_cast(port), + std::chrono::duration_cast( + std::chrono::duration(interval))))}; + env->ReleaseStringUTFChars(name, c_name); + return ret; +} + +/* + * Class: org_photonvision_jni_TimeSyncClient + * Method: start + * Signature: (J)V + */ +JNIEXPORT void JNICALL +Java_org_photonvision_jni_TimeSyncClient_start + (JNIEnv*, jclass, jlong ptr) +{ + TimeSyncClient* server = reinterpret_cast(ptr); + server->Start(); +} + +/* + * Class: org_photonvision_jni_TimeSyncClient + * Method: stop + * Signature: (J)V + */ +JNIEXPORT void JNICALL +Java_org_photonvision_jni_TimeSyncClient_stop + (JNIEnv*, jclass, jlong ptr) +{ + TimeSyncClient* server = reinterpret_cast(ptr); + server->Stop(); +} + +/* + * Class: org_photonvision_jni_TimeSyncClient + * Method: getOffset + * Signature: (J)J + */ +JNIEXPORT jlong JNICALL +Java_org_photonvision_jni_TimeSyncClient_getOffset + (JNIEnv*, jclass, jlong ptr) +{ + TimeSyncClient* server = reinterpret_cast(ptr); + return server->GetOffset(); +} + +/* + * Class: org_photonvision_jni_TimeSyncClient + * Method: getLatestMetadata + * Signature: ()Ljava/lang/Object; + */ +JNIEXPORT jobject JNICALL +Java_org_photonvision_jni_TimeSyncClient_getLatestMetadata + (JNIEnv*, jclass) +{ + return nullptr; +} + +} // extern "C" diff --git a/photon-targeting/src/main/native/jni/TimeSyncServerJNI.cpp b/photon-targeting/src/main/native/jni/TimeSyncServerJNI.cpp new file mode 100644 index 0000000000..afaa2287f5 --- /dev/null +++ b/photon-targeting/src/main/native/jni/TimeSyncServerJNI.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (C) Photon Vision. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include + +#include + +#include +#include +#include + +#include "net/TimeSyncClientServer.h" + +using namespace wpi; + +extern "C" { + +/* + * Class: org_photonvision_jni_TimeSyncServer + * Method: create + * Signature: (I)J + */ +JNIEXPORT jlong JNICALL +Java_org_photonvision_jni_TimeSyncServer_create + (JNIEnv*, jclass, jint port) +{ + return reinterpret_cast(new TimeSyncServer(port)); +} + +/* + * Class: org_photonvision_jni_TimeSyncServer + * Method: start + * Signature: (J)V + */ +JNIEXPORT void JNICALL +Java_org_photonvision_jni_TimeSyncServer_start + (JNIEnv*, jclass, jlong ptr) +{ + TimeSyncServer* server = reinterpret_cast(ptr); + server->Start(); +} + +/* + * Class: org_photonvision_jni_TimeSyncServer + * Method: stop + * Signature: (J)V + */ +JNIEXPORT void JNICALL +Java_org_photonvision_jni_TimeSyncServer_stop + (JNIEnv*, jclass, jlong ptr) +{ + TimeSyncServer* server = reinterpret_cast(ptr); + server->Stop(); +} + +} // extern "C"