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

Common usage of NodeHandle?

asked 2022-07-24 13:51:40 -0500

joaocandre gravatar image

When developing classes that provide ROS-specific functionality, what is the recommended approach (if any) to use the NodeHandle class?

Should they 1) be passed to each class as a constructor argument (copied/referenced from a 'global' handle initialized on main()), or 2) be declared as private/protected members & initialized by each class's constructor? What would be the pros/cons of each implementation?

I've been using (1) for some time, but I wonder if it's what NodeHandle was designed for? Passing around a shared NodeHandle clutters the constructor signatures and otherwise hinders code readability. A NodeHandle is cheap to copy, so I'm assuming performance-wise it's irrelevant, but maybe there is some other aspect I'm not seeing?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-07-25 03:51:48 -0500

Per Edwardsson gravatar image

If you have many components which all need some ROS action, all of them either need their constructor cluttered _or_ all of them provide a node which will clutter your graph and node list. I would rather clutter constructors, but to each their own.

More to your question in particular, cons for 1) include possible parameter handling being messier, re-usability being lower (does it actually need a node handle, or just some data from the ROS network?), and increased complexity with regards to multi threading. Cons for 2) include a larger ROS network, which can make troubleshooting and logging messier.

Perhaps better yet is to de-couple the ROS communication from the rest of your program. Many libs follow this model - make a standalone program and a ROS wrapper. I imagine that for most type of GUI work, if that's your cup of tea, is also done best with a ROS backend serving a frontend which has no nodes or other ROS stuff in it.

edit flag offensive delete link more

Comments

all of them either need their constructor cluttered _or_ all of them provide a node which will clutter your graph and node list

please note: the OP tagged this ROS1, so NodeHandle != node. There is only a single node, no matter the nr of copies or instances of NodeHandle.

gvdhoorn gravatar image gvdhoorn  ( 2022-07-25 04:06:36 -0500 )edit

Instantiating NodeHandle does not initialize a new node though, just a handle to perform ROS operations. So the node diagram would be the same either way. Also, these classes I'm describing are already ROS wrappers by their own right. As for the rest, the only advantage I see in (1) is if there is a need to share a private handle among multiple components.

joaocandre gravatar image joaocandre  ( 2022-07-25 08:48:15 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2022-07-24 13:51:40 -0500

Seen: 114 times

Last updated: Jul 25 '22