How to solve "MemoryError: memory allocation failed, heap is locked"?

I connect LSM9DS1 IMU breakout to I2C(2) of OPENMV3. IMU can be read successfully by codes below :


bus = I2C(2)
lsm = LSM9DS1(bus)

while (True):
print(‘Accelerometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}’.format(*lsm.read_accel()))
print(‘Magnetometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}’.format(*lsm.read_magnet()))
print(‘Gyroscope: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}’.format(*lsm.read_gyro()))
print(“”)
time.sleep_ms(500)


I try to use timer callback to read IMU as below codes, MemoryError occurred, however.


def sensor_read(timer):
print(‘Accelerometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}’.format(*lsm.read_accel()))
print(‘Magnetometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}’.format(*lsm.read_magnet()))
print(‘Gyroscope: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}’.format(*lsm.read_gyro()))
print(" *** *** ***")

bus = I2C(2)
lsm = LSM9DS1(bus)

tim = Timer(2, freq=1) # create a timer object using timer 2 - trigger at 1Hz
tim.callback(sensor_read) # set the callback to read LSM9DS1

while (True):
time.sleep_ms(1000)


How to solve “MemoryError: memory allocation failed, heap is locked” ?

Thanks for your support,

Issue solved by following code -

tim = Timer(2, freq=1) # create a timer object using timer 2 - trigger at 1Hz
micropython.heap_unlock()
tim.callback(sensor_read) # set the callback to read LSM9DS1