ros入门:参数服务器通信
参数服务器通信类似于两个进程通过共享内存进行通信
前期准备
根工作空间下创建功能包
catkin_create_pkg ros_param_server roscpp rospy std_msgs
在功能包目录下创建代码文件
新增参数
新增参数的方式有两种
- 使用节点句柄
- 使用ros::param::set
#include"ros/ros.h"
int main(int argc,char* argv[]){
ros::init(argc,argv,"param_server_node");
ros::NodeHandle param_node_handle;
/*新增参数*/
//方案一:使用节点handle
param_node_handle.setParam("type","pcl");
param_node_handle.setParam("radius",0.15);
//方案二:使用ros::param
ros::param::set("type_param","niao");
ros::param::set("radius_param",0.2);
return 0;
}
修改CMakeLists.txt文件
根空间下编译代码
catkin_make
测试
首先将参数写入共享内存
rosrun ros_param_server param_server_node
查看我们创建的参数
rosparam list
查看参数值
修改参数
修改参数的方式与设置参数的方式是一样的,因为会覆盖掉原来的参数
#include"ros/ros.h"
int main(int argc,char* argv[]){
ros::init(argc,argv,"param_server_node");
ros::NodeHandle param_node_handle;
/*新增参数*/
//方案一:使用节点handle
param_node_handle.setParam("type","pcl");
param_node_handle.setParam("radius",0.15);
//方案二:使用ros::param
ros::param::set("type_param","niao");
ros::param::set("radius_param",0.2);
//修改参数
param_node_handle.setParam ("radius",0.3);
ros::param::set("radius_param",0.4);
return 0;
}
重新编译设置写入参数
获取参数
#include"ros/ros.h"
int main(int argc,char* argv[]){
setlocale(LC_ALL,"");
ros::init(argc,argv,"param_get_node");
ros::NodeHandle param_get_node_handle;
//第一种获取参数方式,如果获取失败就设置rdius参数为10
double radius=param_get_node_handle.param("radius",10);
ROS_INFO("get radius: %.2f",radius);
//第二种参数获取方式,返回值为bool,表示是否获取成功
double radius2;
bool res=param_get_node_handle.getParam("radis",radius2);
if(res){
ROS_INFO("get param successfully >>> %.2f",radius2);
}else{
ROS_INFO("get param error!");
}
//第三种获取方式,如果之前已经获取过该参数,则直接从cache中获取,而不是重新获取
double radius_cached;
res=param_get_node_handle.getParamCached("radius",radius_cached);
if(res){
ROS_INFO("get cached param >>> %.2f",radius_cached);
}else{
ROS_INFO("get cached param error!");
}
//遍历参数
std::vector<std::string> names;
param_get_node_handle.getParamNames(names);
for(auto& name:names){
ROS_INFO("get param name: %s ",name.c_str());
}
//判断键是否存在
bool flag1=param_get_node_handle.hasParam("radius");
bool flag2=param_get_node_handle.hasParam("radiusxxx");
if(flag1){
ROS_INFO("radius exist");
}else{
ROS_INFO("radius not exist");
}
if(flag2){
ROS_INFO("radiusxxx exist");
}else{
ROS_INFO("radiusxxx not exist");
}
return 0;
}
修改编译配置文件
add_executable(param_set_node src/param_set.cc)
add_executable(param_get_node src/param_get.cc)
## 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(param_set_node
${catkin_LIBRARIES}
)
target_link_libraries(param_get_node
${catkin_LIBRARIES}
)
设置参数
rosrun ros_param_server param_set_node
获取参数
rosrun ros_param_server param_get_node
删除参数
#include "ros/ros.h"
int main(int argc,char* argv[]){
setlocale(LC_ALL,"");
ros::init(argc,argv,"param_del_node");
ros::NodeHandle nh;
//使用节点handler
bool flag=nh.deleteParam("type");
if(flag)
ROS_INFO("delete param type successfully!");
else {
ROS_INFO("delete param type error!");
}
//使用ros::param::del
flag=ros::param::del("radius");
if(flag)
ROS_INFO("delete param radius successfully!");
else {
ROS_INFO("delete param radius error!");
}
return 0;
}
原文地址:https://blog.csdn.net/qq_58158950/article/details/143512825
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!