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

Running several nodes under a "top" node

asked 2011-05-11 19:11:21 -0600

Nash gravatar image

updated 2014-01-28 17:09:40 -0600

ngrennan gravatar image

I have organized my code in such a way that when I run a "main" file, it calls methods in other classes, from other packages, to accomplish a task. I soon realized that the classes written, requires a rospy node so that it can communicate with ROS_MASTER. But I cannot initialize other nodes under the "main" node as i think ROS only allows one node to be initialized and not a node inside another.

Example: The classes in the other packages use SimpleActionClient/action client which uses the actionlib, that requires to initialize a node to publish messages and call services. I had only initialized a node for the "main" method. And since I cannot initialize any more nodes, the code does not really do anything

Any advice on how to resolve this issue? or any alternative ideas?


edit retag flag offensive close merge delete


Can you update your question to clarify what you're trying to do, and what isn't working? Possibly provide an example. From your description everything sounds doable.
tfoote gravatar image tfoote  ( 2011-05-11 19:59:42 -0600 )edit
Sorry about that!, hope this edit makes sense..
Nash gravatar image Nash  ( 2011-05-12 11:37:51 -0600 )edit

2 Answers

Sort by » oldest newest most voted

answered 2011-05-11 21:35:08 -0600

From what I understand you want several classes of your program to use ros-node functionality. You can just pass them the node handle in their constructor and store it in a member variable. See here for such a constructor definition.

edit flag offensive delete link more


Thank You! works like a charm!
Nash gravatar image Nash  ( 2011-05-12 19:32:44 -0600 )edit

answered 2011-05-12 15:50:34 -0600

tfoote gravatar image

I'm assuming your using c++.

You can create as many NodeHandles as you want in your code. You only need to call ros::init at the top of your main. Everything else can just create a ros::NodeHandle using any valid constructor.

edit flag offensive delete link more


Actually, Im using Python, according to, it mentions that "You can only have one node in a rospy process, so you can only call rospy.init_node() once" But I got it to work by passing it down to the constructor, like you said.
Nash gravatar image Nash  ( 2011-05-12 19:35:01 -0600 )edit
Are you saying, when I create two node handles in the same executable, they both refer to the same node?
Felix Endres gravatar image Felix Endres  ( 2011-05-14 04:44:29 -0600 )edit
Yes, that's right, Felix. It took me a while to figure that out, too - I used to pass node handles around everywhere, but that is not necessary.
Martin Günther gravatar image Martin Günther  ( 2011-05-16 22:12:16 -0600 )edit

Question Tools


Asked: 2011-05-11 19:11:21 -0600

Seen: 2,062 times

Last updated: May 12 '11