bolderflight / invensense-imu

Arduino and CMake library for communicating with the InvenSense MPU-6500, MPU-9250 and MPU-9255 nine-axis IMUs.
MIT License
513 stars 214 forks source link

Does setSrd() only work with interrupt? #102

Closed ErfanMoosaviMonazzah closed 2 years ago

ErfanMoosaviMonazzah commented 2 years ago

Hi, I have a problem with readSensor() function, I thought this would work with setSrd() and whenever there is a data (according to the sample rate) it returns positive. But with testing it I saw that this is not true. What is the correct way of using setSrd()? Is it meant to be used with interrupt? By the way, very nice and useful library. Thanks a lot.

flybrianfly commented 2 years ago

Can you post more information:

ErfanMoosaviMonazzah commented 2 years ago

Can you post more information:

  • What version of the library are you using?
  • What microcontroller?
  • What breakoutboard? Are you using SPI or I2C?
  • Post the simplest example code that demonstrates the problem.

I'm using the following MPU connected to a WeMOS D1 mini through SPI connection. sen-03-005-3 The following libraries are installed using PIO library manager. bolderflight/Bolder Flight Systems Eigen@^3.0.0 bolderflight/Bolder Flight Systems Unit Conversions@^4.1.0 bolderflight/Bolder Flight Systems MPU9250@^1.0.2

I uploaded the following code to test readSensor() which is a slightly modified version of the example provided at here:

#include <Arduino.h>
/*
Basic_SPI.ino
Brian R Taylor
brian.taylor@bolderflight.com

Copyright (c) 2017 Bolder Flight Systems

Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
and associated documentation files (the "Software"), to deal in the Software without restriction, 
including without limitation the rights to use, copy, modify, merge, publish, distribute, 
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or 
substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "MPU9250.h"

// an MPU9250 object with the MPU-9250 sensor on SPI bus 0 and chip select pin 10
MPU9250 IMU(SPI,16);
int status;

void setup() {
  // serial to display data
  Serial.begin(115200);
  while(!Serial) {}

  // start communication with IMU 
  status = IMU.begin();
  if (status < 0) {
    Serial.println("IMU initialization unsuccessful");
    Serial.println("Check IMU wiring or try cycling power");
    Serial.print("Status: ");
    Serial.println(status);
    while(1) {}
  }

  IMU.setSrd(9);

}

long time_flag1 = 0;
long time_flag2 = 0;
long time_sum = 0;
float record_counter = 0;

void loop() {
  // read the sensor
  if(IMU.readSensor())
  {
      // display the data
      IMU.getAccelX_mss();
      IMU.getAccelY_mss();
      IMU.getAccelZ_mss();
      IMU.getGyroX_rads();
      IMU.getGyroY_rads();
      IMU.getGyroZ_rads();
      IMU.getMagX_uT();
      IMU.getMagY_uT();
      IMU.getMagZ_uT();
      IMU.getTemperature_C();
      time_flag1 = time_flag2;
      time_flag2 = millis();
      time_sum += time_flag2 - time_flag1;
      record_counter++;
      Serial.printf("%f, %f\n", record_counter, time_sum / record_counter);
  }
  else Serial.println("Data not ready");
  //delay(100);
}

Since I'm pretty sure that the loop is running way faster than 100hz, I expected to face "Data not ready" very often on the serial output (considering that setSrd() affect the readSensor()), but not a single such case happended. Also I've added some variables to determine the frequency of running and this is what I faced after a couple of thousand loop iterations:

.
.
.
46287.000000, 2.070279
46288.000000, 2.070277
46289.000000, 2.070276
46290.000000, 2.070274
46291.000000, 2.070273
46292.000000, 2.070271
46293.000000, 2.070270

The second value supposed to converge to the (1/frequency) milliseconds which in case of srd = 9 means 10 milliseconds but as I tested it for multiple times using different values for srd, no changes happened. But the same code except using interrupt instead of loop works fine. (Has the same frequency as intended)

flybrianfly commented 2 years ago

I'm not sure what version fixed it, but the SRD works as expected in the current version of the library, v5.6.0