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

get YAML list of dictionary items with ros::param in C++

asked 2018-05-18 09:55:10 -0600

ysl208 gravatar image

I would like to define a list of items in a yaml file which I read in with ros::param::param<t>()

It works fine if I read in a normal double or string:

 ros::param::param<double>("list/pos_x",pos_x,0.0);

if my yaml file is defined as:

list:
 - {'id': 0, 'name': 'john', 'pos':[0,0,0]}
 - {'id': 1, 'name': 'anna', 'pos':[0,0,0]}
 - {'id': 2, 'name': 'bill', 'pos':[0,0,0]}

How can i access these with ros::param?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2018-05-18 10:35:26 -0600

aarushg gravatar image

You can see the Retrieving lists section in the end of this page http://wiki.ros.org/roscpp/Overview/Parameter%20Server.

For your specific case, I think it's better if you can format your yaml file like :

list:
 - id: 0 
   name: john 
   pos: [0,0,0]

 - id: 1 
   name: anna 
   pos: [0,0,0]

This way you can easily access the values as a dictionary after you fetch each item in the list as a XmlRpc::XmlRpcValue data type.

for (int i = 0; i < list.size(); i++) {
      XmlRpc::XmlRpcValue sublist = list[i];
      id=sublist["id"];
}

This is just a raw example, you should always check for format errors before fetching data to avoid crashing your program. This is the best way that I know of, if anyone else can offer a better alternative, I'll be happy to learn.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2018-05-18 09:55:10 -0600

Seen: 3,580 times

Last updated: May 18 '18