Rosanswers logo


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.


Originally posted by jlepers on ROS Answers with karma: 39 on 2020-02-13

Post score: 0


1 Answer 1


Rosanswers logo

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 https://github.com/ros2/examples/blob/35674eb17494ab3f67cc5fbe5f127c614be2220b/rclcpp/minimal_client/main.cpp 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.

Originally posted by marguedas with karma: 3606 on 2020-02-14

This answer was ACCEPTED on the original site

Post score: 2


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.