ROS(七)----动态参数

一、工程结构

 

 二、创建并编译 cfg

#!/usr/bin/env python
PACKAGE = "book_dynamic_param"

from dynamic_reconfigure.parameter_generator_catkin import *

gen = ParameterGenerator()

gen.add("IntDyParam",int_t,0,"An Int Parameter",0,0,9)
gen.add("DouDyParam",double_t,0,"A Double Parameter",1.5,0,9)
gen.add("StrDyParam",str_t,0,"A String Parameter","Hello,I'm Robot!")
gen.add("BoolDyParam",bool_t,0,"A Bool Parameter",True)

student_info = gen.enum([gen.const("Name",str_t,"LiLei","Name Information"),
                         gen.const("Sex",str_t,"Man","Sex Information"),
                         gen.const("Age",str_t,"18","Age Information")],
                        "A set contain a student information")


gen.add("StudentInfo",str_t,0,"A studenet information set","LiLei",edit_method=student_info)
exit(gen.generate(PACKAGE,"node_DynamicParam","DynamicParam"))

CMakeLists.txt

cmake_minimum_required(VERSION 2.8.3)
project(book_dynamic_param)

find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs dynamic_reconfigure)


generate_dynamic_reconfigure_options(
   cfg/DynamicParam.cfg
   #cfg/DynReconf2.cfg
)


catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES book_dynamic_param
#  CATKIN_DEPENDS other_catkin_pkg
#  DEPENDS system_lib
)
include_directories(
    include ${catkin_INCLUDE_DIRS}
# include
# ${catkin_INCLUDE_DIRS}
)

编译后,在include中有DynamicParamConfig.h

 

 

三、动态参数设置可执行文件

#include"ros/ros.h"
#include"dynamic_reconfigure/server.h"
#include"book_dynamic_param/DynamicParamConfig.h"

void paramCallback(book_dynamic_param::DynamicParamConfig &config,uint32_t level)
{
    ROS_INFO("Request :%d %f %s %s %s",config.IntDyParam,
                                    config.DouDyParam,
                                    config.StrDyParam.c_str(),
                                    config.BoolDyParam? "true":"false",
                                    config.StudentInfo.c_str());
    
}

int main(int argc,char** argv)
{
    //initial node and name
    ros::init(argc,argv,"node_DynamicParam");
   
    // create node handle
    dynamic_reconfigure::Server<book_dynamic_param::DynamicParamConfig> server;
    dynamic_reconfigure::Server<book_dynamic_param::DynamicParamConfig>::CallbackType f;

    f= boost::bind(&paramCallback,_1,_2);
    server.setCallback(f);
    ros::spin();
    return 0;
}

 

posted on 2020-03-05 18:14  feihu_h  阅读(423)  评论(0编辑  收藏  举报

导航