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("