当前位置:网站首页>ROS 笔记(09)— 参数的查询和设置
ROS 笔记(09)— 参数的查询和设置
2022-06-28 07:58:00 【wohu1104】
参数服务器在ROS中主要用于实现不同节点之间的数据共享。参数服务器相当于是独立于所有节点的一个公共容器,可以将数据存储在该容器中,被不同的节点调用,当然不同的节点也可以往其中存储数据,关于参数服务器的典型应用场景如下:
导航实现时,会进行路径规划,比如: 全局路径规划,设计一个从出发点到目标点的大致路径。本地路径规划,会根据当前路况生成时时的行进路径
上述场景中,全局路径规划和本地路径规划时,就会使用到参数服务器:
路径规划时,需要参考小车的尺寸,我们可以将这些尺寸信息存储到参数服务器,全局路径规划节点与本地路径规划节点都可以从参数服务器中调用这些参数
参数服务器,一般适用于存在数据共享的一些应用场景。
1. 参数模型
如上所示,参数模型可以看做是 ROS 的全局字典,各个节点 Node 都可以从参数模型中获取对应的参数信息。
具体可参考:http://wiki.ros.org/Parameter%20server
2. 常用参数命令行
常用参数命令行有以下
$ rosparam
rosparam is a command-line tool for getting, setting, and deleting parameters from the ROS Parameter Server.
Commands:
rosparam set set parameter
rosparam get get parameter
rosparam load load parameters from file
rosparam dump dump parameters to file
rosparam delete delete parameter
rosparam list list parameter names
- 列出当前所有参数
$ rosparam list
/rosdistro
/roslaunch/uris/host_wohu_pc__42765
/rosversion
/run_id
/turtlesim/background_b
/turtlesim/background_g
/turtlesim/background_r
- 显示某个参数值
$ rosparam get /run_id
b37e6c1a-f0fe-11ec-92ca-00e070ce7d11
$ rosparam get /turtlesim/background_b
255
- 设置某个参数值
$ rosparam set /turtlesim/background_b 100
$ rosparam get /turtlesim/background_b
100
$ rosservice call /clear "{}" # 用于使设置的新值生效
- 保存参数到文件
$ rosparam dump config
$ ls
build config devel src
$ cat config
rosdistro: 'melodic '
roslaunch:
uris: {
host_wohu_pc__42765: 'http://wohu-pc:42765/'}
rosversion: '1.14.13 '
run_id: b37e6c1a-f0fe-11ec-92ca-00e070ce7d11
turtlesim: {
background_b: 100, background_g: 86, background_r: 69}
- 从文件中读取参数
$ rosparam load config
$ rosservice call /clear "{}"
- 删除参数
$ rosparam set /aaa 100
$ rosparam get /aaa
100
$ rosparam delete /aaa
3. 参数存储格式
所有参数是存储在一个 YAML 格式的文件中。
rosdistro: 'melodic
'
roslaunch:
uris: {
host_wohu_pc__42765: 'http://wohu-pc:42765/'}
rosversion: '1.14.13
'
run_id: b37e6c1a-f0fe-11ec-92ca-00e070ce7d11
turtlesim: {
background_b: 100, background_g: 86, background_r: 69}
4. 参数设置和查询代码
#!/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('/turtlesim/background_r')
green = rospy.get_param('/turtlesim/background_g')
blue = rospy.get_param('/turtlesim/background_b')
rospy.loginfo("Get Backgroud Color[%d, %d, %d]", red, green, blue)
# 设置背景颜色参数
rospy.set_param("/turtlesim/background_r", 255);
rospy.set_param("/turtlesim/background_g", 255);
rospy.set_param("/turtlesim/background_b", 255);
rospy.loginfo("Set Backgroud Color[255, 255, 255]");
# 读取背景颜色参数
red = rospy.get_param('/turtlesim/background_r')
green = rospy.get_param('/turtlesim/background_g')
blue = rospy.get_param('/turtlesim/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()
运行
$ cd ~/catkin_ws
$ catkin_create_pkg parameter rospy std_msgs geometry_msgs turtlesim
$ catkin_make
$ source ./devel/setup.bash
将 Python 代码放到如下路径
继续执行
$ roscore
$ rosrun turtlesim turtlesim_node
$ rosrun parameter parameter_config.py
结果变化
边栏推荐
猜你喜欢

SOC timer and interrupt configuration

Software design of power control board

asp. Net to search products and realize paging function

2021 programming language ranking summary

Redis cerebral fissure

Soft exam -- software designer -- afternoon question data flow diagram DFD

分析 NFT 项目的 5 个指标

Section VII starting principle and configuration of zynq

Activity隐式跳转

Design and implementation of spark offline development framework
随机推荐
7-2 Finnish wooden chess structure Sorting
分析 NFT 项目的 5 个指标
ES6 use of return in arrow function
ZYNQ_ IIC read / write m24m01 record board status
Design of DSP image data stream
2021 programming language ranking summary
Idea package together, using compact middle packages to solve &
Section VI UART of zynq
SOC timer and interrupt configuration
HJ21 简单密码
asp. Net to search products and realize paging function
Kubernetes理论基础
HJ delete the character with the least number of occurrences in the string
Is it reliable to open an account by digging money? Is it safe?
挖财注册开户靠谱吗?安全吗?
Leetcode learning records
HJ base conversion
Redis cerebral fissure
The solution of "user account control to continue, please enter administrator user name and password" appears in win10 Professional Edition
Explanation and application of instr() function in Oracle