Accel Unit

这是Accel Unit的驱动库,用于从加速度传感器获取数据,并支持运动检测。

支持以下产品:

ACCEL

UiFlow2 应用示例

获取加速度值

在 UiFlow2 上打开 stickcplus2_unit_accel_example.m5f2 项目。

此示例获取加速度单元的加速度值并将其显示在屏幕上。

UiFlow2 代码块:

example.png

示例输出:

MicroPython 应用示例

获取加速度值

此示例获取加速度单元的加速度值并将其显示在屏幕上。

MicroPython 代码块:

 1# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from hardware import *
 9from unit import AccelUnit
10
11
12label0 = None
13label1 = None
14label2 = None
15title0 = None
16label3 = None
17label4 = None
18label5 = None
19i2c0 = None
20accel_0 = None
21
22
23acc = None
24
25
26def setup():
27    global label0, label1, label2, title0, label3, label4, label5, i2c0, accel_0, acc
28
29    M5.begin()
30    label0 = Widgets.Label("x:", 4, 48, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
31    label1 = Widgets.Label("y:", 4, 88, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
32    label2 = Widgets.Label("z:", 4, 128, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
33    title0 = Widgets.Title("ACCEL UNIT", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18)
34    label3 = Widgets.Label("label3", 24, 48, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
35    label4 = Widgets.Label("label4", 24, 88, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
36    label5 = Widgets.Label("label5", 24, 128, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
37
38    i2c0 = I2C(0, scl=Pin(33), sda=Pin(32), freq=100000)
39    accel_0 = AccelUnit(i2c0, 0x53)
40
41
42def loop():
43    global label0, label1, label2, title0, label3, label4, label5, i2c0, accel_0, acc
44    M5.update()
45    acc = accel_0.get_accel()
46    label3.setText(str(acc[0]))
47    label4.setText(str(acc[1]))
48    label5.setText(str(acc[2]))
49
50
51if __name__ == "__main__":
52    try:
53        setup()
54        while True:
55            loop()
56    except (Exception, KeyboardInterrupt) as e:
57        try:
58            from utility import print_error_msg
59
60            print_error_msg(e)
61        except ImportError:
62            print("please update to latest firmware")

示例输出:

API参考

AccelUnit

class unit.accel.AccelUnit(i2c, address=83)

基类:ADXL345

创建一个 AccelUnit 对象。

参数:
  • i2c (I2C) – Accel Unit 所连接的 I2C 总线。

  • address (int) – 设备的 I2C 地址。默认值为 0x53。

UiFlow2 代码块:

init.svg

MicroPython 代码块:

from hardware import I2C
from unit import AccelUnit

acceli2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000)
accel_0 = AccelUnit(i2c0)
disable_freefall_detection()

禁用自由落体检测。

UiFlow2 代码块:

disable_freefall_detection.png

MicroPython 代码块:

accel_0.disable_freefall_detection()
返回类型:

None

disable_motion_detection()

禁用运动检测。

UiFlow2 代码块:

disable_motion_detection.png

MicroPython 代码块:

accel_0.disable_motion_detection()
返回类型:

None

disable_tap_detection()

禁用点击检测。

UiFlow2 代码块:

disable_tap_detection.png

MicroPython 代码块:

accel_0.disable_tap_detection()
返回类型:

None

enable_freefall_detection(*, threshold=10, time=25)

自由落体检测参数:

参数:
  • threshold (int) – 所有轴上的加速度必须低于该值才能记录为下降。比例因子为 62.5 mg/LSB。

  • time (int) – 所有轴上的加速度必须小于“阈值”的时间量才能记录为掉落。比例因子为 5 毫秒/LSB。建议值介于 100 毫秒和 350 毫秒(20 到 70)之间。

返回类型:

None

如果您希望自己设置它们而不是使用默认值,则必须使用关键字参数:

accelerometer.enable_freefall_detection(time=30)

UiFlow2 代码块:

enable_freefall_detection1.png

enable_freefall_detection2.png

MicroPython 代码块:

accel_0.enable_freefall_detection()
enable_motion_detection(*, threshold=18)

活动检测参数。

参数:

threshold (int) – 任何轴上的加速度必须超过该值才能注册为活动值。比例因子为 62.5 mg/LSB。

返回类型:

None

如果您希望自己设置它们而不是使用默认值,则必须使用关键字参数:

accelerometer.enable_motion_detection(threshold=20)

UiFlow2 代码块:

enable_motion_detection1.png

enable_motion_detection2.png

MicroPython 代码块:

accel_0.enable_motion_detection(threshold=18)
enable_tap_detection(*, threshold=20, duration=50, latency=20, window=255)

点击检测参数。

参数:
  • tap_count (int) – 1 仅检测单击,2 仅检测双击。

  • threshold (int) – 点击检测的阈值。比例因子为 62.5 mg/LSB。值越高,检测越不灵敏。

  • duration (int) – 这将限制脉冲持续时间高于“阈值”。任何超过“持续时间”的脉冲都不会被记录为抽头。比例因子为 625 µs/LSB。

  • latency (int) – (仅限双击)初始脉冲低于“阈值”后,启动窗口寻找第二个脉冲的时间长度。比例因子为 1.25 ms/LSB。

  • window (int) – (仅限双击)查找第二次点击的窗口长度。比例因子为 1.25 ms/LSB。

返回类型:

None

如果您希望自己设置它们而不是使用默认值,则必须使用关键字参数:

accelerometer.enable_tap_detection(duration=30, threshold=25)

UiFlow2 代码块:

enable_tap_detection1.png

enable_tap_detection2.png

MicroPython 代码块:

accel_0.enable_tap_detection(tap_count=1, threshold=20, duration=50, latency=20, window=255)
get_accel()

以 3 元组形式返回 x、y、z 加速度值,单位为 \(m / s ^ 2\)

返回:

x、y、z 加速度值,单位为 :math:m / s ^ 2

返回类型:

tuple[float, float, float]

UiFlow2 代码块:

get_accel.svg

MicroPython 代码块:

accel_0.get_accel()
get_data_rate()

获取传感器的数据速率。

返回:

传感器的数据速率。

返回类型:

int

UiFlow2 代码块:

get_data_rate.svg

MicroPython 代码块:

accel_0.get_data_rate()
get_range()

获取传感器的测量范围。

返回:

传感器的测量范围。

返回类型:

int

UiFlow2 代码块:

get_range.svg

MicroPython 代码块:

accel_0.get_range()
is_freefall()

如果检测到自由落体则返回 True。

返回:

如果检测到自由落体则返回 True。

返回类型:

bool

UiFlow2 代码块:

is_freefall.svg

MicroPython 代码块:

accel_0.is_freefall()
is_motion()

如果检测到运动则返回 True。

返回:

如果检测到运动则返回 True。

返回类型:

bool

UiFlow2 代码块:

is_motion.svg

MicroPython 代码块:

accel_0.is_motion()
is_tap()

如果检测到点击则返回 True。

返回:

如果检测到点击则为 True。

返回类型:

bool

UiFlow2 代码块:

is_tap.svg

MicroPython 代码块:

accel_0.is_tap()
set_data_rate(rate)

设置传感器的数据速率。

参数:

rate (int) – 传感器的数据速率。

返回类型:

None

UiFlow2 代码块:

set_data_rate.svg

MicroPython 代码块:

accel_0.set_data_rate(accel_0.RATE_3200_HZ)
set_range(range)

传感器的测量范围。

参数:

range (int) – 传感器的测量范围。

返回类型:

None

UiFlow2 代码块:

set_range.svg

MicroPython 代码块:

accel_0.set_range(accel_0.RANGE_2_G)

ADXL345

class unit.accel.ADXL345(i2c, address=micropython.const)

基类:object

ADXL345 3 轴加速度计的驱动程序。

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

  • address (int) – 传感器的 I2C 设备地址。默认值为 0x53

快速入门:导入和使用设备

以下是使用 ADXL345 类的示例。首先,您需要导入库才能使用传感器:

import machine
import adxl34x

完成后,您可以定义 I2C 对象并定义传感器对象:

i2c = machine.I2C(0)  # uses board default SDA and SCL pins
accelerometer = adxl34x.ADXL343(i2c)

现在你可以访问 acceleration 属性:

acceleration = accelerometer.acceleration
property acceleration: tuple[int, int, int]

以 3 元组形式返回 x、y、z 加速度值,单位为 \(m / s ^ 2\)

property data_rate: int

传感器的数据速率。

disable_freefall_detection()

禁用自由落体检测。

返回类型:

None

disable_motion_detection()

禁用运动检测。

返回类型:

None

disable_tap_detection()

禁用点击检测。

返回类型:

None

enable_freefall_detection(*, threshold=10, time=25)

自由落体检测参数:

参数:
  • threshold (int) – 所有轴上的加速度必须低于该值才能记录为下降。比例因子为 62.5 mg/LSB。

  • time (int) – 所有轴上的加速度必须小于“阈值”的时间量才能记录为掉落。比例因子为 5 毫秒/LSB。建议值介于 100 毫秒和 350 毫秒(20 到 70)之间。

返回类型:

None

如果您希望自己设置它们而不是使用默认值,则必须使用关键字参数:

accelerometer.enable_freefall_detection(time=30)
enable_motion_detection(*, threshold=18)

活动检测参数。

参数:

threshold (int) – 任何轴上的加速度必须超过该值才能注册为活动值。比例因子为 62.5 mg/LSB。

如果您希望自己设置它们而不是使用默认值,则必须使用关键字参数:

accelerometer.enable_motion_detection(threshold=20)
enable_tap_detection(*, tap_count=1, threshold=20, duration=50, latency=20, window=255)

点击检测参数。

参数:
  • tap_count (int) – 1 仅检测单击,2 仅检测双击。

  • threshold (int) – 点击检测的阈值。比例因子为 62.5 mg/LSB。值越高,检测越不灵敏。

  • duration (int) – 这将限制脉冲持续时间高于“阈值”。任何超过“持续时间”的脉冲都不会被记录为抽头。比例因子为 625 µs/LSB。

  • latency (int) – (仅限双击)初始脉冲低于“阈值”后,启动窗口寻找第二个脉冲的时间长度。比例因子为 1.25 ms/LSB。

  • window (int) – (仅限双击)查找第二次点击的窗口长度。比例因子为 1.25 ms/LSB。

如果您希望自己设置它们而不是使用默认值,则必须使用关键字参数:

accelerometer.enable_tap_detection(duration=30, threshold=25)
property events: dict[str, bool]

events 将返回一个字典,其中包含已启用的每个事件类型的键。

可能的关键字有:

关键字

描述

tap

如果最近检测到点击,则为 True。是否寻找单击或双击取决于 enable_tap_detection 的点击参数。

motion

如果传感器检测到的加速度高于使用 enable_motion_detection 设置的阈值,则为真。

freefall

如果传感器处于自由落体状态,则为 True。使用 enable_freefall_detection 启用时设置参数。

property offset: tuple[int, int, int]

x、y、z 偏移量作为原始计数值的元组。

请参阅 offset_calibration 示例以了解用法。

property range: int

传感器的测量范围。

property raw_x: int

原始 x 轴的值。

property raw_y: int

原始 y 轴的值。

property raw_z: int

原始 z 轴的值。