ROS进阶(二): dynamic_reconfigure功能包详解

之前在ros基础系列中的第三章,我们详细讲解了ROS系统中的参数配置。

ROS的参数服务器有缺陷:talker向master更改了参数数值,如果listener不去重新查询的话,仍然保持原来数值。

本节我们来详细探究下,在ros中如何实现动态配置参数。

一、创建配置文件

global_planner中使用的.cfg文件为分析对象:

首先需要在对应package下建立cfg文件夹,然后在文件夹中生成对应的cfg文件:GlobalPlanner.cfg

配置文件使用python语言编写,具体如下:

#!/usr/bin/env python
PACKAGE = "global_planner"

from dynamic_reconfigure.parameter_generator_catkin import ParameterGenerator, int_t, double_t, bool_t

gen = ParameterGenerator()

gen.add("lethal_cost", int_t, 0, "Lethal Cost", 253, 1, 255)
gen.add("neutral_cost", int_t, 0, "Neutral Cost", 50, 1, 255)
gen.add("cost_factor", double_t, 0, "Factor to multiply each cost from costmap by", 3.0, 0.01, 5.0)
gen.add("publish_potential", bool_t, 0, "Publish Potential Costmap", True)

orientation_enum = gen.enum([
    gen.const("None", int_t, 0, "No orientations added except goal orientation"),
    gen.const("Forward", int_t, 1,
              "Positive x axis points along path, except for the goal orientation"),
    gen.const("Interpolate", int_t, 2, "Orientations are a linear blend of start and goal pose"),
    gen.const("ForwardThenInterpolate",
              int_t, 3, "Forward orientation until last straightaway, then a linear blend until the goal pose"),
    gen.const("Backward", int_t, 4,
              "Negative x axis points along the path, except for the goal orientation"),
    gen.const("Leftward", int_t, 5,
              "Positive y axis points along the path, except for the goal orientation"),
    gen.const("Rightward", int_t, 6,
              "Negative y axis points along the path, except for the goal orientation"),
], "How to set the orientation of each point")

gen.add("orientation_mode", int_t, 0, "How to set the orientation of each point", 1, 0, 6,
        edit_method=orientation_enum)
gen.add("orientation_window_size", int_t, 0, "What window to use to determine the orientation based on the "
        "position derivative specified by the orientation mode", 1, 1, 255)

exit(gen.generate(PACKAGE, "global_planner", "GlobalPlanner"))

首先,导入功能包提供的参数生成器(parameter generator)

然后创建实例后就可以开始添加动态参数了。

上述例子中展示了常用数据结构的参数添加,以及枚举类数据的参数添加。

  1. gen.add("lethal_cost", int_t, 0, "Lethal Cost", 253, 1, 255)

add()方法中参数的具体含义:

  • name
  • type
  • level: 参数动态配置回调函数掩码
  • description
  • default
  • min
  • max
  1. gen.add("orientation_mode", int_t, 0, "How to set the orientation of each point", 1, 0, 6, edit_method=orientation_enum)

数据类型需要指定为int_t
枚举类型数据需要在上述输入参数意外,再单独指定edit_method.

最后生成所有C++、python相关文件,然后退出程序。

exit(gen.generate(PACKAGE, "global_planner", "GlobalPlanner"))

其中,第二个参数为运行时的节点名字,第三个参数为生成文件使用的前缀,与文件名保持一致。

CMakeLists的修改

与ROS基础系列中生成msg类似link,我们同样需要修改Cmakelist文件:


generate_dynamic_reconfigure_options(
  cfg/GlobalPlanner.cfg
)

add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

关于add_dependencies的作用,详见link

二、创建服务器节点

同样对应上文中的cfg文件,我们在global_planner规划器的初始化函数中找到了如何使用这些可以动态配置的参数。

// 需要的头文件
#include <dynamic_reconfigure/server.h>

============================================

// 创建dynamic_reconfigure服务器实例
dynamic_reconfigure::Server<global_planner::GlobalPlannerConfig> *dsrv_;

=============================================
// 在初始化函数中截取出来相关的部分:初始化参数服务器
dsrv_ = new dynamic_reconfigure::Server<global_planner::GlobalPlannerConfig>(ros::NodeHandle("~/" + name));


dynamic_reconfigure::Server<global_planner::GlobalPlannerConfig>::CallbackType cb = boost::bind(
                &GlobalPlanner::reconfigureCB, this, _1, _2);
dsrv_->setCallback(cb);

=============================================

// 对应初始化中的回调函数

void GlobalPlanner::reconfigureCB(global_planner::GlobalPlannerConfig& config, uint32_t level) {
    planner_->setLethalCost(config.lethal_cost);
    path_maker_->setLethalCost(config.lethal_cost);
    planner_->setNeutralCost(config.neutral_cost);
    planner_->setFactor(config.cost_factor);
    publish_potential_ = config.publish_potential;
    orientation_filter_->setMode(config.orientation_mode);
    orientation_filter_->setWindowSize(config.orientation_window_size);
}

其中bind()函数将回调函数和服务器绑定,客户端请求修改参数的时候,服务器条转到回调函数进行处理。
回调函数传入两个参数,一个是参数更新的配置值,一个是表示参数修改的掩码。

三、相关工具

1. reconfigure_gui

可视化的动态配置工具

上述功能包编译运行,可以通过可视化工具直接调节动态参数。
在这里插入图片描述

2. 命令行工具

rosrun dynamic_reconfigure dynparam COMMAND
在这里插入图片描述

  • 2
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值