ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
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 -0600

suab123 gravatar image

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

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
1

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

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 image suab123  ( 2019-08-15 09:09:07 -0600 )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 -0600 )edit

Okay.thnaks

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

Question Tools

2 followers

Stats

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

Seen: 323 times

Last updated: Aug 15 '19