Ask Your Question
0

[ROS2] services C++

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

jlepers gravatar image

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

Hi,

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;

init.call(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 ?

init.call(trig)

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::executor::FutureReturnCode::SUCCESS)
  {
    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.

Thanks

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

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

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

2 followers

Stats

Asked: 2020-02-13 03:09:49 -0600

Seen: 23 times

Last updated: Feb 14