< >
Home » ROS2与Python入门教程 » ros2与Python入门教程-使用参数

ros2与Python入门教程-使用参数

ros2与Python入门教程-使用参数

说明:

  • 介绍如何在ros2中使用参数

步骤:

  • 创建包
cd ~/dev_ws/src
ros2 pkg create --build-type ament_python python_parameters --dependencies rclpy
  • --dependencies参数会自动把相关依赖增加到package.xml和CMakeLists.txt文件
  • 更改package.xml
  • 按实际变更如下几行
<description>Python parameter tutorial</description>
<maintainer email="you@email.com">Your Name</maintainer>
<license>Apache License 2.0</license>
  • 在dev_ws/src/python_parameters/python_parameters目录下
  • 新增python_parameters_node.py文件,内容如下:
import rclpy
import rclpy.node
from rclpy.exceptions import ParameterNotDeclaredException
from rcl_interfaces.msg import ParameterType

class MinimalParam(rclpy.node.Node):
    def __init__(self):
        super().__init__('minimal_param_node')
        timer_period = 2  # seconds
        self.timer = self.create_timer(timer_period, self.timer_callback)

        self.declare_parameter("my_parameter")

    def timer_callback(self):
        # First get the value parameter "my_parameter" and get its string value
        my_param = self.get_parameter("my_parameter").get_parameter_value().string_value

        # Send back a hello with the name
        self.get_logger().info('Hello %s!' % my_param)

        # Then set the parameter "my_parameter" back to string value "world"
        my_new_param = rclpy.parameter.Parameter(
            "my_parameter",
            rclpy.Parameter.Type.STRING,
            "world"
        )
        all_new_parameters = [my_new_param]
        self.set_parameters(all_new_parameters)

def main():
    rclpy.init()
    node = MinimalParam()
    rclpy.spin(node)

if __name__ == '__main__':
    main()
  • 变更setup.py,增加入口
  • 修改配置,内容与package.xml保持一致
maintainer='YourName',
maintainer_email='you@email.com',
description='Python parameter tutorial',
license='Apache License 2.0',
  • 修改entry_points
entry_points={
    'console_scripts': [
        'param_talker = python_parameters.python_parameters_node:main',
    ],
},
  • 安装依赖
cd ~/dev_ws
rosdep install -i --from-path src --rosdistro foxy -y
  • 编译包
colcon build --packages-select python_parameters
  • 新开终端,执行
. install/setup.bash
ros2 run python_parameters param_talker
  • 效果:
[INFO] [parameter_node]: Hello world!
  • 列出所有参数
ros2 param list
  • 终端下更改参数值
ros2 param set /minimal_param_node my_parameter earth
  • 通过launch文件来修改参数值
  • 在dev_ws/src/python_parameters/launch目录下
  • 新增python_parameters_launch.py,内容如下:
from launch import LaunchDescription
from launch_ros.actions import Node

def generate_launch_description():
    return LaunchDescription([
        Node(
            package="python_parameters",
            executable="param_talker",
            name="custom_parameter_node",
            output="screen",
            emulate_tty=True,
            parameters=[
                {"my_parameter": "earth"}
            ]
        )
    ])
  • 打开setup.py, 增加如下内容
import os
from glob import glob
# ...

setup(
  # ...
  data_files=[
      # ...
      (os.path.join('share', package_name), glob('launch/*_launch.py')),
    ]
  )
  • 重新编译
colcon build --packages-select python_parameters
  • 新开终端,运行
. install/setup.bash
ros2 launch python_parameters python_parameters_launch.py
  • 效果如下:
[parameter_node-1] [INFO] [custom_parameter_node]: Hello earth!

纠错,疑问,交流: 请进入讨论区点击加入Q群

获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号


标签: ros2与python入门教程