ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The closest analog to a function in ROS (either 1 or 2) is a Service, because you get a response (like a function return value) along with data in the request (like a function's arguments) and they are automatically associated (each response is associated with one request).

If you don't require an association between the input data and some transformed output data, then using two publish/subscribe pairs will also work. These have the benefit of easier introspection (either the requests or responses can be observed by more that one entity).

You'll have to decide which pattern is more appropriate for your use case.

Since you're using ROS 2, I recommend you look at the topic and service examples for clues as to how to implement each case:

  • https://github.com/ros2/demos/tree/master/demo_nodes_cpp/src/topics
  • https://github.com/ros2/demos/tree/master/demo_nodes_cpp/src/services