ROS learning record ⑥: dynamic configuration parameters

1. Create a new feature package

cd catkin_ws/src
catkin_create_pkg pid roscpp rospy rosmsg std_msgs dynamic_reconfigure 

2. Create cfg file

Create a new cfg folder and create a new .cfgfile in it

mkdir cfg
cd cfg
touch PID.cfg

The core API of dynamic parameter tuning:

gen.add(name,type, level,description, default, min,max)

Parameter Description:

name: variable name
type: type name, commonly used types are: int_t, double_t, str_t, bool_t
level: a marker bit, as long as the parameter is modified, it will be changed to this value
description: parameter description
default: default value
min: available Optional, the minimum value of the parameter
max: optional, the maximum value of the parameter

For example, the content of dynamically adjusting PID parameters is as follows:

#! /usr/bin/env python
# coding=utf-8

import roslib
from dynamic_reconfigure.parameter_generator_catkin import *

PACKAGE = "pid"
roslib.load_manifest(PACKAGE)

# 创建一个参数生成器
gen = ParameterGenerator()

# 添加参数说明,便于后续生成界面
#             参数名    类型               等级       参数描述             默认值        最小值         最大值
gen.add("p",		double_t,		0,		"KP param.",			0.0,			-100.00,		100.00)
gen.add("i",		double_t,		0,		"KI param.",			0.0,			-100.00,		100.00)
gen.add("d",		double_t,		0,		"KD param.",			0.0,			-100.00,		100.00)

# 调用生成器生成config配置文件
#                                   包名           节点名称    生成文件名
exit(gen.generate(PACKAGE, PACKAGE, "PID"))

After that, set PID.cfg as an executable file, you can right-click and set properties, or set it through the command line:

chmod a+x PID.cfg

Also turn on the following comments in CMakelist.txt:

## Generate dynamic reconfigure parameters in the 'cfg' folder
generate_dynamic_reconfigure_options(
	cfg/PID.cfg		# 注意这里要填自己的路径
)

3. Node file

The steps of creating a new py node file are the same as normal, so I won’t go into details here

The case here writes a PID dynamic tuning parameter, here is the direct code and comments:

# 导入依赖
from pid.cfg import PIDConfig
from dynamic_reconfigure.server import Server
from std_msgs.msg import Float32MultiArray
import rospy


class UpdatePID():

    def __init__(self):
        rospy.init_node("update_pid")
        rospy.on_shutdown(self.shutdown)
        
        # 执行频率
        rate = rospy.Rate(20)
        
        # 声明一个消息发布者, 将消息发布到driver
        self.publisher_pid = rospy.Publisher("/pid", Float32MultiArray, queue_size=100)
        
        # 启动参数配置服务器         当参数变化时的回调
        Server(PIDConfig, self.dynamic_callback)
        
        # 打印一个启动成功的日志
        rospy.loginfo("updatePID_node start success! Bring up rqt_reconfigure to control the pid ...")
        
        # 让ros节点跑起来
        while not rospy.is_shutdown():
            rate.sleep()
            
	# 通过回调函数中获取rqt界面上的数值
    def dynamic_callback(self, config, level):
        """
        当参数发生变化时的回调函数
        :param config: 配置文件
        :param level: 修改的状态
        :return:
        """
        # 封装消息
        kp = config["p"]
        ki = config["i"]
        kd = config["d"]

        pid_msg = Float32MultiArray()
        pid_msg.data.append(kp)
        pid_msg.data.append(ki)
        pid_msg.data.append(kd)

        # 将消息发布出去
        self.publisher_pid.publish(pid_msg)

        print (config, level)
        return config

    def shutdown(self):
        print ("stop robot ...")


if __name__ == '__main__':
    UpdatePID()

After that, remember to give the py file executable permissions

chmod a+x updatePID_node.py

4. Start configuration

runroscore

roscore

catkin_makeone time

cd ~/catkin_ws
catkin_make

start node file

source devel/setup.bash
rosrun pid UpdatePIDNode.py 

insert image description here

At this point, remember to open rqt_reconfigurethe dynamic parameter adjustment

rosrun rqt_reconfigure rqt_reconfigure

insert image description here

insert image description here

At this time, you can also rqt_topiccheck whether the node is running normally in

insert image description here

Then send the pid parameters to the lower computer according to actual needs to realize dynamic parameter adjustment


Guess you like

Origin blog.csdn.net/Arcann/article/details/109720695