adafruit / Adafruit_ICM20X

Arduino library for ICM20X
Other
14 stars 22 forks source link

Re-initialization of the sensor causing heap memory growth #15

Open gmanrc opened 2 years ago

gmanrc commented 2 years ago

I'm using this Adafruit sensor and library for a sensor node development. One of the requirements is for the node to have extensive battery life, and I am optimizing battery life by using watchdog.sleep on the micro-controller, as well as cutting power to the sensors. As a result, I have to re-initialize the sensor every time it wakes up. My problem is that the initialization appears to be cause memory leak, and the heap stack is growing. Eventually, there is no more free memory available and the node crashes. I have other sensors that do not cause loss of free memory due to re-initialization. Is there anything that can be done in the initialization code. My code is based of adafruit_icm20948_test example code provided.

ladyada commented 2 years ago

are you deleting the object? allocated memory is freed there https://github.com/adafruit/Adafruit_ICM20X/blob/master/Adafruit_ICM20X.cpp#L54

caternuson commented 2 years ago

see: https://github.com/adafruit/Adafruit_MS8607/issues/4#issuecomment-1051278756

gmanrc commented 2 years ago

@caternuson & @ladyada thanks for your help on that for the MS8607. Unfortunately, for the icm20948 does not appear to currently have an init() function for re-initializing the sensors. There is reset(); but calling begin() after reset; is still taking up the free memory.

caternuson commented 2 years ago

calling begin() after reset; is still taking up the free memory.

Call begin only once, in setup. Call reset in the loop.

Nothing in reset should be eating memory: https://github.com/adafruit/Adafruit_ICM20X/blob/6ec93fee035892f780176ffa64be3af1912be559/Adafruit_ICM20X.cpp#L150-L166

gmanrc commented 2 years ago

calling begin() after reset; is still taking up the free memory.

Call begin only once, in setup. Call reset in the loop.

Nothing in reset should be eating memory:

https://github.com/adafruit/Adafruit_ICM20X/blob/6ec93fee035892f780176ffa64be3af1912be559/Adafruit_ICM20X.cpp#L150-L166

Thanks for the quick reply @caternuson. Unfortunately, when I call reset() in the loop without begin(), the the data output of the sensors is only zeroes. Again, as with the other sensor, I am turning this sensor on and off each time to economize battery life.

gmanrc commented 2 years ago

I tried both the reset embedded with icm20x.h and the reset in icm20948.h. The first causes the device to hang. The latter causes the sensors to output zeroes.

caternuson commented 2 years ago

The code in _init is likely needed to be called also, to re-establish the basic sensor config. But that's private, so not accessible in user sketch. It's also where the unchecked allocations are occurring: https://github.com/adafruit/Adafruit_ICM20X/blob/6ec93fee035892f780176ffa64be3af1912be559/Adafruit_ICM20X.cpp#L207-L210

So seems like like maybe this is not possible with the current state of the library.

gmanrc commented 2 years ago

Thanks for identifying this @caternuson. I'll monitor this space for updates to the library, as having this all in sensor is great. But I will need to adjust my hardware for now to multiple sensors to capture the same data and be compliant with my battery saving measures.