Skip to content

BHY2.update() not producing sensor data at even time intervals #111

@perry-tcircuits

Description

@perry-tcircuits

Description of defect

I would like to collect accelerometer data at about 166 Hz so that I can perform a spectral analysis on it, but to do this I need to have data coming in at even time intervals. I've created a simple timing structure to log the acceleration and time every 6 ms over Serial communication, I find that BHY.update() takes variable amounts of time on each loop, which is not ideal. In the code that I've included below, the interval between time points can be up to 10 ms. While i'd expect the interval to between 6 and 7 ms per point, I'm not sure why the delay gets above these values, here's a graph showing how the interval changes suring a 20 second experiment (forgive the x axis being negative, I normalized it incorrectly).

image

Target(s) affected by this defect ?

Nicla Sense ME

Toolchain(s) (name and version) displaying this defect ?

What version of Mbed-os are you using (tag or sha) ?

What version(s) of tools are you using. List all that apply (E.g. mbed-cli)

How is this defect reproduced ?

#include "Nicla_System.h"
#include "Arduino_BHY2.h"


SensorXYZ accelerometer(SENSOR_ID_ACC);

// SensorBSEC bsec(SENSOR_ID_BSEC);

void setup(){
  Serial.begin(115200);



  //Sensors initialization
  // BHY2.begin(NICLA_STANDALONE);
  BHY2.begin();
  // accelerometer.begin(1000,0);
  accelerometer.begin();

  

}

void loop(){

  static unsigned long previousMillis = 0;
  const unsigned long interval = 6;
 
  unsigned long currentMillis = millis();
  BHY2.update(); // I've tried putting this both within and outside of the if-block

  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis; // Save the current time


    float x = accelerometer.x();
    float y = accelerometer.y();
    float z = accelerometer.z();
    float time = currentMillis;

    Serial.println();
    Serial.print(x);
    Serial.print(", ");
    Serial.print(y);
    Serial.print(", ");
    Serial.print(z);
    Serial.print(", ");
    Serial.print(time);
  }

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions