Code: Select all
RAM free 38256 alloc 72912
Traceback (most recent call last):
File "main.py", line 260, in <module>
File "uasyncio/core.py", line 1, in run
File "uasyncio/core.py", line 1, in run_until_complete
File "uasyncio/core.py", line 1, in run_until_complete
File "main.py", line 21, in main
MemoryError: memory allocation failed, allocating 4096 bytes
Code: Select all
Chip is ESP32D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 26MHz
MAC: 24:6f:28:97:7a:f4
Uploading stub...
Running stub...
Stub running...
Manufacturer: ef
Device: 4018
Detected flash size: 16MB
Basically it gives me memory allocation error with a lot of free space.
I first considered that there are too much fragmentation inside my heap. I do manual gc all over, but it doesn't fix. I also don't have dynamic alloc (at least is what I think, because I'm not sure what happens inside mqtt_as, ssd1306 and ulab.
I have no problem to share the entire code. Not so sure where is the best spot.
I have no idea at all of what else I can do.
Code: Select all
async def main(client):
gc.collect()
global Va_np, Vb_np, Vc_np
Va_rms, Vb_rms, Vc_rms = None, None, None
Va_THD, Vb_THD, Vc_THD = None, None, None
await client.connect()
while True:
#sleep_ms(2000)
read_ok = await read_serial_values(uart_esp32)
gc.collect()
if read_ok:
# print('1 RAM free {} alloc {}'.format(gc.mem_free(), gc.mem_alloc()))
Va_np = calibra(Va, offset_A, max_A)
Vb_np = calibra(Vb, offset_B, max_B)
Vc_np = calibra(Vc, offset_C, max_C)
Va_rms = rms(Va_np)
Vb_rms = rms(Vb_np)
Vc_rms = rms(Vc_np)
Va_THD = THD(Va_np)
Vb_THD = THD(Vb_np)
Vc_THD = THD(Vc_np)
# print('2 RAM free {} alloc {}'.format(gc.mem_free(), gc.mem_alloc()))
print(Va_rms, Vb_rms, Vc_rms)
drawScreen(oled, Va_rms, Vb_rms, Vc_rms, Va_THD, Vb_THD, Vc_THD)
await publishRMS(client, [Va_rms, Vb_rms, Vc_rms])
await publishTHD(client, [Va_THD, Vb_THD, Vc_THD])
drawStatus(oled)
def calibra(array, offset, max):
x = 179.61 / max
a_np = ( np.array( array ) - offset ) * x
# print('Calibra RAM free {} alloc {}'.format(gc.mem_free(), gc.mem_alloc()))
return a_np
def THD(array):
"""Calculate the THD"""
Y = spy.signal.spectrogram(array)
Vfun = 2*Y[20]/sr
h_max = 25
sum_abs = 0
# # Sum all the amplitudes
h_max = 25 # Max harmonic order
for i_h in np.arange(40,20*(h_max+1),20):
sum_abs += (Y[i_h]/sr)*2
del Y
gc.collect()
return 100 * np.sqrt( sum_abs ) / Vfun