Skip to content

Commit 31e21b4

Browse files
committed
Tested on Pyboard and ESP8266
1 parent 92c74dc commit 31e21b4

File tree

4 files changed

+26
-28
lines changed

4 files changed

+26
-28
lines changed

README_MPU9150.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ transposing axes. The driver returns vehicle-relative coordinates.
3535
Example:
3636
Example assuming an MPU9150 connected to 'X' I2C interface on the Pyboard:
3737
```python
38-
from mpu1250 import MPU9150
39-
imu = MPU9250('X')
38+
from mpu9150 import MPU9150
39+
imu = MPU9150('X')
4040
print(imu.accel.xyz)
4141
print(imu.gyro.xyz)
4242
print(imu.mag.xyz)

imu.py

+17-21
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Adapted from Sebastian Plamauer's MPU9150 driver:
44
# https://github.com/micropython-IMU/micropython-mpu9150.git
55
# Authors Peter Hinch, Sebastian Plamauer
6-
# V0.1 13th June 2015 Experimental: this code is not yet fully tested
6+
# V0.2 17th May 2017 Platform independent: utime and machine replace pyb
77

88
'''
99
mpu9250 is a micropython module for the InvenSense MPU9250 sensor.
@@ -37,15 +37,15 @@
3737
# At runtime try to continue returning last good data value. We don't want aircraft
3838
# crashing. However if the I2C has crashed we're probably stuffed.
3939

40-
import pyb
40+
from utime import sleep_ms
41+
from machine import I2C
4142
from vector3d import Vector3d
4243

4344

4445
class MPUException(OSError):
4546
'''
4647
Exception for MPU devices
4748
'''
48-
4949
pass
5050

5151

@@ -71,23 +71,18 @@ def __init__(self, side_str, device_addr, transposition, scaling):
7171

7272
self._accel = Vector3d(transposition, scaling, self._accel_callback)
7373
self._gyro = Vector3d(transposition, scaling, self._gyro_callback)
74-
self.buf1 = bytearray([0]*1) # Pre-allocated buffers for reads: allows reads to
75-
self.buf2 = bytearray([0]*2) # be done in interrupt handlers
76-
self.buf3 = bytearray([0]*3)
77-
self.buf6 = bytearray([0]*6)
78-
self.timeout = 10 # I2C tieout mS
79-
80-
tim = pyb.millis() # Ensure PSU and device have settled
81-
if tim < 200:
82-
pyb.delay(200-tim)
83-
if type(side_str) is str:
84-
sst = side_str.upper()
85-
if sst in {'X', 'Y'}:
86-
self._mpu_i2c = pyb.I2C(sst, pyb.I2C.MASTER)
87-
else:
88-
raise ValueError('I2C side must be X or Y')
89-
elif type(side_str) is pyb.I2C:
74+
self.buf1 = bytearray(1) # Pre-allocated buffers for reads: allows reads to
75+
self.buf2 = bytearray(2) # be done in interrupt handlers
76+
self.buf3 = bytearray(3)
77+
self.buf6 = bytearray(6)
78+
79+
sleep_ms(200) # Ensure PSU and device have settled
80+
if isinstance(side_str, str): # Non-pyb targets may use other than X or Y
81+
self._mpu_i2c = I2C(side_str)
82+
elif hasattr(side_str, 'readfrom'): # Soft or hard I2C: issue #3097
9083
self._mpu_i2c = side_str
84+
else:
85+
raise ValueError("Invalid I2C instance")
9186

9287
if device_addr is None:
9388
devices = set(self._mpu_i2c.scan())
@@ -116,14 +111,15 @@ def _read(self, buf, memaddr, addr): # addr = I2C device address, memaddr
116111
'''
117112
Read bytes to pre-allocated buffer Caller traps OSError.
118113
'''
119-
self._mpu_i2c.mem_read(buf, addr, memaddr, timeout=self.timeout)
114+
self._mpu_i2c.readfrom_mem_into(addr, memaddr, buf)
120115

121116
# write to device
122117
def _write(self, data, memaddr, addr):
123118
'''
124119
Perform a memory write. Caller should trap OSError.
125120
'''
126-
self._mpu_i2c.mem_write(data, addr, memaddr, timeout=self.timeout)
121+
self.buf1[0] = data
122+
self._mpu_i2c.writeto_mem(addr, memaddr, self.buf1)
127123

128124
# wake
129125
def wake(self):

mpu9150.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,19 @@
2020
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
THE SOFTWARE.
2222
'''
23+
# 17th May 2017 utime replaces pyb
2324
# 15th June 2015 Now uses subclass of InvenSenseMPU
2425

2526
from imu import InvenSenseMPU, bytes_toint, MPUException
2627
from vector3d import Vector3d
27-
import pyb
28-
28+
from utime import sleep_ms
2929

3030
def default_mag_wait():
3131
'''
3232
delay of 1ms
3333
'''
34-
pyb.delay(1)
34+
sleep_ms(1)
35+
3536

3637

3738
class MPU9150(InvenSenseMPU):

vector3d.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# vector3d.py 3D vector class for use in inertial measurement unit drivers
22
# Authors Peter Hinch, Sebastian Plamauer
33

4+
# V0.7 17th May 2017 pyb replaced with utime
45
# V0.6 18th June 2015
56

67
'''
@@ -23,15 +24,15 @@
2324
THE SOFTWARE.
2425
'''
2526

26-
import pyb
27+
from utime import sleep_ms
2728
from math import sqrt, degrees, acos, atan2
2829

2930

3031
def default_wait():
3132
'''
3233
delay of 50 ms
3334
'''
34-
pyb.delay(50)
35+
sleep_ms(50)
3536

3637

3738
class Vector3d(object):

0 commit comments

Comments
 (0)