如何使用 ROS 从 yaml 文件中读取带有元组的数组?

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

本文关键字:元组 数组 读取 ROS 何使用 yaml 文件      更新时间:2023-10-16

我有一个这样的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>>>不正确吗?

(抱歉,这是一个庞大的项目,很难做一个小而可编译的例子。如果你坚持,我会做一个小的。

它可以通过使用 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);
}
}

根据文档,没有任何函数getParam来获取对向量或向量向量。因此,您需要更改 YAML 文件并仅使用现有函数getParam