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

auto pub photonlib #26

Closed
wants to merge 9 commits into from
Closed
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
66 changes: 66 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,72 @@ jobs:
with:
name: built-docs
path: docs/build/html

build-photonlib-vendorjson:
name: "Build Vendor JSON"
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Checkout vendor-jsons
uses: actions/checkout@v4
with:
repository: PhotonVision/vendor-json-repo
path: vendor-json-repo

- name: Install Java 17
uses: actions/setup-java@v4
with:
java-version: 17
distribution: temurin
- run: git fetch --tags --force
# Generate the JSON and give it the ""standard""" name maven gives it
- run: |
chmod +x gradlew
./gradlew photon-lib:generateVendorJson
mv photon-lib/build/generated/vendordeps/photonlib.json photon-lib/build/generated/vendordeps/photonlib-json-1.0.json
# Upload it here so it shows up in releases
- uses: actions/upload-artifact@v4
with:
name: photonlib-vendor-json
path: photon-lib/build/generated/vendordeps/photonlib.json

- run: find .
working-directory: vendor-json-repo

- run: |
export VERSION=$(git describe --tags --match=v*)
cd vendor-json-repo
git switch -C photonlib-$VERSION
- run: git status
working-directory: vendor-json-repo

- run: rm ./2025/photonlib-*
name: Nuke old photon releases
working-directory: vendor-json-repo
- run: cp photon-lib/build/generated/vendordeps/photonlib-json-1.0.json ./2025/photonlib-$(git describe --tags --match=v*).json
name: Copy new JSON over
working-directory: vendor-json-repo

# apparently we have to push or gh pr create gets upset
- name: Commit and push
run: |
export VERSION=$(git describe --tags --match=v*)
cd vendor-json-repo
git config user.name "GitHub Actions Bot"
git config user.email "<>"
git add .
git commit -m "Update vendor JSON to $VERSION"
git push --set-upstream origin photonlib-$VERSION -f

# Cut a new release
- run: gh pr create --repo PhotonVision/vendor-json-repo --title "Update PhotonLib to asdf" --body "Automatic vendor JSON update" --base main --head $(git branch --show-current)
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ISSUE: ${{ github.event.issue.html_url }}
working-directory: vendor-json-repo

build-photonlib-host:
env:
MACOSX_DEPLOYMENT_TARGET: 13
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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/>.
*/

package org.photonvision.common.logging;

import edu.wpi.first.util.RuntimeDetector;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.photonvision.common.util.TimedTaskManager;
import org.photonvision.jni.QueuedFileLogger;

