lsm6dsox_mlc.py
# 本作品采用MIT许可证授权。
# 版权所有 (c) 2013-2023 OpenMV LLC。保留所有权利。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# LSM6DSOX IMU MLC(机器学习核心)示例。
# 下载原始 UCF 文件,复制到存储并重置。
# 注意:示例的预训练模型(UCF 文件)可以在这里找到:
# https://github.com/STMicroelectronics/STMems_Machine_Learning_Core/tree/master/application_examples/lsm6dsox
from machine import Pin
from machine import SPI
from lsm6dsox import LSM6DSOX
INT_MODE = True # 以中断模式运行。
INT_FLAG = False # 在中断时设置为 True。
def imu_int_handler(pin):
global INT_FLAG
INT_FLAG = True
if INT_MODE is True:
int_pin = Pin("PA1", mode=Pin.IN, pull=Pin.PULL_UP)
int_pin.irq(handler=imu_int_handler, trigger=Pin.IRQ_RISING)
# 振动检测示例
UCF_FILE = "lsm6dsox_vibration_monitoring.ucf"
UCF_LABELS = {0: "no vibration", 1: "low vibration", 2: "high vibration"}
# 注意:选择的数据速率和比例必须与 MLC 数据速率和比例匹配。
lsm = LSM6DSOX(
SPI(5),
cs=Pin("PF6", Pin.OUT_PP, Pin.PULL_UP),
gyro_odr=26,
accel_odr=26,
gyro_scale=2000,
accel_scale=4,
ucf=UCF_FILE,
)
# 头部手势示例
# UCF_FILE = "lsm6dsox_head_gestures.ucf"
# UCF_LABELS = {0:"点头", 1:"摇头", 2:"静止", 3:"摆动", 4:"行走"}
# 注意:选择的数据速率和比例必须与 MLC 数据速率和比例匹配。
# lsm = LSM6DSOX(SPI(5), cs_pin=Pin("PF6", Pin.OUT_PP, Pin.PULL_UP),
# gyro_odr=26, accel_odr=26, gyro_scale=250, accel_scale=2, ucf=UCF_FILE)
print("MLC configured...")
while True:
if INT_MODE:
if INT_FLAG:
INT_FLAG = False
print(UCF_LABELS[lsm.mlc_output()[0]])
else:
buf = lsm.mlc_output()
if buf is not None:
print(UCF_LABELS[buf[0]])