Ask Your Question
1

programmatically load yaml parameters in C

asked 2020-08-10 15:18:56 -0500

mcamurri gravatar image

Hi all, I'm trying to load a YAML file into the parameter server programmatically in C++.

I know it is possible with Python, from this question here, but I couldn't find a way to do it in C++.

I'm using Melodic on Ubuntu 18.04.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2020-08-10 16:06:15 -0500

praskot gravatar image

updated 2020-08-10 16:23:06 -0500

https://github.com/jbeder/yaml-cpp

Checkout the https://github.com/jbeder/yaml-cpp/wi...

ros::NodeHandle nh;
yaml_params_ = YAML::LoadFile(yaml_filename);

for (YAML::const_iterator it = yaml_params_.begin(); it != yaml_params_.end(); ++it) {
    nh.setParam(it->first.as<std::string>(), it->second.as<double>());
}

PS: Not sure if there is a better way, but this is how I do it. Obviously, there is ddynamic_reconfigure & dynamic_reconfigure, where you can store the params to *.yaml and save/load them as needed.

edit flag offensive delete link more

Comments

2

For future readers: I believe it's important to point out that in case a node does this, and the node is started via a .launch file, parameters set by this node will not be visible to other nodes, unless they are started after the parameter-setting node (and there is no way to enforce that, at least not in ROS 1).

Seeing as the order of starting nodes with roslaunch is not guaranteed, there is no way to guarantee these parameters will show up on the parameter server before they are accessed by other nodes.

This is one of the reasons why parameters are set first by roslaunch, and only then are nodes started.

gvdhoorn gravatar image gvdhoorn  ( 2020-08-11 00:12:40 -0500 )edit

But how about cached parameters? Here it says that the nodes are notified when such parameters are changed, but it is not clear how...

mcamurri gravatar image mcamurri  ( 2020-08-11 08:53:14 -0500 )edit
2

answered 2020-08-11 08:57:52 -0500

mcamurri gravatar image

Thanks @praskot, but I did not specify that I don't want to depend explicitly on yaml-cpp. I've resolved by invoking the rosparam command from the code. Assuming that cfg_file_path is a valid boost::filesystem::path object:

  namespace fsys = boost::filesystem;
  if(!fsys::exists(cfg_file_path) ||
     !(fsys::is_regular_file(cfg_file_path) || fsys::is_symlink(cfg_file_path)))
  {
    ROS_WARN_STREAM("Could not open camera param file " << cfg_file_path.string());
  } else {
    // load the YAML file using the external rosparam command
    std::string command = "rosparam load " + cfg_file_path.string();
    int result = std::system(command.c_str());
    if(result != 0){
      ROS_WARN_STREAM("Could not set config file " << cfg_file_path.string()
                      << " to the parameter server.");
    }
  }
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

Stats

Asked: 2020-08-10 15:18:56 -0500

Seen: 887 times

Last updated: Aug 11 '20