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

Adding acoustic subsystem examples #236

Merged
merged 5 commits into from
Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions lrauv_ignition_plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ install(
foreach(EXAMPLE
example_buoyancy
example_controller
example_comms_client
example_comms_echo
example_elevator
example_mass_shifter
example_rudder
Expand All @@ -225,6 +227,7 @@ foreach(EXAMPLE

add_executable(${EXAMPLE_EXEC} example/${EXAMPLE}.cc)
set_property(TARGET ${EXAMPLE_EXEC} PROPERTY CXX_STANDARD 17)
target_include_directories(${EXAMPLE_EXEC} PUBLIC include)
target_link_libraries(${EXAMPLE_EXEC} PRIVATE
gz-sim${GZ_SIM_VER}::gz-sim${GZ_SIM_VER}
dvl_velocity_tracking lrauv_acoustic_message lrauv_command lrauv_init
Expand Down
61 changes: 61 additions & 0 deletions lrauv_ignition_plugins/example/example_comms_client.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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
*/

/**
* This is a small example of usage of CommsClient to send and receive data
* packets using the acoustic subsystem. For more info:
* https:/osrf/lrauv/wiki/Examples-and-tutorials#using-commsclient
*
* Usage:
* $ ./LRAUV_example_comms_client
*/

#include <lrauv_ignition_plugins/comms/CommsClient.hh>

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

int main(int _argc, char **_argv)
{

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marcoag nit: unnecessary blank line.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

// For sending data
// Bind client to address 1
CommsClient client(1, [](const auto msg){
// Your callback function
// To get who the message is from
std::cout << "From: " << msg.from();
// To get the data call
std::cout << "Data: " << msg.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");
client.SendPacket(msg);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marcoag hmm, isn't the service response racing with process shutdown?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tbh, I'm not sure... but could be? I mostly copied whatever was there on the documentation example before.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marcoag could we wait for the response to arrive before exiting?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, just added a sender and a receiver for extra clarity and some more verbosity to the example.


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marcoag nit: unnecessary blank line.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

}
75 changes: 75 additions & 0 deletions lrauv_ignition_plugins/example/example_comms_echo.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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
*/

/**
* This is a small example of usage of gazebo transport to send and receive
* data packets using the acoustic subsystem. For more info:
* https:/osrf/lrauv/wiki/Examples-and-tutorials#direct-method
hidmic marked this conversation as resolved.
Show resolved Hide resolved
*
* Usage:
* $ ./LRAUV_example_comms_echo
*/

#include <unistd.h>
#include <gz/transport/Node.hh>
#include "lrauv_ignition_plugins/lrauv_acoustic_message.pb.h"

int address = 1;
hidmic marked this conversation as resolved.
Show resolved Hide resolved
gz::transport::Node node;
gz::transport::Node::Publisher transmitter;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marcoag perhaps we move these to the main function below? IIRC gz::transport allows for lambdas as subscription callbacks.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the node declaration as is not used in cb. However, I think it's a good idea to keep the example as simple as possible therefore avoid the use of a lambda?

using AcousticMsg = lrauv_ignition_plugins::msgs::LRAUVAcousticMessage;

//////////////////////////////////////////////////
/// \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
returnMsg.set_to(_msg.from());

// From who
returnMsg.set_from(address);

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

// The data
returnMsg.set_data(_msg.data());

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

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); }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marcoag IIRC there's a waitForShutdown API in gz::transport.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, added.

}