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

Get the name of a sub-parameter, not the whole structure

asked 2015-06-28 12:39:17 -0500

alextoind gravatar image

I have a YAML file with a structure like the following:

field_a:
   field_a1: {field_b1, field_b2, ..., field_bM}
   field_a2: {field_b1, field_b2, ..., field_bM}
   ...
   field_aN: {field_b1, field_b2, ..., field_bM}

where field_a and field_bj are known a priori for all j, but field_ai are frequently changed by the user (i.e. can't be hard coded).

If I try to retieve the parameters field_ai using the C++ method getParam(...) or the command line rosparam get /field_a, what I get is everything in the tree hierarchy where /field_a is the root.

Is there a way to retrieve only the names of these field_ai and not their values? I could create another list with those names, but it not scale very well...

Thank you for your help!

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
0

answered 2015-07-02 02:26:03 -0500

alextoind gravatar image

updated 2015-07-02 02:29:17 -0500

Thanks to both! I did not think about XmlRpc and the hint of using the iterator works like a charm!

This is the code for the example above:

std::vector<std::string> field_names;
XmlRpc::XmlRpcValue list;

if (!node_handle_.getParam("/field_a", list)) {
  ROS_ERROR("Can't find '/field_a' in the YAML configuration file.");
  return;
}

for (auto it = list.begin(); it != list.end(); it++) {
  field_names.push_back(it->first);
}

For the sake of completeness it->second is the the nested list {field_b1, field_b2, ..., field_bM}.

edit flag offensive delete link more
2

answered 2015-06-30 02:41:20 -0500

dornhege gravatar image

You can parse the XmlRpcValue after doing getParam on field_a. ROS' version of XmlRpcValue adds an iterator type and begin()/end() calls, so you can iterate over a struct.

If in doubt, check the header in /opt/ros/XXX/include/XmlRpcValue.h for the API, not the official (non-ROS) docs.

edit flag offensive delete link more

Comments

You're obviously right with checking the header in /opt/ros. I took the link which is linked from the ROS wiki Parameter Server page. Maybe we should put this note in there as well...

mgruhler gravatar image mgruhler  ( 2015-07-03 01:24:31 -0500 )edit

Updated. .

dornhege gravatar image dornhege  ( 2015-07-03 03:31:23 -0500 )edit
1

answered 2015-06-30 02:30:00 -0500

mgruhler gravatar image

You can definitely try to parse this manually with XMLRPC (or probably with yaml-cpp).

What you have here should probably be a std::map of std::pair<std::string, std::vector<T>>, but this is not supported, as far as I know. Simpler types work.

If you would choose python, you could use the rospy.get_param_names() function.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2015-06-28 12:39:17 -0500

Seen: 1,281 times

Last updated: Jul 02 '15