Ask Your Question

How to make C++'s service call method wait for server's response?

asked 2019-05-23 11:00:32 -0500

achmad_fathoni gravatar image

updated 2019-05-23 11:23:19 -0500

I need to call three consecutive service call from c++ node like this

ros::ServiceClient srvcl1 = n.serviceClient<gazebo::Randomize>(
    "/randomize", false
ros::ServiceClient srvcl2 = n.serviceClient<amcl::Localization>(
    "/localization", false
ros::ServiceClient srvcl3 = n.serviceClient<std_srvs::Empty>(
    "/check", false

gazebo::Randomize srv1;
amcl::Localization srv2;
std_srvs::Empty srv3;


The problem is the service server sometime takes long time to process client's request but in C++, method always done instantaneously regardless server progress. In order to make current service call perform correctly, it must wait for server that serve previous service call to finish. I try to avoid using wait function that require time constant like ros::Duration(0.5).sleep(). This problem doesn't occur when using bash script such as

rosservice call /randomize;
rosservice call /localization;
rosservice call /check;

Does python have same problem? My last resort is using actionlib.

edit retag flag offensive close merge delete



It would help to know what the implementations of those services are.

Geoff gravatar image Geoff  ( 2019-05-23 19:46:02 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2019-05-23 19:57:15 -0500

Namal Senarathne gravatar image

updated 2019-05-23 19:57:47 -0500

I have a suspicion that may be the service clients are returning prematurely (check because the service calls are issued before the services are actually up and ready. So adding


before the loop should solve this issue.

(I am assuming the service servers are working fine since the rosservice call commands are working fine. It would always help to post a more descriptive code than just snippets to provide a more authoritative answer :))

edit flag offensive delete link more

answered 2019-05-23 19:45:47 -0500

Geoff gravatar image

Services in roscpp are blocking by default. What you are trying to do should be working if the service implementations are taking time before they return. Check if your service implementations are not returning early for some reason.

However, I think that if you will always call these three services together in this order, then using an action is a more appropriate semantic construct.

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: 2019-05-23 11:00:32 -0500

Seen: 2,655 times

Last updated: May 23 '19