ROS 接口调参 加载参数 动态参数 从NodeHandle中加载参数
参数类型
参数接口
获取的两种方式:
通过NodeHandle操作参数
查看参数
从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文件中加载参数
<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
从配置文件中加载参数
<launch> <node name="yaml" pkg="example_2" type="yaml" clear_params="true" output="screen"> <!--相对参数--> <rosparam file="$(find example_2)/config/test.yaml"/></node>
</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中加载参数相关推荐
- 从前台页面取参数到sql的like中比较时参数的预处理
//从前台页面取参数到sql的like中比较时参数的预处理 this.keytxt.Text.Trim().Replace("'", "''").Replace ...
- 当一个对象实例作为一个参数被传递到方法中时,参数的值就是对该对象的引用。对象的内容可以在被调用的方法中改变,但对象的引用是永远不会改变的.
当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性,并可返回变化后的结果,那么这里到底是值传递还是引用传递 答:是值传递.Java 编程语言只有值传递参数.当一个对象实例作为一个参数被传 ...
- 如何动态在maven插件中加载项目及第三方类
在maven插件中是加载不到目标项目的类及目标项目引用的第三方所提供的类的,需要通过动态读取目标项目所依赖的classpath并根据这些classpath生成相应的url数组,以这个url数组作为参数 ...
- python调参工作都是干啥的_Python中Gradient Boosting Machine(GBM)调参方法详解
1.前言 如果一直以来你只把GBM当作黑匣子,只知调用却不明就里,是时候来打开这个黑匣子一探究竟了! 这篇文章是受Owen Zhang (DataRobot的首席产品官,在Kaggle比赛中位列第三) ...
- python中函数的参数_Python小知识-Python中的函数参数(基础篇)
0 总述 这个题目分为2篇,第一篇基本上是概念以及实例,有个相对系统的认识,第二篇想写的深入一些,不同参数类型的区别以及和c/c++的函数参数对比下异同-下面开始是第一篇的内容啦- Python中的函 ...
- java怎么传入参数到方法当中_java中方法传入参数时:值传递还是址传递?
JAVA中的数据类型有两大类型: ① 基本数据类型:逻辑型(boolean).文本型(char).整数型(byte.short.int.long).浮点型(float.double) ② 引用数据类型 ...
- c语言全局变量作为参数_在C / C ++中使用变量参数列表
c语言全局变量作为参数 C/C++ provides a means to pass a variable number of arguments to a function. This artic ...
- 红帽linux7如何加载驱动,在RHEL7安装过程中加载驱动程序
在系统在安装过程中,有时候会出现系统不支持或当硬件比系统版本更新时就需要在系统安装之前就需要先加载驱动(通常是RAID卡驱动,因为不加载RAID驱动无法识别硬盘,从而导致无法安装系统,其它大多驱动可在 ...
- java 命名参数动态替换_使用Kettle的命名参数动态执行作业
关于如何根据传入的不同参数,达到动态运行作业的目的,这里不介绍.只提供一个思路,就是不同的调度进程调度同一个Kettle文件时,传入了不同的参数,从而得到不同的数据. 如下图所示: 1. 先设置参数名 ...
- 一种保护应用程序的方法 模拟Windows PE加载器,从内存资源中加载DLL
暂未整理,成文见: http://hi.baidu.com/kisbuddy/blog/item/f5da581035197e4bf2de32a2.html
最新文章
- ESI最新计算机学科统计:中国78所跻身高被引100强,中南大学夺冠
- JS创建对象学习笔记
- java变量命名规则_浅谈JAVA开发规范与开发细节(上)
- 杭电1108java_按照这个步骤来刷题,迷茫的你两个月亦能成为王者
- html 按键hover,按钮hover效果
- c语言退格键ascii码,【回车键的ASCII码是多少】
- matlab倒立摆pid仿真,一级倒立摆课程设计--倒立摆PID控制及其Matlab仿真
- 电机电缆M12航空插头
- 103000大写加零吗_关于支票金额大写规范写零的问题,比如1008712元,100万后要不要加零...
- cad角度命令怎么输入_CAD中用好这个命令,任意等分角度不再是难事
- 有关密钥的最全总结都在这了
- java秒杀源码_Java秒杀系统实战系列~商品秒杀代码实战
- 协议将电子邮件交付服务器,什么协议用于将电子邮件交付给internet
- PS——图像处理知识点
- 2022年施工升降机司机(建筑特殊工种)考题及答案
- 2018年,给自己加油!
- 适合学生党无线蓝牙耳机,好用又实惠蓝牙耳机推荐
- c语言设计一个程序:计算圆周率,精确到小数点后8位数
- 小红的签到题 (shui
- 斯坦福神经调控疗法(Stanford Neuromodulation Therapy, SNT)