5

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
  • 63,191
  • 45
  • 217
  • 228
  • 1
    After reading your question again, I am not 100% sure, what you are doing. Are you calling the launch-file to start the script (this is what I assume in my answer) or do you only want to read the parameters from the launch-file without actually launching it? – luator Jun 26 '15 at 08:24
  • I am calling the launch file – lmiguelvargasf Jun 26 '15 at 13:33

2 Answers2

7

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')
luator
  • 4,769
  • 3
  • 30
  • 51
  • 2
    For me `rospy.get_param("/ardrone_driver/altitude_max")` worked, but your help was really useful, so thanks. – lmiguelvargasf Jun 26 '15 at 20:50
  • 1
    just 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. – Veilkrand Sep 28 '18 at 08:26
  • 1
    In addition to what @Veilkrand said: When using a launch file, the node name given in `init_node` is irrelevant, instead it uses the `name` set in the `node` 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. – luator Oct 23 '18 at 07:28
1

For me, rospy.get_param("/ardrone_driver/altitude_max") worked

jeff wang
  • 149
  • 1
  • 3