From 6c7a174424c264dab67cf42813f76d127657ae8d Mon Sep 17 00:00:00 2001 From: Chris Gerth Date: Fri, 3 Jan 2025 12:23:27 -0600 Subject: [PATCH] Adding timeSyncServer for Python (#1675) --- photon-lib/py/photonlibpy/photonCamera.py | 4 + .../py/photonlibpy/timesync/__init__.py | 1 + .../py/photonlibpy/timesync/timeSyncServer.py | 94 +++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 photon-lib/py/photonlibpy/timesync/__init__.py create mode 100644 photon-lib/py/photonlibpy/timesync/timeSyncServer.py diff --git a/photon-lib/py/photonlibpy/photonCamera.py b/photon-lib/py/photonlibpy/photonCamera.py index bcbc70d90..512e3633b 100644 --- a/photon-lib/py/photonlibpy/photonCamera.py +++ b/photon-lib/py/photonlibpy/photonCamera.py @@ -27,6 +27,7 @@ from wpilib import RobotController, Timer from .packet import Packet from .targeting.photonPipelineResult import PhotonPipelineResult +from .timesync.timeSyncServer import inst from .version import PHOTONLIB_VERSION # type: ignore[import-untyped] @@ -104,6 +105,9 @@ class PhotonCamera: self._prevHeartbeat = 0 self._prevHeartbeatChangeTime = Timer.getFPGATimestamp() + # Start the time sync server + inst.start() + def getAllUnreadResults(self) -> List[PhotonPipelineResult]: """ The list of pipeline results sent by PhotonVision since the last call to getAllUnreadResults(). diff --git a/photon-lib/py/photonlibpy/timesync/__init__.py b/photon-lib/py/photonlibpy/timesync/__init__.py new file mode 100644 index 000000000..91438c9ee --- /dev/null +++ b/photon-lib/py/photonlibpy/timesync/__init__.py @@ -0,0 +1 @@ +# no one but us chickens diff --git a/photon-lib/py/photonlibpy/timesync/timeSyncServer.py b/photon-lib/py/photonlibpy/timesync/timeSyncServer.py new file mode 100644 index 000000000..f7a860dd9 --- /dev/null +++ b/photon-lib/py/photonlibpy/timesync/timeSyncServer.py @@ -0,0 +1,94 @@ +import logging +import socket +import struct +import threading +from typing import Callable, Optional + +from wpilib import Timer + + +class TspPing: + def __init__(self, version: int, message_id: int, client_time: int): + self.version = version + self.message_id = message_id + self.client_time = client_time + + @staticmethod + def unpack(data: bytes) -> "TspPing": + # Unpack using struct.unpack + version, message_id, client_time = struct.unpack(" bytes: + # Pack using struct.pack + return struct.pack(" bytes: + # Pack using struct.pack + return struct.pack( + " "TspPong": + # Unpack using struct.unpack + version, message_id, client_time, server_time = struct.unpack("