• ros2与Python入门教程-创建发布和订阅


    来源:https://www.ncnynl.com/archives/202008/3818.html

    说明:

    介绍如何创建发布和订阅
    步骤:

    新建包

    cd ~/dev_ws/src
    ros2 pkg create --build-type ament_python py_pubsub
    
    • 1
    • 2

    在目录dev_ws/src/py_pubsub/py_pubsub下
    新建文件publisher_member_function.py
    内容如下:

    import rclpy
    from rclpy.node import Node
    
    from std_msgs.msg import String
    
    
    class MinimalPublisher(Node):
    
        def __init__(self):
            super().__init__('minimal_publisher')
            self.publisher_ = self.create_publisher(String, 'topic', 10)
            timer_period = 0.5  # seconds
            self.timer = self.create_timer(timer_period, self.timer_callback)
            self.i = 0
    
        def timer_callback(self):
            msg = String()
            msg.data = 'Hello World: %d' % self.i
            self.publisher_.publish(msg)
            self.get_logger().info('Publishing: "%s"' % msg.data)
            self.i += 1
    
    
    def main(args=None):
        rclpy.init(args=args)
    
        minimal_publisher = MinimalPublisher()
    
        rclpy.spin(minimal_publisher)
    
        # Destroy the node explicitly
        # (optional - otherwise it will be done automatically
        # when the garbage collector destroys the node object)
        minimal_publisher.destroy_node()
        rclpy.shutdown()
    
    
    if __name__ == '__main__':
        main()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    更改package.xml
    更改description,maintainer ,license相关和增加依赖

    <description>Examples of minimal publisher/subscriber using rclpy</description>
    <maintainer email="you@email.com">Your Name</maintainer>
    <license>Apache License 2.0</license>
    
    <exec_depend>rclpy</exec_depend>
    <exec_depend>std_msgs</exec_depend>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    更改setup.py
    与package.xml的内容匹配

    maintainer='YourName',
    maintainer_email='you@email.com',
    description='Examples of minimal publisher/subscriber using rclpy',
    license='Apache License 2.0',
    
    • 1
    • 2
    • 3
    • 4

    更改console_scripts内容如下:

    entry_points={
            'console_scripts': [
                    'talker = py_pubsub.publisher_member_function:main',
            ],
    },
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在目录dev_ws/src/py_pubsub/py_pubsub下
    新建文件subscriber_member_function.py
    内容如下:

    import rclpy
    from rclpy.node import Node
    
    from std_msgs.msg import String
    
    
    class MinimalSubscriber(Node):
    
        def __init__(self):
            super().__init__('minimal_subscriber')
            self.subscription = self.create_subscription(
                String,
                'topic',
                self.listener_callback,
                10)
            self.subscription  # prevent unused variable warning
    
        def listener_callback(self, msg):
            self.get_logger().info('I heard: "%s"' % msg.data)
    
    
    def main(args=None):
        rclpy.init(args=args)
    
        minimal_subscriber = MinimalSubscriber()
    
        rclpy.spin(minimal_subscriber)
    
        # Destroy the node explicitly
        # (optional - otherwise it will be done automatically
        # when the garbage collector destroys the node object)
        minimal_subscriber.destroy_node()
        rclpy.shutdown()
    
    
    if __name__ == '__main__':
        main()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    更改setup.py,增加入口

    entry_points={
            'console_scripts': [
                    'talker = py_pubsub.publisher_member_function:main',
                    'listener = py_pubsub.subscriber_member_function:main',
            ],
    },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    安装依赖

    cd ~/dev_ws/
    rosdep install -i --from-path src --rosdistro foxy -y
    
    • 1
    • 2

    编译

    colcon build --packages-select py_pubsub
    
    • 1

    新开终端,运行发布

    . install/setup.bash
    ros2 run py_pubsub talker
    
    • 1
    • 2

    效果如下:

    [INFO] [minimal_publisher]: Publishing: "Hello World: 0"
    [INFO] [minimal_publisher]: Publishing: "Hello World: 1"
    [INFO] [minimal_publisher]: Publishing: "Hello World: 2"
    [INFO] [minimal_publisher]: Publishing: "Hello World: 3"
    [INFO] [minimal_publisher]: Publishing: "Hello World: 4"
    
    • 1
    • 2
    • 3
    • 4
    • 5

    新开终端,运行订阅

    . install/setup.bash
    ros2 run py_pubsub listener
    
    • 1
    • 2

    效果如下:

    [INFO] [minimal_subscriber]: I heard: "Hello World: 10"
    [INFO] [minimal_subscriber]: I heard: "Hello World: 11"
    [INFO] [minimal_subscriber]: I heard: "Hello World: 12"
    [INFO] [minimal_subscriber]: I heard: "Hello World: 13"
    [INFO] [minimal_subscriber]: I heard: "Hello World: 14"
    
    • 1
    • 2
    • 3
    • 4
    • 5
  • 相关阅读:
    汉诺塔问题
    MySQL数据库基础
    大型集团企业数据集成研究
    如何 通过使用优先级提示,来控制所有网页资源加载顺序
    NISP中渗透测试需要记录的专业术语
    matlab GPR高斯过程回归与股票价格预测
    15-自动化测试——理论知识
    推荐一个带java环境的tomcat镜像,使用jdk 1.8.0_312
    什么是Linux
    LoRa模块空中唤醒功能原理和物联网应用
  • 原文地址:https://blog.csdn.net/ncnynl/article/details/125620191