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

How to pass argument values from launch file to python script?

asked 2020-11-20 21:39:37 -0500

ba520y gravatar image

updated 2020-11-21 14:48:59 -0500

jayess gravatar image

I am trying to pass argument values from launch file to python script. Why I need this, because a script for each node is same codes. Current code is below.

talk_listen.launch

<launch>
    <node pkg="service_communication" name="talker" type="talker.py"/>
    <node pkg="service_communication" name="listener" type="listener.py" output="screen"/>
    <node pkg="service_communication" name="listener2" type="listener2.py" output="screen"/>
</launch>

listener.py

import rospy
from std_msgs.msg import String

def callback(data):
    rospy.loginfo(rospy.get_caller_id() + 'I heard %s', data.data)

def listener():
    rospy.init_node('**listener**', anonymous=True)
    rospy.Subscriber('**chatter**', String, callback)
    rospy.spin()

listener2.py

import rospy
from std_msgs.msg import String

def callback(data):
    rospy.loginfo(rospy.get_caller_id() + 'I heard %s', data.data)

def listener():
    rospy.init_node('**listener2**', anonymous=True)
    rospy.Subscriber('**chatter2**', String, callback)
    rospy.spin()

And the differences of listener.py and listener2.py are only listener name and chatter name. So I want to pass both name from launch file to python script.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2020-11-21 12:41:30 -0500

Tahir M. gravatar image

updated 2020-11-23 03:11:21 -0500

From your launch file both in .launch and its api you can use args to pass arguments. In python you can use sys.argv to take this arguments as input from your launch file.

edit flag offensive delete link more

Comments

Thank you for your answer.

Instead of arg, I inserted args into a node tag. And then Python script got value from it.

ba520y gravatar image ba520y  ( 2020-11-22 19:35:48 -0500 )edit

Sorry for the typo, now fixed

Tahir M. gravatar image Tahir M.  ( 2020-11-23 03:12:27 -0500 )edit
2

answered 2020-11-23 02:59:43 -0500

gvdhoorn gravatar image

updated 2020-11-23 03:01:06 -0500

Using args for parameters of ROS nodes is possible, but not necessarily recommended. It might be better to use the Parameter server instead.

args is really only supported for scripts which don't support the Parameter server, or which need to access configuration data before initialising their "ROS side".

Having written that:

And the differences of listener.py and listener2.py are only listener name and chatter name. So I want to pass both name from launch file to python script.

Using parameters to configure names of nodes and topic names is what I would consider an anti-pattern.

Setting the name of the node is done using the name attribute of the node element in the .launch file. You seem to already be doing that, so there should be no need to change rospy.init_node('**listener2**', anonymous=True) again.

Changing topic names at configure / run-time should be done using remapping (#q303611).

In your specific case:

<launch>
  <node pkg="service_communication" name="talker" type="talker.py"/>
  <node pkg="service_communication" name="listener" type="listener.py" output="screen"/>
  <node pkg="service_communication" name="listener2" type="listener.py" output="screen">
    <remap from="chatter" to="chatter2" />
  </node>
</launch>

Note: there should be no need for two copies of listener.py, if all you want to do is change node and topic names.

So I've changed the type for listener2 back to listener.py.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2020-11-20 21:39:37 -0500

Seen: 3,628 times

Last updated: Nov 23 '20