Skip to content

Commit fc8ecac

Browse files
authored
Create TSP Server in C++ photonlib (#1516)
Automatically starts a TCP server in C++. Also adds warnings to Python.
1 parent 75e2498 commit fc8ecac

File tree

8 files changed

+82
-4
lines changed

8 files changed

+82
-4
lines changed

photon-lib/py/photonlibpy/photonCamera.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def getAllUnreadResults(self) -> List[PhotonPipelineResult]:
124124
pkt = Packet(byteList)
125125
newResult = PhotonPipelineResult.photonStruct.unpack(pkt)
126126
# NT4 allows us to correct the timestamp based on when the message was sent
127-
newResult.ntReceiveTimestampMicros = timestamp / 1e6
127+
newResult.ntReceiveTimestampMicros = timestamp
128128
ret.append(newResult)
129129

130130
return ret

photon-lib/py/photonlibpy/targeting/photonPipelineResult.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class PhotonPipelineResult:
2424
ntReceiveTimestampMicros: int = -1
2525

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

photon-lib/src/main/native/cpp/photon/PhotonCamera.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "photon/PhotonCamera.h"
2626

2727
#include <hal/FRCUsageReporting.h>
28+
#include <net/TimeSyncServer.h>
2829

2930
#include <string>
3031
#include <string_view>
@@ -59,6 +60,11 @@ inline constexpr std::string_view bfw =
5960
">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"
6061
"\n\n";
6162

63+
// bit of a hack -- start a TimeSync server on port 5810 (hard-coded)
64+
static std::mutex g_timeSyncServerMutex;
65+
static bool g_timeSyncServerStarted;
66+
static wpi::tsp::TimeSyncServer timesyncServer{5810};
67+
6268
namespace photon {
6369

6470
constexpr const units::second_t VERSION_CHECK_INTERVAL = 5_s;
@@ -110,6 +116,14 @@ PhotonCamera::PhotonCamera(nt::NetworkTableInstance instance,
110116
cameraName(cameraName) {
111117
HAL_Report(HALUsageReporting::kResourceType_PhotonCamera, InstanceCount);
112118
InstanceCount++;
119+
120+
{
121+
std::lock_guard lock{g_timeSyncServerMutex};
122+
if (!g_timeSyncServerStarted) {
123+
timesyncServer.Start();
124+
g_timeSyncServerStarted = true;
125+
}
126+
}
113127
}
114128

115129
PhotonCamera::PhotonCamera(const std::string_view cameraName)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) PhotonVision
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
#include <gtest/gtest.h>
26+
#include <hal/HAL.h>
27+
#include <net/TimeSyncClient.h>
28+
#include <net/TimeSyncServer.h>
29+
30+
#include "photon/PhotonCamera.h"
31+
32+
TEST(TimeSyncProtocolTest, Smoketest) {
33+
using namespace wpi::tsp;
34+
using namespace std::chrono_literals;
35+
36+
// start a server implicitly
37+
photon::PhotonCamera camera{"camera"};
38+
39+
TimeSyncClient client{"127.0.0.1", 5810, 100ms};
40+
client.Start();
41+
42+
for (int i = 0; i < 10; i++) {
43+
std::this_thread::sleep_for(100ms);
44+
TimeSyncClient::Metadata m = client.GetMetadata();
45+
46+
// give us time to warm up
47+
if (i > 5) {
48+
EXPECT_TRUE(m.rtt2 > 0);
49+
EXPECT_TRUE(m.pongsReceived > 0);
50+
}
51+
}
52+
53+
client.Stop();
54+
}

photon-lib/src/test/native/cpp/main.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@
2222
* SOFTWARE.
2323
*/
2424

25+
#include <hal/HAL.h>
26+
2527
#include "gtest/gtest.h"
2628

2729
int main(int argc, char** argv) {
30+
HAL_Initialize(500, 0);
2831
::testing::InitGoogleTest(&argc, argv);
2932
int ret = RUN_ALL_TESTS();
33+
HAL_Shutdown();
3034
return ret;
3135
}

photon-targeting/src/test/native/cpp/main.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@
1515
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1616
*/
1717

18+
#include <hal/HAL.h>
19+
1820
#include "gtest/gtest.h"
1921

2022
int main(int argc, char** argv) {
23+
HAL_Initialize(500, 0);
2124
::testing::InitGoogleTest(&argc, argv);
22-
return RUN_ALL_TESTS();
25+
int ret = RUN_ALL_TESTS();
26+
HAL_Shutdown();
27+
return ret;
2328
}

photon-targeting/src/test/native/cpp/net/TimeSyncTest.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ TEST(TimeSyncProtocolTest, Smoketest) {
2424
using namespace wpi::tsp;
2525
using namespace std::chrono_literals;
2626

27-
HAL_Initialize(500, 0);
28-
2927
TimeSyncServer server{5812};
3028
TimeSyncClient client{"127.0.0.1", 5812, 100ms};
3129

photonlib-cpp-examples/poseest/src/test/cpp/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@ int main(int argc, char** argv) {
3030
HAL_Initialize(500, 0);
3131
::testing::InitGoogleTest(&argc, argv);
3232
int ret = RUN_ALL_TESTS();
33+
HAL_Shutdown();
3334
return ret;
3435
}

0 commit comments

Comments
 (0)