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

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
12

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;
    topic_type.assign(i->second["topic_type"]);

    topics.push_back(topic_name);
  }
}

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.

<launch>
    <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" />
    </node>
</launch
edit flag offensive delete link more

Comments

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 http://answers.ros.org/question/3297/how-to-convert-xmlrpcxmlrpcvalue-into-string). Anyway, thanks again.
Felix Tr gravatar image Felix Tr  ( 2011-12-13 04:57:19 -0500 )edit
4

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:

Package.xml:

<build_depend>yaml-cpp</build_depend>
<build_export_depend>yaml-cpp</build_export_depend>
<exec_depend>yaml-cpp</exec_depend>

CMakeLists.txt:

find_package(yaml-cpp REQUIRED)
include_directories(HEADERS_EXECUTABLE
    ${YAML_INCLUDE_DIRS}
)
target_link_libraries(${CMAKE_PROJECT_NAME}
    yaml-cpp
)

main.cpp:

#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
1

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:

topics:
    - 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

Comments

Hi arebgun, I think your suggestion worked well, except the last part...I have some problems there: http://answers.ros.org/question/3297/how-to-convert-xmlrpcxmlrpcvalue-into-string. If you have any idea, please let me know.
Felix Tr gravatar image Felix Tr  ( 2011-12-13 05:00:24 -0500 )edit
0

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 yaml.org 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

3 followers

Stats

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

Seen: 23,228 times

Last updated: Nov 17 '20