PhotonVision C++ dev-v2025.0.0-beta-8-2-gbd1c5c03
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 <wpinet/EventLoopRunner.h>
21#include <wpinet/UDPClient.h>
22#include <wpinet/uv/Buffer.h>
23#include <wpinet/uv/Timer.h>
24#include <wpinet/uv/Udp.h>
25
26#include <atomic>
27#include <chrono>
28#include <cstdlib>
29#include <cstring>
30#include <ctime>
31#include <functional>
32#include <iostream>
33#include <memory>
34#include <mutex>
35#include <string>
36#include <thread>
37
38#include <frc/filter/MedianFilter.h>
39#include <wpi/Logger.h>
40#include <wpi/print.h>
41#include <wpi/static_circular_buffer.h>
42#include <wpi/struct/Struct.h>
43
44#include "TimeSyncStructs.h"
45#include "ntcore_cpp.h"
46
47namespace wpi {
48namespace tsp {
49
51 public:
52 struct Metadata {
53 int64_t offset{0};
54 int64_t rtt2{0};
55 size_t pingsSent{0};
56 size_t pongsReceived{0};
57 uint64_t lastPongTime{0};
58 };
59
60 private:
61 using SharedUdpPtr = std::shared_ptr<uv::Udp>;
62 using SharedTimerPtr = std::shared_ptr<uv::Timer>;
63
64 EventLoopRunner m_loopRunner{};
65
66 wpi::Logger m_logger;
67 std::function<uint64_t()> m_timeProvider;
68
69 SharedUdpPtr m_udp;
70 SharedTimerPtr m_pingTimer;
71
72 std::string m_serverIP;
73 int m_serverPort;
74
75 std::chrono::milliseconds m_loopDelay;
76
77 std::mutex m_offsetMutex{};
78 Metadata m_metadata{};
79
80 // We only allow the most recent ping to stay alive, so only keep track of it
81 TspPing m_lastPing{};
82
83 // 30s is a reasonable guess
84 frc::MedianFilter<int64_t> m_lastOffsets{30};
85
86 void Tick();
87
88 void UdpCallback(uv::Buffer& buf, size_t nbytes, const sockaddr& sender,
89 unsigned flags);
90
91 public:
92 TimeSyncClient(std::string_view server, int remote_port,
93 std::chrono::milliseconds ping_delay);
94
95 void Start();
96 void Stop();
97 int64_t GetOffset();
99};
100
101} // namespace tsp
102} // namespace wpi
Definition TimeSyncClient.h:50
TimeSyncClient(std::string_view server, int remote_port, std::chrono::milliseconds ping_delay)
Definition TimeSyncServer.h:45
Definition TimeSyncClient.h:52
int64_t rtt2
Definition TimeSyncClient.h:54
uint64_t lastPongTime
Definition TimeSyncClient.h:57
size_t pingsSent
Definition TimeSyncClient.h:55
int64_t offset
Definition TimeSyncClient.h:53
size_t pongsReceived
Definition TimeSyncClient.h:56