Service Server and Publisher using same Node
So what i wanted to do is, send a service call to this service and then i wanted this service to publish to another topic. So i was experimenting this with the code as follows. But ofcourse lots of errors and lots of problems when compiling. Please help :)
i was following the format which i saw in this link https://answers.ros.org/question/5972...
#include "ros/ros.h"
#include "practice/AddTwoInts.h"
#include "std_msgs/String.h"
#include <sstream>
class SubscribeAndPublish
{
public:
SubscribeAndPublish()
{
chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);
service = n.advertiseService("add_two_ints", &SubscribeAndPublish::add);
}
bool add(practice::AddTwoInts::Request &req,
practice::AddTwoInts::Response &res)
{
if(req.a == 1) {
msg.data = "hello world";
chatter_pub.publish(msg);
}
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;
}
private:
ros::NodeHandle n;
ros::Publisher chatter_pub;
ros::ServiceServer service;
std_msgs::String msg;
};//End of class SubscribeAndPublish
int main(int argc, char **argv)
{
ros::init(argc, argv, "add_two_ints_server");
SubscribeAndPublish SAPObject;
ROS_INFO("Ready to add two ints.");
ros::spin();
return 0;
}