Different elapsed time between each sensor scan

Hi!

I wanted to know how much time (in microseconds) it takes for a sensor to output data during each scan. I used the following code to print the output of 3-axis accelerometer:

// IMU 1 sensor - I2C //
//Acceleration Data (gravity + linear motion)

  unsigned long startTime = micros();

  imu::Vector<3> accel1 = bno1.getVector(Adafruit_BNO055::VECTOR_ACCELEROMETER);
  imu1_data[0] = accel1.x();
  imu1_data[1] = accel1.y();
  imu1_data[2] = accel1.z();

  unsigned long endTime = micros();
  unsigned long elapsedTime = endTime - startTime;
  Serial.print("Time Interval: ");
  Serial.println(elapsedTime);

The data for each axis is stored into an array called “imu1_data” assigned with a float variable type. I used the micros() function to keep track of the time it takes for that section of code to execute, but the time interval keeps changing (as shown in the attached picture). I was expecting the time interval to be relatively constant for each data scan, and this was the case when I tried using the micros() method to track the time interval for time-of-flight sensor data scan (assigned a uint16_t variable type). However, the time-of-flight data used a for loop to retrieve the data (shown in the code below) instead of storing the values in an array, but I’m not sure if that makes a difference as to why the time interval keeps changing.

// Time of Flight I2C sensors - I2C //

  for (int i = 0; i < TOF_DIM; i++) {
    Wire.requestFrom(tof_address + i, 2);
    uint16_t distance = Wire.read() << 8; distance |= Wire.read();
    tof[i] = distance;
  }

I just wanted to know if anyone had any idea why the time interval keeps changing and whether it has something to do with the way I have been retrieving the sensor data or if the micros() method I used to calculated the elapsed time is wrong. Thanks for the help! I really appreciate it!

time interval capture.PNG

The answer to your question is neither.

The data appears quite predicable within the limited sample you posted. Something is using double the processor cycles for about 7-8 milliseconds then it drops in half for a millisecond. It’s no doubt elsewhere in your mystery program.

Next time, please copy the output from the serial console and post into a code window, far more user friendly, especially for mobile users.

OP’s image:

That is weird. A full millisecond extra delay.

This is the getVector: https://github.com/adafruit/Adafruit_BNO055/blob/master/Adafruit_BNO055.cpp#L379. It calls readLen(). That is all very straightforward, nothing special.

The BNO055 is not a dumb sensor that returns data, it has a processor that returns data when it is in the mood to return data. If there are no mystery interrupt routines, then I assume it is the BNO055.

Do you have a logic analyzer ? Then you could measure the I2C bus to check if the BNO055 holds the I2C for 1ms.

Unfortunately I don’t have a logic analyzer. I was wondering if it had something to do with the nature of IMUs in general and how it retrieves the data? I first thought it might have to do with the way that I wrote the code, so I tried doing it 2 different ways, the first way is how they show it on the Adafruit website:

 imu::Vector<3> accel = bno.getVector(Adafruit_BNO055::VECTOR_ACCELEROMETER);

And the second way was just reading the register values:

Wire.beginTransmission(address);
Wire.write(0X08); 
Wire.requestFrom(address,6);
float accel1_x = (int16_t)(Wire.read() | Wire.read() << 8)/100.00; //m/s^2
float accel1_y = (int16_t)(Wire.read() | Wire.read() << 8)/100.00; //m/s^2
float accel1_z = (int16_t)(Wire.read() | Wire.read() << 8)/100.00; //m/s^2
Wire.endTransmission();

But both codes seem to have inconsistent elapsed times between each sensor scan, as shown below:

