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

[ROS2] services C++

asked 2020-02-13 03:09:49 -0500

jlepers gravatar image

updated 2020-02-14 04:18:53 -0500


I'm trying to use services to work with a ROS2 canopen node. In ROS1 I was able to use this code:

ros::ServiceClient init = n.serviceClient< std_srvs::Trigger>("/driver/init");

std_srvs::Trigger trig;

If I follow the migration guide, I have following code:

rclcpp::Client< std_srvs::srv::Trigger>::SharedPtr client = node->create_client< std_srvs::srv::Trigger>("/driver/init");

std_srvs::srv::Trigger trig;

How do I call the service ?

This doesn't work

Edit: (tutorial services)

auto result = client->async_send_request(request);
  // Wait for the result.
  if (rclcpp::spin_until_future_complete(node, result) ==
    RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "Sum: %ld", result.get()->sum);
  } else {
    RCLCPP_ERROR(rclcpp::get_logger("rclcpp"), "Failed to call service add_two_ints");

This calls the service I think, but I need to send a request with it. How Can I call a service without a request? The Service file can only respond with some variables.


edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2020-02-14 04:23:47 -0500

marguedas gravatar image

This calls the service I think, but I need to send a request with it. How Can I call a service without a request?

In short you cannot call a service without a request. Actually this is the same in ROS 1 as trig constitutes the request holder you are calling the service with. In ROS 2 you need to explicitly pass a request object.

You can create it like this:

auto request = std::make_shared<std_srvs::srv::Trigger::Request>();

And call your service:

auto result_future = client->async_send_request(request);

You can see for an example of a request being created and then used to call a service. The difference in your case is that the request has no fields to set as it will be "empty". So you can just create it and call the service with it.

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: 2020-02-13 03:09:49 -0500

Seen: 1,170 times

Last updated: Feb 14 '20