陀螺仪结合Unity姿态孪生Demo演示 硬件使用:MPU6050,MPU9250。 MCU使用:ESP8266。 自备串口TTL读取工具。 固件选择:MicroPython&NodeMCU。 拷贝Assets\ESP8266Program中对应脚本到MCU内存中运行即可,连接串口后,串口收到消息时,打开Unity运行对应程序即可看到陀螺仪模块的姿态孪生(旋转角度会和陀螺仪保持一致)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
ZGC a8e28bf03e proj Init 3 years ago
..
CHANGELOG.md proj Init 3 years ago
CHANGELOG.md.meta proj Init 3 years ago
LICENSE.txt proj Init 3 years ago
LICENSE.txt.meta proj Init 3 years ago
Makefile proj Init 3 years ago
Makefile.meta proj Init 3 years ago
QueryFunction proj Init 3 years ago
QueryFunction.c proj Init 3 years ago
QueryFunction.c.meta proj Init 3 years ago
QueryFunction.meta proj Init 3 years ago
README.md proj Init 3 years ago
README.md.meta proj Init 3 years ago
ak8963.py proj Init 3 years ago
ak8963.py.meta proj Init 3 years ago
angleQuery.c proj Init 3 years ago
angleQuery.c.meta proj Init 3 years ago
mpu6500.py proj Init 3 years ago
mpu6500.py.meta proj Init 3 years ago
mpu9250.py proj Init 3 years ago
mpu9250.py.meta proj Init 3 years ago
setup.cfg proj Init 3 years ago
setup.cfg.meta proj Init 3 years ago
setup.py proj Init 3 years ago
setup.py.meta proj Init 3 years ago

README.md

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.

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)

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, uT and °C. 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.

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)

More realistic example usage with timer. If you get OSError: 26 or i2c driver install error after soft reboot do a hard reboot.

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)

Magnetometer Calibration

For real life applications you should almost always calibrate the 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.

NOTE! If using MPU9250 you will first need to open the I2C bypass access to AK8963. This is not needed when using a standalone AK8963 sensor.

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)

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.

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)

Gyro Calibration

TODO

License

The MIT License (MIT). Please see License File for more information.