ROS2如何注册复杂数组参数
需求:
由于ros2参数服务器注册/获取的参数类型也就如下
- Bool
- int64_t
- std::vector
- double
- std::vector
- string
- …
如果是复杂的数组:
nets_params:
- npu_id: 1
num_threads: 1
...
- npu_id: 2
num_thread: 1
...
应该怎么写呢?
this->declare_parameter<int64_t>("nets_params[0].npu_id");
...
this->declare_parameter<int64_t>("nets_params[1].npu_id")
这样写没有验证过是否支持,而且也很不优雅,重复代码过多。
解决方案1
首先要改造yaml文件的内容:
nets_params:
model_ids: ["model_1", "model_2"]
model_1:
npu_id: 1
num_threads: 1
...
model_2:
npu_id: 2
num_thread: 1
...
注册/获取参数代码:
this->declare_parameter<std::vector<std::string>>("nets_params.model_ids");
auto model_ids = this->get_parameter("nets_params.model_ids").as_string_array();
for (auto i = 0ul; i < model_ids.size(); ++i)
{
this->declare_parameter<int64_t>("nets_params." + model_ids[i] + ".npu_id");
...
int npu_id = this->get_parameter("nets_params." + model_ids[i] + ".npu_id").as_int();
}