目录
- 创建功能包
- 参数命令行使用
- YAML参数文件
- rosparam
- C++
- 编写程序
- 配置CMakeLists.txt
- 编译并运行
- python
- 编写程序
- 运行
平台:华硕 Thinker Edge R 瑞芯微 RK3399Pro
固件版本:Tinker_Edge_R-Debian-Stretch-V1.0.4-20200615
记录自【古月居】古月·ROS入门21讲 | 一学就会的ROS机器人入门教程 —— 古月居GYH P16 16.参数的使用与编程方法
创建功能包
cd ~/catkin_workspace/src
catkin_create_pkg learning_parameter roscpp rospy std_srvs
参数命令行使用
YAML参数文件
background_b: 255
background_g: 86
background_r: 69
rosdistro: 'lunar'
roslaunch:
uris: {host_hcx_vpc__43763: 'http://hcx-vpc:43763/'}
rosversion: '1.14.31'
run_id: 077058de-a38b-11e9-818b-000c29d22e4d
rosparam
# 新建一个终端
roscore# 新建一个终端
rosrun turtlesim turtlesim_node
# 新建一个终端
rosparam
列出当前参数
rosparam list
显示某个参数值
rosparam get param_key
设置某个参数值
rosparam set param_key param_value
保存参数到文件
rosparam dump file_name
从文件读取参数
rosparam load file_name
删除参数
rosparam delete param_key
C++
编写程序
nano ~/catkin_workspace/src/learning_parameter/src/parameter_config.cpp
/***********************************************************************
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("/background_r", red);ros::param::get("/background_g", green);ros::param::get("/background_b", blue);ROS_INFO("Get Backgroud Color[%d, %d, %d]", red, green, blue);// 设置背景颜色参数ros::param::set("/background_r", 255);ros::param::set("/background_g", 255);ros::param::set("/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;
}
配置CMakeLists.txt
nano ~/catkin_workspace/src/learning_parameter/CMakeLists.txt
添加
add_executable(parameter_config src/parameter_config.cpp)
target_link_libraries(parameter_config ${catkin_LIBRARIES})
编译并运行
cd ~/catkin_workspace
catkin_make# 新建一个终端
source ~/catkin_workspace/devel/setup.bash
roscore
# 新建一个终端
source ~/catkin_workspace/devel/setup.bash
rosrun turtlesim turtlesim_node
# 新建一个终端
source ~/catkin_workspace/devel/setup.bash
rosrun learning_parameter parameter_config
python
编写程序
cd ~/catkin_workspace/src/learning_parameter
mkdir scripts
nano ~/catkin_workspace/src/learning_parameter/scripts/parameter_config.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-########################################################################
#### Copyright 2020 GuYueHome (www.guyuehome.com). ###
######################################################################### 该例程设置/读取海龟例程中的参数import sys
import rospy
from std_srvs.srv import Emptydef parameter_config():# ROS节点初始化rospy.init_node('parameter_config', anonymous=True)# 读取背景颜色参数red = rospy.get_param('/background_r')green = rospy.get_param('/background_g')blue = rospy.get_param('/background_b')rospy.loginfo("Get Backgroud Color[%d, %d, %d]", red, green, blue)# 设置背景颜色参数rospy.set_param("/background_r", 255);rospy.set_param("/background_g", 255);rospy.set_param("/background_b", 255);rospy.loginfo("Set Backgroud Color[255, 255, 255]");# 读取背景颜色参数red = rospy.get_param('/background_r')green = rospy.get_param('/background_g')blue = rospy.get_param('/background_b')rospy.loginfo("Get Backgroud Color[%d, %d, %d]", red, green, blue)# 发现/spawn服务后,创建一个服务客户端,连接名为/spawn的servicerospy.wait_for_service('/clear')try:clear_background = rospy.ServiceProxy('/clear', Empty)# 请求服务调用,输入请求数据response = clear_background()return responseexcept rospy.ServiceException, e:print "Service call failed: %s"%eif __name__ == "__main__":parameter_config()
运行
sudo chmod +x ~/catkin_workspace/src/learning_parameter/scripts/parameter_config.py# 新建一个终端
source ~/catkin_workspace/devel/setup.bash
roscore
# 新建一个终端
source ~/catkin_workspace/devel/setup.bash
rosrun turtlesim turtlesim_node
# 新建一个终端
source ~/catkin_workspace/devel/setup.bash
rosrun learning_parameter parameter_config.py