memory allocation error when file size reduces

Questions and discussion about running MicroPython on a micro:bit board.
Target audience: MicroPython users with a micro:bit.
Post Reply
Rendermaniac
Posts: 5
Joined: Sun May 24, 2020 5:58 am

memory allocation error when file size reduces

Post by Rendermaniac » Sun May 24, 2020 6:09 am

Hi

I have been working on a project (to go in a water bottle rocket) to log altitude by plugging a micro:bit into an enviro:bit - which has a BME280 pressure sensor.

I have run into memory allocation errors before, but this is a weird one - the code I have work fine until I started removing some lines and then I get a Memory allocation error. Has anyone seen this before?

This is the code that does run:

Code: Select all

from microbit import display, Image, button_a, sleep, running_time
import os
import math
import bme280

delay = 5000
duration = 30000
max_height = 0

display.show(Image.HAPPY)
bme = bme280.bme280()
p0 = bme.pressure()
sleep(delay)
display.show(Image.YES)

if not button_a.is_pressed():

    p0 = bme.pressure()
    bme.set_qnh(p0)

    n = len(os.listdir())
    data = open("alt{}.csv".format(n-2), "w")
    data.write("time,altitude,pressure,hyposometric,h2\n")

    while True:
        td = running_time() - delay

        if not td % 500:
            h = bme.altitude()
            p = bme.pressure()
            t = bme.temperature()
            h1 = ((math.pow((p0/p), 1.0/5.257) - 1.0) * (t + 273.15)) / 0.0065
            h2 = math.log(p0/p) * ((287.058 * (t + 273.15)) / 9.80665)

            max_height = h2 if h2 > max_height else max_height
            data.write("{},{},{},{},{}\n".format(td, h, p, h1, h2))

        if td > duration:
            break

    data.close()
    display.scroll("{:.2f}".format(max_height), loop=True)

else:
    display.show(Image.NO)
I have also been having trouble getting good results from the enviro:bit - even leaving it on the floor and recording the altitude results in wildly different results. But this is a secondary problem.

thanks

Simon

Rendermaniac
Posts: 5
Joined: Sun May 24, 2020 5:58 am

Re: memory allocation error when file size reduces

Post by Rendermaniac » Sun May 24, 2020 12:58 pm

Actually I think it's something wrong with the driver that I am using:

https://github.com/pimoroni/micropython-envirobit

Simon

shaoziyang
Posts: 326
Joined: Sun Apr 17, 2016 1:55 pm

Re: memory allocation error when file size reduces

Post by shaoziyang » Mon May 25, 2020 1:12 am

microbit's ram size and flash size is small, so if import too many modules or large modules, it will cause memory error.

You may try remove some unused code, use tab instead of leading blanks to save memory.

To improve the calculation accuracy of altitude, it is necessary to sample many times and carry out digital filtering.

Rendermaniac
Posts: 5
Joined: Sun May 24, 2020 5:58 am

Re: memory allocation error when file size reduces

Post by Rendermaniac » Mon May 25, 2020 11:49 am

I think it was actually something to do with the order I called functions from the driver library. Very weird. I also get a memory allocation error if I try to write to a file whose name is ten digits (including extension) or longer - is that a known limitation?

The memory size definitely is very small. I am thinking about upgrading to something like an Adafruit Clue to get around a lot of the limitations.

thanks

Simon

shaoziyang
Posts: 326
Joined: Sun Apr 17, 2016 1:55 pm

Re: memory allocation error when file size reduces

Post by shaoziyang » Tue May 26, 2020 12:53 am

Yes, the loading order will do affect, maxiam loading size is less than half of the free memory, so loading large file first.

Rendermaniac
Posts: 5
Joined: Sun May 24, 2020 5:58 am

Re: memory allocation error when file size reduces

Post by Rendermaniac » Tue May 26, 2020 2:21 am

My current code is here and seems to be working well:

https://github.com/rendermaniac/wbr/blo ... _logger.py

thanks

Simon

Rendermaniac
Posts: 5
Joined: Sun May 24, 2020 5:58 am

Re: memory allocation error when file size reduces

Post by Rendermaniac » Tue May 26, 2020 10:58 am

And now just dividing a global variable by 1000 caused an allocation error! It seems that there are a lot more things than just file size which can cause this. This is very frustrating as I can't work out the exact conditions that cause these problems!

shaoziyang
Posts: 326
Joined: Sun Apr 17, 2016 1:55 pm

Re: memory allocation error when file size reduces

Post by shaoziyang » Wed May 27, 2020 12:43 am

The most problem for microbit is ramsize, so don't run complex task on it.

And you may try to simplify program functions and modules code to save memory.

Post Reply