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

Use arguments from roslaunch in python.

asked 2021-08-19 01:11:16 -0600

dev_ops gravatar image

updated 2021-08-19 14:38:05 -0600

jayess gravatar image

So I have created a roslaunch file as follows

<node pkg="listen" name="listen_node" type="listen.py" args="$ (arg mode)" output="screen">

So after running this launch file

 roslaunch listen listen.launch mode:=dropping

I print sys.argv to check which arguments are passing, so it give me this list of arguments:

arguments = ['/home/user/ros_ws/devel/lib/listen/listen.py', '__name:=listen', 
'__log:=/home/user/.ros/log/5d25e394-00b2-11ec-b895-c912d3f66a28/listen-2.log', '$' , '(arg' ,'mode)']

So it is only giving the argument name which I passed in launch file not the one I am using in CLI.

Can anyone please tell me how can I use the arguments inside python script?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-08-19 10:09:19 -0600

Ranjit Kathiriya gravatar image

Hello there,

roslaunch listen listen.launch mode:=dropping

You are passing mode in the command line arguments, so you have to first catch this value into your launch file. You can do so by <arg name="mode" default="_default_text_">.

2 step you have to send this argument to python file.

<node name="node_name" pkg="node_pkg_name" type="filename.py" output="screen" clear_params="true">
       <param name="mode" value="$(arg mode)" /> 
</node>

If you are passing an argument to CLI and you want that same argument into your launch file you can access it via value="$(arg mode)"

You can access in python by using this code snippet.

#!/usr/bin/env python
import rospy
device = rospy.get_param('/node_name/mode') # node_name/argsname

So, full launch file structure should be like this:

<launch>
<arg name="mode" default="_default_text_">
<node name="node_name" pkg="node_pkg_name" type="filename.py" output="screen" clear_params="true">
                <param name="mode" value="$(arg mode)" /> 
         </node>
</launch>
edit flag offensive delete link more

Question Tools

Stats

Asked: 2021-08-19 01:11:16 -0600

Seen: 4,474 times

Last updated: Aug 19 '21