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

Revision history [back]

click to hide/show revision 1
initial version

Hello !

My solution is finally the following (any comment welcome) :

In node class definition :

class MyClass
{
    private :
        ros::NodeHandle nhCall_;
        ros::NodeHandle nhSrv_;
        ros::CallbackQueue queueCall;
        ros::CallbackQueue queueSrv;
        ros::Subscriber first_sub;
        ros::ServiceServer first_srv;
        ros::Subscriber second_sub;
        ros::Subscriber third_sub;
        ros::Publisher first_pub;
        (...)
    public :
        MyClass(ros::NodeHandle &nh1, ros::NodeHandle &nh2, ...);
        Some Callbacks to handle message and service reception;
        bool run();
};

Class implementation :

#include "MyClass.h"

MyClass::MyClass(ros::NodeHandle &nh1, ros::NodeHandle &nh2, ...)
{
    // ROS specific : topics, node handler
    nhCall_ = nh1;
    nhSrv_ = nh2;

    // Subscriptions, Advertising
    first_sub = nhCall_.subscribe("topicname1", 1, &MyClass::callback1, this);
    first_srv = nhSrv_.advertiseService("servicename", &MyClass::servicecallback, this);
    second_sub = nhCall_.subscribe("topicname2", 1, &MyClass::callback2, this);
    third_sub = nhCall_.subscribe("topicname3", 1, &MyClass::callback3, this);

    first_pub = nhCall_.advertise<my_msgs::message>("topicname4", 1);
}

// Callback Handling 
void MyClass::callback1(my_msgs::message msg)
{
    // Do something
}
(...)

bool MyClass::run()
{

    while(nhCall_.ok())  // We may check also for the other NodeHandle state
    {
        ros::spinOnce();
    }

    return true;
}

int main(int argc, char** argv)
{
    ros::init(argc, argv, "mynode");
    ros::NodeHandle nh_call;
    ros::NodeHandle nh_srv;

    MyClass mysupernode(nh_call, nh_srv, ...);
    mysupernode.run();

    return EXIT_SUCCESS;
}

As said, advice or comment are welcome ! Hope this answer may be useful for people that need to do Multithreading in ROS nodes !

Hello !

My solution is finally the following (any comment welcome) :

In node class definition :

class MyClass
{
    private :
        ros::NodeHandle nhCall_;
        ros::NodeHandle nhSrv_;
        ros::CallbackQueue queueCall;
        ros::CallbackQueue queueSrv;
        ros::Subscriber first_sub;
        ros::ServiceServer first_srv;
        ros::Subscriber second_sub;
        ros::Subscriber third_sub;
        ros::Publisher first_pub;
        (...)
    public :
        MyClass(ros::NodeHandle &nh1, ros::NodeHandle &nh2, ...);
        ( ... Some Callbacks to handle message and service reception;
reception ...)
        bool run();
};

Class implementation :

#include "MyClass.h"

MyClass::MyClass(ros::NodeHandle &nh1, ros::NodeHandle &nh2, ...)
{
    // ROS specific : topics, node handler
    nhCall_ = nh1;
    nhSrv_ = nh2;

    // Subscriptions, Advertising
    first_sub = nhCall_.subscribe("topicname1", 1, &MyClass::callback1, this);
    first_srv = nhSrv_.advertiseService("servicename", &MyClass::servicecallback, this);
    second_sub = nhCall_.subscribe("topicname2", 1, &MyClass::callback2, this);
    third_sub = nhCall_.subscribe("topicname3", 1, &MyClass::callback3, this);

    first_pub = nhCall_.advertise<my_msgs::message>("topicname4", 1);
}

// Callback Handling 
void MyClass::callback1(my_msgs::message msg)
{
    // Do something
}
(...)

bool MyClass::run()
{

    while(nhCall_.ok())  // We may check also for the other NodeHandle state
    {
        ros::spinOnce();
    }

    return true;
}

int main(int argc, char** argv)
{
    ros::init(argc, argv, "mynode");
    ros::NodeHandle nh_call;
    ros::NodeHandle nh_srv;

    MyClass mysupernode(nh_call, nh_srv, ...);
    mysupernode.run();

    return EXIT_SUCCESS;
}

As said, advice or comment are welcome ! Hope this answer may be useful for people that need to do Multithreading in ROS nodes !