How to make C++'s service call method wait for server's response?
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;
while(ros::ok()){
if(srvcl1.call(srv1)){
if(srvcl2.call(srv2)){
if(srvcl3.call(srv3)){
}
}
}
}
The problem is the service server sometime takes long time to process client's request but in C++, srvcl.call(srv)
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.
It would help to know what the implementations of those services are.