turtlebot之ROS动态参数设置 dynamic reconfigure

动态参数设置(dynamic reconfigure):就是动态地调整、设置正在运行的节点的参数值,通过动态重配置,可以更加高效地开发和测试节点,特别是机器人硬件调试的时候,使用动态重配置参数是一个很不错的选择.
设置动态参数的语句:

rosrun rqt_reconfigure rqt_reconfigure

但是打开之后是空白的,其中里面的点需要自己设计,设计步骤如下:
1、创建工作空间

mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
catkin_init_workspace
cd ..
catkin_make

初始化工作空间其实就是生成一个CMakeLists.txt 文件,会出现
Creating symlink “/home/cez/catkin_ws/src/CMakeLists.txt” pointing to “/opt/ros/kinetic/share/catkin/cmake/toplevel.cmake”
正常现象
2、创建功能包

cd ~/catkin_ws/src
catkin_create_pkg custom_dynamic_reconfigure roscpp rospy std_msgs dynamic_reconfigure
cd ..
catkin_make
echo "source ~/catkin_ws/devel/setup.bash" >> ~/.bashrc
source ~/.bashrc

3、在功能包添加动态重配置文件(.cfg)

roscd custom_dynamic_reconfigure
mkdir cfg && cd cfg
gedit Tutorials.cfg

在文件中添加一下代码

#!/usr/bin/env python
# coding=UTF-8
PACKAGE = "dynamic_reconfigure"

from dynamic_reconfigure.parameter_generator_catkin import *

gen = ParameterGenerator() #创建一个参数生成器对象

#gen.add(参数名称,         参数类型,  位掩码,   参数描述,               默认值,   最小值,   最大值)
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, "custom_dynamic", "Tutorials")) 
#first parameter is namespace's name
#second parameter is node's name
#third parameter is the current file's name

然后对Tutorials.cfg添加权限操作

chmod a+x Tutorials.cfg

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

cmake_minimum_required(VERSION 2.8.3)
project(custom_dynamic_reconfigure)

## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)

## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
  dynamic_reconfigure
  roscpp
  rospy
  std_msgs
)

generate_dynamic_reconfigure_options(
  cfg/Tutorials.cfg
)
catkin_package(
  LIBRARIES custom_dynamic_reconfigure
  CATKIN_DEPENDS roscpp rospy std_msgs dynamic_reconfigure
)
include_directories(${catkin_INCLUDE_DIRS})

5、构建功能包,生成相关头文件

cd ~/catkin_ws
catkin_make

6、在custom_dynamic_reconfigure/src/server.cpp中添加动态重配置节点

#include 
#include 
#include "custom_dynamic_reconfigure/TutorialsConfig.h"

void callback(dynamic_reconfigure::TutorialsConfig &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, "custom_dynamic_server");

  dynamic_reconfigure::Server server;
  dynamic_reconfigure::Server::CallbackType f;

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

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

添加权限

chmod a+x server.cpp

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

cmake_minimum_required(VERSION 2.8.3)
project(custom_dynamic_reconfigure)

## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)

## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
  dynamic_reconfigure
  roscpp
  rospy
  std_msgs
)

generate_dynamic_reconfigure_options(
  cfg/Tutorials.cfg
)
catkin_package(
  LIBRARIES custom_dynamic_reconfigure
  CATKIN_DEPENDS roscpp rospy std_msgs dynamic_reconfigure
)
include_directories(${catkin_INCLUDE_DIRS})
add_executable(custom_dynamic_server src/server.cpp)
add_dependencies(custom_dynamic_server ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(custom_dynamic_server ${catkin_LIBRARIES})

然后catkin_make编译
上述代码亲测是没有问题的,都是改过的,这里记录一下出现的问题
turtlebot之ROS动态参数设置 dynamic reconfigure_第1张图片
这个问题是在CMakeLists.txt中少了rospy

catkin_package(
  LIBRARIES custom_dynamic_reconfigure
  CATKIN_DEPENDS roscpp rospy std_msgs dynamic_reconfigure
)

turtlebot之ROS动态参数设置 dynamic reconfigure_第2张图片
这个错误是没有发现server.cpp文件,但是确实在文件夹下,所以先对这个文件添加权限,然后把所有的包都关了重新catkin_make一下。
turtlebot之ROS动态参数设置 dynamic reconfigure_第3张图片
这个错误是没有给Tutorials.cfg文件加权限
需要注意的是如果代码中出现了中文,一定的加#coding=UTF-8

8、构建功能包,运行动态重配置服务器节点程序(每一次修改程序之后都需要catkin_make一下才会起作用)
一共需要打开三个终端
第一个

roscore

第二个

rosrun custom_dynamic_reconfigure custom_dynamic_server

第三个

rosrun rqt_reconfigure rqt_reconfigure

打开之后的界面如下:
滑动窗口数值会变
turtlebot之ROS动态参数设置 dynamic reconfigure_第4张图片
如果要改变gui界面,先改变Tutorials.cfg文件,例如

gen.add( "speed",    double_t,     0,     "A double parameter", 0.15,      0,       0.4)
gen.add( "a", double_t,  0,     "A double parameter",   1.0,      0,       2)
gen.add( "tolerance", double_t,  0,     "A double parameter",   0.01,      0,       0.1)
gen.add( "b", double_t,  0,  "A double parameter",   1.0,   0,  3)
gen.add( "str_param",    str_t,     0,     "A string parameter",   "success")
gen.add( "start_test",   bool_t,    0,     "A Boolean parameter",  True)

然后在server.cpp中回调它

ROS_INFO("Reconfigure Request: %f %f %f %f %s %s %d",
           config.speed,
           config.a,
           config.tolerance,
           config.b,
           config.str_param.c_str(),
           config.start_test?"True":"False",
           config.size);

不回调会出现这个错误:
turtlebot之ROS动态参数设置 dynamic reconfigure_第5张图片

感谢这位博主:https://blog.csdn.net/l1216766050/article/details/79575423

你可能感兴趣的:(ros)