Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added integration test using multiple lrauv vehicles #243

Merged
merged 19 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
#define __LRAUV_IGNITION_PLUGINS_COMMS_COMMSCLIENT_HH__

#include <functional>
#include <unordered_map>

#include <gz/msgs.hh>
#include <gz/transport/Node.hh>

/* #include "CommsPacket.hh" */

#include "lrauv_ignition_plugins/lrauv_acoustic_message.pb.h"

Expand All @@ -37,11 +37,25 @@ using LRAUVAcousticMessage =
using MessageType = LRAUVAcousticMessage::MessageType;
static constexpr auto LRAUVAcousticMessageType =
MessageType::LRAUVAcousticMessage_MessageType_Other;
auto RangeRequest =
static constexpr auto RangeRequest =
MessageType::LRAUVAcousticMessage_MessageType_RangeRequest;
auto RangeResponse =
static constexpr auto RangeResponse =
MessageType::LRAUVAcousticMessage_MessageType_RangeResponse;

std::unordered_map<MessageType, std::string>
msgToString = {
{LRAUVAcousticMessageType, "LRAUVAcousticMessageType"},
{RangeRequest, "RangeRequest"},
{RangeResponse, "RangeResponse"}
};

std::unordered_map<std::string, MessageType>
stringToMsg = {
{"LRAUVAcousticMessageType", LRAUVAcousticMessageType},
{"RangeRequest", RangeRequest},
{"RangeResponse", RangeResponse}
};

namespace tethys
{
//////////////////////////////////////////////////
Expand Down Expand Up @@ -86,12 +100,15 @@ public: void SendPacket(const CommsMsg& _msg)
// Set message type
auto *frame = message.mutable_header()->add_data();
frame->set_key("msg_type");
if (_msg.type() == LRAUVAcousticMessageType)
frame->add_value("LRAUVAcousticMessageType");
else if (_msg.type() == RangeRequest)
frame->add_value("RangeRequest");
else if (_msg.type() == RangeResponse)
frame->add_value("RangeResponse");
if (msgToString.count(_msg.type()) != 0)
{
frame->add_value(msgToString[_msg.type()]);
}
else
{
std::cerr << "Message being sent does not contain a type"
<< std::endl;
}

// Publish the Dataframe message
this->transmitter.Publish(message);
Expand All @@ -116,12 +133,15 @@ private: void ReceivedPacket(const gz::msgs::Dataframe& _msg)
if (_msg.header().data(i).key() == "msg_type")
{
auto type = _msg.header().data(i).value().Get(0);
if (type == "LRAUVAcousticMessageType")
message.set_type(LRAUVAcousticMessageType);
else if (type == "RangeResponse")
message.set_type(RangeResponse);
else if (type == "RangeRequest")
message.set_type(RangeRequest);
if (stringToMsg.count(type) != 0)
{
message.set_type(stringToMsg[type]);
}
else
{
std::cerr << "Received message does not contain a type"
<< std::endl;
}
}
}

Expand Down
78 changes: 78 additions & 0 deletions lrauv_system_tests/test/vehicle/test_acoustic_comms.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,81 @@ TEST(AcousticComms, BasicSendReceive)
EXPECT_TRUE(messageArrival.wait_for(
lock, 5s, [&] { return messageReceived; }));
}

TEST(AcousticComms, MultiVehicleTest)
{
TestFixture fixture("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);
}

Loading