Micropython学习交流群 学习QQ群:786510434 提供多种固件下载和学习交流。

Micropython-扇贝物联 QQ群:31324057 扇贝物联是一个让你与智能设备沟通更方便的物联网云平台

Micropython学习交流群 学习QQ群:468985481 学习交流ESP8266、ESP32、ESP8285、wifi模块开发交流、物联网。

Micropython老哥俩的IT农场分享QQ群:929132891 为喜欢科创制作的小白们分享一些自制的计算机软硬件免费公益课程,由两位多年从事IT研发的中年大叔发起。

Micropython ESP频道

MicroPython MPU-9250 (MPU-6500 + AK8963) I2C driver


MPU-9250是一款系统级封装(SiP),它结合了两个芯片:MPU-6500,它包含3轴陀螺仪和3轴加速度计,AK8963是一个3轴数字罗盘。

用法
Kevin Wheeler有一个广泛的视频,描述了如何使用这个库。此自述文件中的简短代码片段也可以帮助您入门。首先是具有永无止境循环的简单示例。

import utime
from machine import I2C, Pin
from mpu9250 import MPU9250

i2c = I2C(scl=Pin(22), sda=Pin(21))
sensor = MPU9250(i2c)

print("MPU9250 id: " + hex(sensor.whoami))

while True:
    print(sensor.acceleration)
    print(sensor.gyro)
    print(sensor.magnetic)
    print(sensor.temperature)

    utime.sleep_ms(1000)
默认情况下,库返回加速度,陀螺仪和磁力计(即指南针)的X,Y,Z轴值的3元组。默认单位为 、 和 。还可以获得 加速度值 和 陀螺仪值 。请参阅下面的示例。请注意,MPU6500 和 AK8963 驱动程序都可作为单独的类使用。MPU9250实际上是这两者的复合体。m/s^2rad/suT°Cgdeg/s

import utime
from machine import I2C, Pin
from mpu9250 import MPU9250
from mpu6500 import MPU6500, SF_G, SF_DEG_S

i2c = I2C(scl=Pin(22), sda=Pin(21))
mpu6500 = MPU6500(i2c, accel_sf=SF_G, gyro_sf=SF_DEG_S)
sensor = MPU9250(i2c, mpu6500=mpu6500)

print("MPU9250 id: " + hex(sensor.whoami))

while True:
    print(sensor.acceleration)
    print(sensor.gyro)
    print(sensor.magnetic)
    print(sensor.temperature)

    utime.sleep_ms(1000)
使用计时器的更实际的示例用法。如果获得或后软重启,请执行硬重启。OSError: 26i2c driver install error

import micropython
from machine import I2C, Pin, Timer
from mpu9250 import MPU9250

micropython.alloc_emergency_exception_buf(100)

i2c = I2C(scl=Pin(22), sda=Pin(21))
sensor = MPU9250(i2c)

def read_sensor(timer):
    print(sensor.acceleration)
    print(sensor.gyro)
    print(sensor.magnetic)
    print(sensor.temperature)

print("MPU9250 id: " + hex(sensor.whoami))

timer_0 = Timer(0)
timer_0.init(period=1000, mode=Timer.PERIODIC, callback=read_sensor)
磁力计校准
对于实际应用,您几乎总是应该校准磁力计。AK8963驱动程序支持硬铁和软铁校正。校准功能需要两个参数:是要收集的样本数量,是样本之间的延迟(以毫秒为单位)。countdelay

使用默认值和校准大约需要一分钟。当校准功能运行时,传感器应围绕每个轴旋转多次。256200

注意!如果使用MPU9250,您首先需要打开对AK8963的I2C旁路访问。使用独立的 AK8963 传感器时不需要这样做。

from machine import I2C, Pin
from mpu9250 import MPU9250
from ak8963 import AK8963

i2c = I2C(scl=Pin(22), sda=Pin(21))

dummy = MPU9250(i2c) # this opens the bybass to access to the AK8963
ak8963 = AK8963(i2c)
offset, scale = ak8963.calibrate(count=256, delay=200)

sensor = MPU9250(i2c, ak8963=ak8963)
完成校准后,该方法还会返回硬铁和软铁的元组。为了避免在每次启动后进行校准,最好将这些值存储在NVRAM或配置文件中,并将它们传递给AK8963构造函数。下面的示例仅说明如何使用构造函数。calibrate()offsetscale

from machine import I2C, Pin
from mpu9250 import MPU9250
from ak8963 import AK8963

i2c = I2C(scl=Pin(22), sda=Pin(21))
dummy = MPU9250(i2c) # this opens the bybass to access to the AK8963

ak8963 = AK8963(
    i2c,
    offset=(-136.8931640625, -160.482421875, 59.02880859375),
    scale=(1.18437220840483, 0.923895823933424, 0.931707933618979)
)

sensor = MPU9250(i2c, ak8963=ak8963)

库地址:https://github.com/tuupola/micropython-mpu9250


推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 ESP56.com All Rights Reserved

执行时间: 0.008552074432373 seconds