Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

Jupyter Notebook から rclpy を読み出す実験 その5:Realsense D435の値を取得する

shuzo-kino.hateblo.jp
のシリーズ

実際のところ

前提条件

スクリプト

import rclpy
from rclpy.node import Node
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
import cv2
import numpy as np

class DepthImageSubscriber(Node):

    def __init__(self):
        super().__init__('depth_image_subscriber')
        self.subscription = self.create_subscription(
            Image,
            '/camera/depth/image_rect_raw',
            self.listener_callback,
            10)
        self.subscription  # prevent unused variable warning
        self.bridge = CvBridge()

    def listener_callback(self, data):
        try:
            cv_image = self.bridge.imgmsg_to_cv2(data, "passthrough")
        except CvBridgeError as e:
            self.get_logger().error(str(e))

        # Find the nearest point
        min_val, _, min_loc, _ = cv2.minMaxLoc(cv_image)

        self.get_logger().info(
            f'Nearest point is at: {min_loc} with a depth of: {min_val} units')

rclpyの実行

rclpy.init()

サブスクライバの設定

depth_image_subscriber = DepthImageSubscriber()

本体の実行

rclpy.spin(depth_image_subscriber)

後片付け

depth_image_subscriber.destroy_node()
rclpy.shutdown()