/**
* Listens for and reproduces Linux kernel logs, from /var/log/kern.log, into the Photon logger
* ecosystem
*/
public class KernelLogLogger {
private static KernelLogLogger INSTANCE;

public static KernelLogLogger getInstance() {
if (INSTANCE == null) {
INSTANCE = new KernelLogLogger();
}
return INSTANCE;
}

QueuedFileLogger listener = null;
Logger logger = new Logger(KernelLogLogger.class, LogGroup.General);

public KernelLogLogger() {
if (RuntimeDetector.isLinux()) {
logger.info("Listening for klogs on /var/log/dmesg ! Boot logs:");

try {
var bootlog = Files.readAllLines(Path.of("/var/log/dmesg"));
for (var line : bootlog) {
logger.log(line, LogLevel.DEBUG);
}
} catch (IOException e) {
logger.error("Couldn't read /var/log/dmesg - not printing boot logs");
}

listener = new QueuedFileLogger("/var/log/kern.log");
} else {
System.out.println("NOT for klogs");
}

// arbitrary frequency to grab logs. The underlying native buffer will grow unbounded without
// this, lol
TimedTaskManager.getInstance().addTask("outputPrintk", this::outputNewPrintks, 1000);
}

public void outputNewPrintks() {
for (var msg : listener.getNewlines()) {
// We currently set all logs to debug regardless of their actual level
logger.log(msg, LogLevel.DEBUG);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ public enum LogGroup {
Config,
CSCore,
NetworkTables,
System,
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,34 @@
import org.photonvision.common.dataflow.events.OutgoingUIEvent;
import org.photonvision.common.util.TimedTaskManager;

@SuppressWarnings("unused")
/** TODO: get rid of static {} blocks and refactor to singleton pattern */
public class Logger {
private static final HashMap<LogGroup, LogLevel> levelMap = new HashMap<>();
private static final List<LogAppender> currentAppenders = new ArrayList<>();

private static final UILogAppender uiLogAppender = new UILogAppender();

// // TODO why's the logger care about this? split it out
// private static KernelLogLogger klogListener = null;

static {
levelMap.put(LogGroup.Camera, LogLevel.INFO);
levelMap.put(LogGroup.General, LogLevel.INFO);
levelMap.put(LogGroup.WebServer, LogLevel.INFO);
levelMap.put(LogGroup.Data, LogLevel.INFO);
levelMap.put(LogGroup.VisionModule, LogLevel.INFO);
levelMap.put(LogGroup.Config, LogLevel.INFO);
levelMap.put(LogGroup.CSCore, LogLevel.TRACE);
levelMap.put(LogGroup.NetworkTables, LogLevel.DEBUG);
levelMap.put(LogGroup.System, LogLevel.DEBUG);

currentAppenders.add(new ConsoleLogAppender());
currentAppenders.add(uiLogAppender);
addFileAppender(PathManager.getInstance().getLogPath());

cleanLogs(PathManager.getInstance().getLogsDir());
}

public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_BLACK = "\u001B[30m";
public static final String ANSI_RED = "\u001B[31m";
Expand All @@ -50,8 +76,6 @@ public class Logger {
private static final List<Pair<String, LogLevel>> uiBacklog = new ArrayList<>();
private static boolean connected = false;

private static final UILogAppender uiLogAppender = new UILogAppender();

private final String className;
private final LogGroup group;

Expand Down Expand Up @@ -89,27 +113,6 @@ public static String format(
return builder.toString();
}

private static final HashMap<LogGroup, LogLevel> levelMap = new HashMap<>();
private static final List<LogAppender> currentAppenders = new ArrayList<>();

static {
levelMap.put(LogGroup.Camera, LogLevel.INFO);
levelMap.put(LogGroup.General, LogLevel.INFO);
levelMap.put(LogGroup.WebServer, LogLevel.INFO);
levelMap.put(LogGroup.Data, LogLevel.INFO);
levelMap.put(LogGroup.VisionModule, LogLevel.INFO);
levelMap.put(LogGroup.Config, LogLevel.INFO);
levelMap.put(LogGroup.CSCore, LogLevel.TRACE);
levelMap.put(LogGroup.NetworkTables, LogLevel.DEBUG);
}

static {
currentAppenders.add(new ConsoleLogAppender());
currentAppenders.add(uiLogAppender);
addFileAppender(PathManager.getInstance().getLogPath());
cleanLogs(PathManager.getInstance().getLogsDir());
}

@SuppressWarnings("ResultOfMethodCallIgnored")
public static void addFileAppender(Path logFilePath) {
var file = logFilePath.toFile();
Expand Down
2 changes: 1 addition & 1 deletion photon-lib/py/photonlibpy/photonCamera.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def getAllUnreadResults(self) -> List[PhotonPipelineResult]:
pkt = Packet(byteList)
newResult = PhotonPipelineResult.photonStruct.unpack(pkt)
# NT4 allows us to correct the timestamp based on when the message was sent
newResult.ntReceiveTimestampMicros = timestamp / 1e6
newResult.ntReceiveTimestampMicros = timestamp
ret.append(newResult)

return ret
Expand Down
2 changes: 2 additions & 0 deletions photon-lib/py/photonlibpy/targeting/photonPipelineResult.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class PhotonPipelineResult:
ntReceiveTimestampMicros: int = -1

targets: list[PhotonTrackedTarget] = field(default_factory=list)
# Python users beware! We don't currently run a Time Sync Server, so these timestamps are in
# an arbitrary timebase. This is not true in C++ or Java.
metadata: PhotonPipelineMetadata = field(default_factory=PhotonPipelineMetadata)
multiTagResult: Optional[MultiTargetPNPResult] = None

Expand Down
14 changes: 14 additions & 0 deletions photon-lib/src/main/native/cpp/photon/PhotonCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "photon/PhotonCamera.h"

#include <hal/FRCUsageReporting.h>
#include <net/TimeSyncServer.h>

#include <string>
#include <string_view>
Expand Down Expand Up @@ -59,6 +60,11 @@ inline constexpr std::string_view bfw =
">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"
"\n\n";

// bit of a hack -- start a TimeSync server on port 5810 (hard-coded)
static std::mutex g_timeSyncServerMutex;
static bool g_timeSyncServerStarted;
static wpi::tsp::TimeSyncServer timesyncServer{5810};

namespace photon {

constexpr const units::second_t VERSION_CHECK_INTERVAL = 5_s;
Expand Down Expand Up @@ -110,6 +116,14 @@ PhotonCamera::PhotonCamera(nt::NetworkTableInstance instance,
cameraName(cameraName) {
HAL_Report(HALUsageReporting::kResourceType_PhotonCamera, InstanceCount);
InstanceCount++;

{
std::lock_guard lock{g_timeSyncServerMutex};
if (!g_timeSyncServerStarted) {
timesyncServer.Start();
g_timeSyncServerStarted = true;
}
}
}

PhotonCamera::PhotonCamera(const std::string_view cameraName)
Expand Down
54 changes: 54 additions & 0 deletions photon-lib/src/test/native/cpp/PhotonCameraTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* MIT License
*
* Copyright (c) PhotonVision
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include <gtest/gtest.h>
#include <hal/HAL.h>
#include <net/TimeSyncClient.h>
#include <net/TimeSyncServer.h>

#include "photon/PhotonCamera.h"

TEST(TimeSyncProtocolTest, Smoketest) {
using namespace wpi::tsp;
using namespace std::chrono_literals;

// start a server implicitly
photon::PhotonCamera camera{"camera"};

TimeSyncClient client{"127.0.0.1", 5810, 100ms};
client.Start();

for (int i = 0; i < 10; i++) {
std::this_thread::sleep_for(100ms);
TimeSyncClient::Metadata m = client.GetMetadata();

// give us time to warm up
if (i > 5) {
EXPECT_TRUE(m.rtt2 > 0);
EXPECT_TRUE(m.pongsReceived > 0);
}
}

client.Stop();
}
4 changes: 4 additions & 0 deletions photon-lib/src/test/native/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@
* SOFTWARE.
*/

#include <hal/HAL.h>

#include "gtest/gtest.h"

int main(int argc, char** argv) {
HAL_Initialize(500, 0);
::testing::InitGoogleTest(&argc, argv);
int ret = RUN_ALL_TESTS();
HAL_Shutdown();
return ret;
}
3 changes: 3 additions & 0 deletions photon-server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ apply from: "${rootDir}/shared/common.gradle"
dependencies {
implementation project(':photon-core')

// Zip
implementation 'org.zeroturnaround:zt-zip:1.14'

// Needed for Javalin Runtime Logging
implementation "org.slf4j:slf4j-simple:2.0.7"
}
Expand Down
5 changes: 5 additions & 0 deletions photon-server/src/main/java/org/photonvision/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.photonvision.common.hardware.HardwareManager;
import org.photonvision.common.hardware.PiVersion;
import org.photonvision.common.hardware.Platform;
import org.photonvision.common.logging.KernelLogLogger;
import org.photonvision.common.logging.LogGroup;
import org.photonvision.common.logging.LogLevel;
import org.photonvision.common.logging.Logger;
Expand Down Expand Up @@ -437,6 +438,10 @@ public static void main(String[] args) {
Logger.setLevel(LogGroup.General, logLevel);
logger.info("Logging initialized in debug mode.");

// Add Linux kernel log->Photon logger
KernelLogLogger.getInstance();

// Add CSCore->Photon logger
PvCSCoreLogger.getInstance();

logger.debug("Loading ConfigManager...");
Expand Down
Loading
Loading