ROS Param YAML Format

In this tutorial I’ll show you how to handle ROS params with a YAML file. ROS parameters are a great way to modify settings in your program without having to re-compile anything.

But when you start to have many ROS params – sometimes more than a few hundreds – things  start to become quite complicated. You’ll definitely not want to set up all ROS parameters from your nodes directly. Also, forget about setting them one by one with the rosparam command line tool.

Writing the parameters into a launch file (XML) is a viable solution, but for hundreds of parameters, XML can be quite tedious.

Solution: write your ROS parameters into a YAML file, and then load this file from a launch file.

Let’s get started!

Write a YAML file with ROS parameters

What is YAML?

YAML stands for: “YAML Ain’t Markup Language”. Basically you can just consider YAML as a superset of JSON and XML. It serves the same purpose, but was made to be more human-readable and easy to write.

<!-- XML example -->

<text>Hello</text>
<number_int>42</number_int>
# YAML example, more readable by humans

text: "Hello"
number_int: 42

I won’t give a detailed explanation here. You just need to know that ROS supports YAML for parameters, and it’s quite handy to use it.

Where to put your ROS param YAML file?

There is no specific rule about where to put your ROS param YAML files in your ROS catkin workspace.

What I recommend: create a /config folder inside your package.

Your package architecture will look like this:

/include          : Cpp headers
/src              : Cpp code
/scripts          : Python code
/config           : YAML files
/launch           : Launch files
CMakeLists.txt
package.xml

Types you can use

You can use any type supported by the ROS Parameter Server.

text: "Hello"
number_int: 42 
number_float: 21.3
enable_boolean: true
list_of_elements:
    - 1
    - 2
    - 3
    - 4
dictionary: {
    another_text: "World",
    another_number: 12,
}

So, we’ll write this basic YAML file and put it inside the /config folder of the package.

Loading params from a YAML file

With command line

Use the “rosparam load” command to load all parameters from a YAML file to the Parameter Server (don’t forget to start a roscore in another terminal before).

$ rosparam load my_params.yaml
$ rosparam list
/dictionary/another_number
/dictionary/another_text
/enable_boolean
/list_of_anything
/number_float
/number_int
/rosdistro
/roslaunch/uris/host_ed_pc__35397
/rosversion
/run_id
/text

You can also do the opposite: dump all existing parameters from the Parameter Server to a YAML file. For this, just use the rosparam dump command. This can be useful for debugging.

$ rosparam dump dump.yaml
$ cat dump.yaml
dictionary: {another_number: 12, another_text: World}
enable_boolean: true
list_of_anything: [1, 2, 3, 4]
number_float: 21.3
number_int: 42
rosdistro: 'melodic

  '
roslaunch:
  uris: {host_ed_pc__35397: 'http://ed-pc:35397/'}
rosversion: '1.14.3

  '
run_id: 87212008-594f-11e9-a1f2-3ca9f44ca860
text: Hello

In a launch file

You can also load a YAML file directly into a launch file. This is quite handy and it’s the best way to handle ROS parameters.

Let’s create a new launch file inside the /launch folder of the package.

<launch>
    <rosparam file="$(find my_custom_package)/config/my_params.yaml" />
</launch>

You can use the <rosparam> tag with the “file” argument to load all parameters from a YAML file. Here we don’t even need to give an absolute path. By using $(find my_package), ROS will automatically get the path to your package, and you just need to complete with the relative path to your YAML file.

$ roslaunch my_custom_package test.launch

In the terminal logs following this command, you’ll see:

SUMMARY
========

PARAMETERS
 * /dictionary/another_number: 12
 * /dictionary/another_text: World
 * /enable_boolean: True
 * /list_of_anything: [1, 2, 3, 4]
 * /number_float: 21.3
 * /number_int: 42
 * /rosdistro: melodic
 * /rosversion: 1.14.3
 * /text: Hello

You can also check with rosparam list if all the parameters from the YAML file have been added into the Parameter Server.

And if you want to add a prefix to all the params in the YAML file, then only one change is required:

<launch>
    <group ns="custom_prefix">
        <rosparam file="$(find my_custom_package)/config/my_params.yaml" />
    </group>
</launch>

Here’s the result:

SUMMARY
========

PARAMETERS
 * /custom_prefix/dictionary/another_number: 12
 * /custom_prefix/dictionary/another_text: World
 * /custom_prefix/enable_boolean: True
 * /custom_prefix/list_of_anything: [1, 2, 3, 4]
 * /custom_prefix/number_float: 21.3
 * /custom_prefix/number_int: 42
 * /custom_prefix/text: Hello
 * /rosdistro: melodic
 * /rosversion: 1.14.3

Using YAML parameters in your code

From your code (Python, Cpp, …), you just have to set and get the parameters as usual. Once the parameters are loaded from YAML into the ROS Parameter Server, nothing is different.

In Python:

rospy.get_param("/custom_prefix/number_float")

In Cpp:

ros::NodeHandle nh;
double number_to_get;
nh.getParam("/custom_prefix/number_float", number_to_get);

Getting a parameter from the code inside ROS nodes is always the same, no matter how you created the parameters – with command lines, YAML load, launch file, launch file with YAML, etc.

Leave a Comment