Description
Description:
When using the example script provided with the Arduino_BMI270_BMM150 library and setting the accelerometer sampling rate to 800 Hz in the BMI270.cpp file, the actual output on the Serial Monitor does not reach 800 Hz. Instead, the sampling appears to max out at approximately every 4 ms (~250 Hz).
The issue seems related to the time it takes for the IMU.readAcceleration() function to execute, which I measured to take around 4 ms per call.
The sample rate is being correctly set in the library as IMU.accelerationSampleRate() correctly returns 800. However, the observed behavior indicates that the IMU.readAcceleration() function might be causing the bottleneck.
Steps to Reproduce:
- Modify the BMI270.cpp file to set the accelerometer sampling rate to 800 Hz:
sens_cfg[0].cfg.acc.odr = BMI2_ACC_ODR_800HZ; // Set to 800 Hz
sens_cfg[0].cfg.acc.bwp = BMI2_ACC_NORMAL_AVG4; // Use normal avg4
sens_cfg[0].cfg.acc.range = BMI2_ACC_RANGE_2G; // Set range to 2G
change conversion factor:
#define INT16_to_G (16384.0f) //for 2G range
- Upload the provided example code Arduino BMI270 - Simple Accelerometer with the above library modifications.
- Observe the Serial Monitor output.
Observed Behavior:
The serial output only updates approximately every 8 ms. By editing the code and using buffer before printing I could speed the serial output to approximately every 4 ms.
The actual sampling rate is far lower than the configured 800 Hz.
Expected Behavior:
The accelerometer sampling rate should match the configured value of 800 Hz.
Code Example:
#include "Arduino.h"
#include "Arduino_BMI270_BMM150.h"
void setup() {
Serial.begin(9600);
while (!Serial);
if (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
while (1);
}
Serial.print("Accelerometer sample rate = ");
Serial.println(IMU.accelerationSampleRate());
}
void loop() {
float x, y, z;
int timestamp = millis();
if (IMU.accelerationAvailable()) {
IMU.readAcceleration(x, y, z);
Serial.print(timestamp);
Serial.print("\t");
Serial.print(x);
Serial.print("\t");
Serial.print(y);
Serial.print("\t");
Serial.println(z);
}
}
Enviroment
- Board: Arduino Nano 33 BLE Sense Rev2
- Library: Arduino_BMI270_BMM150
- using VSCode with PlatformIO
Additional Notes:
This issue seems to stem from the time required for the IMU.readAcceleration() function to complete. At higher sample rates, this latency is significant enough to prevent achieving the configured rate.
Could this be optimized in the library, or is there a limitation in the sensor or library design that I should consider? Any guidance or fixes would be appreciated.
Thank you!