Ros.11 參數(shù) 動態(tài)參數(shù)

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 命令 也可以查看 動態(tài)參數(shù)

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、動態(tài)參數(shù)設置 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
)

  1. 在功能包的 src/cat1/package.xml 文件添加
    <exec_depend>dynamic_reconfigure</exec_depend>
  2. 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

"""
    動態(tài)參數(shù)服務端: 參數(shù)被修改時直接打印
    實現(xiàn)流程:
        1.導包
        2.初始化 ros 節(jié)點
        3.創(chuàng)建服務對象
        4.回調函數(shù)處理
        5.spin
"""
# 回調函數(shù)


def cb(config, level):
    rospy.loginfo("python 動態(tài)參數(shù)服務解析:{},{},{},{},{}"
                  .format(
                      config.int_param,
                      config.double_param,
                      config.str_param,
                      config.bool_param,
                      config.size)
                  )
    return config


if __name__ == "__main__":
    print("init .... ")
    # 2.初始化 ros 節(jié)點
    rospy.init_node("dr_p")
    # 3.創(chuàng)建服務對象
    server = Server(drConfig, cb)
    # 4.回調函數(shù)處理
    # 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

image.png

修改數(shù)據(jù)后 服務端的回調會及時通知

[INFO] [1626313953.868176]: python 動態(tài)參數(shù)服務解析:28,0.5,Hello World,True,1
[INFO] [1626313960.147794]: python 動態(tài)參數(shù)服務解析:28,0.2,Hello World,True,1
[INFO] [1626313964.071892]: python 動態(tài)參數(shù)服務解析:28,0.2,fasdfdsf,True,1
[INFO] [1626313966.472072]: python 動態(tài)參數(shù)服務解析: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}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。