Accel Unit
这是Accel Unit的驱动库,用于从加速度传感器获取数据,并支持运动检测。
支持以下产品:
UiFlow2 应用示例
获取加速度值
在 UiFlow2 上打开 stickcplus2_unit_accel_example.m5f2 项目。
此示例获取加速度单元的加速度值并将其显示在屏幕上。
UiFlow2 代码块:
示例输出:
无
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 代码块:
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 代码块:
MicroPython 代码块:
accel_0.disable_freefall_detection()
- 返回类型:
None
- disable_motion_detection()
禁用运动检测。
UiFlow2 代码块:
MicroPython 代码块:
accel_0.disable_motion_detection()
- 返回类型:
None
- disable_tap_detection()
禁用点击检测。
UiFlow2 代码块:
MicroPython 代码块:
accel_0.disable_tap_detection()
- 返回类型:
None
- enable_freefall_detection(*, threshold=10, time=25)
自由落体检测参数:
- 参数:
- 返回类型:
None
如果您希望自己设置它们而不是使用默认值,则必须使用关键字参数:
accelerometer.enable_freefall_detection(time=30)
UiFlow2 代码块:
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 代码块:
MicroPython 代码块:
accel_0.enable_motion_detection(threshold=18)
- enable_tap_detection(*, threshold=20, duration=50, latency=20, window=255)
点击检测参数。
- 参数:
- 返回类型:
None
如果您希望自己设置它们而不是使用默认值,则必须使用关键字参数:
accelerometer.enable_tap_detection(duration=30, threshold=25)
UiFlow2 代码块:
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\)。
UiFlow2 代码块:
MicroPython 代码块:
accel_0.get_accel()
- get_data_rate()
获取传感器的数据速率。
- 返回:
传感器的数据速率。
- 返回类型:
UiFlow2 代码块:
MicroPython 代码块:
accel_0.get_data_rate()
- is_freefall()
如果检测到自由落体则返回 True。
- 返回:
如果检测到自由落体则返回 True。
- 返回类型:
UiFlow2 代码块:
MicroPython 代码块:
accel_0.is_freefall()
- is_motion()
如果检测到运动则返回 True。
- 返回:
如果检测到运动则返回 True。
- 返回类型:
- UiFlow2 代码块:
MicroPython 代码块:
accel_0.is_motion()
- is_tap()
如果检测到点击则返回 True。
- 返回:
如果检测到点击则为 True。
- 返回类型:
UiFlow2 代码块:
MicroPython 代码块:
accel_0.is_tap()
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
- disable_freefall_detection()
禁用自由落体检测。
- 返回类型:
None
- disable_motion_detection()
禁用运动检测。
- 返回类型:
None
- disable_tap_detection()
禁用点击检测。
- 返回类型:
None
- enable_freefall_detection(*, threshold=10, time=25)
自由落体检测参数:
- 参数:
- 返回类型:
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)
点击检测参数。
- 参数:
如果您希望自己设置它们而不是使用默认值,则必须使用关键字参数:
accelerometer.enable_tap_detection(duration=30, threshold=25)