Ask Your Question
4

Node naming and running. rospy.init_node() and .launch file nodes.

asked 2014-09-03 13:17:30 -0600

updated 2014-09-03 13:28:14 -0600

Hello everyone

For learning purposes I have created a simple launch file and simple python scripts. This is my launch file

<launch>
    <node name="rf_sim_node_1" pkg="rf_trilateration" type="rf_sim_node.py" args="5,5"/>
    <node name="rf_sim_node_2" pkg="rf_trilateration" type="rf_sim_node.py" args="-5,-5"/>
    <node name="rf_sim_node_3" pkg="rf_trilateration" type="rf_sim_node.py" args="-5,5"/>
    <node name="rf_sim_node_4" pkg="rf_trilateration" type="rf_sim_node.py" args="5,-5"/>
    <node name="rf_trilateration_service" pkg="rf_trilateration" type="rf_trilateration_server.py"/>
</launch>

and this is the rf_sim_node.py

#!/usr/bin/env python

import rospy


def start_server():
    rospy.init_node("rf_node", anonymous=True)
    rospy.spin()

if __name__ == "__main__":
    start_server()

When I launch my file with roslaunch

$rosnode list gives out this

/rf_sim_node_1
/rf_sim_node_2
/rf_sim_node_3
/rf_sim_node_4
/rf_trilateration_service
/rosout

Now I am wondering what does rospy.init_node does actually do?. I was expecting to see "rf_node-14 " kinda nodes but end up wtih the node names from the launching file.

What if I make multiple calls to rospy.init_node() will I get new nodes or am I doing something wrong. I read at rospy documentation that I should only call this once but why is the nodes name defined or created at creation of process ?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
4

answered 2014-09-04 05:07:35 -0600

Chrissi gravatar image

updated 2015-01-22 02:19:25 -0600

130s gravatar image

I suggest you have a look at this wiki page.

The short explanation for the observed phenomenon would be:

rosp.init_node() defines a default name which is used if you don't overwrite it (e.g. rosrun rf_trilateration rf_sim_node.py). The name tag in the launch file on the other hand overwrites this name and therefore you will always find the node named the same as the name tag in the launch file.

The most important part about naming nodes is that the name has to be unique. If you try to start two nodes with the same name, the first node would be killed when the second one is started. The argument anonymous=True makes sure that you always have a unique name for your node. If you would run:

rosrun rf_trilateration rf_sim_node.py
rosrun rf_trilateration rf_sim_node.py

so the same command twice, you would see a different rosnode list output.

Edit: To answer the comment

name is actually a mandatory field. If you give an empty string name="" it will create a name from the type=myscript.py tag myscript_py_machinename_id_id_id which is the same as using name=$(anon some_name) which will create nodes called: some_name_manchinename_id_id_id so it uses the name some_name instead of the name of the binary or python script given to type.

The name tag however voids the default name and the anonymous attribute given to init_node(). So from my experience and what I could find on the internet, there is no way of using the default name when starting the node via roslaunch.

edit flag offensive delete link more

Comments

I see thank you for your answer and the link. Just to be clear roslaunch's <node name="bla"/> attribute overwrites whatever name I can give at init_node parameter inside my code. Is there a way I can use roslaunch without giving node names?

Ali.Akdurak gravatar imageAli.Akdurak ( 2014-09-04 05:29:46 -0600 )edit

Thanks a lot :D, You were very helpfull

Ali.Akdurak gravatar imageAli.Akdurak ( 2014-09-04 06:30:08 -0600 )edit

You're very welcome.

Chrissi gravatar imageChrissi ( 2014-09-04 07:30:46 -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

2 followers

Stats

Asked: 2014-09-03 13:17:30 -0600

Seen: 6,994 times

Last updated: Jan 22 '15