11 ROS 动态参数调节

博客转载:https://blog.csdn.net/weixin_42005898/article/details/100114870

1. 创建功能包(package)

cd ~/catkin_ws/src
catkin_create_pkg dynamic_refg roscpp dynamic_reconfigure
cd ..
catkin_make

2. 向功能包添加动态重配置文件(.cfg)

roscd dynamic_refg
mkdir cfg && cd cfg
gedit My_cfg.cfg
  • 2.1向My_cfg.cfg文件输入如下的内容
#!/usr/bin/env python
PACKAGE = "dynamic_refg"
 
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_refg", "My_cfg")) 

代码详解

其中  gen = ParameterGenerator()  创建一个参数生成器对象
     gen.add 加入不同的参数。其中gen.add(...)格式如下:
 
 gen.add(name, type, level, description, default, min, max)
     name: 参数的名称
     type: 参数类型
     level:一个传递给回调的位掩码
     description: 一个描述参数
     default: 节点启动的初始值
     min: 参数最小值
     max: 参数最大值
 
 exit  生成必要的文件并退出
       first parameter is namespace's name
       second parameter is node's name
       third parameter is the current file's name
在编译之前,必须运行
      chmod a+x src/dynamic_refg/cfg/My_cfg.cfg
    添加权限。

3. 修改CMakeLists.txt文件

将CMakeLists.txt文件的内容修改为如下所示:

cmake_minimum_required(VERSION 2.8.3)
project(dynamic_refg)
 
find_package(catkin REQUIRED COMPONENTS
  dynamic_reconfigure
  roscpp
  rospy
  std_msgs
)
 
 
## Generate dynamic reconfigure parameters in the 'cfg' folder
 generate_dynamic_reconfigure_options(
   cfg/My_cfg.cfg
 )
 
 
catkin_package(
#  INCLUDE_DIRS include
   LIBRARIES dynamic_refg
   CATKIN_DEPENDS dynamic_reconfigure roscpp rospy std_msgs
#  DEPENDS system_lib
)
 
 
include_directories(
# include
  ${catkin_INCLUDE_DIRS}
)

4.构建功能包,生成相关头文件

cd ~/catkin_ws
catkin_make

5.向功能包添加动态重配置节点

node.cpp

#include <ros/ros.h>
 
#include <dynamic_reconfigure/server.h>
#include "dynamic_refg/My_cfgConfig.h"
 
void callback(dynamic_refg::My_cfgConfig &config)
{
  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, "node_dynamic_reconfigure");
 
  dynamic_reconfigure::Server<dynamic_refg::My_cfgConfig> server;
  dynamic_reconfigure::Server<dynamic_refg::My_cfgConfig>::CallbackType f;
 
  f = boost::bind(&callback, _1); //绑定回调函数
  server.setCallback(f); //为服务器设置回调函数, 节点程序运行时会调用一次回调函数来输出当前的参数配置情况
 
  ROS_INFO("Spinning node");
  ros::spin(); //服务器循环监听重配置请求,当服务器收到重配置请求的时候,就会自动调用回调函数
  return 0;
}

6.修改CMakeLists.txt文件

将CMakeLists.txt文件的内容修改为如下:

cmake_minimum_required(VERSION 2.8.3)
project(dynamic_refg)
 
find_package(catkin REQUIRED COMPONENTS
  dynamic_reconfigure
  roscpp
  rospy
  std_msgs
)
 
 generate_dynamic_reconfigure_options(
   cfg/My_cfg.cfg
 )
 
catkin_package(
#  INCLUDE_DIRS include
   LIBRARIES dynamic_refg
   CATKIN_DEPENDS dynamic_reconfigure roscpp rospy std_msgs
#  DEPENDS system_lib
)
 
 
include_directories(
# include
  ${catkin_INCLUDE_DIRS}
)
 
 
add_executable(node src/node.cpp)
add_dependencies(node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(node
   ${catkin_LIBRARIES}
 )
 

7.构建功能包,生成相关头文件

cd ~/catkin_ws
catkin_make

8.构建功能包,运行动态重配置服务器节点程序

打开终端,输入:

rosrun dynamic_refg node 

打开另一个终端,输入:

rosrun rqt_reconfigure rqt_reconfigure

 

posted @ 2020-04-21 10:52  采男孩的小蘑菇  阅读(2064)  评论(0编辑  收藏  举报