Use arguments from roslaunch in python.
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?
Asked by dev_ops on 2021-08-19 01:11:16 UTC
Answers
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>
Asked by Ranjit Kathiriya on 2021-08-19 10:09:19 UTC
Comments