0
点赞
收藏
分享

微信扫一扫

ROS学习笔记9_参数的使用和编程方法

该Parameter Server的作用相当于全局变量之于程序,各个Node可能不一定位于同一个PC中,但是只要在同一个ros环境里,都可以通过Parameter Server来访问全局字典的参数。

rosparam相关指令

列出当前所有参数

rosparam list

 

显示某个参数值

rosparam get param_key

设置某个参数值 

rosparam set param_key param_value

 

尽管目前重新设置了这个值,但是小海龟的背景并没有被立即刷新,需要发送一个request,这样系统就会检查参数,如果参数发生改变,就刷新当前参数。

rosservice call /clear "{}"

 

 

保存参数到文件

rosparam dump file_name %一般是.yaml格式

此时在主文件夹中会生成一个.yaml文件

 

 其中包含着当前状态所有参数信息。

 

从文件中读取参数

rosparam load file_name

将刚刚文件中的参数做修改后读入。

 

 

 删除参数

rosparam delete param_key

通过程序实现参数的配置和使用

在/src目录下创建功能包。

catkin_create_pkg learning_parameter roscpp rospy std_srvs

之后在建立的功能包下/src目录下建立cpp文件

/*
	This program will set/read the parameters in turtlesim
*/

#include<ros/ros.h>
#include<string>
#include<std_srvs/Empty.h>


int main(int argc, char **argv)
{
	int red, green, blue;
	
	//initiate ros node
	ros::init(argc, argv, "parameter_config");
	
	//create node handle
	ros::NodeHandle node;
	
	//read the color parameters of background 
	ros::param::get("/turtlesim/background_r", red);
	ros::param::get("/turtlesim/background_g", green);
	ros::param::get("/turtlesim/background_b", blue);
	
	ROS_INFO("Get Background Color[%d, %d, %d]", red, green, blue);
	
	//set the color parameters of background
	ros::param::set("/turtlesim/background_r", 255);
	ros::param::set("/turtlesim/background_g", 255);
	ros::param::set("/turtlesim/background_b", 255);
	
	ROS_INFO("Set Background Color[255, 255, 255]");
	
	//read the color parameters of background
	ros::param::get("/turtlesim/background_r", red);
	ros::param::get("/turtlesim/background_g", green);
	ros::param::get("/turtlesim/background_b", blue);
	
	ROS_INFO("Re-get the Background Color[%d, %d, %d]", red, green, blue);
	
	//call for the service to refresh the background color
	ros::service::waitForService("/clear");
	ros::ServiceClient clear_background = node.serviceClient<std_srvs::Empty>("/clear");
	std_srvs::Empty srv;
	clear_background.call(srv);
	
	sleep(1);
	
	return 0;
		
}

老样子加编译链接说明

add_executable(parameter_config src/parameter_config.cpp)
target_link_libraries(parameter_config ${catkin_LIBRARIES})

运行龟龟模拟器。(愿称这只为最可爱!)

执行前: 

rosrun learning_parameter parameter_config后

 

 参考链接: http://wiki.ros.org/Parameter%20Server

举报

相关推荐

0 条评论