Ask Your Question
8

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

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

Felix Tr gravatar image

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

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

3 Answers

Sort by » oldest newest most voted
11

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

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 imageFelix Tr ( 2011-12-13 04:57:19 -0600 )edit
1

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

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 imageFelix Tr ( 2011-12-13 05:00:24 -0600 )edit
0

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

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

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

3 followers

Stats

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

Seen: 17,035 times

Last updated: Dec 13 '11