• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

ROS2入门教程——16. 创建一个简单的订阅者和发布者(Python)

人工智能 古月 2009次浏览 0个评论

上一篇我们通过C++实现了发布者和订阅者,本篇我们试试用Python来实现同样的功能。  

1.创建功能包

首先我们在之前创建的dev_ws工作空间中来创建一个放置节点代码的功能包。   打开一个新的终端,cd到dev_ws/src目录下,然后运行创建功能包的指令:

ros2 pkg create --build-type ament_python py_pubsub

  很快就创建好了一个叫做py_pubsub的功能包。  

2.创建发布者节点

在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()

  这就是一个简单的发布者代码,我们来对代码做下解析。  

import rclpy
from rclpy.node import Node

首先是引入需要的模块,包括ROS2的python接口和节点类。  

from std_msgs.msg import String

紧接着是导入String字符串消息。  

class MinimalPublisher(Node):

然后创建一个继承于Node基类的MinimalPublisher节点子类。  

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

这里是MinimalPublisherd的构造函数,super().__init__ 中会调用父类Node的构造函数,节点名初始化为“minimal_publisher”。然后创建了一个发布者,发布的话题名是topic,话题消息是String,保存消息的队列长度是10,然后创建了一个定时器timer_,做了一个0.5秒的定时,每次触发定时器后,都会运行回调函数timer_callback。  

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

timer_callback是这里的关键,每次触发都会发布一次话题消息。message中保存的字符串是Hello world加一个计数值,然后通过info函数打印一次日志信息,再通过发布者的publish方法将消息发布出去。  

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()

最后就是main函数啦,先初始化rclpy,再创建MinimalPublisher,进入自旋锁,当退出锁时,就会关闭节点结束啦。   完成以上发布者的代码后,功能包里还有一些内容需要设置。  

  • 设置依赖项

  打开功能包的package.xml文件,先把这些基础信息填写好:

<description>Examples of minimal publisher/subscriber using rclpy</description>
<maintainer email="you@email.com">Your Name</maintainer>
<license>Apache License 2.0</license>

  然后还需要添加依赖项,放到ament_python下边:

<exec_depend>rclpy</exec_depend>
<exec_depend>std_msgs</exec_depend>

 

  • 设置程序入口

  接下来打开setup.py文件,同样需要补充以下内容:

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

  然后在 entry_points 下添加如下内容:

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

 

  • 检查setup.cfg文件

  setup.cfg文件中内容是自动添加的,可以打开看下,内容如下:

[develop]script-dir=$base/lib/py_pubsub
[install]install-scripts=$base/lib/py_pubsub

  主要含义就是把可执行的python文件在编译时放到install的lib下边,这样ros2 run命令才能找的到。  

3.创建订阅者

还是回到功能包py_pubsub/py_pubsub文件夹下,接下来创建订阅者的代码subscriber_member_function.py:  

import rclpyfrom 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()

  还是来解析下代码。  

self.subscription = self.create_subscription(
    String,
    'topic',
    self.listener_callback,
    10)

订阅者的代码整体流程和发布者类似,现在的节点名叫minimal_subscriber,构造函数中创建了订阅者,订阅String消息,订阅的话题名叫做“topic”,保存消息的队列长度是10,当订阅到数据时,会进入回调函数topic_callback。  

def listener_callback(self, msg):
    self.get_logger().info('I heard: "%s"' % msg.data)

回调函数中会收到String消息,然后并没有做太多处理,只是通过info打印出来。   main函数中的内容和发布者几乎是一致的,就不再赘述。   由于该节点的依赖项和发布者一样,我们就不需要修改package.xml和 setup.cfg文件了,不过程序入口 setup.py还是得加一些内容:

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

 

4.编译并运行

编译前先确认下功能包的依赖项有没有都安装好,在dev_ws路径下运行如下命令:

rosdep install -i --from-path src --rosdistro foxy -y

  安装完毕后还是在该路径下编译py_pubsub功能包:

colcon build --packages-select py_pubsub

  ROS2入门教程——16. 创建一个简单的订阅者和发布者(Python)   编译完成后,打开一个新的终端,设置工作空间的环境变量后,运行发布者:

. install/setup.bash
ros2 run py_pubsub talker

  运行成功后可以看到终端每隔0.5s打印一次日志信息:   ROS2入门教程——16. 创建一个简单的订阅者和发布者(Python)   再打开一个新的终端,类似的操作,运行订阅者:

. install/setup.bash   
ros2 run py_pubsub listener

  订阅者启动啦,终端中会显示当前订阅者收到的消息内容:   ROS2入门教程——16. 创建一个简单的订阅者和发布者(Python)  

在终端中按Ctrl+C即可退出。   好啦,本篇我们一起使用Python创建了发布者和订阅者节点。以上代码也可以在这里找到: https://github.com/ros2/examples/tree/master/rclpy/topics

 


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明ROS2入门教程——16. 创建一个简单的订阅者和发布者(Python)
喜欢 (0)

您必须 登录 才能发表评论!

加载中……