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

How to write and parse "yaml" file for ROS?

asked 2011-12-12 21:49:25 -0500

Felix Tr gravatar image

updated 2011-12-12 23:14:07 -0500

Hello all,

My goal is to subscribe to an "unknown" number of topics. I want to do the next algorithm:

  1. Create a "yaml" file for ROS, where all my topics are listed.
  2. Extract it´s data as a string.
  3. Parse the data, according to which I will know the number and the names of the topics to which I want to subscribe.
  4. Subscribe in a loop to all of them.

The problem is that I have never written a "yaml" file, and the explanations in the Wikipedia page are not good for ROS. I would also be happy to know what is the best way to "parse" the info in my code (C).

Thank you for your help, Felix.

edit retag flag offensive close merge delete

4 Answers

Sort by » oldest newest most voted

answered 2011-12-13 01:08:26 -0500

DimitriProsser gravatar image

First of all, ROS itself uses yaml-cpp, so that's not a bad way to go about it.

Second, I'd like to propose an alternative way to solve your problem (since I've just done the same thing myself). roslaunch supports all forms of XmlRpc values, including lists. Therefore, you can use the following code:

XmlRpc::XmlRpcValue topicList;
std::vector<std::string> topics;

if (private_nh.getParam("topics", topicList))
  std::map<std::string, XmlRpc::XmlRpcValue>::iterator i;
  for (i = topicList.begin(); i != topicList.end(); i++)
    std::string topic_name;
    std::string topic_type;

    topic_name = i->first;


Essentially, this code will read all parameters that are under the "topics" namespace into a single list. You then iterate through that list to grab all topic names. Each new topic can have as many sub-fields (topic_type) as you desire. Here's an example launchfile statement for the above code.

    <node pkg="my_package" type="my_node" name="my_node" output="screen">
        <param name="topics/topic1/topic_type" type="string" value="sensor_msgs/LaserScan" />
        <param name="topics/topic2/topic_type" type="string" value="sensor_msgs/PointCloud2" />
edit flag offensive delete link more


Hello Dimitri, many thanks for your detailed answer, but since I am not very fluent in C++, I will try first to do it in the second way proposed here (which doesn´t really works well now Anyway, thanks again.
Felix Tr gravatar image Felix Tr  ( 2011-12-13 04:57:19 -0500 )edit

answered 2020-11-17 08:20:46 -0500

updated 2020-11-17 08:49:04 -0500

You can use yaml-cpp along with ROS2. Just add following codes to your package.xml, CMakeLists.txt and source file:




find_package(yaml-cpp REQUIRED)


#include <yaml-cpp/yaml.h>

int main(int argc, char const *argv[])
    YAML::Node config = YAML::LoadFile("[Name Of File].yaml");

    if (config["SOMETHING"]) {
      std::cout << config["SOMETHING"] << "\n";
    return 0;

You can find more information about yaml-cpp here.

edit flag offensive delete link more

answered 2011-12-13 01:03:08 -0500

arebgun gravatar image

Here is an example yaml file you can use to specify topic names to be connected to:

    - topic1
    - topic2
    - topic3
    - topic4

What you see here is a map with one key "topics", its value is a list with 4 string elements. That's pretty much all you need to know about yaml syntax to cover 90% use cases in ROS (both reading and writing config files).

To read those topic names in a ROS node using roscpp, see this page. Scroll all the way down to "Retrieving Lists".

edit flag offensive delete link more


Hi arebgun, I think your suggestion worked well, except the last part...I have some problems there: If you have any idea, please let me know.
Felix Tr gravatar image Felix Tr  ( 2011-12-13 05:00:24 -0500 )edit

answered 2011-12-12 23:47:13 -0500

sebastian gravatar image

There are some libraries out there to parse yaml files in C/C++. Have a look at in the C/C++ Projects section.

For C++ this Page shows how to parse an example yaml file with the yaml-cpp parser.

edit flag offensive delete link more

Question Tools



Asked: 2011-12-12 21:49:25 -0500

Seen: 23,388 times

Last updated: Nov 17 '20