参考:http://wiki.ros.org/dynamic_reconfigure/Tutorials
dynamic_reconfigure是用来在线设置节点参数用的,不必为了设置和修改参数而重启节点。
一、写自己的.cfg文件
1.首先建立一个包dynamic_tutorials
roscreate-pkg dynamic_tutorials rospy roscpp dynamic_reconfigure2.写自己的.cfg文件
1 #!/usr/bin/env python
2 PACKAGE = "dynamic_tutorials"
3
4 import roslib;roslib.load_manifest(PACKAGE)
5
6 from dynamic_reconfigure.parameter_generator import *
7
8 gen = ParameterGenerator()
9
10 gen.add("int_param", int_t, 0, "An Integer parameter", 50, 0, 100)
11 gen.add("double_param", double_t, 0, "A double parameter", .5, 0, 1)
12 gen.add("str_param", str_t, 0, "A string parameter", "Hello World")
13 gen.add("bool_param", bool_t, 0, "A Boolean parameter", True)
14
15 size_enum = gen.enum([ gen.const("Small", int_t, 0, "A small constant"),
16 gen.const("Medium", int_t, 1, "A medium constant"),
17 gen.const("Large", int_t, 2, "A large constant"),
18 gen.const("ExtraLarge", int_t, 3, "An extra large constant") ],
19 "An enum to set size")
20
21 gen.add("size", int_t, 0, "A size parameter which is edited via an enum", 1, 0, 3, edit_method=size_enum)
22
23 exit(gen.generate(PACKAGE, "dynamic_tutorials", "Tutorials")
-
name - a string which specifies the name under which this parameter should be stored
-
type - defines the type of value stored, and can be any of int_t, double_t, str_t, or bool_t
-
level - A bitmask which will later be passed to the dynamic reconfigure callback. When the callback is called all of the level values for parameters that have been changed are ORed together and the resulting value is passed to the callback.
-
description - string which describes the parameter
-
default - specifies the default value
-
min - specifies the min value (optional and does not apply to strings and bools)
-
max - specifies the max value (optional and does not apply to strings and bools)
exit(gen.generate(PACKAGE, "dynamic_tutorials", "Tutorials")
这句话告诉生成器生成必须的文件,并且退出程序。第二个参数是运行节点的名字;第三个参数该文件的名字。
3.使用cfg文件
为了使文件可用,必须执行下面命令:
chmod a+x cfg/Tutorials.cfg同时将下列信息,加入到CMakeLists.txt
#add dynamic reconfigure api rosbuild_find_ros_package(dynamic_reconfigure) include(${dynamic_reconfigure_PACKAGE_PATH}/cmake/cfgbuild.cmake) gencfg()
二、为一个节点设置Dynamic Reconfigure
建立cfg文件后,在dynamic_tutorials包中,创建一个新的文件夹nodes,创建一个新的文件称为:server.cpp
1 #include <ros/ros.h>
2
3 #include <dynamic_reconfigure/server.h>
4 #include <dynamic_tutorials/TutorialsConfig.h>
5
6 void callback(dynamic_tutorials::TutorialsConfig &config, uint32_t level) {
7 ROS_INFO("Reconfigure Request: %d %f %s %s %d",
8 config.int_param, config.double_param,
9 config.str_param.c_str(),
10 config.bool_param?"True":"False",
11 config.size);
12 }
13
14 int main(int argc, char **argv) {
15 ros::init(argc, argv, "dynamic_tutorials");
16
17 dynamic_reconfigure::Server<dynamic_tutorials::TutorialsConfig> server;
18 dynamic_reconfigure::Server<dynamic_tutorials::TutorialsConfig>::CallbackType f;
19
20 f = boost::bind(&callback, _1, _2);
21 server.setCallback(f);
22
23 ROS_INFO("Spinning node");
24 ros::spin();
25 return 0;
26 }