]>
vault307.fbx.one Git - m5Atom.git/blob - mpu6886.py
2 # MicroPython library for the MPU6886 imu ( M5StickC / ATOM Matrix )
3 # Based on https://github.com/m5stack/M5StickC/blob/master/src/utility/MPU6886.cpp
5 from machine
import I2C
8 MPU6886_ADDRESS
= const(0x68)
9 MPU6886_WHOAMI
= const(0x75)
10 MPU6886_ACCEL_INTEL_CTRL
= const(0x69)
11 MPU6886_SMPLRT_DIV
= const(0x19)
12 MPU6886_INT_PIN_CFG
= const(0x37)
13 MPU6886_INT_ENABLE
= const(0x38)
14 MPU6886_ACCEL_XOUT_H
= const(0x3B)
15 MPU6886_ACCEL_XOUT_L
= const(0x3C)
16 MPU6886_ACCEL_YOUT_H
= const(0x3D)
17 MPU6886_ACCEL_YOUT_L
= const(0x3E)
18 MPU6886_ACCEL_ZOUT_H
= const(0x3F)
19 MPU6886_ACCEL_ZOUT_L
= const(0x40)
21 MPU6886_TEMP_OUT_H
= const(0x41)
22 MPU6886_TEMP_OUT_L
= const(0x42)
24 MPU6886_GYRO_XOUT_H
= const(0x43)
25 MPU6886_GYRO_XOUT_L
= const(0x44)
26 MPU6886_GYRO_YOUT_H
= const(0x45)
27 MPU6886_GYRO_YOUT_L
= const(0x46)
28 MPU6886_GYRO_ZOUT_H
= const(0x47)
29 MPU6886_GYRO_ZOUT_L
= const(0x48)
31 MPU6886_USER_CTRL
= const(0x6A)
32 MPU6886_PWR_MGMT_1
= const(0x6B)
33 MPU6886_PWR_MGMT_2
= const(0x6C)
34 MPU6886_CONFIG
= const(0x1A)
35 MPU6886_GYRO_CONFIG
= const(0x1B)
36 MPU6886_ACCEL_CONFIG
= const(0x1C)
37 MPU6886_ACCEL_CONFIG2
= const(0x1D)
38 MPU6886_FIFO_EN
= const(0x23)
40 #consts for Acceleration & Resolution scale
46 GFS_250DPS
= const(0x00)
47 GFS_500DPS
= const(0x01)
48 GFS_1000DPS
= const(0x02)
49 GFS_2000DPS
= const(0x03)
53 def __init__(self
, i2c
, Gscale
= GFS_2000DPS
, Ascale
= AFS_8G
):
58 self
.setAccelFsr(Ascale
)
59 self
.setGyroFsr(Gscale
)
65 # set I2C reg (1 byte)
66 def setReg(self
, reg
, dat
):
67 self
.i2c
.writeto(MPU6886_ADDRESS
, bytearray([reg
, dat
]))
69 # get I2C reg (1 byte)
70 def getReg(self
, reg
):
71 self
.i2c
.writeto(MPU6886_ADDRESS
, bytearray([reg
]))
72 t
= self
.i2c
.readfrom(MPU6886_ADDRESS
, 1)
76 def getnReg(self
, reg
, n
):
77 self
.i2c
.writeto(MPU6886_ADDRESS
, bytearray([reg
]))
78 t
= self
.i2c
.readfrom(MPU6886_ADDRESS
, n
)
82 tempdata
= self
.getReg(MPU6886_WHOAMI
)
87 self
.setReg(MPU6886_PWR_MGMT_1
, regdata
)
90 self
.setReg(MPU6886_PWR_MGMT_1
, regdata
)
93 self
.setReg(MPU6886_PWR_MGMT_1
, regdata
)
96 self
.setReg(MPU6886_ACCEL_CONFIG
, regdata
)
99 self
.setReg(MPU6886_GYRO_CONFIG
, regdata
)
102 self
.setReg(MPU6886_CONFIG
, regdata
)
105 self
.setReg(MPU6886_SMPLRT_DIV
, regdata
)
108 self
.setReg(MPU6886_INT_ENABLE
, regdata
)
111 self
.setReg(MPU6886_ACCEL_CONFIG2
, regdata
)
114 self
.setReg(MPU6886_USER_CTRL
, regdata
)
117 self
.setReg(MPU6886_FIFO_EN
, regdata
)
120 self
.setReg(MPU6886_INT_PIN_CFG
, regdata
)
123 self
.setReg(MPU6886_INT_ENABLE
, regdata
)
130 if self
.Gscale
== GFS_250DPS
:
131 self
.gRes
= 250.0 / 32768.0
132 elif self
.Gscale
== GFS_500DPS
:
133 self
.gRes
= 500.0/32768.0
134 elif self
.Gscale
== GFS_1000DPS
:
135 self
.gRes
= 1000.0/32768.0
136 elif self
.Gscale
== GFS_2000DPS
:
137 self
.gRes
= 2000.0/32768.0
139 self
.gRes
= 250.0/32768.0
142 if self
.Ascale
== AFS_2G
:
143 self
.aRes
= 2.0/32768.0
144 elif self
.Ascale
== AFS_4G
:
145 self
.aRes
= 4.0/32768.0
146 elif self
.Ascale
== AFS_8G
:
147 self
.aRes
= 8.0/32768.0
148 elif self
.Ascale
== AFS_16G
:
149 self
.aRes
= 16.0/32768.0
151 self
.aRes
= 2.0/32768.0
153 def getAccelAdc(self
):
154 buf
= self
.getnReg(MPU6886_ACCEL_XOUT_H
,6)
156 ax
= (buf
[0]<<8) | buf
[1]
157 ay
= (buf
[2]<<8) | buf
[3]
158 az
= (buf
[4]<<8) | buf
[5]
161 def getAccelData(self
):
162 ax
,ay
,az
= self
.getAccelAdc()
174 def getGyroAdc(self
):
175 buf
= self
.getnReg(MPU6886_GYRO_XOUT_H
,6)
176 gx
= (buf
[0]<<8) | buf
[1]
177 gy
= (buf
[2]<<8) | buf
[3]
178 gz
= (buf
[4]<<8) | buf
[5]
181 def getGyroData(self
):
182 gx
,gy
,gz
= self
.getGyroAdc()
194 def getTempAdc(self
):
195 buf
= self
.getnReg(MPU6886_TEMP_OUT_H
,2)
196 return (buf
[0]<<8) | buf
[1]
198 def getTempData(self
):
199 return self
.getTempAdc() / 326.8 + 25.0
201 def setGyroFsr(self
,scale
):
203 self
.setReg(MPU6886_GYRO_CONFIG
, regdata
)
208 def setAccelFsr(self
,scale
):
210 self
.setReg(MPU6886_ACCEL_CONFIG
, regdata
)