how did I break micros() so that multiple calls give the same time stamp?

I’m using the IMU for sensor fusion on the Nano 33. I’m running the SensorFusion library (GitHub - aster94/SensorFusion: A simple implementation of some complex Sensor Fusion algorithms) available in Arduino IDE → Manage Libraries. It is based on Paul Stoffregen’s IMU library GitHub - PaulStoffregen/MadgwickAHRS.

I’m trying to time how long it takes to run the MadgwickUpdate routine using micros(). But micros() is usually returning the same result before and after the call. Presuming this is incorrect.

code snippet (full .ino attached):

#ifdef DEBUG_SF_TIMING
    SFthen = micros();
#endif
    //choose only one of these two:
    //fusion.MahonyUpdate(gx, gy, gz, ax, ay, az, deltat);  //Mahony is suggested if there isn't the mag and the mcu is slow
    fusion.MadgwickUpdate(gx, gy, gz, ax, ay, az, mx, my, mz, deltat);  //else use the Madgwick. It is slower but more accurate

#ifdef DEBUG_SF_TIMING
    SFnow = micros();
    Serial.print("SensorFusion elapsed time (uS): ");
    SFelapsedus = (SFnow - SFthen); //uS
    Serial.println(SFelapsedus, 3);
    Serial.println(SFnow);
    Serial.println(SFthen);
    Serial.println();
#endif

The sensorfusion algorithm is working well when output to Processing (after many days of work). Output of timestamps to serial monitor looks like:

Orientation:, 25, -7, -3
SensorFusion elapsed time (uS): 0.000
96490295
96490295

Orientation:, 25, -7, -3
SensorFusion elapsed time (uS): 0.000
96497253
96497253

Orientation:, 25, -7, -3
SensorFusion elapsed time (uS): 31.000
96503113
96503082

Orientation:, 25, -7, -3
SensorFusion elapsed time (uS): 0.000
96510040
96510040

Orientation:, 25, -7, -3
SensorFusion elapsed time (uS): 0.000
96515899
96515899

where you can see the timestamp pairs returned by micros() are often identical uS so elapsed is 0.

I am not aware of any interrupts anywhere, that would stop micros() from working.

I’m planning to post a tutorial on running sensorfusion on the Nano 33 after it’s all working. 3DOF orientation tracking is pretty good at this point but I’m failing on timing the execution.

BTW, I have modified LSM9DS1.cpp to change IMU sensor frequencies as shown here: https://forum.arduino.cc/index.php?topic=648307.msg4603257#msg4603257.

Thanks for having a look!

IMU-SensorFusion-Nano33.ino (7.12 KB)

Sorry I can't help you on the micros(), but it looks like you are doing things I was looking for.

Hi femmeverbeek. thanks for your note. Let me know if I can be of help. I'm planning to make a tutorial soon. I put a couple (newbie) weeks into this so far.

I wrote some updates on the LSM9DS1 library. I'll post them soon in this forum. Offset, scale, sample rate and full scale setting on all DOF.

Sounds good femmeverbeek. Looking forward to check out your mods!