1、创建一个动态参数配置文件
vim /ros_workspace/src/hollo_ros/cfg/my_cfg.cfg
#!/usr/bin/env python
PACKAGE = "hello_ros"
from dynamic_reconfigure.parameter_generator_catkin import *
#初始化ROS,并导入参数生成器
gen = ParameterGenerator()
#添加参数 (name , typt, level, description, default, min,max)
gen.add("int_param", int_t, 0, "An Integer parameter", 1, 0, 100)
gen.add("double_param", double_t, 0, "A double parameter", 0.1, 0, 1)
gen.add("str_param", str_t, 0, "A string parameter", "Chapter2_dynamic_reconfigure")
gen.add("bool_param", bool_t, 0, "A Boolean parameter", True)
size_enum = gen.enum([ gen.const("Low", int_t, 0, "Low is 0"),
gen.const("Medium", int_t, 1, "Medium is 1"),
gen.const("High", int_t, 2, "Hight is 2")],
"Select from the list")
gen.add("size", int_t, 0, "Select from the list", 1, 0, 3, edit_method=size_enum)
#生成必要文件并退出
exit(gen.generate(PACKAGE, "hello_ros", "my_cfg"))
#注意包名 以及cfg文件名 尤其是最后一个参数,很关键
chomd a+x cfg/my_cfg.cfg 修改权限
修改CMakeList.txt
find_package(catkin REQUIRED COMPONENTS
dynamic_reconfigure)
generate_dynamic_reconfigure_options(
cfg/my_cfg.cfg
)
修改package.xml
<build_depend>dynamic_reconfigurebuild_depend>
编写动态配置支持的新节点
void callback(hello_ros::my_cfgConfig &config, uint32_t level) {
ROS_INFO("Reconfigure Request: %d %f %s %s %d",
config.int_param, config.double_param,
config.str_param.c_str(),
config.bool_param?"True":"False",
config.size);
}
int main(int argc, char **argv) {
ros::init(argc, argv, "dynamic_recfig_node");
dynamic_reconfigure::Server::my_cfgConfig> server;
dynamic_reconfigure::Server::my_cfgConfig>::CallbackType ff;
ff = boost::bind(&callback, _1, _2);
server.setCallback(f);
//当参数服务器得到重新配置参数的请求,就会调用回调函数
ROS_INFO("Spinning node");
ros::spin();
return 0;
}