ROS 2 Iron 教程 第二章 Client libraries 第四节 编写一个简单的发布者和订阅者(Python))
- 前言
- 背景
- 先决条件
- 任务
- 1 Create a package (创建一个包)
- 2 Write the publisher node (编写发布者节点)
- 2.1 Examine the code (分析代码)
- 2.2 Add dependencies (添加依赖)
- 2.3 Add an entry point (添加入口点)
- 2.4 Check setup.cfg (检查 setup.cfg)
- 3 Write the subscriber node (编写订阅者节点)
- 3.1 Examine the code (分析代码)
- 3.2 Add an entry point (添加启动点)
- 4 Build and run (构建并运行)
- 总结
前言
本系列文章是由笔者翻译自ROS 2 官方教程。笔者水平有限,如有错误,还请读者指正。
本节教程中部分代码为 Liunx下,请使用其他操作系统的读者前往原文查看。
背景
在本节教程中,你将会创建以字符串消息形式在话题上相互传递信息的节点。本节使用的示例是一个简单的“诉说者”和“倾听者”系统;一个节点发布数据,另一个订阅话题来接收数据。
此处 可以找到示例中使用的代码。
先决条件
在过去的教程中,我们已经学会了如何创建一个工作空间和包。
对Python的基本了解是推荐的,但不必是全面了解。
任务
1 Create a package (创建一个包)
打开一个新终端并添加你的 ROS 2 安装,这样ros2
命令将会可用。
cd
进入过去教程中创建的ros2_ws
目录。
请回忆一下,包应该被创建在src
目录中,而不是工作空间的根目录。所以,cd
进入ros2_ws/src
目录,运行以下创建包命令:
ros2 pkg create --build-type ament_python --license Apache-2.0 py_pubsub
你的终端将会返回信息以证明py_pubsub
包的创建以及必要文件和文件夹创建成功。
2 Write the publisher node (编写发布者节点)
cd
进入ros2_ws/src/py_pubsub/py_pubsub
,请回忆一下这个目录是一个Python包,与它嵌套的 ROS 2 包同名。
运行如下名命令下载示例发布者节点源码(Liunx):
wget https://raw.githubusercontent.com/ros2/examples/iron/rclpy/topics/minimal_publisher/examples_rclpy_minimal_publisher/publisher_member_function.py
现在在_init_.py
边上将会有一个名为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()
2.1 Examine the code (分析代码)
注释后的第一行代码导入了rclpy
,这样Node
类将会可用。
import rclpy
from rclpy.node import Node
下一行代码导入内建的字符串类型消息,节点用此消息类型构建数据并在话题上传递。
from std_msgs.msg import String
上述几行代码说明节点的依赖。请回忆一下,节点的依赖应该添加到package.xml
,你将会在下一部分完成这项工作。
接下来,MinimalPublisher
类被创建,继承自(或是子类)Node
。
class MinimalPublisher(Node):
随后是类结构的定义,super()._init_
调用了Node
类的构造函数并命名为minimal_publisher
。
create_publisher
声明节点发布String
类型消息(从std_msgs.msg
模块导入),且在名为topic
,“队列”大小为10的话题上发布。队列大小是必须的 QoS (服务质量)设置,它限制了排队消息的数量,假如订阅者接收消息的速度不够快。
接下来,每 0.5 秒通过回调执行的定时器被创建,self.i
是在回调中使用的计数器。
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
timer_callback
创建了一条带有计数器值的消息,并通过get_logger().info
将其发布至终端。
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()
在主函数中首先初始化rclpy
库,随后节点被创建并启动,它的回调函数被调用。
2.2 Add dependencies (添加依赖)
回到ros2_ws/src/py_pubsub
目录,在此setup.py
,setup.cfg
和package.xml
文件已经被创建。
使用你喜爱的文本编辑器打开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>
上方的代码声明包在执行代码时需要rclpy
和std_msgs
。
确保你保存了文件。
2.3 Add an entry point (添加入口点)
打开setup.py
,将package.xml
的maintainer
,maintainer_email
,description
和license
标签的内容填入setup.py
对应字段内:
maintainer='YourName',
maintainer_email='you@email.com',
description='Examples of minimal publisher/subscriber using rclpy',
license='Apache License 2.0',
在entry_points
字段的console_scripts
括号内添加以下行:
entry_points={
'console_scripts': [
'talker = py_pubsub.publisher_member_function:main',
],
},
记得保存文件。
2.4 Check setup.cfg (检查 setup.cfg)
setup.cfg
文件的内容应被自动正确填充,如下所示:
[develop]
script_dir=$base/lib/py_pubsub
[install]
install_scripts=$base/lib/py_pubsub
这告诉启动工具将你的可执行文件放入lib
,ros2 run
将在lib
中寻找它们。
你现在可以构建包了,但我们最好先创建订阅者节点,以便将整个运行的系统展现在你的面前。
3 Write the subscriber node (编写订阅者节点)
cd
回到ros2_ws/src/py_pubsub/py_pubsub
中来创建下一个节点。在你的终端中运行如下代码(Liunx):
wget https://raw.githubusercontent.com/ros2/examples/iron/rclpy/topics/minimal_subscriber/examples_rclpy_minimal_subscriber/subscriber_member_function.py
现在在目录中应该有如下文件:
__init__.py publisher_member_function.py subscriber_member_function.py
3.1 Examine the code (分析代码)
使用你喜爱的文本编辑器打开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()
订阅者节点的代码与发布者的代码几乎相同。订阅者节点使用几乎相同的参数调用构造函数创建订阅者。回忆一下在话题教程中提到的内容:发布者和订阅者使用的主题名称和消息类型必须匹配,它们之间才能进行通信。
self.subscription = self.create_subscription(
String,
'topic',
self.listener_callback,
10)
订阅者的构造函数和回调不包含任何定时器定义,因为它不需要定时器。一旦收到消息,订阅者就会调用它的回调。
回调的定义只是将消息以及接收到的数据打印到终端中。回想一下发布者的定义:msg.data = 'Hello World: %d' % self.i
def listener_callback(self, msg):
self.get_logger().info('I heard: "%s"' % msg.data)
main
函数的内容几乎是一样的,区别在于创建和启动的节点是订阅者而不是发布者。
minimal_subscriber = MinimalSubscriber()
rclpy.spin(minimal_subscriber)
既然发布者和订阅者节点的依赖是相同的,package.xml
和setup.cfg
不需要添加内容。
3.2 Add an entry point (添加启动点)
再次打开setup.py
,在发布者启动点下方添加订阅者的启动点,entry_points
字段现在应该是这样:
entry_points={
'console_scripts': [
'talker = py_pubsub.publisher_member_function:main',
'listener = py_pubsub.subscriber_member_function:main',
],
},
请不要忘记保存,随后你的发布者-订阅者系统已经蓄势待发。
4 Build and run (构建并运行)
你的 ROS 2 系统中应该已经有了rclcpp
和std_msgs
包,构建前在工作空间根目录运行rosdep
来查找是否有缺失的包是好的习惯(Liunx):
rosdep install -i --from-path src --rosdistro iron -y
仍然在你工作空间的根目录ros2_ws
,构建你的新包( Liunx):
colcon build --packages-select py_pubsub
打开一个新终端,cd
进入ros2_ws
,并添加启动文件:
. install/setup.bash
现在运行发布者节点:
ros2 run py_pubsub talker
终端应该开始每隔0.5s发布信息:
[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"
打开另一个终端,再次添加启动文件,随后启动订阅者节点:
ros2 run py_pubsub listener
订阅者将会开始在终端上打印消息,从第一次接收到的发布者消息计数开始:
[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"
在每个终端中键入Ctrl+C
来停止节点运行。
总结
在本节教程中我们创建了两个节点来发布和订阅话题上的数据。在编译和运行它们之前,我们已将其依赖项和可执行文件添加到包配置文件中。