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

What are possible reasons for empty service responses?

asked 2016-11-07 08:17:41 -0500

anonymous user

Anonymous

updated 2016-11-08 01:21:17 -0500

Hey, I have a problem regarding a service call. My code is very similar to the example given below. This example WORKS WELL, I am aware of that. However, in my very long code I have the problem that in a similar situation, the response in printer.cpp is empty after the service call. The service call itself is executed and the result is calculated, but the response object after the call is empty. That is very strange as I give the request and response as references and thus it should work on the given object. I would like to provide you with the relevant code, but I have not a single clue where the issue lies and thus would have to paste everything here, which would be way too much.

So, basically, I would like to know what possible reasons there are for a service response to be empty/uninitialized. Any help is appreciated.

calcu.cpp

#include "ros/ros.h"
#include "std_msgs/String.h"
#include "beginner_tutorials/AddTwoInts.h"
#include <sstream>

bool calcuCallback(beginner_tutorials::AddTwoInts::Request &req,
                    beginner_tutorials::AddTwoInts::Response &res)
{
    res.sum = req.a + req.b;
}
int main (int argc, char** argv)
{
    ROS_INFO("calcu running");
    ros::init (argc, argv, "calcu");
    ros::NodeHandle nh;
    // create a ROS service
    ros::ServiceServer service= nh.advertiseService("add_two_ints", &calcuCallback);
    // spin
    ros::spin();
    return (0);
}

printer.cpp

#include <ros/ros.h>
#include <opencv2/opencv.hpp>
#include "beginner_tutorials/AddTwoInts.h"
#include "beginner_tutorials/Num.h"

void numCallback(const beginner_tutorials::NumPtr msg)
{
    static beginner_tutorials::AddTwoInts::Request request;
    static beginner_tutorials::AddTwoInts::Response response;
    request.a = msg->num;
    request.b = 3;
    ros::service::call("add_two_ints", request, response);
    std::cout << "Response sum = " << response.sum << "\n"; // RESPONSE WOULD BE EMPTY/UNINITIALIZED
}
int main (int argc, char** argv)
{
    ROS_INFO("printer running");
    ros::init (argc, argv, "printer");
    ros::NodeHandle nh;
    if(!ros::service::waitForService("add_two_ints",10000)) return -1;
    // create ROS subscribers for local landmarks and dGPS pose estimates
    ros::Subscriber sub = nh.subscribe("inputs", 1, &numCallback);
    // spin
    ros::spin();
    return (0);
}

addTwoInts.srv

int64 a
int64 b
---
int64 sum

Num.msg

int64 num
edit retag flag offensive close merge delete

Comments

1

Hard to tell. Sounds like a problem on the service-server side. Does calling the service from the command line also result in an empty response?

Felix Endres gravatar image Felix Endres  ( 2016-11-07 12:10:33 -0500 )edit

Alright, so when calling the service from the command line it prints ERROR: service [/add_two_ints] responded with an error: . Any idea how to procede? I googled this error and found that maybe a ros::service::waitForService call is the reason, but no idea why. I added the code in my question.

anonymous userAnonymous ( 2016-11-08 01:20:23 -0500 )edit

Should add that I called the service by invoking rosservice call /add_two_ints "args" and the service callback is doing its work until the last line, but leads to the error. Note that in my actual program no error is printed out, but the response is empty.

anonymous userAnonymous ( 2016-11-08 01:32:56 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2016-11-08 02:34:10 -0500

anonymous user

Anonymous

updated 2016-11-08 02:46:27 -0500

After adding return true; to my bool callback, it works now. Whole example function (obviously my function is way bigger):

bool calcuCallback(beginner_tutorials::AddTwoInts::Request &req,
                    beginner_tutorials::AddTwoInts::Response &res)
{
    res.sum = req.a + req.b;
    return true; // this was needed
}

Any idea when the return true; statement is needed and when it is not (or if it is just a workaround for another deeper issue)? When someone knows it, he can give the whole answer so I can mark it. Thanks Felix for setting me on track.

edit flag offensive delete link more

Comments

1

The return value signals whether the callback was processed successfully. The code still compiles (btw you should compile with warnings: -Wall -Wextra) but uses the byte in memory where the return value would be. In your case that byte was 0 and the callback is thus assumed to be failed.

Felix Endres gravatar image Felix Endres  ( 2016-11-10 08:20:54 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2016-11-07 08:17:41 -0500

Seen: 2,472 times

Last updated: Nov 08 '16