0

MPU6050 memory issue on Feather M0 Express
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

MPU6050 memory issue on Feather M0 Express

by jakal14 on Sun Jan 26, 2020 2:42 am

Hoping to get some help here. I've got a Feather M0 Express running CircuitPython 5.0.0 Beta 4. I want to display the data from a MPU6050 6-axis Accel/Gyro onto a Featherwing TFT display. I can get the MPU6050 to work and stream data to the serial window in Mu, and I can get the TFT display to work and show "hello world" text. The issue that I'm running into is that when I load the adafruit_mpu6050 library, it sucks up almost all of the memory, and I don't have enough memory to initialize the TFT display.

Code example:
Code: Select all | TOGGLE FULL SIZE
import gc
gc.mem_free()
import board
import busio
import displayio
gc.mem_free()
import adafruit_mpu6050
gc.mem_free()


Run from REPL gives:
Adafruit CircuitPython 5.0.0-beta.4 on 2020-01-22; Adafruit Feather M0 Express with samd21g18
>>> import gc
>>> gc.mem_free()
20720
>>> import board
>>> import busio
>>> import displayio
>>> gc.mem_free()
20320
>>> import adafruit_mpu6050
>>> gc.mem_free()
5808


I've tried this with Version 4, 5 Beta 3, and 5 Beta 4 with the same results. Searching for most of the evening has just returned standard advice for memory allocation errors: minimizing file size and using .mpy (all libraries are part of the standard bundle and are already .mpy).

So my questions are:
1) Does this kind of memory usage from the MPU6050 library seem right - i.e., is it normal behavior for the MPU library (or other i2c devices)?
2) Is there a workaround for this? If all I can do with the MPU on my M0 is stream data to the serial window, it's a pretty limited setup. Perhaps I need a M4?

Any help here would be appreciated.

Thanks!!

jakal14
 
Posts: 2
Joined: Wed Jan 15, 2020 1:44 am

Re: MPU6050 memory issue on Feather M0 Express

by tannewt on Mon Jan 27, 2020 1:38 am

jakal14 wrote:So my questions are:
1) Does this kind of memory usage from the MPU6050 library seem right - i.e., is it normal behavior for the MPU library (or other i2c devices)?


It doesn't surprise me. There are likely optimizations that can be done to reduce the size. For example, the _ variables at the top could have their value wrapped in `const()`, then their use will be replaced with the value. Right now, the variable names are all being stored in RAM.

In general, we should have an automated test for libraries that measures their size on load. Once we are aware of the size, then we can optimize it.

jakal14 wrote:2) Is there a workaround for this? If all I can do with the MPU on my M0 is stream data to the serial window, it's a pretty limited setup. Perhaps I need a M4?


Easiest thing is to remove the parts of the library you won't need. Right now, on import, all the code for all of the different things people may want to do is loaded into RAM.

In the end, having more RAM will allow you to create larger projects. That is the other easy option, get more RAM. :-)

tannewt
 
Posts: 1490
Joined: Thu Oct 06, 2016 8:48 pm

Re: MPU6050 memory issue on Feather M0 Express

by jakal14 on Wed Feb 05, 2020 1:55 am

That makes sense now that I've had a chance to work with the M0 Express and get to know CircuitPython better. I rewrote the MPU6050 package to wrap the variables in const() functions and strip out all the functions/properties I didn't need. It was still a bit hefty, though, and it seemed mostly to do with the i2c. In the end, though, I picked up a M4 so I'd have plenty of room. The M0 is going to start its new life as a GPS logger for work. Thanks!

jakal14
 
Posts: 2
Joined: Wed Jan 15, 2020 1:44 am

Please be positive and constructive with your questions and comments.