ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Send Request Funcion

asked 2019-08-05 10:01:03 -0500

mikepark93 gravatar image

updated 2019-08-05 10:08:13 -0500

The add_two_ints_client.cpp file in the ros2/demos repository has the following send request function.

image description

Is there a way to make this send request function generic for other services I have created?

edit retag flag offensive close merge delete


Please don't use an image to display text/code. Images are not searchable and people cannot copy and paste the text from the image.

jayess gravatar image jayess  ( 2019-08-05 14:29:31 -0500 )edit

1 Answer

Sort by » oldest newest most voted

answered 2019-08-05 10:50:37 -0500

alsora gravatar image

updated 2019-08-05 10:51:04 -0500

Yes, you can use a template.

template<typename ServiceT>
typename ServiceT::Response::SharedPtr send_request(
  rclcpp::Node::SharedPtr node,
  typename rclcpp::Client<ServiceT>::SharedPtr client,
  typename ServiceT::Request::SharedPtr request)
  auto result = client->async_send_request(request);
  // Wait for the result.
  if (rclcpp::spin_until_future_complete(node, result) ==
    return result.get();
  } else {
    return NULL;

Then you can call it like this

  using ServiceT1 = ....
  using ServiceT2 = ....

  auto node = rclcpp::Node::make_shared("client_node");
  auto client_1 = node->create_client<ServiceT1>("my_service_1_name");
  auto client_2 = node->create_client<ServiceT2>("my_service_2_name");

  auto request_1 = std::make_shared<ServiceT1::Request>();
  auto result_1 = send_request<ServiceT1>(node, client_1, request_1);

  auto request_2 = std::make_shared<ServiceT2::Request>();
  auto result_2 = send_request<ServiceT2>(node, client_2, request_2);

P.S. In ROS2 this is not the recommended style for creating nodes. You should create a class that extends rclcpp::Node and define the send_request function and the clients as members of this class.

edit flag offensive delete link more


When I try building, I get an error saying the following:

error: ‘rclcpp::Client<ServiceT>::SharedPtr’ is not a type rclcpp::Client<ServiceT>::SharedPtr client
mikepark93 gravatar image mikepark93  ( 2019-08-05 11:21:31 -0500 )edit

How have you defined servicet in your code?

alsora gravatar image alsora  ( 2019-08-05 11:29:03 -0500 )edit

This was how I defined it before:

rclcpp::Client<orion_sensor::srv::SetFocus>::SharedPtr client
mikepark93 gravatar image mikepark93  ( 2019-08-05 11:35:03 -0500 )edit

So you should use using ServiceT = orion_sensor::srv::SetFocus;. Or you remove this line and you just keep using orion_sensor::srv::SetFocus as before, changing only the send_request function and adding the template parameter when calling it i.e. send_request<orion_sensor::srv::SetFocus>(...)

alsora gravatar image alsora  ( 2019-08-05 11:43:59 -0500 )edit

Question Tools

1 follower


Asked: 2019-08-05 10:01:03 -0500

Seen: 152 times

Last updated: Aug 05 '19