Accel Unit
Support the following products:
Micropython Example:
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")
UIFLOW2 Example:
stickcplus2_unit_accel_example.m5f2
class AccelUnit
Constructors
Methods
- AccelUnit.get_accel() tuple[float, float, float]
The x, y, z acceleration values returned in a 3-tuple in \(m / s ^ 2\)
- Returns:
x, y, z acceleration values in \(m / s ^ 2\)
UIFLOW2:
- AccelUnit.enable_motion_detection(*, threshold: int = 18) None
The activity detection parameters.
- Parameters:
threshold (int) – The value that acceleration on any axis must exceed to register as active. The scale factor is 62.5 mg/LSB.
If you wish to set them yourself rather than using the defaults, you must use keyword arguments:
accelerometer.enable_motion_detection(threshold=20)
UIFLOW2:
- AccelUnit.is_tap() bool
Returns True if a tap has been detected.
- Returns:
True if a tap has been detected.
UIFLOW2:
- AccelUnit.is_motion() bool
Returns True if motion has been detected.
- Returns:
True if motion has been detected.
UIFLOW2:
- AccelUnit.is_freefall() bool
Returns True if freefall has been detected.
- Returns:
True if freefall has been detected.
UIFLOW2:
- AccelUnit.enable_freefall_detection(*, threshold: int = 10, time: int = 25) None
Freefall detection parameters:
- Parameters:
threshold (int) – The value that acceleration on all axes must be under to register as dropped. The scale factor is 62.5 mg/LSB.
time (int) – The amount of time that acceleration on all axes must be less than
threshold
to register as dropped. The scale factor is 5 ms/LSB. Values between 100 ms and 350 ms (20 to 70) are recommended.
If you wish to set them yourself rather than using the defaults, you must use keyword arguments:
accelerometer.enable_freefall_detection(time=30)
UIFLOW2:
- AccelUnit.enable_tap_detection(*, tap_count: int = 1, threshold: int = 20, duration: int = 50, latency: int = 20, window: int = 255) None
The tap detection parameters.
- Parameters:
tap_count (int) – 1 to detect only single taps, and 2 to detect only double taps.
threshold (int) – A threshold for the tap detection. The scale factor is 62.5 mg/LSB The higher the value the less sensitive the detection.
duration (int) – This caps the duration of the impulse above
threshold
. Anything aboveduration
won’t register as a tap. The scale factor is 625 µs/LSB.latency (int) – (double tap only) The length of time after the initial impulse falls below
threshold
to start the window looking for a second impulse. The scale factor is 1.25 ms/LSB.window (int) – (double tap only) The length of the window in which to look for a second tap. The scale factor is 1.25 ms/LSB.
If you wish to set them yourself rather than using the defaults, you must use keyword arguments:
accelerometer.enable_tap_detection(duration=30, threshold=25)
UIFLOW2:
- AccelUnit.get_data_rate() int
The data rate of the sensor.
- Returns:
The data rate of the sensor.
UIFLOW2:
Constants
- AccelUnit.RATE_3200_HZ
- AccelUnit.RATE_1600_HZ
- AccelUnit.RATE_800_HZ
- AccelUnit.RATE_400_HZ
- AccelUnit.RATE_200_HZ
- AccelUnit.RATE_100_HZ
- AccelUnit.RATE_50_HZ
- AccelUnit.RATE_25_HZ
- AccelUnit.RATE_12_5_HZ
- AccelUnit.RATE_6_25HZ
- AccelUnit.RATE_3_13_HZ
- AccelUnit.RATE_1_56_HZ
- AccelUnit.RATE_0_78_HZ
- AccelUnit.RATE_0_39_HZ
- AccelUnit.RATE_0_20_HZ
- AccelUnit.RATE_0_10_HZ
The data rate of the sensor.