Can a Node be a service and client
Hello everyone,
I am new to ROS. I am trying to communicate service and client in the single Node. Is it even possible.
Code:
bool add(beginner_tutorials::AddTwoInts::Request &req, beginner_tutorials::AddTwoInts::Response &res)
{
res.sum = req.a + req.b;
ROS_INFO("\n request: a=%ld, b=%ld", (long int)req.a, (long int)req.b);
ROS_INFO("\n sending back response: [%ld]", (long int)res.sum);
return true;
}
int main(int argc, char *argv[])
{
ros::init(argc, argv, "serclntrtest");
ros::NodeHandle n;
beginner_tutorials::AddTwoInts srv;
srv.request.a = 3;
srv.request.b = 6;
ros::ServiceServer service = n.advertiseService("test", add);
std::cout<< "\n \n After the service \n" << std::endl;
ros::ServiceClient client = n.serviceClient<beginner_tutorials::AddTwoInts>("test");
if(client.call(srv))
{
ROS_INFO("Sum: %ld", (long int)srv.response.sum);
}
else
{
ROS_ERROR("Failed to call service add_two_ints");
return 1;
}
ros::spinOnce();
return 0;
}
I have tried it with two different nodes, but with single node it is getting stuck at client.call() function. Could anyone please explain me how it would be done, if there a way.
Thanks in advance