Ros service not advertised in functions
Followed the tutorial, adapted it to my project. When I call the advertiseService in the main, it works. When it is called in a function called by main, it doesn't.
Here some code. First, tutorial example which works :
#include "ros/ros.h"
#include "testService/AddTwoInts.h"
bool add(testService::AddTwoInts::Request &req, testService::AddTwoInts::Response &res)
{
res.sum = req.a + req.b;
ROS_INFO("request: x=%ld, y=%ld", (long int)req.a, (long int)req.b);
ROS_INFO("sending back response: [%ld]", (long int)res.sum);
return true;
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "add_two_ints_server");
ros::NodeHandle n;
ros::ServiceServer service = n.advertiseService("add_two_ints", add);
ROS_INFO("Ready to add two ints.");
ros::spin();
return 0;
}
In my use case, I need to set up a service in a separate file. I"m surprised this doesn't work
#include "ros/ros.h"
#include "testService/AddTwoInts.h"
void testFunction(ros::NodeHandle &n);
bool add(testService::AddTwoInts::Request &req,
testService::AddTwoInts::Response &res)
{
res.sum = req.a + req.b;
ROS_INFO("request: x=%ld, y=%ld", (long int)req.a, (long int)req.b);
ROS_INFO("sending back response: [%ld]", (long int)res.sum);
return true;
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "add_two_ints_server");
ros::NodeHandle n;
testFunction(n);
ROS_INFO("Ready to add two ints.");
ros::spin();
return 0;
}
void testFunction(ros::NodeHandle &n)
{
ros::ServiceServer service = n.advertiseService("add_two_ints", add);
}
The service doesn't show up when typing rosservice list.
First, I made a new node handle in my function. Then, I tried to pass the main's node Handle by value, reference, pointer, global variable to my function. No changes. I thought about creating a function returning an object with topic name and function pointer inside it, but function pointer callback are callback-specific and my main can't know their type. Any one got a lead to how to do this?
Can you edit your question to show us your code ?