Can node subscribe to its own service.
Im trying to create a node that is subscribing to its own service.
Below is a trivial example what im training to do. Node is publishing a service and next this node trying use it. Is it even possible in ROS?
add_two_ints_client_server.cpp
#include <cstdlib>
#include "ros/ros.h"
#include "sample_service/AddTwoInts.h"
bool add(sample_service::AddTwoInts::Request &req, sample_service::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_client_server");
if (argc != 3){
ROS_INFO("usage: add_two_ints_client X Y");
return 1;
}
ros::NodeHandle n;
ros::ServiceServer service = n.advertiseService("add_two_ints", add);
ROS_INFO("Ready to add two ints.");
ros::ServiceClient client = n.serviceClient<sample_service::AddTwoInts>("add_two_ints");
sample_service::AddTwoInts srv;
srv.request.a = atoll(argv[1]);
srv.request.b = atoll(argv[2]);
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::spin();
return 0;
}
sorry for formation of code. first time to try use it.
why you need this? if you already have the data you can do the operation there only.