PhotonVision C++ v2026.2.2
Loading...
Searching...
No Matches
TimeSyncClient.h
Go to the documentation of this file.
1/*
2 * Copyright (C) Photon Vision.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include <atomic>
21#include <chrono>
22#include <cstdlib>
23#include <cstring>
24#include <ctime>
25#include <functional>
26#include <iostream>
27#include <memory>
28#include <mutex>
29#include <string>
30#include <thread>
31
32#include <frc/filter/MedianFilter.h>
33#include <wpi/Logger.h>
34#include <wpi/print.h>
35#include <wpi/static_circular_buffer.h>
36#include <wpi/struct/Struct.h>
37#include <wpinet/EventLoopRunner.h>
38#include <wpinet/UDPClient.h>
39#include <wpinet/uv/Buffer.h>
40#include <wpinet/uv/Timer.h>
41#include <wpinet/uv/Udp.h>
42
43#include "TimeSyncStructs.h"
44#include "ntcore_cpp.h"
45
46namespace wpi {
47namespace tsp {
48
50 public:
51 struct Metadata {
52 int64_t offset{0};
53 int64_t rtt2{0};
54 size_t pingsSent{0};
55 size_t pongsReceived{0};
56 uint64_t lastPongTime{0};
57 };
58
59 private:
60 using SharedUdpPtr = std::shared_ptr<uv::Udp>;
61 using SharedTimerPtr = std::shared_ptr<uv::Timer>;
62
63 EventLoopRunner m_loopRunner{};
64
65 wpi::Logger m_logger;
66 std::function<uint64_t()> m_timeProvider;
67
68 SharedUdpPtr m_udp;
69 SharedTimerPtr m_pingTimer;
70
71 std::string m_serverIP;
72 int m_serverPort;
73
74 std::chrono::milliseconds m_loopDelay;
75
76 std::mutex m_offsetMutex{};
77 Metadata m_metadata{};
78
79 // We only allow the most recent ping to stay alive, so only keep track of it
80 TspPing m_lastPing{};
81
82 // 30s is a reasonable guess
83 frc::MedianFilter<int64_t> m_lastOffsets{30};
84
85 void Tick();
86
87 void UdpCallback(uv::Buffer& buf, size_t nbytes, const sockaddr& sender,
88 unsigned flags);
89
90 public:
91 TimeSyncClient(std::string_view server, int remote_port,
92 std::chrono::milliseconds ping_delay);
93
94 void Start();
95 void Stop();
96 int64_t GetOffset();
98
99 // public for testability
100 void UpdateStatistics(uint64_t pong_local_time, wpi::tsp::TspPing ping,
101 wpi::tsp::TspPong pong);
102};
103
104} // namespace tsp
105} // namespace wpi
Definition TimeSyncClient.h:49
TimeSyncClient(std::string_view server, int remote_port, std::chrono::milliseconds ping_delay)
void UpdateStatistics(uint64_t pong_local_time, wpi::tsp::TspPing ping, wpi::tsp::TspPong pong)
Definition TimeSyncServer.h:44
Definition TimeSyncClient.h:51
int64_t rtt2
Definition TimeSyncClient.h:53
uint64_t lastPongTime
Definition TimeSyncClient.h:56
size_t pingsSent
Definition TimeSyncClient.h:54
int64_t offset
Definition TimeSyncClient.h:52
size_t pongsReceived
Definition TimeSyncClient.h:55
Definition TimeSyncStructs.h:26
Definition TimeSyncStructs.h:32