LoginSignup
6
5

More than 1 year has passed since last update.

YAMLファイルによるROS2のパラメータ設定

Last updated at Posted at 2019-06-13

ROS2関係トップページへ
ROS2レクチャー:初級 -ROS1 style-
ROS2レクチャー:初級 -class style-

【前:ROS2の最小構成parameter:初級 -ROS1 style-
【前:ROS2の最小構成parameter:初級 -class type-

ROS2でのパラメータはYAMLファイルを経由しても設定できる.
YAMLファイルを使い実験パラメータを設定することで「実験のパラメータがYAMLファイルとしてちゃんと残る」ので,利用推奨.

YAMLについて

公式githubのrcl/rcl_yaml_param_parser/に説明あり.

YAMLを使用したパラメータ設定

実行時にYAMLファイルを渡すと,コンストラクタでdeclare_parameterしたものに上書きしてくれる.
公式のロードマップに詳しい.

例えば以下のような感じ.

terminal
$ ros2 run test_package test_target --ros-args --params-file file.yaml

ソースコードとYAML

パラメータの種類については公式のAPIのParameterのメンバ関数を参考に.
基本的なものとしては,int(int64_t), double, string, bool, byte array(vector<uint8_t>), bool array(vector<bool>), integer array(vector<int64_t>), double array(vector<double>), string array(vector<string>)がある.
ソースコードでは,型が分かるようにdeclareして(例えばintなら0など値はどうでもよい)宣言して使用する.YAMLが__params:=で読み込まれると宣言のすぐ後に自動的に読み込まれて設定されている状態になっている.

基本的な使用

test.yaml
test_node:
  ros__parameters:
    param1: 10 # int
    param2: 10.0 # double
    param3: "ok" # string
    param4: true # bool
    param5: [10,20] # byte array,","で要素を区切るのでこの場合2つの要素
    param6: [true, false, false] # bool array, 3つ以上ももちろんok
    param7: [30] # interger array, 要素が1個の場合の書き方
    param8: [20.0, 30.0, 40.0, 50.0] # double array
    param9: ["ok", "ng", "hoge", "really?"] # string array
declare_parameter("param1", 0);
declare_parameter("param2", 0.0);
declare_parameter("param3", "");
declare_parameter("param4", true);
std::vector<uint8_t> param5(2,0); # 適当なものをつくってdeclare(書き方1)
declare_parameter("param5",param5);
declare_parameter("param6",std::vector<bool>(3,true)); # 適当なものをつくってdeclare(書き方2)
declare_parameter("param7",std::vector<int64_t>(1,0));
declare_parameter("param8",std::vector<double>(4,0.0));
declare_parameter("param9",std::vector<string>(4,""));
...
auto a1 = node->get_parameter("param1").as_int();
auto a2 = node->get_parameter("param2").as_double();
auto a3 = node->get_parameter("param3").as_string();
auto a4 = node->get_parameter("param4").as_bool();
auto a5 = node->get_parameter("param5").as_byte_array();
auto a6 = node->get_parameter("param6").as_bool_array();
auto a7 = node->get_parameter("param7").as_integer_array();
auto a8 = node->get_parameter("param8").as_double_array();
auto a9 = node->get_parameter("param9").as_string_array();

パラメータをグループ化

YAMLにて以下のように設定したとする.

test.yaml
test_node:
  ros__parameters:
    group:
     param1: 100
     param2: 5.0

このようにネストされたものをプログラム内で扱うときには,"."(ピリオド)でつなげる.
例えば以下のように.

declare_parameter("group.param1",0);
...
auto a = node->get_parameter("group.param2").as_double();

旧情報:YAMLを使用したパラメータ設定

2020/06/12既に非推奨のやり方になっていました.

やり方

実行時に__params:=で受け渡すと,コンストラクタでdeclare_parameterしたものに上書きしてくれる.
公式のロードマップに詳しい.

例えば以下のような感じ.

terminal
$ ros2 run test_package test_target __params:=file.yaml

【前:ROS2の最小構成parameter:初級 -ROS1 style-
【前:ROS2の最小構成parameter:初級 -class type-

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5