Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

Memory allocation issues when introducing MotorKit?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Memory allocation issues when introducing MotorKit?

by burrito_poots on Mon Aug 19, 2019 11:59 am

Hi all,

Can anyone help me figure out my issue here? Hitting a memory allocation issue on my M0 Express. I'm trying to run this seemingly small piece of code:

Code: Select all | TOGGLE FULL SIZE
import time
from adafruit_motorkit import MotorKit
import board
import busio
import digitalio
import adafruit_ssd1306
import rotaryio

i2c = busio.I2C(board.SCL, board.SDA)
display = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c)

encoder = rotaryio.IncrementalEncoder(board.D10, board.D11)

button_d = digitalio.DigitalInOut(board.D12)
button_d.direction = digitalio.Direction.INPUT
button_d.pull = digitalio.Pull.UP
button_a = digitalio.DigitalInOut(board.D9)
button_a.pull = digitalio.Pull.UP
button_b = digitalio.DigitalInOut(board.D6)
button_b.pull = digitalio.Pull.UP
button_c = digitalio.DigitalInOut(board.D5)
button_c.pull = digitalio.Pull.UP

prox_sensor = digitalio.DigitalInOut(board.D2)  #reads false/low value when cans in front of it

kit = MotorKit()

while True:
    if prox_sensor.value: #prox_sensor reads true/high value, meaning no cans in front of it
        print("starting cycle")
        kit.motor1.throttle = 1.0 #push cans down chute
        print("pushing cans down chute")
        time.sleep(4)
        kit.motor1.throttle = 0 #turning off chute push piston
        print("retracting piston")
        kit.motor2.throttle = 1.0 #dropping fill arms
        print("dropping filling valves")
        kit.motor3.throttle = 1.0 #starting c02 purge
        print("starting purge")
        time.sleep(.75) #really the only spot where monotonic is useful so we do fill/purge together.
        kit.motor3.throttle = 0 #ending c02 purge
        print("ending purge")
        kit.motor4.throttle = 1.0 #starting filling
        print("starting fill")
        time.sleep(timing_value)
        kit.motor4.throttle = 0 #ending filling
        print("ending fill")
        kit.motor2.throttle = 0 #lifting cans
        print("lifting filling valves")
        #should I add a time sleep here in case someone pulls cans early before
        #this block finishes or will it automatically wait for that????
    else: #prox_sensor is reading false/low, meaning cans are in front of it, needs to wait for them to be pulled
        print("cycle end, pull filled cans")



My error message I'm getting is:
"Traceback (most recent call last):
File "main.py", line 6, in <module>
File "adafruit_ssd1306.py", line 38, in <module>
MemoryError: memory allocation failed, allocating 197 bytes"

My lib folder's info according to my Mac: "31,784 bytes (57 KB on disk) for 17 items"

I literally just tried to add the MotorKit library and what it kept saying was needed: adafruit_motorkit.mpy, adafruit_pca9685.mpy, and adafruit_register folder and then I hit this error. I only have those + adafruit_bus_device folder, adafruit_framebuf.mpy, and adafruit_ssd1306.mpy, in my lib folder. I'm stumped, as I assumed this wouldn't hit my memory limit this soon, and previously my code files seemed much longer they just didn't have any MotorKit stuff added into the mix. Any help here would be sincerely appreciated!

burrito_poots
 
Posts: 72
Joined: Thu May 30, 2019 5:02 pm

Re: Memory allocation issues when introducing MotorKit?

by siddacious on Mon Aug 19, 2019 9:48 pm

The M0 Express does have a limited amount of memory (RAM) so it's not surprising that you might get an error like this when trying to import many libraries together. That said I'll forward this to someone who can do a better job than me of seeing if there is something you can change to improve the memory usage.

siddacious
 
Posts: 275
Joined: Fri Apr 21, 2017 3:09 pm

Re: Memory allocation issues when introducing MotorKit?

by siddacious on Mon Aug 19, 2019 10:32 pm

I just checked and apparently the ssd1306 library is particularly large so it won't fit with the others. If you need both, you'll want to upgrade to an M4 board such as the ItsyBitsy M4 Express:

https://www.adafruit.com/product/3800

or the Feather M4 Express when it's back in stock:

https://www.adafruit.com/product/3857

siddacious
 
Posts: 275
Joined: Fri Apr 21, 2017 3:09 pm

Re: Memory allocation issues when introducing MotorKit?

by tannewt on Mon Aug 19, 2019 10:59 pm

You could also try the displayio SSD1306 driver instead: https://github.com/adafruit/Adafruit_Ci ... IO_SSD1306

It needs the latest build from S3 but should work.

It should take less memory than the framebuf option.

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

Re: Memory allocation issues when introducing MotorKit?

by zero_ohm on Sat Jul 18, 2020 1:37 pm

In trying to get ssd1306 running on itsybitsy m4 this line

# Create the SSD1306 OLED class.
display = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3c)

gives me this error

TypeError: function does not take keyword arguments

zero_ohm
 
Posts: 18
Joined: Wed Nov 06, 2013 12:31 pm

Please be positive and constructive with your questions and comments.