1. 程式人生 > >ros之dynamic_reconfigure

ros之dynamic_reconfigure

ros之dynamic_reconfigure

1. 編寫.cfg檔案

在ros功能包資料夾下,新建cfg資料夾, 在cfg資料夾下新建Tutorials.cfg檔案

cd rosbag 
mkdir cfg
touch cfg/Tutorials.cfg

Tutorials.cfg檔案示例:

#!/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)

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)
    注: gen.enum用於生成列舉引數

2. 使用.cfg檔案

chmod a+x cfg/Tutorials.cfg

在CMakeLists.txt下新增以下命令

#add dynamic reconfigure api
find_package(catkin REQUIRED dynamic_reconfigure)
generate_dynamic_reconfigure_options(
  cfg/Tutorials.cfg
  #...
)

# make sure configure headers are built before any node using them
add_dependencies(example_node ${PROJECT_NAME}_gencfg)

3. 在.cpp檔案中加入

#include <ros/ros.h>

#include <dynamic_reconfigure/server.h>
#include <dynamic_tutorials/TutorialsConfig.h>

void callback(dynamic_tutorials::TutorialsConfig &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_tutorials");

  dynamic_reconfigure::Server<dynamic_tutorials::TutorialsConfig> server;
  dynamic_reconfigure::Server<dynamic_tutorials::TutorialsConfig>::CallbackType f;

  f = boost::bind(&callback, _1, _2);
  server.setCallback(f);

  ROS_INFO("Spinning node");
  ros::spin();
  return 0;
}

注: 引數來配置檔案的標頭檔案路徑為:package_name/file_name+"Config.h"
注: 在callback函式中進行引數賦值操作。

4. 執行

rosrun rqt_reconfigure rqt_reconfigure

參考

dynamic_reconfigure - ROS Wiki