ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Can node subscribe to its own service.

asked 2018-05-31 05:03:03 -0500

voyt gravatar image

updated 2018-05-31 06:50:12 -0500

gvdhoorn gravatar image

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.

edit retag flag offensive close merge delete

Comments

why you need this? if you already have the data you can do the operation there only.

simbha gravatar image simbha  ( 2018-05-31 06:58:29 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2018-05-31 07:11:06 -0500

gvdhoorn gravatar image

Yes, this is possible.

Usefulness aside, you'll have to use one of the multithreaded spinner options, because there is a chance you'll run into a deadlock otherwise.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-05-31 05:01:29 -0500

Seen: 112 times

Last updated: May 31 '18