topic订阅是ROS中最重要最基本的数据传递机制,无论是ROS的官方文档,还是国内一些进行ROS教育的先锋,都没有把topic的订阅机制解释清楚,甚至有些资料中进行了错误的介绍,使得开发者对topic的使用停留在较为粗浅的状态,没办法把topic的功能完全挖掘出来。
本文通过实例完整的解释topic的订阅机制,也把目前对topic使用中一些误区进行纠正。
初步实例:
topic的publish节点代码:
import rclpy
from rclpy.node import Node
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
import cv2
class ImagePublisher(Node):
def __init__(self, name):
super().__init__(name)
self.publisher_ = self.create_publisher(Image, 'chatter1', 10)
self.timer = self.create_timer(0.1, self.timer_callback)
self.cap = cv2.VideoCapture(0)
self.cv_bridge = CvBridge()
def timer_callback(self):
ret, frame = self.cap.read()
if ret == True:
self.publisher_.publish(
self.cv_bridge.cv2_to_imgmsg(frame, 'bgr8'))
self.get_logger().info('

最低0.47元/天 解锁文章
5578

被折叠的 条评论
为什么被折叠?



