参数类型

 ROS参数服务器本质上是可以网络访问的参数词典,ROS参数服务器能保存的数据类型包括字符串 、整型、浮点型、布尔型、列表、词典等;
 参数根据类型不同可以分为全局参数、相对参数、私有参数等;
 其中以“/”开头的参数为全局参数,以“~”开头的参数为私有参数,其他参数为相对参数;

参数接口

操作参数的四种方法:
 ROS提供了两种管理参数的API实时的存储、获取参数:
一个是通过ros::param命令空间                      一个是通过ros::NodeHandle接口
 从Launch文件中加载参数
 从参数文件中加载参数
 通过动态参数dynamic_reconfigure接口调整参数

获取的两种方式:

通过NodeHandle操作参数

参数的设置、删除、获取、查询等操作方式。
 void NodeHandle::setParam(const std::string& key, double d)                             设置
 bool NodeHandle::deleteParam(const std::string& key)                                        删除
 bool NodeHandle::getParam(const std::string& key, double& d)                           获取
 bool NodeHandle::searchParam(const std::string& key, std::string& result_out)   查询

查看参数

在ROS中,
rosparam命令可以用来查看参数。需注意,利用rosparam操作参数必须在roscore启动后才能
进行,主要方法有:
 #设置参数            $rosparam set [参数名]
 #删除参数            $rosparam delete
 #获取参数            $rosparam get [参数名]
 #查看参数列表     $rosparam list

从NodeHandle中加载参数---实例查看参数:

#include <ros/ros.h>int main(int argc, char **argv){ros::init(argc,argv,"hello");        // 初始化节点ros::NodeHandle nh("~");             // 初始化句柄bool isRun = false;nh.setParam("is_run",true);          // 注入全局参数nh.setParam("/is_test",true);      if(nh.hasParam("is_run") == true){   // 判断是否有该节点nh.getParam("is_run",isRun);ROS_INFO("the status is %d",isRun);}else{ROS_INFO("the is_run status is not defined");}return 0;
}

从launch文件中加载参数

参数通过param标签实现。
 参数为全局参数,param标签作为launch标签的子标签
 参数为相对参数,param标签作为node标签的子标签
<launch><param name="serial" value="5"/><node name="client" pkg="example_3" type="client" output="screen"><param name="client_x" type="int" value="1" /></node>
</launch

从配置文件中加载参数

         YAML是一种可读性高、轻量级的标记语言,
         广泛用于ROS、Spring等框架的配置,它对大小写敏感,
         使用缩进表示层级关系,但缩进使用空格(2个或者4个)而非Tab键表示。
         YAML支持对象、数组、常量三种 数据类型。
 参数配置在YAML文件中,注意冒号后面有空格
 YAML文件通常放置于程序包的config目录下
 通过launch文件的rosparam标签加载配置文件来完成参数配置,添加路径
<launch> <node name="yaml" pkg="example_2" type="yaml" clear_params="true" output="screen"> <!--相对参数--> <rosparam file="$(find example_2)/config/test.yaml"/></node>
</launch>

加载参数示例

 步骤1:创建load.launch(从Launch中加载参数)
 步骤2:创建配置文件config/test.yaml
 步骤3:创建yaml.launch(从配置文件中加载参数)
 步骤4:读取参数,创建yaml.cpp
 步骤5:修改CMakeLists.txt
 步骤1:创建load.launch
<launch><param name="isLoad" type="bool" value="true"/>       #定义全局标签 bool <node name="yaml" pkg="example_2" type="yaml" clear_params="true" output="screen">#四个相对应的参数<param name="first_name" type="string" value="fabio" /><param name="last_name" type="string" value="miao" /><param name="age" type="int" value="39" /><param name="score" type="int" value="150" /></node>
</launch>

 步骤2:创建配置文件config/test.yaml

first_name: fabio
last_name: miaozl
age: 37
score: 100#注意中间一定需要空格

 步骤4:读取参数,创建yaml.cpp

