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

Why isn't the response filled if my service returns false?

asked 2017-08-04 03:44:08 -0500

VictorLamoine gravatar image

I have a service callback:

bool myServiceCallback(service_test::MyService::Request &req,
                       service_test::MyService::Response &res)
{
  ROS_INFO_STREAM("Input string = " << req.input);

  if (req.input.empty())
  {
    res.text_error = "";
    return true;
  }
  else
  {
    res.text_error = "Not empty!";
    return false;
  }
}

It returns true if the request string is empty, false otherwise. It also fills a text_error string to indicate the user what happened in case the service returns false.

The client is like this:

 ros::ServiceClient client = nh.serviceClient<service_test::MyService>("my_service");

  bool return_value;
  service_test::MyService srv;

  srv.request.input = "bla";
  return_value = client.call(srv);
  ROS_INFO_STREAM("Return value = " << (return_value ? "True": "False"));
  ROS_INFO_STREAM("Response text error = " << srv.response.text_error);

The terminal output of the client node is the following:

[ INFO] [1501836030.156100067]: Return value = False
[ INFO] [1501836030.156145499]: Response text error =

It feels like the response is not filled if a service returns false. Why is that? How can I return an error string indicating what was wrong?

Using an action instead might help me solve this issue, but I would like to keep the simplicity of a blocking service call.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
3

answered 2017-08-04 05:37:24 -0500

Hello,

I think this is related to the implementation of the serialization of the service response. I'm not an expert in this domain but I found this :

When you call a service, the ServiceServer call this function at line 170

Then, it call the serializeServiceResponse. As you can see, the serialization differ is the call returned True or False.

I don't think your code is the error, but ROS cannot return the response message if the call failed. Maybe it only serialize the Header, I don't really know.

What you can do is to use the same service as setBool, with a success boolean and an error message, and always return True.

edit flag offensive delete link more
2

answered 2017-08-04 13:59:20 -0500

Wolf gravatar image

Your calling Code will also Return false if anything fails in the underlying communication. In that case any content of the Response would not make sense, as it may contain incomplete or corrupted data.

If you need a yes or no (true or false) response from your service server node, add such a field to the response Part of the service definition and fill it in the serving node an return true in both cases. This assures if call returns true that the communication succeeded and the response returned ist what the server node wants to tell you.

edit flag offensive delete link more

Question Tools

3 followers

Stats

Asked: 2017-08-04 03:44:08 -0500

Seen: 3,745 times

Last updated: Aug 04 '17