# MicroPython MPU-9250 (MPU-6500 + AK8963) I2C driver
MPU-9250 is a System in Package (SiP) which combines two chips: MPU-6500 which contains 3-axis gyroscope and 3-axis accelerometer and an AK8963 which is a 3-axis digital compass.
## Usage
Simple test with never ending loop.
```python
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)
utime.sleep_ms(1000)
```
By default the library returns 3-tuple of X, Y, Z axis values for either acceleration, gyroscope and magnetometer ie compass. Default units are `m/s^2`, `rad/s` and `uT`. It is possible to also get acceleration values in `g` and gyro values `deg/s`. See the example below. Note that both the MPU6500 and the AK8963 drivers are available as separate classes. MPU9250 is actually a composite of those two.
```python
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)
utime.sleep_ms(1000)
```
More realistic example usage with timer. If you get `OSError: 26` or `i2c driver install error` after soft reboot do a hard reboot.
```python
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("MPU9250 id: " + hex(sensor.whoami))
timer_0 = Timer(0)
timer_0.init(period=1000, mode=Timer.PERIODIC, callback=read_sensor)
```
## Magnetometer Calibration
For real life applications you should almost always [calibrate the magnetometer](https://appelsiini.net/2018/calibrate-magnetometer/). The AK8963 driver supports both hard and soft iron correction. Calibration function takes two parameters: `count` is the number of samples to collect and `delay` is the delay in millisecods between the samples.
With the default values of `256` and `200` calibration takes aproximately one minute. While calibration function is running the sensor should be rotated multiple times around each axis.
```python
from machine import I2C, Pin
from mpu9250 import MPU9250
from ak8963 import AK8963
i2c = I2C(scl=Pin(22), sda=Pin(21))
ak8963 = AK8963(i2c)
offset, scale = ak8963.calibrate(count=256, delay=200)
sensor = MPU9250(i2c, ak8963=ak8963)
```
After finishing calibration the `calibrate()` method also returns tuples for both hard iron `offset` and soft iron `scale`. To avoid calibrating after each startup it would make sense to strore these values in NVRAM or config file and pass them to the AK8963 constructor. Below example only illustrates how to use the constructor.
```python
from machine import I2C, Pin
from mpu9250 import MPU9250
from ak8963 import AK8963
i2c = I2C(scl=Pin(22), sda=Pin(21))
ak8963 = AK8963(
i2c,
offset=(-136.8931640625, -160.482421875, 59.02880859375),
scale=(1.18437220840483, 0.923895823933424, 0.931707933618979)
)
sensor = MPU9250(i2c, ak8963=ak8963)
```
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
没有合适的资源?快使用搜索试试~ 我知道了~
micropython mpu9250 (MPU6500+AK8963)库
需积分: 50 22 下载量 185 浏览量
2018-11-22
23:35:35
上传
评论 2
收藏 6KB GZ 举报
温馨提示
共11个文件
py:4个
txt:3个
pkg-info:2个
MPU-9250 is a System in Package (SiP) which combines two chips: MPU-6500 which contains 3-axis gyroscope and 3-axis accelerometer and an AK8963 which is a 3-axis digital compass.
资源推荐
资源详情
资源评论
收起资源包目录
micropython-mpu9250-0.2.0.tar.gz (11个子文件)
micropython-mpu9250-0.2.0
PKG-INFO 5KB
mpu6500.py 5KB
mpu9250.py 2KB
setup.cfg 79B
ak8963.py 5KB
setup.py 1KB
README.md 3KB
micropython_mpu9250.egg-info
PKG-INFO 5KB
SOURCES.txt 232B
top_level.txt 23B
dependency_links.txt 1B
共 11 条
- 1
资源评论
USART0
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功