#include <ros/ros.h>int main(int argc, char **argv)
{ros::init(argc, argv, "yaml");           // 生成yaml的节点ros::NodeHandle nh;                      // std::string first,last;int age = 0 , score = 100;bool isLoad = false;nh.getParam("/yaml/first_name",first);  // 读出节点中的值加入读出nh.getParam("/yaml/last_name",last);ROS_INFO("his first name is %s,his last name is %s",first.c_str(),last.c_str());  nh.getParam("/yaml/age",age);            // 读年龄 读分数ROS_INFO("first name is %d",age);nh.getParam("/yaml/score",score);ROS_INFO("first name is %d",score);nh.getParam("/isLoad",isLoad);if(isLoad){ROS_INFO("the param is load");}else{ROS_INFO("the param isn't load");}
}

 步骤5:修改CMakeLists.txt

主要添加修改文件 ---  编译link Libraries 库文件

cmake_minimum_required(VERSION 3.0.2)
project(example_2)## 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 COMPONENTSroscpprospystd_msgsmessage_generationdynamic_reconfigure
)## System dependencies are found with CMake's conventions
# find_package(Boost REQUIRED COMPONENTS system)## Uncomment this if the package has a setup.py. This macro ensures
## modules and global scripts declared therein get installed
## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html
# catkin_python_setup()################################################
## Declare ROS messages, services and actions ##
################################################## To declare and build messages, services or actions from within this
## package, follow these steps:
## * Let MSG_DEP_SET be the set of packages whose message types you use in
##   your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...).
## * In the file package.xml:
##   * add a build_depend tag for "message_generation"
##   * add a build_depend and a exec_depend tag for each package in MSG_DEP_SET
##   * If MSG_DEP_SET isn't empty the following dependency has been pulled in
##     but can be declared for certainty nonetheless:
##     * add a exec_depend tag for "message_runtime"
## * In this file (CMakeLists.txt):
##   * add "message_generation" and every package in MSG_DEP_SET to
##     find_package(catkin REQUIRED COMPONENTS ...)
##   * add "message_runtime" and every package in MSG_DEP_SET to
##     catkin_package(CATKIN_DEPENDS ...)
##   * uncomment the add_*_files sections below as needed
##     and list every .msg/.srv/.action file to be processed
##   * uncomment the generate_messages entry below
##   * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...)## Generate messages in the 'msg' folder
# add_message_files(
#   FILES
#   Message1.msg
#   Message2.msg
# )## Generate services in the 'srv' folder
# add_service_files(
#   FILES
#   Service1.srv
#   Service2.srv
# )## Generate actions in the 'action' folder
# add_action_files(
#   FILES
#   Action1.action
#   Action2.action
# )## Generate added messages and services with any dependencies listed here
# generate_messages(
#   DEPENDENCIES
#   std_msgs
# )################################################
## Declare ROS dynamic reconfigure parameters ##
################################################## To declare and build dynamic reconfigure parameters within this
## package, follow these steps:
## * In the file package.xml:
##   * add a build_depend and a exec_depend tag for "dynamic_reconfigure"
## * In this file (CMakeLists.txt):
##   * add "dynamic_reconfigure" to
##     find_package(catkin REQUIRED COMPONENTS ...)
##   * uncomment the "generate_dynamic_reconfigure_options" section below
##     and list every .cfg file to be processed## Generate dynamic reconfigure parameters in the 'cfg' foldergenerate_dynamic_reconfigure_options(cfg/Test.cfg)###################################
## catkin specific configuration ##
###################################
## The catkin_package macro generates cmake config files for your package
## Declare things to be passed to dependent projects
## INCLUDE_DIRS: uncomment this if your package contains header files
## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES example_2
#  CATKIN_DEPENDS roscpp rospy std_msgs
#  DEPENDS system_lib
)###########
## Build ##
############# Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
# include${catkin_INCLUDE_DIRS}
)## Declare a C++ library
# add_library(${PROJECT_NAME}
#   src/${PROJECT_NAME}/example_2.cpp
# )## Add cmake target dependencies of the library
## as an example, code may need to be generated before libraries
## either from message generation or dynamic reconfigure
# add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})## Declare a C++ executable
## With catkin_make all packages are built within a single CMake context
## The recommended prefix ensures that target names across packages don't collide
# add_executable(${PROJECT_NAME}_node src/example_2_node.cpp)
add_executable(yaml src/yaml.cpp)
add_executable(param src/param.cpp)
## Rename C++ executable without prefix
## The above recommended prefix causes long target names, the following renames the
## target back to the shorter version for ease of user use
## e.g. "rosrun someones_pkg node" instead of "rosrun someones_pkg someones_pkg_node"
# set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX "")## Add cmake target dependencies of the executable
## same as for the library above
# add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})## Specify libraries to link a library or executable target against
# target_link_libraries(${PROJECT_NAME}_node
#   ${catkin_LIBRARIES}
# )
target_link_libraries(yaml${catkin_LIBRARIES}
)
target_link_libraries(param${catkin_LIBRARIES}
)
#############
## Install ##
############## all install targets should use catkin DESTINATION variables
# See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html## Mark executable scripts (Python etc.) for installation
## in contrast to setup.py, you can choose the destination
# catkin_install_python(PROGRAMS
#   scripts/my_python_script
#   DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )## Mark executables for installation
## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_executables.html
# install(TARGETS ${PROJECT_NAME}_node
#   RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )## Mark libraries for installation
## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_libraries.html
# install(TARGETS ${PROJECT_NAME}
#   ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
#   LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
#   RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
# )## Mark cpp header files for installation
# install(DIRECTORY include/${PROJECT_NAME}/
#   DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
#   FILES_MATCHING PATTERN "*.h"
#   PATTERN ".svn" EXCLUDE
# )## Mark other files for installation (e.g. launch and bag files, etc.)
# install(FILES
#   # myfile1
#   # myfile2
#   DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
# )#############
## Testing ##
############### Add gtest based cpp test target and link libraries
# catkin_add_gtest(${PROJECT_NAME}-test test/test_example_2.cpp)
# if(TARGET ${PROJECT_NAME}-test)
#   target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})
# endif()## Add folders to be run by python nosetests
# catkin_add_nosetests(test)

