日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

yaml文件的加載使用

作者:不盈新成 更新時(shí)間: 2022-07-22 編程語(yǔ)言

yaml文件的加載

    • 1. yaml-cpp加載
      • (1)安裝編譯
      • (2)使用
      • (3)加載參數(shù)
    • 2. 使用rosparam
      • 補(bǔ):添加參數(shù)的另一種方式

yaml文件為常用的參數(shù)文件,方便調(diào)參避免不必要的編譯。最近發(fā)現(xiàn)了幾種加載方法,逐個(gè)搜索學(xué)習(xí)了一下,并進(jìn)行了實(shí)際操作。在此記錄一下。

  1. yaml-cpp包加載
  2. 使用rosparam加載
    補(bǔ): 在launch文件中直接設(shè)置參數(shù)

1. yaml-cpp加載

(1)安裝編譯

git clone https://github.com/jbeder/yaml-cpp.git
cd yaml-cpp
mkdir build 
cd build
cmake ..  -D BUILD_SHARED_LIBS=ON
make -j
sudo make install

(2)使用

如果不想每次都到 copy 頭文件到不同的工程中,那么你可以將整個(gè)文件夾 copy 到系統(tǒng)默認(rèn)的頭文件目錄, ubuntu 的地址是 /usr/local/include/;將庫(kù)文件libyaml-cpp.so拷貝到系統(tǒng)默認(rèn)的 lib 文件夾,ubuntu 是 /usr/local/lib/。

# 復(fù)制文件夾
sudo cp -r yaml-cpp  /usr/local/include/
# 復(fù)制庫(kù)文件
sudo cp libyaml-cpp.so  /usr/local/lib/

頭文件添加

#include "yaml-cpp/yaml.h"

CMakeLists.txt中添加

target_link_libraries(${PROJECT_NAME} ${INTERFACE}libyaml-cpp.so)

(3)加載參數(shù)

舉個(gè)梨子,配置一個(gè)這樣的config.yaml文件

name: frank
sex: male
age: 18
 
skills: 
  c++: 1
  java: 1
  android: 1
  python: 1

yaml文件的編寫要注意格式 參數(shù)名: 值,冒號(hào)后面一定要加一個(gè)空格。

加載,若相對(duì)路徑不行,則需要使用絕對(duì)路徑

YAML::Node config = YAML::LoadFile("path");
cout << "name:" << config["name"].as<string>() << endl;
cout << "age:" << config["age"].as<int>() << endl;
cout << "skills c++:" << config["skills"]["c++"].as<int>() << endl;

2. 使用rosparam

在launch文件中添加下面這句加載yaml文件,修改路徑(功能包名、進(jìn)入功能包后的文件的路徑)

<rosparam command="load" file="$(find pkgname) /config/config.yaml" />

加載參數(shù)

ros::NodeHandle nh;
string name1;
int age1;
nh.setParam("name", tom);  
nh.getParam("/name",name1);
nh.setParam("age", 20);  
nh.getParam("/age",age1);
cout << "name1:" <<name1<< endl;
cout << "age1:" <<age1<< endl;

set為從服務(wù)器加載,若加載不到則自動(dòng)生成一個(gè)并取默認(rèn)值。然后再用get獲取參數(shù)。也可以不用set直接用get去獲取。

補(bǔ):添加參數(shù)的另一種方式

在launch文件中用此句話添加參數(shù)

<param name="name" type="string" value="frank"/>

啟動(dòng)launch文件時(shí)就會(huì)自動(dòng)加載參數(shù)到參數(shù)服務(wù)器中,此方法適用于少量參數(shù)的場(chǎng)景。
加載參數(shù)的方法與2相同使用get進(jìn)行獲取加載。

原文鏈接:https://blog.csdn.net/weixin_51498169/article/details/125914795

欄目分類
最近更新