How to get ROS xml param from launch file using Python
Solution 1
Parameters set in a launch file are stored at the ROS parameter server.
Accessing these parameters from a python node is quite easy as is shown on this wiki page. In your case the parameters are defined as private parameters of the node (because they are defined inside the <node>
tag), so you have to prefix them with ~
when accessing them:
altitude_max = rospy.get_param('~altitude_max')
Solution 2
For me, rospy.get_param("/ardrone_driver/altitude_max") worked
lmiguelvargasf
I enjoy solving problems. I like math, science, philosophy and technology. LinkedIn
Updated on June 12, 2022Comments
-
lmiguelvargasf almost 2 years
I have a launch file which extension is xml, and I would like to get the value of a parameter. This launch file is called ardrone.launch
<!-- This is a sample lanuch file, please change it based on your needs --> <launch> <node name="ardrone_driver" pkg="ardrone_autonomy" type="ardrone_driver" output="screen" clear_params="true"> <param name="outdoor" value="1" /> <param name="flight_without_shell" value="1" /> <param name="max_bitrate" value="4000" /> <param name="bitrate" value="4000" /> <param name="navdata_demo" value="0" /> <param name="altitude_max" value="10000" /> <param name="altitude_min" value="50" /> <param name="euler_angle_max" value="0.35" /> <param name="control_vz_max" value="2000" /> <param name="control_yaw" value="1.75" /> </node> </launch>
For example, I would like to get the value from altitude_max, altitude_min, and the others using python. I have to add that this file is inside a directory called launch and the file where I am calling it is at a directory called scripts, and scripts and launch are both in the same directory.
-
lmiguelvargasf almost 9 yearsFor me
rospy.get_param("/ardrone_driver/altitude_max")
worked, but your help was really useful, so thanks. -
rana88 over 5 yearsjust make sure you call before
rospy.init_node("ardrone_driver", anonymous=True)
with the correct node name and you will be able to map private params using rospy.get_param('~altitude_max'), no need to reference the full param path. -
luator over 5 yearsIn addition to what @Veilkrand said: When using a launch file, the node name given in
init_node
is irrelevant, instead it uses thename
set in thenode
tag of the launch file. Therefore it is better to always use~param_name
when addressing private parameters, as the node itself has no direct control over its name.