motion.arduino.ICM42670P
motion.arduino.ICM42670P copied to clipboard
Leftover FIFO samples after wake‑on‑motion with ICM42670P & ESP32
Hello everyone,
I’m running into an issue with reading the FIFO on my ESP32 using the ICM42670P and the Arduino IDE. I need to perform a time‑critical measurement immediately after waking up the controller, then put it back into deep sleep via:
IMU.startWakeOnMotion(WakeUp_Pin, irq_handler2);
An interrupt on WakeUp_Pin brings the ESP32 back online and trigger my measurement function again. However, on the second (and subsequent) runs, the first 80 samples out of my 2,000‑sample buffer are leftover from the very first measurement, so the data is no longer evenly spaced.
I’ve tried flushing the FIFO with a dummy callback:
// Dummy callback to clear the FIFO void dummy_cb(inv_imu_sensor_event_t* evt) { (void)evt; }
// …inside measureAndSaveRaw(): while (IMU.getDataFromFifo(dummy_cb) == 0) { // discard old samples } —but I still end up reading old data before the new samples arrive. Interestingly, when I lower my sampling rate from 800 Hz to 400 Hz, the problem shrinks to only the first 20 leftover samples. Here’s the core of my measurement routine:
void measureAndSaveRaw(const char* fileName) { rawData = (float(*)[7])malloc(sizeof(float) * numSamples * 7); if (!rawData) { Serial.println("Error: could not allocate heap memory."); return; }
sampleIndex = 0; irq_received = 0;
// Configure FIFO interrupt (watermark = 20) IMU.enableFifoInterrupt(WakeUp_Pin, irq_handler, /watermark=/20);
// Start the sensors IMU.startAccel(Accl_ODR, Accl_FSR); IMU.startGyro(Gyr_ODR, Gyr_FSR); delay(100); // wait for first watermark
// Read samples while (sampleIndex < numSamples) { if (irq_received) { irq_received = 0; IMU.getDataFromFifo(rawEventCb); } }
detachInterrupt(digitalPinToInterrupt(WakeUp_Pin)); start_SD_Card(); saveDataToSD( fileName, "time_us,accelX_g,accelY_g,accelZ_g,gyroX_dps,gyroY_dps,gyroZ_dps", (float*)rawData, numSamples, 7 );
free(rawData); rawData = nullptr; }