ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

question about the nodehandle for subscribing and publishing

asked 2014-11-09 05:42:11 -0600

Marcus gravatar image

Hello everyone,

I have created my own package (with catkin) in which I am subscribing and publishing to several topics. So far, for each topic (wheather for subcribing or publishing) I have created a seperate nodehandle and I was wondering if that is actually necessary or if something like this (basically just having one single nodehandle) is possible:

ros::NodeHandle nh_my_pkg("~");     
ros::Publisher publisher = nh_my_pkg.advertise<geometry_msgs::Twist>("/topic name", 1);
ros::Subscriber subscriber = nh_my_pkg.subscribe<geometry_msgs::Twist>("/topic name", 1, &my_class::callback_fct, this);

Thanks and have a nice day


edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2014-11-09 11:31:28 -0600

gvdhoorn gravatar image

updated 2014-11-09 11:35:55 -0600

Yes, reusing your NodeHandle instance is perfectly fine in general.

In your specific example though, you are creating a NodeHandle referencing the private namespace ("~") of the node, then start creating publishers and subscribers through it that publish and subscribe to topics that appear to be in the global namespace. I'm not sure, but that either won't work, or the node will try to find /topic_name in its private namespace, and fail.

edit flag offensive delete link more


Thanks for the answer. Just to clarify tho, you would suggest to use:

ros::NodeHandle nh_my_pkg;

instead of:

ros::NodeHandle nh_my_pkg("~");


Marcus gravatar image Marcus  ( 2014-11-10 06:12:43 -0600 )edit

No: NodeHandles for parameters usually access a node's private namespace ("~"). Topics are normally in the global / relative namespace, so you'd need a second NodeHandle instance for that. The second one can then be re-used by your publishers / subscribers.

gvdhoorn gravatar image gvdhoorn  ( 2014-11-10 06:38:14 -0600 )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

1 follower


Asked: 2014-11-09 05:42:11 -0600

Seen: 994 times

Last updated: Nov 09 '14