执行launch后得到

... logging to /home/xtark/.ros/log/deb4219e-2151-11ed-a25f-874955d4690a/roslaunch-xtark-vmpc-6737.log
Checking log directory for disk usage. This may take a while.
Press Ctrl-C to interrupt
Done checking log file disk usage. Usage is <1GB.started roslaunch server http://xtark-vmpc:33649/SUMMARY
========CLEAR PARAMETERS* /yaml/PARAMETERS* /isLoad: True* /rosdistro: noetic* /rosversion: 1.15.13* /yaml/age: 39* /yaml/first_name: fabio* /yaml/last_name: miao* /yaml/score: 150NODES/yaml (example_2/yaml)ROS_MASTER_URI=http://localhost:11311process[yaml-1]: started with pid [6755]
[ INFO] [1661087150.225233609]: his first name is fabio,his last name is miao
[ INFO] [1661087150.226000645]: first name is 39
[ INFO] [1661087150.226231003]: first name is 150
[ INFO] [1661087150.226447102]: the param is load
[yaml-1] process has finished cleanly
log file: /home/xtark/.ros/log/deb4219e-2151-11ed-a25f-874955d4690a/yaml-1*.log
all processes on machine have died, roslaunch will exit
shutting down processing monitor...
... shutting down processing monitor complete
done

