Search code examples
c++yamlros

How to read in an array with tuples from a yaml file with ROS?


I have a yaml-file like this:

object_0:
  - v: 1.55
  - t_x: 110.281
  - t_y: 367.959
  - traj_const_dist: 1.0
  - trajectory: [[117, 356], [116, 356], [115, 356], [114, 356], [113, 356], [113, 357], [113, 358], [113, 359], [113, 360]]

The parameter trajectory is defined like this: std::vector<std::pair<double,double>> trajectory_;

When I read in the parameters:

ros::NodeHandle nh_;
nh_.param<std::vector<std::pair<double, double>>>(obj_topic, trajectory_, std::vector<std::pair<double, double>>());

... I get this error:

error: no matching function for call to ‘ros::NodeHandle::getParam(const string&, std::vector<std::pair<double, double> >&) const’
       if (getParam(param_name, param_val))

It would help if you give me suggestions. Is the data type std::vector<std::pair<double, double>>> not correct?

(Sorry, it is a huge project and difficult to make a small and compilable example. I will do a small one if you insist on it.)


Solution

  • It can be done by making use of XmlRpc::XmlRpcValue

    Assuming the parameters in yaml file already loaded to parameter server,the following code snippet can be used:

    XmlRpc::XmlRpcValue trajectory;
    nh_.getParam("/param_name", trajectory);
       
    /*To ensure the reading will happen if the data is provided in right format*/
    if (trajectory.getType() == XmlRpc::XmlRpcValue::TypeArray)
    {
        for (int i = 0; i < trajectory.size(); i++)
        {
            XmlRpc::XmlRpcValue trajectoryObject = trajectory[i];
    
            /*Individual coordinate points in trajectory*/
    
            int xCoordinate = trajectoryObject[0];
            int yCoordinate = trajectoryObject[1];
            ROS_INFO("The %d  th coordinate values are : %d and %d", ixCoordinate, yCoordinate);
        }
    }