ROS2 入门应用 引用自定义消息(Python)


1. 查看自定义消息

引用在《ROS2 入门应用 创建自定义接口》中自定义的消息Sphere.msg

ros2 interface show tutorial_interfaces/msg/Sphere

# geometry_msgs/Point center
#         float64 x
#         float64 y
#         float64 z
# float64 radius

需要对《ROS2 入门应用 发布和订阅(Python)》中创建的发布者/订阅者功能包稍作修改

cd ~/ros2_ws/src/py_pubsub/py_pubsub

将把数值的字符串更改为球体半径


2. 修改话题发布

修改publisher_member_function.py话题发布源文件,涉及话题类型变更和应用变化

import rclpy
from rclpy.node import Node

from tutorial_interfaces.msg import Sphere  # CHANGE


class MinimalPublisher(Node):
    '''
    发布器节点类
    '''
    def __init__(self):
        # 初始化节点名、发布器、每0.5s回调的定时器和计数器
        super().__init__('minimal_publisher')
        self.publisher_ = self.create_publisher(Sphere, 'topic', 10)  # CHANGE
        timer_period = 0.5 
        self.timer = self.create_timer(timer_period, self.timer_callback)
        self.i = 0

    def timer_callback(self):
        '''
        定时器回调函数
        '''
        # 打印并发布字符串附加计数器值的信息
        msg = Sphere()                                           # CHANGE
        msg.radius = float(self.i)                               # CHANGE
        self.publisher_.publish(msg)
        self.get_logger().info('Publishing: "%d"' % msg.radius)  # CHANGE
        self.i += 1


def main(args=None):
    # 初始化ROS2
    rclpy.init(args=args)

    # 创建节点
    minimal_publisher = MinimalPublisher()

    # 运行节点
    rclpy.spin(minimal_publisher)

    # 销毁节点,退出ROS2
    minimal_publisher.destroy_node()
    rclpy.shutdown()


if __name__ == '__main__':
    main()

3. 修改话题订阅

修改subscriber_member_function.py话题订阅源文件,涉及话题类型变更和应用变化

import rclpy
from rclpy.node import Node

from tutorial_interfaces.msg import Sphere  # CHANGE


class MinimalSubscriber(Node):
    '''
    订阅器节点类
    '''
    def __init__(self):
        # 初始化节点
        super().__init__('minimal_subscriber')

        # 初始化订阅器,话题类型Sphere,话题topic,回调函数listener_callback
        self.subscription = self.create_subscription(Sphere, 'topic', self.listener_callback, 10)  # CHANGE
        self.subscription  # 防止未使用变量警告

    def listener_callback(self, msg):
        '''
        订阅器回调函数
        '''
        # 打印订阅话题的消息数据
        self.get_logger().info('I heard: "%d"' % msg.radius)  # CHANGE


def main(args=None):
    # 初始化ROS2
    rclpy.init(args=args)

    # 创建节点
    minimal_subscriber = MinimalSubscriber()

    # 运行节点
    rclpy.spin(minimal_subscriber)

    # 销毁节点,退出ROS2
    minimal_subscriber.destroy_node()
    rclpy.shutdown()


if __name__ == '__main__':
    main()

4. 修改依赖关系

在package.xml清单文件中,添加对自定义消息的依赖项的声明

<exec_depend>tutorial_interfaces</exec_depend>

Python源文件应用不涉及到CMake编译信息,因此不用修改CMakeLists.txt编译文件

setup.py安装软件包说明的程序入口点entry_points字段在之前已经设置完成


entry_points={
    'console_scripts': [
        'talker = py_pubsub.publisher_member_function:main',
        'listener = py_pubsub.subscriber_member_function:main',
    ],
},

5. 编译和运行

进入工作空间根目录

cd ~/ros2_ws

编译:

colcon build --packages-select py_pubsub

打开一个新终端,运行话题发布节点:

ros2 run py_pubsub talker

# [INFO] [minimal_publisher]: Publishing: "0"
# [INFO] [minimal_publisher]: Publishing: "1"
# [INFO] [minimal_publisher]: Publishing: "2"

打开一个新终端,运行话题订阅节点:

ros2 run py_pubsub listener

# [INFO] [minimal_subscriber]: I heard: "10"
# [INFO] [minimal_subscriber]: I heard: "11"
# [INFO] [minimal_subscriber]: I heard: "12"

谢谢