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

Calling ROS service from C++ code

asked 2022-04-06 04:02:09 -0500

marc wellman gravatar image

How can I call a ROS-service that was neither written nor published by me from within C++ code?

I see the service listed when using rosservice list, which shows me

enter image description here

and I can call it easily by using rosservice call /airsim_node/SimpleFlight1/takeoff for instance.

Now, what I want, is to work with this service from within a C++ file (send request, receive response).

The documentation explains how to do this but requires you to have access to the service's source such as its compiled source code or even its *srv-file.

But I want to call an arbitrary service that is out of my scope. I, as a user, am able to do this via rosservice call, so I guess it should be simple to do the same via code?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2022-04-10 14:24:43 -0500

Mike Scheutzow gravatar image

You have misunderstood the requirement. The client-side does not need access to the server-side c or python source code, but it does need access to the message definition files generated from the .srv file. Those message definitions are what is in the '.h` file in the example documentation.

These message definitions are often created in a different ros package from the server-side node, precisely because this ros package needs to be installed on both client- and server-side hosts.

edit flag offensive delete link more


I am wondering why this is so because if I as a user call a service at the client-side, I do not need any .h files - I just need to know the type and format of the reequest-message.

marc wellman gravatar image marc wellman  ( 2022-04-12 01:34:18 -0500 )edit

rosservice call is doing the work for you to find and load the class definition for the needed messages. It would be grossly inefficient, but you could do the same in your c++ client.

Mike Scheutzow gravatar image Mike Scheutzow  ( 2022-04-12 06:34:16 -0500 )edit

Question Tools



Asked: 2022-04-06 04:02:09 -0500

Seen: 191 times

Last updated: Apr 10 '22