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

How to set parameters names for python nodes in roslaunch

asked 2022-09-05 07:46:36 -0600

Vic gravatar image

updated 2022-09-07 06:27:59 -0600

ravijoshi gravatar image

I am trying to automate a rosrun into a roslaunch. My command is shown below:

rosrun robotiq_2f_gripper_control Robotiq2FGripperRtuNode.py /dev/ttyUSB0

Problem:

I can not find a way to pass my argument as a parameter. The source code uses sys.argv[1] as the command line parameter. Is there a way to make this work in a launch file?

This is what I tried:

<launch>
    <node pkg="robotiq_2f_gripper_control" type="Robotiq2FGripperRtuNode.py" name="Robotiq2FGripperRtuNode" output="screen">
    <param name="device" value="/dev/ttyUSB1" />
</launch>

However, the execution is similar to mistyping the name of the device port in the rosrun command. I can edit the source code. Furthermore, I know that I could call the rosrun command in a .sh script, but I would appreciate using the proper tool for the job.

Offending package: TAMS-Group/robotiq

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-09-07 06:26:03 -0600

ravijoshi gravatar image

updated 2022-09-07 06:26:44 -0600

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.

edit flag offensive delete link more

Comments

Thank you for your answer. Will try it out and let you know if that solve my problem.

Vic gravatar image Vic  ( 2022-09-13 04:04:03 -0600 )edit

Setting args="/dev/ttyUSB0" solved the issue. Thanks.

Vic gravatar image Vic  ( 2022-09-13 04:14:04 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2022-09-05 07:46:36 -0600

Seen: 81 times

Last updated: Sep 07 '22