ESP8266 HUZZAH breakout + LSM9DS1 9DOF

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
bergamont
Posts: 3
Joined: Tue Jun 13, 2017 1:43 pm

ESP8266 HUZZAH breakout + LSM9DS1 9DOF

Post by bergamont » Tue Jun 13, 2017 2:28 pm

Hello!
I am quite new to programming and working with microcontrollers. The Firmware on my ESP8266 ist Micropython: esp8266-20170108-v1.8.7.bin
I want to connect my ESP8266 HUZZAH breakout microcontroller with LSM9DS1 9DOF. I have found a library for this sensor. But it was written to work with the PyBoard, I guess.
https://github.com/hoihu/projects/blob/ ... lsm9ds1.py
I tried to make it work with my ESP8266, but I have some issues with it.
So this is what I write into WebREPL.
WebREPL connected
>>> import machine
>>> from lsm9ds1 import LSM9DS1
>>> from machine import Pin, I2C
>>> import array
>>> i2c = I2C(scl=Pin(5), sda=Pin(4))
>>> i2c.start()
>>> lsm9 = LSM9DS1(i2c)
right id's are given
>>> lsm9.read_gyro()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "lsm9ds1.py", line 167, in read_gyro
TypeError: can't convert memoryview to int

I totally don't know how to get rid of this error.
This is what the function looks like:

def read_gyro(self):
"""Returns gyroscope vector in degrees/sec."""
i2c = self.i2c
mv = memoryview(array.array('h',[0,0,0]))
f = self.scale_gyro
self.i2c.readfrom_mem(107, 0x18 | 0x80, mv)
return (mv[0]/f, mv[1]/f, mv[2]/f)

Thank you all in advance!!

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: ESP8266 HUZZAH breakout + LSM9DS1 9DOF

Post by deshipu » Tue Jun 13, 2017 8:16 pm

The third argument to readfrom_mem should be the number of bytes to read, and not the buffer to read them into, as this code seems to assume. I think this code is simply wrong.

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

Re: ESP8266 HUZZAH breakout + LSM9DS1 9DOF

Post by Turbinenreiter » Tue Jun 13, 2017 9:19 pm

Not sure how similar they are, but maybe this helps:
https://github.com/micropython-IMU/micropython-lsm9ds0

bergamont
Posts: 3
Joined: Tue Jun 13, 2017 1:43 pm

Re: ESP8266 HUZZAH breakout + LSM9DS1 9DOF

Post by bergamont » Wed Jun 14, 2017 10:04 am

Thank you for answers!
I solved this problem by using the i2c.readfrom_mem_into function. And I changed self.scratch_int from self.scratch_int = array.array('h',[0,0,0]) to self.scratch_int = bytearray(3) and also self.scratch = array.array('B',[0,0,0,0,0,0]) to self.scratch = bytearray(6). This finally made the functions work. I will share the ESP8266-specified lsm9ds1.py file as soon as I can confirm that it works properly.

User avatar
marfis
Posts: 215
Joined: Fri Oct 31, 2014 10:29 am
Location: Zurich / Switzerland

Re: ESP8266 HUZZAH breakout + LSM9DS1 9DOF

Post by marfis » Wed Jun 14, 2017 12:38 pm

I'm the author of this driver. It was originally designed to work with the pyb module and its I2C interface.

Later I started to port the driver to work with the machine module but unfortunately I couldn't test it because my SenseHat board was broken. It seems that I've overlooked a couple of things.

I'm happy to accept pull requests- it would be nice to test it on the pyboard as well.

bergamont
Posts: 3
Joined: Tue Jun 13, 2017 1:43 pm

Re: ESP8266 HUZZAH breakout + LSM9DS1 9DOF

Post by bergamont » Mon Jun 19, 2017 10:11 am

Hey marfis,
Thanks for your respond!
As soon as I finish my project, I will try to run this driver on the pyboard. Could you tell me how to visualize the data LSM9DS1 shows me ?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: ESP8266 HUZZAH breakout + LSM9DS1 9DOF

Post by pythoncoder » Mon Jun 19, 2017 4:38 pm

A common requirement is to derive heading, pitch and roll values from the three sensors. This is known as sensor fusion and is mathematically fairly involved. This library https://github.com/micropython-IMU/micr ... fusion.git achieves this. It is designed to be device independent but you will need to do some work to interface it to your sensor (it was developed with MPU6050, MPU9150 and MPU9250 devices).

I have run it on the ESP8266 but had to use frozen bytecode because of lack of RAM.

You may like to offer some code in due course ;)
Peter Hinch
Index to my micropython libraries.

User avatar
marfis
Posts: 215
Joined: Fri Oct 31, 2014 10:29 am
Location: Zurich / Switzerland

Re: ESP8266 HUZZAH breakout + LSM9DS1 9DOF

Post by marfis » Mon Jun 19, 2017 6:49 pm

see also here:
viewtopic.php?t=1693

I used @pythoncoders fusion lib to work with the lsm9ds1 sensor chip and made a balancing ball example (video link is also in the link above)

Post Reply