ROS Service (client inside server node)
Is it possible to put a client node inside a server node when making service?
Iam trying to make a Service and as far as I know, all the functions are stored in the server node and inside the client node is where the parameters or arguments you want for the server. What Im trying to make is a service that will spawn turtle n times. The code compiles flawlessly also gives me the response when using rosservice command. Problem is I cant spawn any turtle, (turtlesim_spawn_client.call(srv)) always returns to false. Please help
#include "ros/ros.h"
#include "srv_turtlesim/spawn_turtle.h"
#include "turtlesim/Spawn.h"
ros::ServiceClient turtlesim_spawn_client;
class spawn_turtle_class
{
public:
spawn_turtle_class(ros::NodeHandle& nh)
{
turtlesim_spawn_client = nh.serviceClient<turtlesim::Spawn>("turtlesim_spawn");
}
bool spawn_callback(srv_turtlesim::spawn_turtle::Request &req,
srv_turtlesim::spawn_turtle::Response &res)
{
bool not_spawn=false;
turtlesim::Spawn srv;
ROS_INFO("request: n=%d", (int)req.num_turtle);
/----------------------------------------------------------------------------------------/
for (int i = 0; req.num_turtle > i; i++)
{
srv.request.x = random() % 12;
srv.request.y = random() % 12;
srv.request.theta = random() % 360;
if (turtlesim_spawn_client.call(srv))
{
ROS_INFO("response: You got turt");
}
else
{
ROS_INFO("response: No turt :(");
not_spawn = true;
}
}
/----------------------------------------------------------------------------------------/
if (not_spawn)
{
res.status = "Incomplete";
}
else
{
res.status = "Done";
}
return true;
}
};
int main (int argc, char**argv)
{
ros::init(argc,argv, "spawn_node");
ros::NodeHandle nh;
spawn_turtle_class spawn2(nh);
ros::ServiceServer service = nh.advertiseService("spawn_turtle_srv", &spawn_turtle_class::spawn_callback, &spawn2);
ros::spin();
return 0;
}
Can you prepare a MWE (Minimal Working Example) of your project with these node and service (or at least your attempts)? Then we can try to help easier :)
include "ros/ros.h"
From that comment on my callback, I want to call the turtlesim/Spawn service client but the creation of the client is inside the main function making my request for turtlesim/Spawn undefined
Can you add this code in the question? Formatting will be better
Im sorry for that, Im new with this ROS and with this forum.
No problem, you will find out everything. BTW there is no
;
after theturtlesim::Spawn srv
, what kind of error does this show you? Can you copy & paste the entire compile errror from the terminal to the question?Hi I finally managed to make a decent code than the previous code. It compiles and gives me the response. One problem tho is that turtlesim_spawn_client.call(srv) is not working. it always returns to false. I edited the question already. the separated lines of code are where I got problem.