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

Multiple Service Calls in ROS2

asked 2019-07-31 11:29:16 -0500

mikepark93 gravatar image

I have a sensor library that I am using ROS2 services to make function calls to that library. Is there a way to have multiple services in one node or do I need to make a new node for each individual service call?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2019-07-31 13:04:01 -0500

alsora gravatar image

updated 2019-07-31 13:04:31 -0500

A node can have any number of publisher, subscription, service or client.

using std::placeholders::_1;
using std::placeholders::_2;
using std::placeholders::_3;

class MyNode : public rclcpp::Node
MyNode(std::string name = "my_node") : Node(name)
    _service1 = this->create_service<ServiceT1>("service1", std::bind(&MyNode::handle_service_1, this, _1, _2, _3));
    _service2 = this->create_service<ServiceT2>("service2", std::bind(&MyNode::handle_service_2, this, _1, _2, _3));

    void handle_service_1(
        const std::shared_ptr<rmw_request_id_t> request_header,
        const std::shared_ptr<ServiceT1::Request> request,
        const std::shared_ptr<ServiceT1::Response> response)
     std::cout<<"This is service 1"<<std::endl;

    void handle_service_2(
        const std::shared_ptr<rmw_request_id_t> request_header,
        const std::shared_ptr<ServiceT2::Request> request,
        const std::shared_ptr<ServiceT2::Response> response);
     std::cout<<"This is service 2"<<std::endl;

    rclcpp::Service<ServiceT1>::SharedPtr _service1;
    rclcpp::Service<ServiceT2>::SharedPtr _service2;
edit flag offensive delete link more


Can we have the same service names for both services and let it be decided by the request type which service to be used for the response.

ShivamPR21 gravatar image ShivamPR21  ( 2020-07-29 09:18:12 -0500 )edit

Question Tools



Asked: 2019-07-31 11:12:10 -0500

Seen: 1,506 times

Last updated: Jul 31 '19