DoF6 Unit
此库是 Unit DoF6 的驱动程序。
支持以下产品:
UiFlow2 应用示例
读取姿态
在 UiFlow2 中打开 cores3_unit_dof6_example.m5f2 项目。
此示例演示如何读取并显示偏航角、俯仰角和滚转角。
UiFlow2 代码块:
示例输出:
无
MicroPython 应用示例
读取姿态
此示例演示如何读取并显示偏航角、俯仰角和滚转角。
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 Pin 9from hardware import I2C 10from unit import DoF6Unit 11import time 12 13 14title = None 15live = None 16yaw_caption = None 17pitch_caption = None 18roll_caption = None 19label_yaw = None 20label_pitch = None 21label_roll = None 22orientation_caption = None 23degree_unit = None 24sensor_caption = None 25i2c0 = None 26dof6_0 = None 27 28 29last_time = None 30UPDATE_DURATION_MS = None 31attitude = None 32 33 34def setup(): 35 global \ 36 title, \ 37 live, \ 38 yaw_caption, \ 39 pitch_caption, \ 40 roll_caption, \ 41 label_yaw, \ 42 label_pitch, \ 43 label_roll, \ 44 orientation_caption, \ 45 degree_unit, \ 46 sensor_caption, \ 47 i2c0, \ 48 dof6_0, \ 49 last_time, \ 50 UPDATE_DURATION_MS, \ 51 attitude 52 53 M5.begin() 54 Widgets.setRotation(0) 55 Widgets.fillScreen(0x10171D) 56 title = Widgets.Label( 57 "DoF6 / Attitude", 12, 8, 1.0, 0x4CD7D0, 0x10171D, Widgets.FONTS.Montserrat18 58 ) 59 live = Widgets.Label("Live", 272, 11, 1.0, 0x63D08C, 0x10171D, Widgets.FONTS.Montserrat14) 60 yaw_caption = Widgets.Label("Yaw", 12, 60, 1.0, 0x8FA1AD, 0x10171D, Widgets.FONTS.Montserrat14) 61 pitch_caption = Widgets.Label( 62 "Pitch", 113, 60, 1.0, 0x8FA1AD, 0x10171D, Widgets.FONTS.Montserrat14 63 ) 64 roll_caption = Widgets.Label( 65 "Roll", 212, 60, 1.0, 0x8FA1AD, 0x10171D, Widgets.FONTS.Montserrat14 66 ) 67 label_yaw = Widgets.Label("---.-", 12, 84, 1.0, 0xF2F6F8, 0x10171D, Widgets.FONTS.Montserrat24) 68 label_pitch = Widgets.Label( 69 "---.-", 112, 84, 1.0, 0xF2F6F8, 0x10171D, Widgets.FONTS.Montserrat24 70 ) 71 label_roll = Widgets.Label( 72 "---.-", 212, 84, 1.0, 0xF2F6F8, 0x10171D, Widgets.FONTS.Montserrat24 73 ) 74 orientation_caption = Widgets.Label( 75 "FUSED ORIENTATION", 12, 151, 1.0, 0x8FA1AD, 0x10171D, Widgets.FONTS.Montserrat14 76 ) 77 degree_unit = Widgets.Label( 78 "DEG", 212, 151, 1.0, 0xFFB454, 0x10171D, Widgets.FONTS.Montserrat14 79 ) 80 sensor_caption = Widgets.Label( 81 "BMI270 / 6-AXIS", 12, 207, 1.0, 0x63A8FF, 0x10171D, Widgets.FONTS.Montserrat14 82 ) 83 84 i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=400000) 85 dof6_0 = DoF6Unit(i2c0, addr=0x68) 86 UPDATE_DURATION_MS = 20 87 last_time = time.ticks_ms() 88 89 90def loop(): 91 global \ 92 title, \ 93 live, \ 94 yaw_caption, \ 95 pitch_caption, \ 96 roll_caption, \ 97 label_yaw, \ 98 label_pitch, \ 99 label_roll, \ 100 orientation_caption, \ 101 degree_unit, \ 102 sensor_caption, \ 103 i2c0, \ 104 dof6_0, \ 105 last_time, \ 106 UPDATE_DURATION_MS, \ 107 attitude 108 M5.update() 109 if (time.ticks_diff((time.ticks_ms()), last_time)) >= UPDATE_DURATION_MS: 110 last_time = time.ticks_ms() 111 attitude = dof6_0.get_attitude() 112 label_yaw.setText(str(round(attitude[0], 1))) 113 label_pitch.setText(str(round(attitude[1], 1))) 114 label_roll.setText(str(round(attitude[2], 1))) 115 116 117if __name__ == "__main__": 118 try: 119 setup() 120 while True: 121 loop() 122 except (Exception, KeyboardInterrupt) as e: 123 try: 124 from utility import print_error_msg 125 126 print_error_msg(e) 127 except ImportError: 128 print("please update to latest firmware")
示例输出:
无
API
DoF6Unit
构造函数
- class DoF6Unit(i2c, addr=0x68)
在主机 I2C 总线上创建 DoF6Unit。BMI270 地址可选
0x68或0x69,默认为0x68。- 参数:
- 抛出:
TypeError – addr 必须为整数。
ValueError – addr 必须为 0x68 或 0x69。
UnitError – 所选地址未检测到 BMI270。
OSError – 传感器初始化或 I2C 通信失败。
UiFlow2 代码块:

