melexis / mlx90640-library

MLX90640 library functions
Apache License 2.0
241 stars 192 forks source link

I can't get the MLX90640 to operate above ~4 Hz. #100

Open davepruitt opened 2 years ago

davepruitt commented 2 years ago

I am using an MLX90640 with a SAMD21 microcontroller. The schematic of my board is similar to the Arduino Zero, just as an FYI. The SAMD21 is clocked at 48 MHz, and I tried using the MLX90640 with I2C clocked both at 400 kHz and at 1 MHz.

I've also tried 2 different versions of the MLX90640 code. I first found the official Melexis library last year, and downloaded the library in April of 2021. I've also recently updated my code to use the latest version of the library found on this Github repository.

The main issue I wanted to bring up in this post is related to the GetFrameData function. I would think that since my SAMD21 is clocked at 48 Mhz and my I2C is at 1 MHz, I should be able to "keep up" the MLX90640 and read the data at 64 Hz. That doesn't seem to be the case.

Right now I am clocking just the GetFrameData function. On average, regardless of the frame rate I choose, it takes 164 ms to read a single subpage. This puts my actual frame rate at 6 Hz (for reading single subpages) or 3 Hz (for reading both subpages). I don't have any other code running that would consume the microcontroller or interrupt the GetFrameData function.

To illustrate things further, I've created a table in which I tested the "old code" (the April 2021 version of this repository) and the "new code" (the November 2022 version of this repository) at various different framerates, and I've recorded the actual time it takes for GetFrameData to complete in each condition:

0.5 Hz 1 Hz 2 Hz 4 Hz 16 Hz 32 Hz 64 Hz
Old code 1636 ms 627 ms 166 ms 328 ms 817 ms 817 ms 817 ms 817 ms
New code 1639 ms 625 ms 300 ms 164 ms 164 ms 164 ms 164 ms 164 ms

Obviously some bugs in this library must have been fixed between April 2021 and November 2022, but I still can't get above 3 Hz/6 Hz. Is this a known issue in the library, or a known issue with the MLX90640? Is there something I should be doing in my code to get it to run at a higher frame rate?

slavysis commented 2 years ago

Hi,

Unfortunately, I do not have a SAMD21 at hand, but was ran some tests on a LPC1768. I wanted to test only the GetFrameData function so I ran something like the code bellow:

status = MLX90640_SynchFrame(slaveAddress); if(status < 0) pc.printf("Frame sync failed \n\r");

while (frameCnt < FRAMES_NUM)
        pc.printf("Reading Frame data  for MLX90640 device... \r");
        timer.reset();
        timer.start();
        status = MLX90640_GetFrameData(slaveAddress, pFrame);
        timer.stop();
        pc.printf("Reading data for MLX90640 takes %dms\r", timer.read_ms());

    frameCnt++;

}   

The result was that I was not dropping data even at 64Hz. Of course, I suspect that if I start doing some stuff with the data, I will start dropping frames (depending on the complexity of the data processing). The only blocking point in the GetFrameData function would be waiting for the 'Data ready' bit to be set. Once this bit is set, the function may return error, but should not pro-long the execution time. Can you run similar code to the one I've shared and see the timings? Have you verified the I2C speed with an oscilloscope?

Best regards