-
Notifications
You must be signed in to change notification settings - Fork 2
/
mpu9250.py
100 lines (85 loc) · 3.27 KB
/
mpu9250.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Copyright (c) 2018-2020 Mika Tuupola
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copied of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# https://github.com/tuupola/micropython-mpu9250
"""
MicroPython I2C driver for MPU9250 9-axis motion tracking device
"""
# pylint: disable=import-error
from micropython import const
from mpu6500 import MPU6500
from ak8963 import AK8963
# pylint: enable=import-error
__version__ = "0.3.0"
# Used for enabling and disabling the I2C bypass access
_INT_PIN_CFG = const(0x37)
_I2C_BYPASS_MASK = const(0b00000010)
_I2C_BYPASS_EN = const(0b00000010)
_I2C_BYPASS_DIS = const(0b00000000)
class MPU9250:
"""Class which provides interface to MPU9250 9-axis motion tracking device."""
def __init__(self, i2c, mpu6500 = None, ak8963 = None):
if mpu6500 is None:
self.mpu6500 = MPU6500(i2c)
else:
self.mpu6500 = mpu6500
# Enable I2C bypass to access AK8963 directly.
char = self.mpu6500._register_char(_INT_PIN_CFG)
char &= ~_I2C_BYPASS_MASK # clear I2C bits
char |= _I2C_BYPASS_EN
self.mpu6500._register_char(_INT_PIN_CFG, char)
if ak8963 is None:
self.ak8963 = AK8963(i2c)
else:
self.ak8963 = ak8963
@property
def acceleration(self):
"""
Acceleration measured by the sensor. By default will return a
3-tuple of X, Y, Z axis values in m/s^2 as floats. To get values in g
pass `accel_fs=SF_G` parameter to the MPU6500 constructor.
"""
return self.mpu6500.acceleration
@property
def gyro(self):
"""
Gyro measured by the sensor. By default will return a 3-tuple of
X, Y, Z axis values in rad/s as floats. To get values in deg/s pass
`gyro_sf=SF_DEG_S` parameter to the MPU6500 constructor.
"""
return self.mpu6500.gyro
@property
def temperature(self):
"""
Die temperature in celcius as a float.
"""
return self.mpu6500.temperature
@property
def magnetic(self):
"""
X, Y, Z axis micro-Tesla (uT) as floats.
"""
return self.ak8963.magnetic
@property
def whoami(self):
return self.mpu6500.whoami
def __enter__(self):
return self
def __exit__(self, exception_type, exception_value, traceback):
pass