用于MPU9250 9轴运动跟踪设备的micropython i2c驱动程序
micropython-mpu9250的Python项目详细描述
micropython mpu-9250(mpu-6500+ak8963)i2c驱动程序
MPU-9250是一种由两个芯片组成的系统封装(SIP):MPU-6500,包含三轴陀螺仪和三轴加速度计和AK8963,这是一个三轴数字罗盘。
用法
简单的循环测试。
importutimefrommachineimportI2C,Pinfrommpu9250importMPU9250i2c=I2C(scl=Pin(22),sda=Pin(21))sensor=MPU9250(i2c)print("MPU9250 id: "+hex(sensor.whoami))whileTrue:print(sensor.acceleration)print(sensor.gyro)print(sensor.magnetic)utime.sleep_ms(1000)
默认情况下,库返回加速度、陀螺仪和磁力仪IE罗盘的x、y、z轴值的三元组。默认单位是m/s^2
、rad/s
和uT
。也可以在g
和gyro值deg/s
中获得加速度值。请参阅下面的示例。请注意,MPU6500和AK8963驱动程序都作为单独的类提供。MPU9250实际上是这两者的组合。
importutimefrommachineimportI2C,Pinfrommpu9250importMPU9250frommpu6500importMPU6500,SF_G,SF_DEG_Si2c=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))whileTrue:print(sensor.acceleration)print(sensor.gyro)print(sensor.magnetic)utime.sleep_ms(1000)
更现实的例子使用计时器。如果在软重新启动后得到OSError: 26
或i2c driver install error
,请执行硬重新启动。
importmicropythonfrommachineimportI2C,Pin,Timerfrommpu9250importMPU9250micropython.alloc_emergency_exception_buf(100)i2c=I2C(scl=Pin(22),sda=Pin(21))sensor=MPU9250(i2c)defread_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)
磁强计校准
对于现实生活中的应用程序,您几乎应该总是calibrate the magnetometer。AK8963驱动程序支持硬铁和软铁校正。校准函数采用两个参数:count
是要采集的样本数,delay
是样本之间的毫秒延迟。
默认值为{{CD10>}和^ {CD11}}校准需要近一分钟。当校准功能正在运行时,传感器应该围绕每个轴旋转多次。
frommachineimportI2C,Pinfrommpu9250importMPU9250fromak8963importAK8963i2c=I2C(scl=Pin(22),sda=Pin(21))ak8963=AK8963(i2c)offset,scale=ak8963.calibrate(count=256,delay=200)sensor=MPU9250(i2c,ak8963=ak8963)
在完成校准之后,calibrate()
方法还返回硬铁offset
和软铁scale
的元组。为了避免每次启动后进行校准,可以在nvram或config文件中对这些值进行strore并将它们传递给ak8963构造函数。下面的示例仅说明如何使用构造函数。
frommachineimportI2C,Pinfrommpu9250importMPU9250fromak8963importAK8963i2c=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 File。