Skip to content

Sending and receiving data packets on the acoustic subsystem.

Louise Poubel edited this page Nov 2, 2021 · 1 revision

In this tutorial we will be exploring how to send and receive data packets. There are two ways of doing this. The first is using the lrauv_ignition_plugins/comms/CommsClient.hh header and the second is directly calling the relevant ignition topics (reading the linked header should also give you a rough idea of how to write your own client).

Using CommsClient

CommsClient is an extremely easy to use method for handling sending of messages, however it needs C++11 or above to be used. The usage is as follows:

#include <lrauv_ignition_plugins/comms/CommsClient.hh>

using namespace tethys;
using AcousticMsg = lrauv_ignition_plugins::msgs::LRAUVAcousticMessage;

// Bind client to address 1
CommsClient client(1, [](const auto msg){
   // Your callback function
   // To get the data call
   msg.data();
   // To get who the message is from
   msg.from();
});

// For sending data

AcousticMsg msg;
// Who to send to
msg.set_to(2);
// From who
msg.set_from(1);
// `LRAUVAcousticMessage_MessageType_Other` means its a data packet
msg.set_type(AcousticMsg::MessageType::LRAUVAcousticMessage_MessageType_Other);
// The data
msg.set_data("test_message");
client1.SendPacket(msg);

An example may be found in the tests here.

Direct method

For this method you will need to understand how ignition-transport works. I recommend reading this tutorial first.

To get started you can instantiate a world using ignition:

ign gazebo -v4 acoustic_comms_test.sdf

You will see two boxes spawned. Each of these boxes has an instance of the comms plugin and the range bearing plugin. The boxes publish what they receive on the /comms/external/{address}/rx topic. The comms client will also forward data published from the /comms/external/{address}/txtopic to the relevant client. In a new terminal enter:

ign topic --list

You should see two sets of RX and TX topics. Here is a minimal echo program:

  #include <ignition/transport/Node.hh>
  #include "lrauv_acoustic_message.pb.h"

  int address = 1;
  ignition::transport::Node node;
  ignition::transport::Node::Publisher transmitter;
  
  //////////////////////////////////////////////////
  /// \brief Function called each time a message is recieved by the comms subsystem.
  void cb(const lrauv_ignition_plugins::msgs::LRAUVAcousticMessage &_msg)
  {
    std::cout << msg.from() << ": " << msg.data();

    lrauv_ignition_plugins::msgs::LRAUVAcousticMessage returnMsg;
    // Who to send to
    msg.set_to(msg.from());
    // From who  
    msg.set_from(address);

    // `LRAUVAcousticMessage_MessageType_Other` means its a data packet
    msg.set_type(AcousticMsg::MessageType::LRAUVAcousticMessage_MessageType_Other);

    // The data
    msg.set_data(msg.data);

    // Send the data
    transmitter.Publish(msg);
  }

  int main(int argc, char** argv)
  {
    transmitter = node.Advertise<lrauv_ignition_plugins::msgs::LRAUVAcousticMessage>(
      "/comms/external/" + std::to_string(address) + "/tx");

    node.Subscribe(
      "/comms/external/" + std::to_string(address) + "/rx",
      cb
    );

    while(true) { sleep(1); }
  }