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

How are YAML files used in ROS?

asked 2020-07-07 06:26:10 -0500

Kansai gravatar image

I searched and couldn't find. I would like to know what are YAML files for in ROS and how to use them? Examples or tutorials would be great. I have read about the YAML format used in command lines and that is not what I am asking. Specifically I have a system developed in ROS and it has YAML files and I want to understand its use.

Oh and also, are they still used in ROS2?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2020-07-07 06:56:47 -0500

Weasfas gravatar image

Hi @Kansai,

YAML files are usually used in ROS to load Node configuration parameters in the ROS Parameter server. There are several ways lo load a YAMLfile in the parameter server:

You can load the file with the command line:

rosparam load my_cfg.yaml

Note: If you want to know more about rosparam take a look here.

You can also use this YAML file from a launch file with the rosparam tag:

<rosparam command="load" file="$(find my_pkg)/my_cfg.yaml" />

Note: If tou want to know more about this feaure check this, and if you do not know what roslaunch you can check this.

Then, after loading the file in the parameter server you will be able to get any parameter from there.

With command line:

rosparam list # List all ROS parameters
rosparam get /my_parameter # Get my_parameter value

But the most interesting part is that you can do this programatically generating a specific configuration for your node. Both roscpp and rospy have functions to deal with parameters, with get, set, search... instructions.

For instance if you have a C++ node and you want this node to have a configurable rate, you can load your rate in the parameter server:

rosparam set /node_rate "20.0" # or with yaml a file.

and retrieve this parameter in the node with:

ros::NodeHandle nh;
double rate;
nh.getParam("node_rate" , rate);

even you can set default params if the parameter is not found in the server with:

nh.param<double>("node_rate ", rate, 10.0);

The python API is very similar so If you want to know more about roscpp you can check this, for python this.

As for your last question, of course it is used in ROS2 since it is a very powerful tool to generate any kind of configurable enviroment.

The main difference is that in ROS2 there is no "global parameter server" since all parameters are "owned" by individual nodes. Apart from that, the idea is very similar, although function usage are a bit different.

You can learn more about command line tool for ROS2 parameters here and how to use it programatically here: C++ and Python.

Hope this helped you solving your doubts about this topic.


edit flag offensive delete link more



This is a beautiful answer, with complete explanation and all helpful links. Thanks for going out of your way to be so helpful and make this community better!

parzival gravatar image parzival  ( 2020-07-07 09:15:48 -0500 )edit

This was wonderfully put.

karry3775 gravatar image karry3775  ( 2020-11-15 12:33:36 -0500 )edit

Really helpul. now I am able to connect things .Thanks!

noob_nerd gravatar image noob_nerd  ( 2023-04-12 04:06:41 -0500 )edit

Question Tools



Asked: 2020-07-07 06:26:10 -0500

Seen: 9,737 times

Last updated: Jul 07 '20