ROS 接口调参 加载参数 动态参数 从NodeHandle中加载参数相关推荐

  1. 从前台页面取参数到sql的like中比较时参数的预处理

    //从前台页面取参数到sql的like中比较时参数的预处理 this.keytxt.Text.Trim().Replace("'", "''").Replace ...

  2. 当一个对象实例作为一个参数被传递到方法中时,参数的值就是对该对象的引用。对象的内容可以在被调用的方法中改变,但对象的引用是永远不会改变的.

    当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性,并可返回变化后的结果,那么这里到底是值传递还是引用传递 答:是值传递.Java 编程语言只有值传递参数.当一个对象实例作为一个参数被传 ...

  3. 如何动态在maven插件中加载项目及第三方类

    在maven插件中是加载不到目标项目的类及目标项目引用的第三方所提供的类的,需要通过动态读取目标项目所依赖的classpath并根据这些classpath生成相应的url数组,以这个url数组作为参数 ...

  4. python调参工作都是干啥的_Python中Gradient Boosting Machine(GBM)调参方法详解

    1.前言 如果一直以来你只把GBM当作黑匣子,只知调用却不明就里,是时候来打开这个黑匣子一探究竟了! 这篇文章是受Owen Zhang (DataRobot的首席产品官,在Kaggle比赛中位列第三) ...

  5. python中函数的参数_Python小知识-Python中的函数参数(基础篇)

    0 总述 这个题目分为2篇,第一篇基本上是概念以及实例,有个相对系统的认识,第二篇想写的深入一些,不同参数类型的区别以及和c/c++的函数参数对比下异同-下面开始是第一篇的内容啦- Python中的函 ...

  6. java怎么传入参数到方法当中_java中方法传入参数时:值传递还是址传递?

    JAVA中的数据类型有两大类型: ① 基本数据类型:逻辑型(boolean).文本型(char).整数型(byte.short.int.long).浮点型(float.double) ② 引用数据类型 ...

  7. c语言全局变量作为参数_在C / C ++中使用变量参数列表

    c语言全局变量作为参数 C/C++ provides a means to pass a variable number of arguments to a function.  This artic ...

  8. 红帽linux7如何加载驱动,在RHEL7安装过程中加载驱动程序

    在系统在安装过程中,有时候会出现系统不支持或当硬件比系统版本更新时就需要在系统安装之前就需要先加载驱动(通常是RAID卡驱动,因为不加载RAID驱动无法识别硬盘,从而导致无法安装系统,其它大多驱动可在 ...

  9. java 命名参数动态替换_使用Kettle的命名参数动态执行作业

    关于如何根据传入的不同参数,达到动态运行作业的目的,这里不介绍.只提供一个思路,就是不同的调度进程调度同一个Kettle文件时,传入了不同的参数,从而得到不同的数据. 如下图所示: 1. 先设置参数名 ...

  10. 一种保护应用程序的方法 模拟Windows PE加载器,从内存资源中加载DLL

    暂未整理,成文见: http://hi.baidu.com/kisbuddy/blog/item/f5da581035197e4bf2de32a2.html

最新文章

  1. ESI最新计算机学科统计:中国78所跻身高被引100强,中南大学夺冠
  2. JS创建对象学习笔记
  3. java变量命名规则_浅谈JAVA开发规范与开发细节(上)
  4. 杭电1108java_按照这个步骤来刷题,迷茫的你两个月亦能成为王者
  5. html 按键hover,按钮hover效果
  6. c语言退格键ascii码,【回车键的ASCII码是多少】
  7. matlab倒立摆pid仿真,一级倒立摆课程设计--倒立摆PID控制及其Matlab仿真
  8. 电机电缆M12航空插头
  9. 103000大写加零吗_关于支票金额大写规范写零的问题,比如1008712元,100万后要不要加零...
  10. cad角度命令怎么输入_CAD中用好这个命令,任意等分角度不再是难事
  11. 有关密钥的最全总结都在这了
  12. java秒杀源码_Java秒杀系统实战系列~商品秒杀代码实战
  13. 协议将电子邮件交付服务器,什么协议用于将电子邮件交付给internet
  14. PS——图像处理知识点
  15. 2022年施工升降机司机(建筑特殊工种)考题及答案
  16. 2018年,给自己加油!
  17. 适合学生党无线蓝牙耳机,好用又实惠蓝牙耳机推荐
  18. c语言设计一个程序:计算圆周率,精确到小数点后8位数
  19. 小红的签到题 (shui
  20. 斯坦福神经调控疗法(Stanford Neuromodulation Therapy, SNT)

热门文章

  1. 北京单身狗都去哪儿了
  2. 尚学堂Java第七章编码题
  3. IT部门绩效考核管理思考
  4. mysql自增列防止恢复初始_重置mysql的自增列 AUTO_INCREMENT初时值 | 瑕疵学院 – 学习无瑕疵,成就有保障...
  5. mongo数据库的使用
  6. SpringBoot图片上传报404无法回显,重启服务后又正常
  7. 应用开发不可不知的五大BaaS后端云服务
  8. 作业中关于H5中动画的实现——animation
  9. pyqt5,波形进度条
  10. ----实现查看历史记录及清除功能的具体过程----