节点是通过ROS graph 进行通信的可执行程序。在本教程中,节点将通过主题以字符串消息的形式相互传递信息。这里使用的示例是一个简单的“talker”和“listener”系统。一个节点发布数据,另一个节点订阅该主题,以便它可以接收该数据,基于python
② 前提创建好工作空间
③ 任务 Ⅰ 创建一个包创建一个pubsub的包
切换到dev_ws/src
路径下
ros2 pkg create --build-type ament_python py_pubsub
Ⅱ 写发布节点
切换到dev_ws/src/py_pubsub/py_pubsub
路径下执行命令下载demo
wget https://raw.githubusercontent.com/ros2/examples/master/rclpy/topics/minimal_publisher/examples_rclpy_minimal_publisher/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()
代码解析
修改 package.xml
修改描述
Examples of minimal publisher/subscriber using rclpy
Your Name
Apache License 2.0
增加依赖
rclpy
std_msgs
修改 setup.py
这个和xml一样的
maintainer='YourName',
maintainer_email='you@email.com',
description='Examples of minimal publisher/subscriber using rclpy',
license='Apache License 2.0',
在 entry_points
下的 console_scripts
里添加,这是个list可以写无数个
entry_points={
'console_scripts': [
'talker = py_pubsub.publisher_member_function:main',
],
},
检查 setup.cfg
是这样
[develop]
script-dir=$base/lib/py_pubsub
[install]
install-scripts=$base/lib/py_pubsub
这个配置是说明 可执行文件是在 lib
下面,ros2 run 的时候会到目录下查找
在dev_ws/src/py_pubsub/py_pubsub
下载demo
wget https://raw.githubusercontent.com/ros2/examples/master/rclpy/topics/minimal_subscriber/examples_rclpy_minimal_subscriber/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()
代码解析
修改 package.xml
基于前面的pub没有新增依赖,所以不用修改
修改 setup.py在刚刚的entry_points 在加一行变成这样
entry_points={
'console_scripts': [
'talker = py_pubsub.publisher_member_function:main',
'listener = py_pubsub.subscriber_member_function:main',
],
},
Ⅳ 编译和运行
养成好习惯,编译前先解决依赖,切换到dev_ws
下
udo rosdep install -i --from-path src --rosdistro eloquent -y
只编译新增的包
colcon build --packages-select py_pubsub
安装环境
. install/setup.bash
运行两个节点
ros2 run py_pubsub talker
ros2 run py_pubsub listener
④ 总结
您创建了两个节点来发布和订阅主题上的数据。在编译和运行它们之前,您已将它们的依赖项和可执行文件添加到包配置文件中。
这些例子中使用的代码可以在这里找到。