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

I m trying to make my Nodehandle global,below is my code.But I m unable to listen to subscribed topic.

asked 2019-08-14 11:15:04 -0500

suab123 gravatar image

updated 2019-08-14 13:41:18 -0500

jayess gravatar image

I m trying to make my Nodehandle global,below is my code.But I m unable to listen to subscribed topic


        using namespace std;

        class Node{
                ros::NodeHandle nh;
                Node(ros::NodeHandle &nh){
                ros::NodeHandle getnh(){
                    return this->nh;

        void dataRecevied(const::std_msgs::Float32::ConstPtr &msg){
            ROS_INFO("Range is %f",msg->data);

        void turn(Node n){
            ros::NodeHandle nh=n.getnh();
            ros::Subscriber sub;


        int main(int argv,char **argc){
            ros::NodeHandle nh;
            Node pub(nh);
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2019-08-15 05:28:03 -0500

The problem with your subscriber is not related to the node handles, although I'll get to that later.

Your subscriber will only function while the sub object exists, since you declare it as a local variable in the turn() method as soon as that function finishes the subscriber is destroyed. This is the reason why you're not receiving any messages.

I would recommend putting everything into your class and storing the subscriber as a private member of the class.

Node handles are slightly unusual in that they always behave as the same object whenever you create them, so there is no point storing and passing a node handle object, because any code can create a new node handle and it will behave exactly the same way.

If you move all your ROS code into the class then your constructor would create a nodeHandle as usual, and use it to create the subscriber.

Hope this helps.

edit flag offensive delete link more


Thank You,your method works..But I have two doubt now,first is that I dont have to declare my NodeHandle global whenever I want to use NodeHandle outside the main function I will just create a new one and it will work fine isnt it?

My second doubt is little out of box and silly..What is the use of OOPs when we are just creating a ros Node isnt the main function and the callback functions are sufficient.

suab123 gravatar image suab123  ( 2019-08-15 09:09:07 -0500 )edit

Your first point is correct, you can create a new nodeHandle object whenever you need one.

Your second point is a big question. You can do everything in ROS without using OOP, but it does get tidier for larger projects to use it. Some would say it's a matter of taste, others would say its the more efficient way of engineering large projects.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2019-08-15 10:23:33 -0500 )edit


suab123 gravatar image suab123  ( 2019-08-15 14:59:44 -0500 )edit

Question Tools



Asked: 2019-08-14 11:15:04 -0500

Seen: 249 times

Last updated: Aug 15 '19