Mini ToF-90° Unit

这是Mini ToF-90° Unit的驱动程序库,用于从距离传感器获取数据。

支持以下产品:

ToF90Unit

UiFlow2 示例

获取距离值

在 UiFlow2 中打开 tof90_core2_example.m5f2 项目。

此示例获取 Mini ToF-90° 设备的距离值并将其显示在屏幕上。

UiFlow2 代码块:

example.png

示例输出:

None

MicroPython 示例

获取距离值

此示例获取 Mini ToF-90° 设备的距离值并将其显示在屏幕上。

MicroPython 代码块:

 1# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from hardware import I2C
 9from hardware import Pin
10from unit import ToF90Unit
11
12
13title0 = None
14label0 = None
15label1 = None
16i2c0 = None
17minitof90_0 = None
18
19
20def setup():
21    global title0, label0, label1, i2c0, minitof90_0
22
23    M5.begin()
24    Widgets.fillScreen(0x222222)
25    title0 = Widgets.Title(
26        "Core2 Mini ToF-90  Example", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18
27    )
28    label0 = Widgets.Label("label0", 2, 110, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
29    label1 = Widgets.Label("label1", -85, 149, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
30
31    i2c0 = I2C(0, scl=Pin(33), sda=Pin(32), freq=100000)
32    minitof90_0 = ToF90Unit(i2c0, 0x29)
33    minitof90_0.start_continuous()
34
35
36def loop():
37    global title0, label0, label1, i2c0, minitof90_0
38    M5.update()
39    if minitof90_0.get_data_ready():
40        label0.setText(str((str("Distance:") + str((str((minitof90_0.get_range())) + str("mm"))))))
41
42
43if __name__ == "__main__":
44    try:
45        setup()
46        while True:
47            loop()
48    except (Exception, KeyboardInterrupt) as e:
49        try:
50            from utility import print_error_msg
51
52            print_error_msg(e)
53        except ImportError:
54            print("please update to latest firmware")

示例输出:

None

API

ToF90Unit

class unit.tof.ToF90Unit(i2c, address=41)

基类:VL53L0X

创建一个 VL53L0X 对象。

参数:
  • i2c (I2C) – VL53L0X 连接的 I2C 总线。

  • address (int) – VL53L0X 的 I2C 地址。默认为 0x29。

UiFlow2 代码块:

init.png

MicroPython 代码块:

from unit import ToF90Unit

i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000)
tof_0 = ToF90Unit(i2c0)

VL53L0X

class driver.vl53l0x.VL53L0X(i2c, address=41, io_timeout_ms=0)

基类:object

创建一个 VL53L0X 对象。

参数:
  • i2c (I2C) – VL53L0X 连接的 I2C 总线。

  • address (int) – VL53L0X 的 I2C 地址。默认为 0x29。

  • io_timeout_ms (int) – I/O 操作的超时时间。默认为 0。

MicroPython 代码块:

from driver import VL53L0X

i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000)
vl53l0x_0 = VL53L0X(i2c0)
get_measurement_timing_budget()

获取以微秒为单位的测量定时预算。

返回:

以微秒为单位的测量定时预算。

返回类型:

int

UiFlow2 代码块:

get_measurement_timing_budget.png

MicroPython 代码块:

budget_ms = vl53l0x_0.get_measurement_timing_budget()
set_measurement_timing_budget(budget_us)

以微秒为单位设置测量定时预算。

参数:

budget_us (int) – 以微秒为单位的测量定时预算(范围 20000 - 200000)。

返回类型:

None

UiFlow2 代码块:

get_measurement_timing_budget.png

MicroPython 代码块:

budget_ms = vl53l0x_0.get_measurement_timing_budget()
get_distance()

单次读取传感器前方物体的距离,并以厘米为单位返回。

返回:

以厘米为单位的距离。

返回类型:

float

MicroPython 代码块:

distance = vl53l0x_0.get_distance()
get_range()

单次读取传感器前方物体的距离,并以毫米为单位返回。

返回:

以毫米为单位的距离。

返回类型:

float

UiFlow2 代码块:

get_range.png

MicroPython 代码块:

distance = vl53l0x_0.get_range()
get_data_ready()

获取传感器的数据就绪状态。

返回:

传感器的数据就绪状态。

返回类型:

bool

UiFlow2 代码块:

get_data_ready.png

MicroPython 代码块:

data_ready = vl53l0x_0.get_data_ready()
is_continuous_mode()

获取传感器的连续模式状态。

返回:

传感器的连续模式状态。

返回类型:

bool

UiFlow2 代码块:

is_continuous_mode.png

MicroPython 代码块:

continuous_mode = vl53l0x_0.is_continuous_mode()
start_continuous()

将传感器设置为连续模式。

UiFlow2 代码块:

start_continuous.png

MicroPython 代码块:

vl53l0x_0.start_continuous()
返回类型:

None

stop_continuous()

将传感器设置为单次测距模式。

UiFlow2 代码块:

stop_continuous.png

MicroPython 代码块:

vl53l0x_0.stop_continuous()
返回类型:

None

set_address(new_address=41)

为传感器设置新的 I2C 地址。

参数:

new_address (int) – 将分配给 VL53L0X 传感器的I2C地址

返回类型:

None

UiFlow2 代码块:

set_address.png

MicroPython 代码块:

vl53l0x_0.set_address(0x2A)