ROS2+Gazebo+OpenCV之mobot仿真视觉传感器

Posted zhangrelay

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ROS2+Gazebo+OpenCV之mobot仿真视觉传感器相关的知识,希望对你有一定的参考价值。

之前介绍了使用笔记本或者USB摄像头的一些方法,比如获取图像然后再做简单处理。

预备基础:

ROS2之OpenCV的微笑入门资料篇

ROS2之OpenCV怎么理解一段代码 

ROS2机器人个人教程博客汇总(2021共6套) 


其中:

使用机器人操作系统ROS 2和仿真软件Gazebo 9服务进阶实战(八)- mobot行驶至目标位置


当然,这些案例dashing/foxy/galactic/humble都是通用的。

如:ROS2之OpenCV怎么理解一段代码,一样。

只需修改一行代码即可实现:

在这样环境中,可以做红绿等识别和赛道巡线等基础视觉教学任务。

已经测试三轮,在充分听取学生建议的基础上优化和改进,并全部公开。


提示:

图片右侧显示mobot_camera,参考:


参考python代码:

# Basic ROS 2 program to subscribe to real-time streaming 
# video from your built-in webcam
# Author:
# - Addison Sears-Collins
# - https://automaticaddison.com
  
# Import the necessary libraries
import rclpy # Python library for ROS 2
from rclpy.node import Node # Handles the creation of nodes
from sensor_msgs.msg import Image # Image is the message type
from cv_bridge import CvBridge # Package to convert between ROS and OpenCV Images
import cv2 # OpenCV library
 
class ImageSubscriber(Node):
  """
  Create an ImageSubscriber class, which is a subclass of the Node class.
  """
  def __init__(self):
    """
    Class constructor to set up the node
    """
    # Initiate the Node class's constructor and give it a name
    super().__init__('image_subscriber')
      
    # Create the subscriber. This subscriber will receive an Image
    # from the video_frames topic. The queue size is 10 messages.
    self.subscription = self.create_subscription(
      Image, 
      'video_frames', 
      self.listener_callback, 
      10)
    self.subscription # prevent unused variable warning
      
    # Used to convert between ROS and OpenCV images
    self.br = CvBridge()
   
  def listener_callback(self, data):
    """
    Callback function.
    """
    # Display the message on the console
    self.get_logger().info('Receiving video frame')
 
    # Convert ROS Image message to OpenCV image
    current_frame = self.br.imgmsg_to_cv2(data)
    
    # Display image
    cv2.imshow("camera", current_frame)
    
    cv2.waitKey(1)
  
def main(args=None):
  
  # Initialize the rclpy library
  rclpy.init(args=args)
  
  # Create the node
  image_subscriber = ImageSubscriber()
  
  # Spin the node so the callback function is called.
  rclpy.spin(image_subscriber)
  
  # Destroy the node explicitly
  # (optional - otherwise it will be done automatically
  # when the garbage collector destroys the node object)
  image_subscriber.destroy_node()
  
  # Shutdown the ROS client library for Python
  rclpy.shutdown()
  
if __name__ == '__main__':
  main()

需要修改:

    # from the video_frames topic. The queue size is 10 messages.
    self.subscription = self.create_subscription(
      Image, 
      '**********', 
      self.listener_callback, 
      10)

频率为10hz,可修改为仿真虚拟传感器对应发布频率,*******修改为对应主题。 

不重要的修改:

    # Display image
    cv2.imshow("camera", current_frame)

部分调试过程截图:

全景

局部


部分版本接口函数不一致: 

 

dashing+foxy一切ok。

 

需要修改,后续再补充。


 

 

开发者涨薪指南 48位大咖的思考法则、工作方式、逻辑体系

以上是关于ROS2+Gazebo+OpenCV之mobot仿真视觉传感器的主要内容,如果未能解决你的问题,请参考以下文章

ROS2+Gazebo11+Car+OpenCV巡线识别和速度转向控制学习

ROS2之OpenCV人脸识别foxy~galactic~humble

ROS2之OpenCV的微笑入门资料篇

ROS2+Gazebo11+Car+OpenCV获取巡线视觉数据学习

ROS2+Gazebo11+Car+OpenCV录制视觉数据和控制学习

《动手学ROS2》10.2 Gazebo仿真环境准备