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

Revision history [back]

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
{
public:
MyNode(std::string name = "my_node")
{
    _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));
}

private:
    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;
};

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
{
public:
MyNode(std::string name = "my_node")
"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));
}

private:
    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;
};