Skip to content

Commit

Permalink
Fix things
Browse files Browse the repository at this point in the history
  • Loading branch information
mcm001 committed Sep 20, 2024
1 parent dd9c619 commit 3dfc14f
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 67 deletions.
1 change: 0 additions & 1 deletion photon-lib/nanobind
Submodule nanobind deleted from 9641bb
7 changes: 3 additions & 4 deletions photon-targeting/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand All @@ -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
}

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,12 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include <org_photonvision_jni_CalibrationHelper.h>
package org.photonvision.jni;

#include <cstdio>
public class TimeSyncServer {
public static native long create(int port);

#include <opencv2/core.hpp>
#include <opencv2/core/mat.hpp>
#include <opencv2/imgcodecs.hpp>
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"
35 changes: 22 additions & 13 deletions photon-targeting/src/main/native/cpp/net/TimeSyncClientServer.cpp
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

#include "net/TimeSyncClientServer.h"

#include <fmt/core.h>
#include <wpinet/UDPClient.h>
#include <wpinet/uv/util.h>

#include <atomic>
#include <chrono>
#include <cstdlib>
Expand All @@ -14,11 +31,7 @@
#include <thread>

#include <wpi/Logger.h>
// #include <wpi/print.h>
#include <fmt/core.h>
#include <wpi/struct/Struct.h>
#include <wpinet/UDPClient.h>
#include <wpinet/uv/util.h>

#include "ntcore_cpp.h"

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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};
Expand Down
35 changes: 24 additions & 11 deletions photon-targeting/src/main/native/include/net/TimeSyncClientServer.h
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

#pragma once

#include <fmt/core.h>
#include <wpinet/EventLoopRunner.h>
#include <wpinet/UDPClient.h>
#include <wpinet/uv/Buffer.h>
#include <wpinet/uv/Timer.h>
#include <wpinet/uv/Udp.h>

#include <atomic>
#include <chrono>
#include <cstdlib>
Expand All @@ -17,14 +37,7 @@
#include <thread>

#include <wpi/Logger.h>
// #include <wpi/print.h>
#include <fmt/core.h>
#include <wpi/struct/Struct.h>
#include <wpinet/EventLoopRunner.h>
#include <wpinet/UDPClient.h>
#include <wpinet/uv/Buffer.h>
#include <wpinet/uv/Timer.h>
#include <wpinet/uv/Udp.h>

#include "ntcore_cpp.h"

Expand Down Expand Up @@ -59,7 +72,7 @@ class TimeSyncServer {

public:
explicit TimeSyncServer(int port = 5810,
std::function<uint64_t()> timeProvider = nt::Now);
std::function<uint64_t()> timeProvider = nt::Now);

/**
* Start listening for pings
Expand Down
104 changes: 104 additions & 0 deletions photon-targeting/src/main/native/jni/TimeSyncClientJNI.cpp
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

#include <org_photonvision_jni_TimeSyncClient.h>

#include <cstdio>

#include <opencv2/core.hpp>
#include <opencv2/core/mat.hpp>
#include <opencv2/imgcodecs.hpp>

#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<jlong>(
new TimeSyncClient(cpp_name, static_cast<int>(port),
std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::duration<double>(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<TimeSyncClient*>(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<TimeSyncClient*>(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<TimeSyncClient*>(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"
Loading

0 comments on commit 3dfc14f

Please sign in to comment.