Module for MPU9150

Showroom for MicroPython related hardware projects.
Target audience: Users wanting to show off their project!
iron2414
Posts: 6
Joined: Tue Oct 18, 2016 7:29 pm

Re: Module for MPU9150

Postby iron2414 » Sun Mar 12, 2017 11:58 am

Hey guys, i just got an MPU9150 sensor, and wanted to try out your lib. What i wanna do is simply read the accelerometer often (i would like to do it with a timer, but if you have any other suggestion then its welcome).
I upgraded my WiPy firmware as shown here: http://docs.micropython.org/en/latest/w ... er-the-air to 1.8.2

Downloaded the libary from here: https://github.com/micropython-IMU/micropython-mpu9x50

After that i copied mpu9150.py, vector3d.py and imu.py to the wipy. Opened a telnet to the wipy and tried the following:
http://puu.sh/uFoK8/72753ebf33.png

I can see the sensor, when i do the following:

Code: Select all

from machine import I2C
import os

def test():
   mch = os.uname().machine
   if 'LaunchPad' in mch:
       i2c_pins = ('GP11', 'GP10')
   elif 'WiPy' in mch:
       i2c_pins = ('GP24', 'GP23')
   else:
       raise Exception('Board not supported!')
   i2c = I2C(0, mode=I2C.MASTER, baudrate=400000, pins=i2c_pins)
   addr = i2c.scan()
   print(addr)


the addr is 105, i have no idea what i'm doing wrong, could someone help me out? Even after that the mpu9150 module initialization is not very clear to me either. Its probably my bad tho, since i'm new to python, but i could really appreciate some help! :)

Turbinenreiter
Posts: 255
Joined: Sun May 04, 2014 8:54 am

Re: Module for MPU9150

Postby Turbinenreiter » Sun Mar 12, 2017 4:21 pm

The error is in this line of the code:
https://github.com/micropython-IMU/micropython-mpu9x50/blob/master/mpu9150.py#L187

Code: Select all

scale = 0.3


That's not a decimal, but a float. I vaguely remember the WiPy not having floating point support and the docs confirm that: http://docs.micropython.org/en/latest/wipy/wipy/general.html

So, what you can do is change:

Code: Select all

scale = 0.3                             # 0.3uT/LSB
self._mag._vector[0] = self._mag._ivector[0]*self.mag_correction[0]*scale

to:

Code: Select all

scale = 2
self._mag._vector[0] = self._mag._ivector[0]*self.mag_correction[0]//scale


Same for all other occurrences of floats.

User avatar
pythoncoder
Posts: 1382
Joined: Fri Jul 18, 2014 8:01 am

Re: Module for MPU9150

Postby pythoncoder » Mon Mar 13, 2017 8:10 am

Turbinenreiter wrote:...
So, what you can do is change:

Code: Select all

scale = 0.3                             # 0.3uT/LSB
self._mag._vector[0] = self._mag._ivector[0]*self.mag_correction[0]*scale

to:

Code: Select all

scale = 2
self._mag._vector[0] = self._mag._ivector[0]*self.mag_correction[0]//scale

Scale = 3 is surely closer.

@iron2414 On the more general point, the MPU9x50 library uses floating point extensively. I don't think anyone has adapted it for use on the WiPy. The Vector3d library uses trigonometric functions from the math library. Owing to the WiPy's lack of floating point these are unsupported.

If you are new to Python adapting these libraries may be challenging: it would definitely be easier to use a platform other than the WiPy V1.

If you do decide to adapt the library, it might be best to ignore the Vector3d library. In the MPU9150 class plan to use the get_mag_irq(), get_gyro_irq() and get_accel_irq() methods: these are designed to be called from an interrupt service routine so don't use floating point. Your modified library could then delete all the unused methods which are reliant on FP.

I'll try to offer advice if you go this route, but I have no plans to port the library to the WiPy V1. In my view to make good use of quality IMU sensors a platform offering floating point support is essential.
Peter Hinch

iron2414
Posts: 6
Joined: Tue Oct 18, 2016 7:29 pm

Re: Module for MPU9150

Postby iron2414 » Wed Mar 15, 2017 3:18 pm

Thanks for the tips guys, and yes im new to Python. I tried to remove all the float numbers from the libary, but i still get this error:

Code: Select all

>>> from mpu9150 import MPU9150
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportE


I tried to make my own little code, since i need the accelerometer values only:

Code: Select all

from machine import I2C
import os

def test():
   i2c_pins = ('GP24', 'GP23')
   i2c = I2C(0, mode=I2C.MASTER, baudrate=400000, pins=i2c_pins)
   addr = i2c.scan()
   print(addr)
   while True:
      data = i2c.readfrom_mem(105, 0x3b, 6)
      print(data)


It always writes out "b'\x00\x00\x00\x00\x00\x00'". I assume its not working
instead of i2c.readfrom_mem(105, 0x3b, 6) i tried i2c.readfrom_mem(0x69, 0x3b, 6) too, still nothing good. Any idea how can i make this work? I followed every documentation i found and its still not working.


PS: For now i just want the accelero meter datas, but later on i might wanna port this libary for WiPy, all the help appreciated @pythoncoder

User avatar
pythoncoder
Posts: 1382
Joined: Fri Jul 18, 2014 8:01 am

Re: Module for MPU9150

Postby pythoncoder » Wed Mar 15, 2017 4:22 pm

If you look at imu.py you'll see that you need to wake the device before you can access it.
Peter Hinch


Return to “Hardware Projects”

Who is online

Users browsing this forum: No registered users and 2 guests