How to pass argument values from launch file to python script? [closed]

asked 2020-11-20 21:39:31 -0600

ba520y 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 reopen merge delete

Closed for the following reason duplicate question by miura
close date 2020-11-22 09:18:15.263030

Comments

miura gravatar image miura  ( 2020-11-22 09:18:31 -0600 )edit