参考网址:
(128条消息) ROS-节点参数param_TYINY的博客-CSDN博客_node.param
(128条消息) 【ROS学习】ROS param 的使用_wongHome的博客-CSDN博客_ros::param::get
例程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| include "ros/ros.h" #include <cstdlib> using namespace std; int main(int argc,char** argv) { ros::init(argc,argv,"node_param"); if(argc!=2) { cout<<"Error command paramter! Please run command eg:\n"\ <<"rosrun book_param book_param 1\n"\ <<"help information:\n"\ <<" 1 ------ set param mode(ros::param::set())\n"\ <<" 2 ------ set param mode(ros::NodeHandle::setParam())\n"\ <<" 3 ------ get param mode(ros::param::get())\n"\ <<" 4 ------ get param mode(ros::NodeHandle::getParam())\n"\ <<" 5 ------ get param mode(ros::NodeHandle::param())\n"\ <<endl; return 1; } ros::NodeHandle nh; int IntParam; string StrParam; bool isIntParam, isStrParam; int flag = atoi(argv[1]); switch(flag) { case 1: ROS_INFO("set param mode(ros::param::set()):"); ros::param::set("IntParam",1); ros::param::set("StrParam","stringdemo"); break; case 2: ROS_INFO("set param mode(ros::NodeHandle::setParam()):"); nh.setParam("IntParam",1); nh.setParam("StrParam","stringdemo"); break; case 3: ROS_INFO("get param mode(ros::param::get()):"); isIntParam = ros::param::get("IntParam",IntParam); isStrParam = ros::param::get("StrParam",StrParam); if(isIntParam){ ROS_INFO("The IntParam is:%d",IntParam); }else{ ROS_INFO("Get IntParam fail!"); } if(isIntParam){ ROS_INFO("The StrParam is:%s",StrParam.c_str()); }else{ ROS_INFO("Get StrParam fail!"); } break; case 4: ROS_INFO("get param mode(ros::NodeHandle::getParam()):"); isIntParam = nh.getParam("IntParam",IntParam); isStrParam = nh.getParam("StrParam",StrParam); if(isIntParam){ ROS_INFO("The IntParam is:%d",IntParam); }else{ ROS_INFO("Get IntParam fail!"); } if(isIntParam){ ROS_INFO("The StrParam is:%s",StrParam.c_str()); }else{ ROS_INFO("Get StrParam fail!"); } break; case 5: ROS_INFO("get param mode(ros::NodeHandle::param()):"); nh.param("IntParam",IntParam,11); nh.param<std::string>("StrParam",StrParam,"stringdemo_default"); ROS_INFO("\nThe IntParam is:%d\nThe StrParam is:%s",IntParam,StrParam.c_str()); break; default: ROS_INFO("flag value is not in range: [1,5]"); } return 0; } }
|
launch文件配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <launch> <param name="param1" value="1" /> <param name="param2" value="2" /> <rosparam> param3: 3 param4: 4 param5: 5 </rosparam> <node pkg="param_demo" type="param_demo" name="param_demo" output="screen" > </node> </launch>
|
yaml文件