tof — time-of-flight sensor driver¶
The tof module is used for controlling the time-of-flight sensor.
Example usage:
import csi, tof
# Setup camera.
csi0 = csi.CSI()
csi0.reset()
csi0.pixformat(csi.RGB565)
csi0.framesize(csi.QVGA)
csi0.snapshot(time=2000)
tof.init()
# Show image.
while(True):
img = csi0.snapshot()
depth, depth_min, depth_max = tof.read_depth()
tof.draw_depth(image, depth)
print("====================")
print("Min depth in mm seen: %0.2f" % depth_min)
print("Max depth in mm seen: %0.2f" % depth_max)
If you want to rotate the depth array/image by multiples of 90 degrees pass the
following hmirror/vflip/transpose combinations to read_depth,
draw_depth, or snapshot:
vflip=False, hmirror=False, transpose=False-> 0 degree rotation
vflip=True, hmirror=False, transpose=True-> 90 degree rotation
vflip=True, hmirror=True, transpose=False-> 180 degree rotation
vflip=False, hmirror=True, transpose=True-> 270 degree rotation
Functions¶
- tof.init(type: int = -1) None¶
Initializes an onboard depth sensor.
typeindicates the type of TOF sensor:tof.TOF_VL53LX: 8x8 pixels.
By default
typeis-1which causestof.init()to automatically scan and initialize an attached TOF sensor based on the I2C address.
- tof.width() int¶
Returns the width (horizontal resolution) of the depth sensor in-use. Raises a
RuntimeErrorif the sensor is not initialized.
- tof.height() int¶
Returns the height (vertical resolution) of the depth sensor in-use. Raises a
RuntimeErrorif the sensor is not initialized.
- tof.type() int¶
Returns the type of the depth sensor in-use:
Raises a
RuntimeErrorif the sensor is not initialized.
- tof.refresh() int¶
Returns the refresh rate (in Hz) of the depth sensor in-use:
tof.TOF_VL53LX: 15 Hz.
Raises a
RuntimeErrorif the sensor is not initialized.
- tof.read_depth(hmirror: bool = False, vflip: bool = False, transpose: bool = False, timeout: int = 100) Tuple[List[float], float, float]¶
Returns a tuple containing the depth list (
width * heightfloats in mm), the minimum depth seen, and the maximum depth seen.hmirrorifTruehorizontally mirrors the depth array.vflipifTruevertically flips the depth array.transposeifTruetransposes the depth array.timeouthow many milliseconds to wait for the new frame before raising aRuntimeError. If0waits forever.
- 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¶
Draws a
deptharray (as returned byread_depth) ontoimagewhose top-left corner starts at locationx,y.x_scalecontrols how much the displayed image is scaled by in the x direction (float). If this value is negative the image will be flipped horizontally. If unspecified it matchesy_scaleto maintain aspect ratio.y_scalecontrols how much the displayed image is scaled by in the y direction (float). If this value is negative the image will be flipped vertically. If unspecified it matchesx_scaleto maintain aspect ratio.roiis the region-of-interest rectangle tuple(x, y, w, h)of the source depth array to draw.rgb_channelis the RGB channel (0=R, 1=G, 2=B) to extract from the source.-1(default) uses all channels.alphacontrols how much of the source is blended into the destination image.255is opaque,0results in no modification. Range: 0-255.color_paletteis a color palette enum (e.g.image.PALETTE_DEPTH,image.PALETTE_RAINBOW) or a 256-pixel RGB565 image used as a color lookup table on the grayscale depth value.alpha_paletteif notNoneis a 256-pixel GRAYSCALE image used as an alpha lookup table modulatingalphaper pixel.hintis a logical OR ofimageflags such asimage.BILINEAR,image.BICUBIC,image.AREA,image.CENTER,image.HMIRROR,image.VFLIP,image.TRANSPOSE,image.EXTRACT_RGB_CHANNEL_FIRST,image.APPLY_COLOR_PALETTE_FIRST,image.SCALE_ASPECT_KEEP,image.SCALE_ASPECT_EXPAND,image.SCALE_ASPECT_IGNORE,image.ROTATE_90,image.ROTATE_180,image.ROTATE_270.scaleis a two-value tuple(min, max)controlling the min and max depth (in mm) used to scale the depth image. Defaults to the depth array’s actual min and max.Note
read_depthremembers if it was called withtranspose=Trueanddraw_depthuses that internally to size the source array.
- 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¶
Reads a frame from the depth sensor and returns a new
image.Imageobject that is eitherimage.GRAYSCALEorimage.RGB565.hmirrorifTruehorizontally mirrors the new image.vflipifTruevertically flips the new image.transposeifTruetransposes the new image.x_scalecontrols how much the image is scaled in the x direction (float). Negative values flip horizontally. If unspecified it matchesy_scale.y_scalecontrols how much the image is scaled in the y direction (float). Negative values flip vertically. If unspecified it matchesx_scale.roiis the region-of-interest rectangle tuple(x, y, w, h)of the source to extract.rgb_channelis the RGB channel (0=R, 1=G, 2=B) to extract.-1(default) uses all channels.alphacontrols source-to-destination blending.255is opaque,0leaves the destination unchanged. Range: 0-255.color_paletteis a color palette enum (e.g.image.PALETTE_DEPTH) or a 256-pixel RGB565 image used as a color lookup table.alpha_paletteif notNoneis a 256-pixel GRAYSCALE image used as an alpha lookup table.hintis a logical OR ofimagescaling/orientation flags (seedraw_depth).scaleis a two-value tuple(min, max)controlling the min and max depth (in mm) used to scale the image. Defaults to the frame’s actual min/max.pixformatcontrols the final image pixel format. Must beimage.GRAYSCALEorimage.RGB565.copy_to_fbifTruewrites the new image into the frame buffer instead of allocating it on the MicroPython heap.timeouthow many milliseconds to wait for the new frame before raising aRuntimeError. If0waits forever.