1、全局共享字典
rospy.set_param("aa",{"x":1,"y":2})
print(rospy.get_param("aa"))
print(rospy.get_param("aa/x"))
print(rospy.search_param("aa"))
命令行查看
rosparam get aa
rosparam 命令 也可以查看 动态参数
rosparam info dr_p
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
2、动态参数设置 cfg
https://zhuanlan.zhihu.com/p/39899955
https://zhuanlan.zhihu.com/p/367817775
2.1 定义 cfg文件
#!/usr/bin/env python
PACKAGE = "cat1"
from dynamic_reconfigure.parameter_generator_catkin import *
gen = ParameterGenerator()
gen.add("int_param", int_t, 0, "An Integer parameter", 50, 0, 100)
gen.add("double_param", double_t, 0, "A double parameter", .5, 0, 1)
gen.add("str_param", str_t, 0, "A string parameter", "Hello World")
gen.add("bool_param", bool_t, 0, "A Boolean parameter", True)
size_enum = gen.enum([ gen.const("Small", int_t, 0, "A small constant"),
gen.const("Medium", int_t, 1, "A medium constant"),
gen.const("Large", int_t, 2, "A large constant"),
gen.const("ExtraLarge", int_t, 3, "An extra large constant")], "An enum to set size")
gen.add("size", int_t, 0, "A size parameter which is edited via an enum", 1, 0, 3, edit_method=size_enum)
exit(gen.generate(PACKAGE, "cat1", "Tutorials"))
2.2 编译 src/cat1/CMakeLists.txt 已介绍的很详细了
################################################
## Declare ROS dynamic reconfigure parameters ##
################################################
## To declare and build dynamic reconfigure parameters within this
## package, follow these steps:
## * In the file package.xml:
## * add a build_depend and a exec_depend tag for "dynamic_reconfigure"
## * In this file (CMakeLists.txt):
## * add "dynamic_reconfigure" to
## find_package(catkin REQUIRED COMPONENTS ...)
## * uncomment the "generate_dynamic_reconfigure_options" section below
## and list every .cfg file to be processed
## Generate dynamic reconfigure parameters in the 'cfg' folder
generate_dynamic_reconfigure_options(
cfg/Tutorials.cfg
# cfg/DynReconf2.cfg
)
- 在功能包的 src/cat1/package.xml 文件添加
<exec_depend>dynamic_reconfigure</exec_depend>
- src/cat1/CMakeLists.txt
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
actionlib_msgs
actionlib
message_generation
dynamic_reconfigure #新添加的
)
3、src/cat1/CMakeLists.txt
generate_dynamic_reconfigure_options(
cfg/Tutorials.cfg
cfg/DynReconf2.cfg
)
编译 catkin_make
2.3 编写 服务端
#!/usr/bin/env python
# -*- coding=utf-8 -*-
# @File : service1.py
# @Version : 1.0
# @Author : xin
# @Time : 2021/07/15 09:40:31
# Description:
import rospy
from dynamic_reconfigure.server import Server
from cat1.cfg import TutorialsConfig as drConfig
"""
动态参数服务端: 参数被修改时直接打印
实现流程:
1.导包
2.初始化 ros 节点
3.创建服务对象
4.回调函数处理
5.spin
"""
# 回调函数
def cb(config, level):
rospy.loginfo("python 动态参数服务解析:{},{},{},{},{}"
.format(
config.int_param,
config.double_param,
config.str_param,
config.bool_param,
config.size)
)
return config
if __name__ == "__main__":
print("init .... ")
# 2.初始化 ros 节点
rospy.init_node("dr_p")
# 3.创建服务对象
server = Server(drConfig, cb)
# 4.回调函数处理
# 5.spin
rospy.spin()
启动 服务端程序 service1.py
2.4 客户端
#!/usr/bin/env python
# -*- coding=utf-8 -*-
# @File : client1.py
# @Version : 1.0
# @Author : xin
# @Time : 2021/07/15 10:04:32
#Description:
import datetime
import rospy
from dynamic_reconfigure.client import Client
if __name__ == "__main__":
rospy.init_node("ss")
ss=Client("dr_p")
print(ss.get_configuration()) #获取值
ss.update_configuration({"int_param":22,"str_param":"bbbb"}) #设置某个值
ss.close()
pass
2.5 ui查看
启动客户端:
rosrun rqt_gui rqt_gui -s rqt_reconfigure
或
rosrun rqt_reconfigure rqt_reconfigure
修改数据后 服务端的回调会及时通知
[INFO] [1626313953.868176]: python 动态参数服务解析:28,0.5,Hello World,True,1
[INFO] [1626313960.147794]: python 动态参数服务解析:28,0.2,Hello World,True,1
[INFO] [1626313964.071892]: python 动态参数服务解析:28,0.2,fasdfdsf,True,1
[INFO] [1626313966.472072]: python 动态参数服务解析:28,0.2,fasdfdsf,True,3
2.6 命令行查看
rosparam get dr_p
{bool_param: true, double_param: 0.2, int_param: 22, size: 3, str_param: bbbb}