Ask Your Question
1

Explanation of service example in ROS2

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

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.

add_two_ints_server
class ServerNode : public rclcpp::Node
{
public:
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
  {
    (void)request_header;
    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);
}
private:
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")) {
   print_usage();
   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 ?
  rclcpp::spin(node);

  rclcpp::shutdown();
  return 0;
  }
edit retag flag offensive close merge delete

Comments

2

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 imagePeteBlackerThe3rd ( 2018-05-16 11:03:00 -0500 )edit
2

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

PeteBlackerThe3rd gravatar imagePeteBlackerThe3rd ( 2018-05-16 11:03:31 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

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

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: http://en.cppreference.com/w/cpp/lang...

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 https://github.com/ros2/rcutils/blob/...

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.

rclcpp::spin(node);

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

2 followers

Stats

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

Seen: 704 times

Last updated: May 17 '18