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

question about the nodehandle for subscribing and publishing

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

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

Marcus

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

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

gvdhoorn gravatar image

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

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

Comments

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("~");

correct?

Marcus gravatar image Marcus  ( 2014-11-10 06:12:43 -0500 )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 -0500 )edit

Question Tools

1 follower

Stats

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

Seen: 1,129 times

Last updated: Nov 09 '14