1. 创建parameter节点
$ cd ~/catkin_ws/src
$ catkin_create_pkg learning_parameter roscpp rospy std_srvs
2 操作参数命令
#列出当前有多少参数
$ rosparam list
#显示某个参数值
$ rosparam set param_key param_value
#设置某个参数值
$ rosparam set param_key param_value
#保存参数到文件
$ rosparam dump file_name
# 从文件读取参数
$ rosparam load file_name
# 删除参数
$ rosparam delete param_key
3.以修改小海龟为例
#启动rosmanager节点
$ roscore
#另一个tap,启动小海龟节点
$ rosrun turtlesim turtlesim_node
4.获取设置小海龟背景色
#获取当前参数
$ rosparamlist
#设置背景色
$ rosparam set /background_b 100
# 发送变背景色请求
$ sorservice call /clear "{}"
5 python 文件实现参数修改,在节点中创建param.pypython文件
# !/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import rospy
from std_srvs.srv import Empty
def 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的service
rospy.wait_for_service('/clear')
try:
clear_background = rospy.ServiceProxy('/clear', Empty)
# 请求服务调用,输入请求数据
response = clear_background()
return response
except rospy.ServiceException, e:
print "Service call failed: %s"%e
if __name__ == "__main__":
parameter_config
6. 前提运行roscore,然后运行param.py文件
#运行python文件
$ python param.py