tof --- 飞行时间传感器驱动

tof 模块驱动通过 I2C 连接到 OpenMV Cam 的飞行时间(ToF)测距传感器。每帧为 8x8 区域网格(VL53L5CX / VL53L8CX)返回以毫米为单位的逐像素深度值,可使用 snapshot() 将其渲染为独立的深度图像,或使用 draw_depth() 将其合成到来自 CSI 传感器的可见光帧上,通常通过诸如 image.PALETTE_DEPTH 之类的调色板进行。

用法示例:

import csi
import tof

csi0 = csi.CSI()
csi0.reset()
csi0.pixformat(csi.RGB565)
csi0.framesize(csi.QVGA)

tof.init()

while True:
    img = csi0.snapshot()
    depth, depth_min, depth_max = tof.read_depth()
    tof.draw_depth(img, depth)
    print("====================")
    print("Min depth in mm seen: %0.2f" % depth_min)
    print("Max depth in mm seen: %0.2f" % depth_max)

如果你想将深度数组/图像旋转 90 度的倍数,请将以下 hmirror/vflip/transpose 组合传给 read_depthdraw_depthsnapshot

  • vflip=False, hmirror=False, transpose=False -> 0 度旋转

  • vflip=True,  hmirror=False, transpose=True -> 90 度旋转

  • vflip=True,  hmirror=True,  transpose=False -> 180 度旋转

  • vflip=False, hmirror=True,  transpose=True -> 270 度旋转

函数

tof.init(type: int = -1) None

初始化板载深度传感器。

type 指示 TOF 传感器的类型:

默认情况下 type-1,这会使 tof.init() 根据 I2C 地址自动扫描并初始化已连接的 TOF 传感器。

tof.reset() None

重置深度传感器状态。

tof.deinit() None

反初始化深度传感器,释放资源。

tof.width() int

返回所用深度传感器的宽度(水平分辨率)。如果传感器未初始化,则引发 RuntimeError

tof.height() int

返回所用深度传感器的高度(垂直分辨率)。如果传感器未初始化,则引发 RuntimeError

tof.type() int

返回所用深度传感器的类型:

如果传感器未初始化,则引发 RuntimeError

tof.refresh() int

返回所用深度传感器的刷新率(以 Hz 为单位):

如果传感器未初始化,则引发 RuntimeError

tof.read_depth(hmirror: bool = False, vflip: bool = False, transpose: bool = False, timeout: int = 100) Tuple[List[float], float, float]

返回一个元组,包含深度列表(width * height 个以毫米为单位的浮点数)、所见的最小深度和所见的最大深度。

hmirror 若为 True 则水平镜像深度数组。

vflip 若为 True 则垂直翻转深度数组。

transpose 若为 True 则转置深度数组。

timeout 在引发 RuntimeError 之前等待新帧的毫秒数。若为 0 则永久等待。

tof.draw_depth(image: image.Image, depth: List[float], x: int = 0, y: int = 0, x_scale: float | None = None, y_scale: float | None = None, roi: Tuple[int, int, int, int] | None = None, rgb_channel: int = -1, alpha: int = 255, color_palette: int = image.PALETTE_DEPTH, alpha_palette: int | None = None, hint: int = 0, scale: Tuple[float, float] | None = None) None

depth 数组(由 read_depth 返回)绘制到 image 上,其左上角从位置 xy 开始。

x_scale 控制所显示图像在 x 方向上的缩放比例(浮点数)。如果此值为负,图像将水平翻转。如果未指定,则与 y_scale 匹配以保持纵横比。

y_scale 控制所显示图像在 y 方向上的缩放比例(浮点数)。如果此值为负,图像将垂直翻转。如果未指定,则与 x_scale 匹配以保持纵横比。

roi 是要绘制的源深度数组的感兴趣区域矩形元组 (x, y, w, h)

rgb_channel 是要从源中提取的 RGB 通道(0=R,1=G,2=B)。-1(默认)使用所有通道。

alpha 控制源混合到目标图像中的程度。255 为不透明,0 则不作任何修改。范围:0-255。

color_palette 是一个调色板枚举(例如 image.PALETTE_DEPTHimage.PALETTE_RAINBOW)或一个 256 像素的 RGB565 图像,用作灰度深度值上的颜色查找表。

alpha_palette 若不为 None,则为一个 256 像素的 GRAYSCALE 图像,用作 alpha 查找表,对每个像素调制 alpha

hint 是以下各项的逻辑或:

scale 是一个二元值元组 (min, max),控制用于缩放深度图像的最小和最大深度(以毫米为单位)。默认为深度数组实际的最小值和最大值。

备注

read_depth 会记住它是否以 transpose=True 调用,draw_depth 在内部利用这一点来确定源数组的尺寸。

tof.snapshot(hmirror: bool = False, vflip: bool = False, transpose: bool = False, x_scale: float | None = None, y_scale: float | None = None, roi: Tuple[int, int, int, int] | None = None, rgb_channel: int = -1, alpha: int = 255, color_palette: int = image.PALETTE_DEPTH, alpha_palette: int | None = None, hint: int = 0, scale: Tuple[float, float] | None = None, pixformat: int = image.RGB565, copy_to_fb: bool = False, timeout: int = 100) image.Image

从深度传感器读取一帧,并返回一个新的 image.Image 对象,其格式为 image.GRAYSCALEimage.RGB565

hmirror 若为 True 则水平镜像新图像。

vflip 若为 True 则垂直翻转新图像。

transpose 若为 True 则转置新图像。

x_scale 控制图像在 x 方向上的缩放比例(浮点数)。负值会水平翻转。如果未指定,则与 y_scale 匹配。

y_scale 控制图像在 y 方向上的缩放比例(浮点数)。负值会垂直翻转。如果未指定,则与 x_scale 匹配。

roi 是要提取的源的感兴趣区域矩形元组 (x, y, w, h)

rgb_channel 是要提取的 RGB 通道(0=R,1=G,2=B)。-1(默认)使用所有通道。

alpha 控制源到目标的混合。255 为不透明,0 则使目标保持不变。范围:0-255。

color_palette 是一个调色板枚举(例如 image.PALETTE_DEPTH)或一个 256 像素的 RGB565 图像,用作颜色查找表。

alpha_palette 若不为 None,则为一个 256 像素的 GRAYSCALE 图像,用作 alpha 查找表。

hint 是以下各项的逻辑或:

scale 是一个二元值元组 (min, max),控制用于缩放图像的最小和最大深度(以毫米为单位)。默认为该帧实际的最小/最大值。

pixformat 控制最终图像的像素格式。必须是 image.GRAYSCALEimage.RGB565

copy_to_fb 若为 True,则将新图像写入帧缓冲区,而不是在 MicroPython 堆上分配它。

timeout 在引发 RuntimeError 之前等待新帧的毫秒数。若为 0 则永久等待。

常量

tof.TOF_VL53LX: int

VL53L5CX 或 VL53L8CX TOF 传感器(8x8 像素)。