Time Interval: 942
Time Interval: 946
Time Interval: 964
Time Interval: 999
Time Interval: 1212
Time Interval: 1043
Time Interval: 1066
Time Interval: 1090
Time Interval: 1117
Time Interval: 1329
Time Interval: 1166
Time Interval: 1189
Time Interval: 1123
Time Interval: 1146
Time Interval: 1361
Time Interval: 1198
Time Interval: 1224
Time Interval: 1219
Time Interval: 1220
Time Interval: 1413
Time Interval: 1223
Time Interval: 1230
Time Interval: 1327
Time Interval: 1326
Time Interval: 1518
Time Interval: 1331
Time Interval: 1327
Time Interval: 1327
Time Interval: 1331
Time Interval: 1519
Time Interval: 1525
Time Interval: 1481
Time Interval: 1525
Time Interval: 1530
Time Interval: 1718
Time Interval: 1540
Time Interval: 1559
Time Interval: 1530
Time Interval: 1530
Time Interval: 1732
Time Interval: 1571
Time Interval: 1560
Time Interval: 1556
Time Interval: 1561
Time Interval: 1763
Time Interval: 1571
Time Interval: 1560
Time Interval: 1561
Time Interval: 1591
Time Interval: 1763
Time Interval: 1690
Time Interval: 1689
Time Interval: 1680
Time Interval: 1694
Time Interval: 1875
Time Interval: 1691
Time Interval: 1680
Time Interval: 1687
Time Interval: 1691
Time Interval: 1995
Time Interval: 1811
Time Interval: 1830
Time Interval: 1813
Time Interval: 1803
Time Interval: 1999
Time Interval: 1799
Time Interval: 1829
Time Interval: 1810
Time Interval: 1855
Time Interval: 2112
Time Interval: 1934
Time Interval: 1929
Time Interval: 1949
Time Interval: 1925
Time Interval: 2117
Time Interval: 1931
Time Interval: 1926
Time Interval: 1960
Time Interval: 1872
Time Interval: 1900
Time Interval: 1890
Time Interval: 1899
Time Interval: 1879
Time Interval: 1890
Time Interval: 1879
Time Interval: 1871
Time Interval: 1874
Time Interval: 938
Time Interval: 1875
Time Interval: 1875
Time Interval: 1874
Time Interval: 1912
Time Interval: 942
Time Interval: 1968
Time Interval: 1639
Time Interval: 1812
Time Interval: 1832
Time Interval: 941
Time Interval: 1643
Time Interval: 1681
Time Interval: 1682
Time Interval: 1724
Time Interval: 941
Time Interval: 1733
Time Interval: 1587
Time Interval: 1541
Time Interval: 1617
Time Interval: 942
Time Interval: 1461
Time Interval: 1501
Time Interval: 1482
Time Interval: 1336
Time Interval: 942
Time Interval: 1371
Time Interval: 1376
Time Interval: 1363
Time Interval: 1214
Time Interval: 942
Time Interval: 1275
Time Interval: 1134
Time Interval: 1059
Time Interval: 1104
Time Interval: 941
Time Interval: 1139
Time Interval: 941
Time Interval: 938
Time Interval: 941
Time Interval: 941
Time Interval: 937
Time Interval: 942
Time Interval: 941
Time Interval: 941
Time Interval: 942
Time Interval: 941
Time Interval: 942
Time Interval: 941
Time Interval: 941

When I tried taking the elapsed time between time-of-flight sensor readings, they seemed to be pretty consistent, which is why I was thinking it had something to do with the way IMUs work. Here is the time-of-flight code:

  Wire.beginTransmission(address);
  Wire.write(READ_DISTANCE);
  Wire.endTransmission(false); //repeated start
  Wire.requestFrom(address, 2, true);
  distance = Wire.read() << 8; distance |= Wire.read();

And the elapsed time between each reading:

Time Interval: 1285
Time Interval: 1284
Time Interval: 1284
Time Interval: 1285
Time Interval: 1283
Time Interval: 1285
Time Interval: 1284
Time Interval: 1283
Time Interval: 1285
Time Interval: 1283
Time Interval: 1283
Time Interval: 1285
Time Interval: 1284
Time Interval: 1285
Time Interval: 1285
Time Interval: 1284
Time Interval: 1283
Time Interval: 1286
Time Interval: 1284
Time Interval: 1283
Time Interval: 1296
Time Interval: 1284
Time Interval: 1283
Time Interval: 1285
Time Interval: 1286
Time Interval: 1285
Time Interval: 1285
Time Interval: 1284
Time Interval: 1285
Time Interval: 1284
Time Interval: 1283
Time Interval: 1285
Time Interval: 1284
Time Interval: 1284
Time Interval: 1285
Time Interval: 1282
Time Interval: 1284
Time Interval: 1285
Time Interval: 1284
Time Interval: 1284
Time Interval: 1285
Time Interval: 1286
Time Interval: 1286
Time Interval: 1284
Time Interval: 1287
Time Interval: 1286
Time Interval: 1285
Time Interval: 1283

As I wrote before, the BNO055 is not a dumb sensor that returns data, it has a processor. So you have to look inside the processor of the BNO055 to see what the cause is. Or measure what it is doing on the I2C bus. Or accept that it seems to be busy sometimes and needs more time.

This is not because it is a IMU, it is about the processor inside the BNO055.