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
| /* 参数服务器操作之查询_C++实现: 在 roscpp 中提供了两套 API 实现参数操作 ros::NodeHandle
param(键,默认值) 存在,返回对应结果,否则返回默认值
getParam(键,存储结果的变量) 存在,返回 true,且将值赋值给参数2 若果键不存在,那么返回值为 false,且不为参数2赋值
getParamCached键,存储结果的变量)--提高变量获取效率 存在,返回 true,且将值赋值给参数2 若果键不存在,那么返回值为 false,且不为参数2赋值
getParamNames(std::vector<std::string>) 获取所有的键,并存储在参数 vector 中
hasParam(键) 是否包含某个键,存在返回 true,否则返回 false
searchParam(参数1,参数2) 搜索键,参数1是被搜索的键,参数2存储搜索结果的变量
ros::param ----- 与 NodeHandle 类似
*/ #include "ros/ros.h"
int main(int argc, char *argv[]) { /* code */ ros::init(argc,argv,"get_param_c"); ros::NodeHandle nh; double radius=nh.param("radius",0.5); ROS_INFO("radius=%.2f",radius);
double radius2=0.0; bool result=nh.getParam("radius",radius2); if (result) { ROS_INFO("radius is %.2f",radius2); }else{ ROS_INFO("radius not exist"); }
std::vector<std::string> names; nh.getParamNames(names); for (auto &&name : names) { ROS_INFO("paramName is %s",name.c_str()); }
bool flag1=nh.hasParam("radius"); bool flag2=nh.hasParam("radiusxxx"); ROS_INFO("radius is exist? %d",flag1); ROS_INFO("radiusxxx is exist? %d",flag2);
std::string key; nh.searchParam("radius",key); ROS_INFO("result is %s",key.c_str());
double radius_param=ros::param::param("radius",100.5); ROS_INFO("radius_param =%.2f",radius_param);
std::vector<std::string> names_param; ros::param::getParamNames(names_param); for (auto &&name_param : names_param) { ROS_INFO("key:%s",name_param.c_str()); } return 0; }
|