Call a rosservice from inside another rosservice

asked 2019-03-21 01:56:41 -0500

vishnu gravatar image

Hi,

I want to have a rosservice, “/update_goal” for example, which takes goal position (x, y, theta) as request. Once the goal is given as input, the agent should request planner for a path.I have already written a service that gives the plan as a response. Now, my question is can I call the service from inside another service.

bool multi_agent_planner::Agent::updateGoal_Server(UpdateGoal::Request &req,
                              Response &res)
{

    GetPlan srv;

    srv.request.goal.x = req.goal.x;
    srv.request.goal.y = req.goal.y;
    srv.request.id.data = req.id.data;

    if(ros::service::call("get_plan",srv))
    {
        res.response.data = "Path Received...";
    }
    else
    {
        ROS_INFO("Failed to call the service...");
        return 1;
    }

    return true;
}

I have written like this, but I am getting "Failed to call the service..." as a response. Is this possible at all, if it is how can I achieve this?

Thank you

edit retag flag offensive close merge delete

Comments

In general, it should be possible to call a service from another service. You may want to look more closely at the error from the service that you tried to call. Is it actually advertised by a node? Did it return an error?

ahendrix gravatar imageahendrix ( 2019-03-21 02:16:19 -0500 )edit

Hi @ahendrix, It is advertised by a node. I took close notice at the node, the service callback was receiving robot's feedback using 'waitForMessage()'. So it was blocking the next statements. I changed to a subscriber callback and it worked. But, I failed to understand as to why it was returning false when it was waiting for a message. Is there any timeout by default in ros::service::call()?

vishnu gravatar imagevishnu ( 2019-03-22 07:44:39 -0500 )edit