首页 > 解决方案 > 如何从带有 ROS 的 yaml 文件中读取包含元组的数组?

问题描述

我有一个这样的 yaml 文件:

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]]

参数trajectory定义如下:std::vector<std::pair<double,double>> trajectory_;

当我读入参数时:

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

...我收到此错误:

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))

如果你给我建议会有所帮助。数据类型std::vector<std::pair<double, double>>>不正确?

(不好意思,项目很大,很难做一个小的可编译的例子,如果你坚持的话,我会做一个小的。)

标签: c++yamlros

解决方案


可以通过使用 XmlRpc::XmlRpcValue 来完成

假设 yaml 文件中的参数已经加载到参数服务器,可以使用以下代码片段:

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);
    }
}

推荐阅读