0.参数管理机制
1.Talker利用RPC通信向ROS MASTER发送参数及其数据,ROS MASTER将其保存在参数列表
2.Listener发送请求给ROS MASTER,查找已知参数名对应的参数值
3.ROS MASTER将参数值发送给Listener
1.建立功能包
catkin_create_pkg learning_parameter roscpp rospy std_srvs
2.编写源码
/*
Node Function:
Get Parameter from Parameter Server in ROSMASTER
*/
#include<string>
#include<ros/ros.h>
#include<std_srvs/Empty.h>
int main(int argc,char **argv)
{
int red,green,blue;
//ROS_INIT
ros::init(argc,argv,"parameter_config");
//NodeHandle_INIT
ros::NodeHandle n;
//Read background color Parameter and set variables
ros::param::get("/turtlesim/background_r",red);
ros::param::get("/turtlesim/background_b",blue);
ros::param::get("/turtlesim/background_g",green);
//Print INFO of current color
ROS_INFO("Get backround Color[%d,%d,%d]",red,green,blue);
//Set background color Parameter
ros::param::set("/turtlesim/background_r",255);
ros::param::set("/turtlesim/background_g",255);
ros::param::set("/turtlesim/background_b",255);
//Print INFO
ROS_INFO("Set Background Color[255,255,255]");
//Read background color Parameter again
ros::param::get("/turtlesim/background_r",red);
ros::param::get("/turtlesim/background_b",blue);
ros::param::get("/turtlesim/background_g",green);
//Print INFO of current color
ROS_INFO("Get backround Color[%d,%d,%d]",red,green,blue);
//Call Service /clear to refresh background
ros::service::waitForService("/clear");
ros::ServiceClient clear_background = n.serviceClient<std_srvs::Empty>("/clear");
std_srvs::Empty srv;
clear_background.call(srv);
sleep(1);
return 0;
}
clear_background.call(srv);
值得注意的是,在源码中,此处若无.call则报错如下
error: no match for call to ‘(ros::ServiceClient) (std_srvs::Empty&)’
47 | clear_background(srv);
| ^ make[2]: *** [learning_parameter/CMakeFiles/parameter_config.dir/build.make:63:
learning_parameter/CMakeFiles/parameter_config.dir/src/parameter_config.cpp.o] Error 1 make[1]: *** [CMakeFiles/Makefile2:1020:
learning_parameter/CMakeFiles/parameter_config.dir/all] Error 2 make:
*** [Makefile:141: all] Error 2
3.修改CMakeLists.txt
在build下添加
add_executable(parameter_config src/parameter_config.cpp)
target_link_libraries(parameter_config ${catkin_LIBRARIES})