ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

Explanation of service example in ROS2

asked 2018-05-16 10:05:12 -0600

aks gravatar image

I am trying to work with ROS and ROS2 for sometime now. Honestly I had zero programming experience about a month ago but i have learnt a lot in this period. Thanks to the excellent Wiki and the supporters. I have tried to understand certain concepts like shared pointers but when it comes to implementation, it gets a bit difficult for me.

Through this question i would like if someone can patiently explain each line of the following code and thus it would be more clear to me. I found the best practise as trying to answer some questions myself and i would be happy to be corrected.

class ServerNode : public rclcpp::Node
explicit ServerNode(const std::string & service_name) : Node("add_two_ints_server")

/* In this section, first a class ServerNode is created which inherits from the base class rclcpp::Node. An explicit constructor is called (why not implicitly ?) and the member function Node() is initialised with the name "add_two_ints_server". */

 // Create a callback function for when service requests are received.
auto handle_add_two_ints = 
  [this](const std::shared_ptr<rmw_request_id_t> request_header,
    const std::shared_ptr<example_interfaces::srv::AddTwoInts::Request> request,
    std::shared_ptr<example_interfaces::srv::AddTwoInts::Response> response) -> void
    RCLCPP_INFO(this->get_logger(), "Incoming request\na: %" PRId64 " b: %" PRId64,
      request->a, request->b);
    response->sum = request->a + request->b;

/* Here we create a callback function and pass 3 pointer parameters, namely request_header, request and response which are taken from the serviceFile.srv. Here why an "auto" specifier is used ? Also instead of a handle, we could also use a"void service_callback()" function ? What does "this

// Create a service that will use the callback function to handle requests.
srv_ = create_service<example_interfaces::srv::AddTwoInts>(service_name, handle_add_two_ints);
rclcpp::Service<example_interfaces::srv::AddTwoInts>::SharedPtr srv_;

/Here the service is created/

int main(int argc, char * argv[]) 
   if (rcutils_cli_option_exist(argv, argv + argc, "-h")) {
   return 0;

/*What does this exactly means? */

   rclcpp::init(argc, argv);  //library initialisation call

   auto service_name = std::string("add_two_ints"); //naming the service ?
   if (rcutils_cli_option_exist(argv, argv + argc, "-s")) {
   service_name = std::string(rcutils_cli_get_option(argv, argv + argc, "-s"));

/* what happens here? */

  auto node = std::make_shared<ServerNode>(service_name); //passing the service name to the node ?

  return 0;
edit retag flag offensive close merge delete



As has been said before ROS is not a good way of learning C++, it's a very complex system. We really recommend learning the basics of C++ before getting too involved! There are some very high level concepts involved in some of these questions...

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2018-05-16 11:03:00 -0600 )edit

It would be good the read up on object orientated programming theory and work through some C++ courses first.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2018-05-16 11:03:31 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2018-05-17 04:31:42 -0600

To give a quick answer (even if I agree with the comments given):

explicit constructor is called (why not implicitly ?)

I would suggest you to read about this keyword:

Here why an "auto" specifier is used ?

Auto is used to avoid specifying the type of _handle_add_two_ints_ as it can be found at compile time. If you wish, you can find the type and replace auto.

What does this exactly means?

It will call the function _rcutils_cli_option_exist_ and look if the user specified the option '-h' when calling the binary. If yes, print how to use the binary. Check

auto service_name = std::string("add_two_ints"); //naming the service ?

Is declaring a string which will be used to name the service

if (rcutils_cli_option_exist(argv, argv + argc, "-s")) {

If the '-s' option has been specified, then the user gave a service name, so _rcutils_cli_get_option_ will return the argument specified after '-s'

auto node = std::make_shared<servernode>(service_name); //passing the service name to the node ?

This create an instance of the class ServerNode with the service_name as argument.


Process any event from the node ( if a request to the service occures as example)

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2018-05-16 10:05:12 -0600

Seen: 1,566 times

Last updated: May 17 '18