2、创建功能包
cd ~/catkin_ws/src
catkin_create_pkg learning_parameter roscpp rospy std_srvs
3、参数命令行使用
■
\blacksquare
■ rosparam
∙
\bullet
∙ 列出当前所有的参数:rosparam list
∙
\bullet
∙显示某个参数值:rosparam get param_key
∙
\bullet
∙设置某个参数值:rosparam set param_key param_value
∙
\bullet
∙ 保存参数到文件:rosparam dump file_name
∙
\bullet
∙ 从文件读取参数:rosparam load file_name
∙
\bullet
∙删除参数:rosparam delete param_key
4、编程方法
■
\blacksquare
■ 如何获取/设置参数
∙
\bullet
∙ 初始化 ROS 节点;
∙
\bullet
∙ get函数获取参数;
∙
\bullet
∙ set函数设置参数;
/***********************************************************************
Copyright 2020 GuYueHome (www.guyuehome.com).
***********************************************************************/
/**
* 该例程设置/读取海龟例程中的参数
*/
#include <string>
#include <ros/ros.h>
#include <std_srvs/Empty.h>
int main(int argc, char **argv)
{
int red, green, blue;
// ROS节点初始化
ros::init(argc, argv, "parameter_config");
// 创建节点句柄
ros::NodeHandle node;
// 读取背景颜色参数
ros::param::get("/turtlesim/background_r", red);
ros::param::get("/turtlesim/background_g", green);
ros::param::get("/turtlesim/background_b", blue);
ROS_INFO("Get Backgroud Color[%d, %d, %d]", red, green, blue);
// 设置背景颜色参数
ros::param::set("/turtlesim/background_r", 255);
ros::param::set("/turtlesim/background_g", 255);
ros::param::set("/turtlesim/background_b", 255);
ROS_INFO("Set Backgroud Color[255, 255, 255]");
// 读取背景颜色参数
ros::param::get("/background_r", red);
ros::param::get("/background_g", green);
ros::param::get("/background_b", blue);
ROS_INFO("Re-get Backgroud Color[%d, %d, %d]", red, green, blue);
// 调用服务,刷新背景颜色
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;
}
6、编译并运行发布者
cd ~/catkin_ws
catkin_make
source devel/setup.bash
roscore
rosrun turtlesim turtlesim_node
rosrun learning_parameter parameter_config
小海龟的背景从蓝色编程了白色。