MicroPython 代码块:
dof = DoF6Unit(i2c) # Default BMI270 address: 0x68 # Use this instead when the BMI270 is configured at 0x69: # dof = DoF6Unit(i2c, addr=0x69)
方法
- get_accel()
返回映射后的加速度
(x, y, z),单位为m/s2。满量程由set_accel_range()选择。UiFlow2 代码块:

- get_accel_raw()
返回范围为
-32768到32767的有符号 16 位 BMI270 计数值。UiFlow2 代码块:

- get_gyro()
返回经过映射和偏移校正的角速度
(x, y, z),单位为rad/s。UiFlow2 代码块:

- get_gyro_raw()
返回范围为
-32768到32767的有符号 16 位 BMI270 陀螺仪计数值。UiFlow2 代码块:

- get_temperature()
返回 BMI270 温度,单位为摄氏度。
- 返回:
IMU 温度。
- 返回类型:
UiFlow2 代码块:

- get_attitude()
采样 BMI270 并返回融合后的
(yaw, pitch, roll),单位为度。UiFlow2 代码块:

- set_accel_range(accel_scale)
将满量程设置为
ACCEL_RANGE_2G、ACCEL_RANGE_4G、ACCEL_RANGE_8G或ACCEL_RANGE_16G。- 参数:
accel_scale (int) – 满量程范围,单位为 g。
- 抛出:
ValueError – 如果量程不受支持。
UiFlow2 代码块:

- set_gyro_range(gyro_scale)
将满量程设置为
GYRO_RANGE_125DPS、GYRO_RANGE_250DPS、GYRO_RANGE_500DPS、GYRO_RANGE_1000DPS或GYRO_RANGE_2000DPS。- 参数:
gyro_scale (int) – 满量程范围,单位为 deg/s。
- 抛出:
ValueError – 如果量程不受支持。
UiFlow2 代码块:

- set_accel_gyro_odr(accel_odr, gyro_odr)
设置 BMI270 输出数据率。
ACCEL_ODR_VALUES为(0.78, 1.5, 3.1, 6.25, 12.5, 25, 50, 100, 200, 400, 800, 1600)Hz;GYRO_ODR_VALUES为(25, 50, 100, 200, 400, 800, 1600, 3200)Hz。- 参数:
- 抛出:
ValueError – 如果输出数据率不受支持。
UiFlow2 代码块:

- set_gyro_offsets(x, y, z)
设置映射后的陀螺仪偏移量,单位为
rad/s。UiFlow2 代码块:

- calibrate_gyro(samples=32)
设备静止时估算偏移量,成功时返回
True。UiFlow2 代码块:

- set_fusion_time_constant(seconds)
设置非负的互补滤波器时间常数,单位为秒。
- 参数:
seconds (float) – 非负滤波器时间常数。
- 抛出:
ValueError – 如果 seconds 为负数。
UiFlow2 代码块:

- reset_fusion()
重置姿态滤波器;下一次姿态采样时会重新初始化。
UiFlow2 代码块:

- set_axis_mapping(sensor, x_axis, y_axis, z_axis)
映射
accelerometer或gyroscope轴。1/-1、2/-2和3/-3分别选择带符号的源 X/Y/Z 轴;源轴不能重复。- 参数:
- 抛出:
ValueError – 如果传感器名称或轴映射无效。
UiFlow2 代码块:


