-
Notifications
You must be signed in to change notification settings - Fork 15
/
test_acoustic_comms.cc
179 lines (144 loc) · 5.6 KB
/
test_acoustic_comms.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* Copyright (C) 2021 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/*
* Development of this module has been funded by the Monterey Bay Aquarium
* Research Institute (MBARI) and the David and Lucile Packard Foundation
*/
#include <gtest/gtest.h>
#include <chrono>
#include <condition_variable>
#include <mutex>
#include <lrauv_gazebo_plugins/lrauv_acoustic_message.pb.h>
#include <lrauv_gazebo_plugins/comms/CommsClient.hh>
#include <lrauv_gazebo_plugins/comms/CommsPacket.hh>
#include <google/protobuf/util/message_differencer.h>
#include "lrauv_system_tests/TestFixture.hh"
#include "TestConstants.hh"
using namespace tethys;
using namespace lrauv_system_tests;
using MessageDifferencer =
google::protobuf::util::MessageDifferencer;
TEST(AcousticComms, PacketConversions)
{
LRAUVAcousticMessage message;
message.set_to(20);
message.set_from(30);
message.set_type(LRAUVAcousticMessageType);
message.set_data("test");
const auto now = std::chrono::steady_clock::now();
const auto vector = gz::math::Vector3d(0, 0, 1);
const auto packet = CommsPacket::make(message, vector, now);
const auto encoded = packet.ToInternalMsg();
const auto packet2 = CommsPacket::make(encoded);
EXPECT_EQ(packet, packet2);
const auto decoded = packet.ToExternalMsg();
EXPECT_TRUE(MessageDifferencer::Equals(decoded, message));
}
TEST(AcousticComms, BasicSendReceive)
{
TestFixture fixture(worldPath("acoustic_comms_fixture.sdf"));
constexpr int senderAddress = 1;
CommsClient sender(senderAddress, [](const auto){});
bool messageReceived = false;
std::mutex messageArrivalMutex;
std::condition_variable messageArrival;
constexpr int receiverAddress = 2;
CommsClient receiver(receiverAddress, [&](const auto message)
{
ASSERT_EQ(message.data(), "test_message");
{
std::lock_guard<std::mutex> lock(messageArrivalMutex);
messageReceived = true;
}
messageArrival.notify_all();
});
fixture.Step(50u);
LRAUVAcousticMessage message;
message.set_to(receiverAddress);
message.set_from(senderAddress);
message.set_type(LRAUVAcousticMessageType);
message.set_data("test_message");
sender.SendPacket(message);
fixture.Step(50u);
using namespace std::literals::chrono_literals;
std::unique_lock<std::mutex> lock(messageArrivalMutex);
EXPECT_TRUE(messageArrival.wait_for(
lock, 5s, [&] { return messageReceived; }));
}
TEST(AcousticComms, MultiVehicleTest)
{
TestFixture fixture(worldPath("acoustic_comms_multi_vehicle.sdf"));
constexpr int senderAddressTriton = 1;
CommsClient senderTriton(senderAddressTriton, [](const auto){});
// Monitor messages on comms for Tethys and Daphne
std::chrono::time_point<std::chrono::system_clock> receivedTimeTethys;
bool messageReceivedTethys = false;
std::mutex messageArrivalMutexTethys;
std::condition_variable messageArrivalTethys;
constexpr int receiverAddressTethys = 2;
CommsClient receiverTethys(receiverAddressTethys, [&](const auto message)
{
ASSERT_EQ(message.data(), "test_message");
{
std::lock_guard<std::mutex> lock(messageArrivalMutexTethys);
messageReceivedTethys = true;
receivedTimeTethys = std::chrono::system_clock::now();
}
messageArrivalTethys.notify_all();
});
std::chrono::time_point<std::chrono::system_clock> receivedTimeDaphne;
bool messageReceivedDaphne = false;
std::mutex messageArrivalMutexDaphne;
std::condition_variable messageArrivalDaphne;
constexpr int receiverAddressDaphne = 3;
CommsClient receiverDaphne(receiverAddressDaphne, [&](const auto message)
{
ASSERT_EQ(message.data(), "test_message");
{
std::lock_guard<std::mutex> lock(messageArrivalMutexDaphne);
messageReceivedDaphne = true;
receivedTimeDaphne = std::chrono::system_clock::now();
}
messageArrivalDaphne.notify_all();
});
fixture.Step(50u);
// Send the messages from Triton
LRAUVAcousticMessage message;
message.set_to(receiverAddressTethys);
message.set_from(senderAddressTriton);
message.set_type(LRAUVAcousticMessageType);
message.set_data("test_message");
senderTriton.SendPacket(message);
message.set_to(receiverAddressDaphne);
senderTriton.SendPacket(message);
auto startTime = std::chrono::system_clock::now();
fixture.Step(50u);
// Check if the messages were received
using namespace std::literals::chrono_literals;
std::unique_lock<std::mutex> lockTethys(messageArrivalMutexTethys);
EXPECT_TRUE(messageArrivalTethys.wait_for(
lockTethys, 5s, [&] { return messageReceivedTethys; }));
std::unique_lock<std::mutex> lockDaphne(messageArrivalMutexDaphne);
EXPECT_TRUE(messageArrivalDaphne.wait_for(
lockDaphne, 5s, [&] { return messageReceivedDaphne; }));
std::chrono::duration<double> diffTethys = receivedTimeTethys - startTime;
std::chrono::duration<double> diffDaphne = receivedTimeDaphne - startTime;
// Since the distance between Triton and Daphne is twice the distance between
// Triton and Tethys, the ratio of time taken by the comms signal to reach them
// should be in the same ratio.
EXPECT_NEAR(diffDaphne.count() / diffTethys.count(), 2.0, 0.1);
}