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

Revision history [back]

Please use the args attribute of the <node> tag. To assist you better, I made a sample code. Please see below:

  1. I created my_args.launch file as shown below:

    ravi@dell:~/ros_ws/src/ros_tutorials/rospy_tutorials/001_talker_listener$ cat my_args.launch 
    <launch>
        <node pkg="rospy_tutorials" type="listener.py" name="listener" args="/dev/ttyUSB0" output="screen" />
    </launch>
    
  2. I edited the listener.py so that it can capture sys.argv[1] as shown below:

    #!/usr/bin/env python
    # license removed for brevity
    import sys
    import rospy
    from std_msgs.msg import String
    
    def callback(data):
        rospy.loginfo(rospy.get_caller_id() + 'I heard %s', data.data)
    
    def listener(param):
        rospy.init_node('listener', anonymous=True)
        rospy.loginfo("param:" + param)
        rospy.Subscriber('chatter', String, callback)
        rospy.spin()
    
    if __name__ == '__main__':
        listener(sys.argv[1])
    
  3. Finally, I ran the launch file. I can see the value in the terminal. Please see below:

    $ roslaunch rospy_tutorials my_args.launch 
    ... logging to /home/ravi/.ros/log/828983fa-2e9e-11ed-bbf8-d707e2da4a66/roslaunch-dell-90235.log
    Checking log directory for disk usage. This may take a while.
    Press Ctrl-C to interrupt
    Done checking log file disk usage. Usage is <1GB.
    
    started roslaunch server http://dell:33681/
    
    SUMMARY
    ========
    
    PARAMETERS
     * /rosdistro: noetic
     * /rosversion: 1.15.14
    
    NODES
      /
        listener (rospy_tutorials/listener.py)
    
    ROS_MASTER_URI=http://localhost:11311
    
    process[listener-1]: started with pid [90264]
    [INFO] [1662549440.441759]: param:/dev/ttyUSB0
    [INFO] [1662549452.298103]: /listenerI heard hello world 1662549452.294159
    [INFO] [1662549452.398594]: /listenerI heard hello world 1662549452.3944724
    [INFO] [1662549452.498860]: /listenerI heard hello world 1662549452.4945526
    [INFO] [1662549452.596540]: /listenerI heard hello world 1662549452.5943813
    

Please note that the original package in this post is taken from ros/ros_tutorials repository.

Please use the args attribute of the <node> tag. To assist you better, I made a sample code. Please see below:

  1. I created my_args.launch file as shown below:

    ravi@dell:~/ros_ws/src/ros_tutorials/rospy_tutorials/001_talker_listener$ cat my_args.launch 
    <launch>
        <node pkg="rospy_tutorials" type="listener.py" name="listener" args="/dev/ttyUSB0" output="screen" />
    </launch>
    
  2. I edited the listener.py so that it can capture sys.argv[1] as shown below:

    #!/usr/bin/env python
    # license removed for brevity
    import sys
    import rospy
    from std_msgs.msg import String
    
    def callback(data):
        rospy.loginfo(rospy.get_caller_id() + 'I heard %s', data.data)
    
    def listener(param):
        rospy.init_node('listener', anonymous=True)
        rospy.loginfo("param:" rospy.loginfo('param:' + param)
        rospy.Subscriber('chatter', String, callback)
        rospy.spin()
    
    if __name__ == '__main__':
        listener(sys.argv[1])
    
  3. Finally, I ran the launch file. I can see the value in the terminal. Please see below:

    $ roslaunch rospy_tutorials my_args.launch 
    ... logging to /home/ravi/.ros/log/828983fa-2e9e-11ed-bbf8-d707e2da4a66/roslaunch-dell-90235.log
    Checking log directory for disk usage. This may take a while.
    Press Ctrl-C to interrupt
    Done checking log file disk usage. Usage is <1GB.
    
    started roslaunch server http://dell:33681/
    
    SUMMARY
    ========
    
    PARAMETERS
     * /rosdistro: noetic
     * /rosversion: 1.15.14
    
    NODES
      /
        listener (rospy_tutorials/listener.py)
    
    ROS_MASTER_URI=http://localhost:11311
    
    process[listener-1]: started with pid [90264]
    [INFO] [1662549440.441759]: param:/dev/ttyUSB0
    [INFO] [1662549452.298103]: /listenerI heard hello world 1662549452.294159
    [INFO] [1662549452.398594]: /listenerI heard hello world 1662549452.3944724
    [INFO] [1662549452.498860]: /listenerI heard hello world 1662549452.4945526
    [INFO] [1662549452.596540]: /listenerI heard hello world 1662549452.5943813
    

Please note that the original package in this post is taken from ros/ros_tutorials repository.