(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Setting Up Dynamic Reconfigure for a Node

Description: Setting Up Dynamic Reconfigure for a Node

Tutorial Level:

A simple example of a dynamically reconfigurable node can be found under dynamic_reconfigure/test

Setting Up Dynamic Reconfigure for a Node

This example shows how to setup a dynamic reconfigure node to reconfigure variables in the ROS message driver_base/SensorLevels.h. Suppose you are adding the dynamic reconfigure server node to your package which we'll refer to as your_package from now on.

  1. Make your package depend on dynamic_reconfigure by adding this line to your manifest.xml,

      <depend package="dynamic_reconfigure" />

The example below will also need

  •   <depend package="driver_base" />
  • Create a configuration file, saved it as your_package/cfg/MyStuff.cfg:

       1 #! /usr/bin/env python
       2 # Forearm camera configuration
       4 PACKAGE='your_package'
       5 import roslib; roslib.load_manifest(PACKAGE)
       7 from math import pi
       9 from driver_base.msg import SensorLevels
      10 from dynamic_reconfigure.parameter_generator import *
      12 gen = ParameterGenerator()
      13 angles = gen.add_group("Angles")
      14 #       Name                    Type      Reconfiguration level             Description                                                                                      Default    Min   Max
      15 angles.add("min_ang",              double_t, SensorLevels.RECONFIGURE_STOP,    "The angle of the first range measurement. The unit depends on ~ang_radians.",                   -pi/2,     -pi, pi)
      16 angles.add("max_ang",              double_t, SensorLevels.RECONFIGURE_STOP,    "The angle of the first range measurement. The unit depends on ~ang_radians.",                   pi/2,      -pi, pi)
      17 gen.add("intensity",            bool_t,   SensorLevels.RECONFIGURE_STOP,    "Whether or not the hokuyo returns intensity values.",                                           False)
      18 gen.add("cluster",              int_t,    SensorLevels.RECONFIGURE_STOP,    "The number of adjacent range measurements to cluster into a single reading",                    1,         0,   99)
      19 gen.add("skip",                 int_t,    SensorLevels.RECONFIGURE_STOP,    "The number of scans to skip between each measured scan",                                        0,         0,    9)
      20 gen.add("port",                 str_t,    SensorLevels.RECONFIGURE_CLOSE,   "The serial port where the hokuyo device can be found",                                          "/dev/ttyACM0")
      21 gen.add("calibrate_time",       bool_t,   SensorLevels.RECONFIGURE_CLOSE,   "Whether the node should calibrate the hokuyo's time offset",                                    Tre)
      22 gen.add("frame_id",             str_t,    SensorLevels.RECONFIGURE_RUNNING, "The frame in which laser scans will be returned",                                               "laser")
      23 gen.add("time_offset",          double_t, SensorLevels.RECONFIGURE_RUNNING, "An offet to add to the timestamp before publication of a scan",                                 0,     -0.25, 0.25)
      24 gen.add("allow_unsafe_settings",bool_t,   SensorLevels.RECONFIGURE_CLOSE,   "Turn this on if you wish to use the UTM-30LX with an unsafe angular range. Turning this option on may cause occasional crashes or bad data. This option is a tempory workaround that will hopefully be removed in an upcoming driver version.", False)
      26 exit(gen.generate(PACKAGE, "dynamic_reconfigure_node", "MyStuff"))


  • Make the .cfg file executable

    chmod a+x your_package/cfg/MyStuff.cfg
  • Create a ROS node that processes callbacks, for example, save the following file as your_package/src/dynamic_reconfigure_node.cpp:

    #include <dynamic_reconfigure/server.h>
    #include <your_package/MyStuffConfig.h>
    void callback(your_package::MyStuffConfig &config, uint32_t level)
      ROS_INFO("Reconfigure request : %f %f %i %i %i %s %i %s %f %i",
               config.groups.angles.min_ang, config.groups.angles.max_ang,
               (int)config.intensity, config.cluster, config.skip, 
               config.frame_id.c_str(), config.time_offset,   
      // do nothing for now
    int main(int argc, char **argv)
      ros::init(argc, argv, "dynamic_reconfigure_node");
      dynamic_reconfigure::Server<your_package::MyStuffConfig> srv;
      dynamic_reconfigure::Server<your_package::MyStuffConfig>::CallbackType f;
      f = boost::bind(&callback, _1, _2);
      ROS_INFO("Starting to spin...");
      return 0;
  • Update your CMakeLists.txt to compile above by adding these lines

    # add dynamic reconfigure api
    rosbuild_add_executable(dynamic_reconfigure_node src/dynamic_reconfigure_node.cpp)
  • rosmake your package and launch the node.
    rosrun your_package dynamic_reconfigure_node

  1. taken directly from forearm camera configuration. (1)

Wiki: Ze'ev Klapow/dynamic/setup (last edited 2011-07-06 00:52:04 by Ze'ev Klapow)