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

Reusing nodes by renaming them

asked 2016-06-14 08:24:17 -0600

rbaleksandar gravatar image

updated 2016-06-14 08:26:12 -0600

I'm currently working on a small project that involves the mux from the topic_tools package. The question isn't directly related to the topic mutex. The mutex was just a scenario that revealed the problem I'm having namely properly renaming nodes and reusing code by modifying only a launch file.

In order to illustrated what I'm talking about here is the Python code for a talker node:

#!/usr/bin/env python
import rospy
from std_msgs.msg import String
import sys

def talker():
    msg = 'default message'
    if rospy.has_param('msg'):                         # Never works due to missing namespace
      msg = rospy.get_param('msg', msg)
      rospy.loginfo('Found "msg" parameter with value "%s"' % msg)
      rospy.loginfo('No "msg" parameter found. Fallback to default "%s"' % msg)

    pub = rospy.Publisher('chatter', String, queue_size=1)      
    while not rospy.is_shutdown():
        str = "%s %s" % (msg, rospy.get_time())

if __name__ == '__main__':
    except rospy.ROSInterruptException:

Nodes have to have unique names otherwise naming conflicts occur. Same applies to topics. So for my mutex I decieded to reuse the talker node but with a different name and a different message:


  <remap from="chatter" to="chatter1"/>
  <node name="talker1" pkg="beginner_tutorials" type="" output="screen">
    <param name="msg" type="str" value="message1" />

  <remap from="chatter" to="chatter2"/>
  <node name="talker2" pkg="beginner_tutorials" type="" output="screen">
    <param name="msg" type="str" value="message2" />

  <remap from="chatter" to="chatter3"/>
  <node name="talker3" pkg="beginner_tutorials" type="" output="screen">
    <param name="msg" type="str" value="message3" />

  <node name="talkerMux" pkg="topic_tools" type="mux" args="chatterMUX chatter1 chatter2 chatter3 mux:=talkerMux" />

As you can see I start three instances of and give these accordingly the names talker1, talker2 and talker3 along with a different msg parameter containing some string.

The issue appeared when I ran the launch file and got three times:

No "msg" parameter found. Fallback to default "default message"

Upon calling rosparam list I discovered that the namespaces are the problem since instead of getting msg I get


Of course this is how it's supposed to be due to the fact that parameters have to be unique hence adding the namespace is required when running multiple nodes that have the same parameters.

Every once in a while when I have to deal with namespaces in ROS things get messy. This case is no different. That's why I would like to ask for a tip how to solve my problem - use the same node multiple times with different name and message WITHOUT creating multiple versions of that node by writing code. I thought about a way to retrieve the name of the node from the launch file but have no idea how to that without writing a huge blob of code...Is it even possible?

I can easily copy-paste my and ... (more)

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2016-06-14 08:45:52 -0600

rbaleksandar gravatar image

Got it! There is the rospy.get_name() which returns the current name (even when renamed with <node name="..." />). I can use this to get my msg parameter:

def talker():
    name = rospy.get_name()
    msg = 'default message'
    if rospy.has_param('/' + name + '/msg'):
      msg = rospy.get_param('/' + name + '/msg', msg)

edit flag offensive delete link more



Please look at the private parameter concept. That solves your problem and is really how you should approach this.

gvdhoorn gravatar image gvdhoorn  ( 2016-06-14 09:36:16 -0600 )edit

With private you mean inside the node tag right? If so, I've just changed that since its more readable. Will check how it affects the namespaces. Thanks!

rbaleksandar gravatar image rbaleksandar  ( 2016-06-14 10:14:03 -0600 )edit

Thanks, will take a look.

rbaleksandar gravatar image rbaleksandar  ( 2016-06-15 07:20:47 -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: 2016-06-14 08:24:17 -0600

Seen: 700 times

Last updated: Jun 14 '16