ROS环境读写yaml文件以及数组操作
利用yaml-cpp读写yaml配置文件
1. cmake文件 target_link_libraries
加入yaml-cpp库
2. 在对应头文件中添加
#include <yaml-cpp/yaml.h>
#include <fstream>
参考连接 https://blog.csdn.net/briblue/article/details/89515470/
特殊Demo 读取yaml文件中数组
利用ROS自带的XmlRpc::XmlRpcValue实现一维数组或者二位数组等类似json数据的读取
C++代码实现如下
XmlRpc::XmlRpcValue scanner_params; nh.getParam("scanners", scanner_params); for(size_t i = 0; i<scanner_params.size(); ++i) { const String& server_ip = scanner_params[i]["server_ip"]; const int& server_port = scanner_params[i]["server_port"]; const String& frame_id = scanner_params[i]["frame_id"]; const String& pub_topic = scanner_params[i]["pub_topic"]; }
Yaml文件中数据如下
scanners:
- { pub_topic: "scan_head", frame_id: "laser_scanner_link_head",server_ip: "192.168.167.100",server_port: 2111}
- { pub_topic: "scan_middle",frame_id: "laser_scanner_link_middle",server_ip: "192.168.167.101",server_port: 2111}
- {pub_topic: "scan_tail",frame_id: "laser_scanner_link_tail",server_ip: "192.168.167.102",server_port: 2111}
在launch文件中加载yaml参数文件,即可在程序中读取