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

Empty service call is not executed

asked 2014-12-08 04:15:24 -0600

odelay gravatar image

I implemented the basic empty service as proposed by the tutorial in my program:

bool callback(std_srvs::Empty::Request& request, std_srvs::Empty::Response& response) {
    return true;
}
ros::NodeHandle * privateNodeHandle; 
int main(int argc, char **argv) { ros::init(argc, argv, node_name); ros::NodeHandle n; privateNodeHandle = new ros::NodeHandle("~"); privateNodeHandle->param("publishFrequency", publishFrequency, 1.0); privateNodeHandle->param("maximumWaterLevel", maximumWaterLevel, 1.0);
ros::Publisher water_level_pub = n.advertise<std_msgs::float64>( topic, 1000); ros::ServiceServer service = n.advertiseService("my_service", callback); ros::Rate loop_rate(publishFrequency);
tank = new WaterTank(maximumWaterLevel); int count = 0; while (ros::ok()) { std_msgs::Float64 msg; std::stringstream ss; ss << "Water level at " << count << " is " << tank->get_water_level(); msg.data = tank->get_water_level(); ROS_INFO("%s", ss.str().c_str()); water_level_pub.publish(msg); loop_rate.sleep(); ++count; } delete tank; delete privateNodeHandle; return 0; }
It compiles fine and I see the service with rosservice list. But when I try to call it rosservice call /my_service rosservice seems to wait for the service to finish and never returns. This is weird since it is a service with empty parameter and return signature. I also added some debug output to the callback and it seems like it never gets executed. Is there some service blocking part in my main loop?

edit retag flag offensive close merge delete

Comments

are you able to see your service in rosservice list ?

bvbdort gravatar image bvbdort  ( 2014-12-08 05:10:40 -0600 )edit

Yes it is listed by rosservice list

odelay gravatar image odelay  ( 2014-12-08 05:18:38 -0600 )edit

2 Answers

Sort by » oldest newest most voted
4

answered 2014-12-08 05:54:38 -0600

odelay gravatar image

updated 2014-12-08 05:55:25 -0600

The solution is to add a ros::spinOnce(); to the while loop. It will process all pending callbacks and then return.

edit flag offensive delete link more

Comments

If this works for you please mark the answer as correct.

BennyRe gravatar image BennyRe  ( 2014-12-08 06:41:29 -0600 )edit
0

answered 2018-01-02 00:22:48 -0600

No spaces behind "rosservice call /my_service" ,because it is a service with empty parameter。

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2014-12-08 04:15:24 -0600

Seen: 5,459 times

Last updated: Dec 08 '14