Ask Your Question

What is the use of multiple nodehandles

asked 2015-09-28 11:02:00 -0600

takahashi gravatar image

I didn't really get the concept of nodehandles from the ROS wiki and couldn't find any concise answer with google. I understood that a ROS node can have multiple nodehandles and they refer to the same node. In a node's main(), I first initialize the node using ros::Init() and then use a nodehandle for starting and shutting down the node. So what does the declaration of multiple nodehandles do in this case?

edit retag flag offensive close merge delete

4 Answers

Sort by » oldest newest most voted

answered 2015-09-29 02:57:47 -0600

Adolfo Rodriguez T gravatar image

Two reasons for having different NodeHandle instances in the same node are:

1. namespace scope: This is the most common usecase, and specifies the namespace prefix that applies to all graph resource names that hang from it. This is what @Boris refers to in his answer. For instance, the following two constructs are equivalent:


2. control callback spinning: This usecase is more advanced, or at least less common. It is possible to assign different callback queues to different NodeHandle instances, so you can for instance have a node in which some callbacks are processed at high-frequency (e.g. control commands), while others are processed at a slower rate.

edit flag offensive delete link more

answered 2015-09-28 17:26:00 -0600

updated 2015-09-28 17:26:55 -0600

The most common use is to get access to both private and global ROS namespaces.

So a very typical thing to do would be something like this:

node_ = ros::NodeHandlePtr(new ros::NodeHandle);
pnode_ = ros::NodeHandlePtr(new ros::NodeHandle("~"));

with this approach you can publish your topics on the global namespace by using node_->advertise<...>(...), while accessing parameters under private namespace with pnode_->param(...).

Also note that NodeHandlePtr is actually a shared pointer (smart pointer), so it does internal reference counting, thus it controls object's lifetime.

edit flag offensive delete link more


Thank you for the answer and especially for the note about the shared pointer nature.

takahashi gravatar imagetakahashi ( 2015-09-29 01:41:20 -0600 )edit

Do you have an example of another possible use case?

takahashi gravatar imagetakahashi ( 2015-09-29 01:54:57 -0600 )edit

Not really, apart from what Adolfo mentioned.

Boris gravatar imageBoris ( 2015-09-29 06:28:25 -0600 )edit

answered 2015-09-29 04:20:43 -0600

SVS gravatar image

You can use this blog for better understanding.

edit flag offensive delete link more

answered 2019-04-09 02:03:50 -0600

七小丘人 gravatar image

In my opinion, sometimes I need to use a service client in a callback, then I can create a new nodehandle.

edit flag offensive delete link more

Your Answer


Add Answer

Question Tools



Asked: 2015-09-28 11:02:00 -0600

Seen: 5,525 times

Last updated: Apr 09 '19