Table of Contents

Dynamic Reconfigure

The dynamic_reconfigure package provides a means to change node parameters at any time without having to restart the node.

To create parameters that can be dynamically reconfigured do the following:

Setup the package dependencies

If starting a new package do:

catkin_create_pkg package_name ropsy roscpp dynamic_reconfigure

If modifying existing package:

<build_depend>dynamic_reconfigure</build_depend>
<build_depend>roscpp</build_depend>
<build_depend>rospy</build_depend>
 
<build_export_depend>dynamic_reconfigure</build_export_depend>
<build_export_depend>roscpp</build_export_depend>
<build_export_depend>rospy</build_export_depend>
 
<exec_depend>dynamic_reconfigure</exec_depend>
<exec_depend>roscpp</exec_depend>
<exec_depend>rospy</exec_depend>

Make a .cfg file.

Add a config file and call it Something.cfg. Place it in package/cfg directory.

#!/usr/bin/env python
PACKAGE = "dynamic_tutorials"
 
from dynamic_reconfigure.parameter_generator_catkin import *
 
gen = ParameterGenerator()
 
gen.add("int_param",    int_t,    0, "An Integer parameter", 50,  0, 100)
gen.add("double_param", double_t, 0, "A double parameter",    .5, 0,   1)
gen.add("str_param",    str_t,    0, "A string parameter",  "Hello World")
gen.add("bool_param",   bool_t,   0, "A Boolean parameter",  True)
 
size_enum = gen.enum([ gen.const("Small",      int_t, 0, "A small constant"),
                       gen.const("Medium",     int_t, 1, "A medium constant"),
                       gen.const("Large",      int_t, 2, "A large constant"),
                       gen.const("ExtraLarge", int_t, 3, "An extra large constant")],
                     "An enum to set size")
 
gen.add("size", int_t, 0, "A size parameter which is edited via an enum", 1, 0, 3, edit_method=size_enum)
 
# This is important. The second parameter should be the name of the pacakge
# and the third parameter is the prefix of the generated libaries
exit(gen.generate(PACKAGE, "dynamic_tutorials", "Tutorials"))

Set the config file to be exectutable:

chmod a+x cfg/Tutorials.cfg

Add the following to the CmakeLists.txt:

# add dynamic reconfigure api
#find_package(catkin REQUIRED dynamic_reconfigure)
generate_dynamic_reconfigure_options(
    cfg/Tutorials.cfg
)
 
# If using a c++ node, make sure that you add this dependency so that 
# the library gets generated before building the node
# add_dependencies(example_node ${PROJECT_NAME}_gencfg)

Using the .cfg library in your node

You can do this in python or c++. Import the generated Config library and attach a callback function to get the parameter updates. Then do whatever you want with the parameters! Yay! Follow these tutorials:

Python C++