Ask Your Question
0

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

#include<ros/ros.h>
        #include<tf/transform_listener.h>
        #include<std_msgs/Float32.h>

        using namespace std;

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

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

        void turn(Node n){
            ros::NodeHandle nh=n.getnh();
            ros::Subscriber sub;
            ROS_INFO("yes2");
            sub=nh.subscribe("test1",1,dataRecevied);

        }

        int main(int argv,char **argc){
            ros::init(argv,argc,"tf_listener");
            ros::NodeHandle nh;
            Node pub(nh);
            ROS_INFO("yes1");
            turn(pub);
            ros::spin();
        }
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

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

Comments

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 imagesuab123 ( 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 imagePeteBlackerThe3rd ( 2019-08-15 10:23:33 -0500 )edit

Okay.thnaks

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

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

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

Seen: 18 times

Last updated: Aug 15