I want to read the data from an IMU (MPU9250) periodically. To do that I use a ticker as shown in the following code:
#include <Ticker.h>
#include "MPU9250.h"
constexpr int SERIAL_BAUDRATE = 115200;
constexpr float SAMPLING_FREQUENCY = 1.0;
Ticker TimerIMU;
MPU9250 IMU(Wire, 0x68);
void readIMU()
{
// read the sensor
IMU.readSensor();
// display the data
Serial.print(IMU.getAccelX_mss(),6);
Serial.print("\t");
Serial.print(IMU.getAccelY_mss(),6);
Serial.print("\t");
Serial.print(IMU.getAccelZ_mss(),6);
Serial.print("\t");
Serial.print(IMU.getGyroX_rads(),6);
Serial.print("\t");
Serial.print(IMU.getGyroY_rads(),6);
Serial.print("\t");
Serial.println(IMU.getGyroZ_rads(),6);
}
void setup()
{
// serial to display data
Serial.begin(115200);
while(!Serial) {}
// start communication with IMU
int retVal = IMU.begin();
if(retVal < 0)
Serial.println("IMU initialization unsuccessful: check your wiring or try resetting power to the sensor.");
// start ticker
TimerIMU.attach(1.0 / SAMPLING_FREQUENCY, readIMU);
}
void loop()
{
}
This approach does not produce the expected results: the IMU returns always the same values. Putting the readIMU function in the main loop and commenting the Ticker works well. I use an ESP32-WROVER.
Where is my error? Any suggestion would be appreciated.