04-25-2022 04:01 PM
Hey everyone,
I have posted this question two weeks ago in the Arduino forum but have not revceived any answers.
I did an extensive amount of research and went throught the Arduino_BHY2 source code, but at this point, help would be much appreciated.
I want record an acceleration signal with a rate of 1000 to 3200 Hz with a duration of 10 seconds, if possible. Afterwards I forward the data to a PC using USB. I use the Nicla Sense ME because it has an accelerometer and a 2 Mb data logging flash.
I have written code (below) that does that at 1000 Hz, but for a shorter sample length of 0.5 seconds.
I have two questions:
1. My measurement is for verification of a mechanical system and I need to be able to trust the measurements. Therefore I would like to have a timestamp from the sample creation.
I store my own timestamp in datalines[i][0]. As you can see in the output, the timing is all over the place, which is not much of a surprise, because my timestamp is made after transfer via serial from the sensor and buffering. Does anyone know how to get a timestamp for the respective reading from the sensor? Or can the sensor just be trusted with 1kHz samples?
2. With my 0.5 second sample I already max out the Nicla's RAM. There is a data logging flash (MX25R1635FZUIH0) connected via SPI. I really don't want to start from scratch using the flash's data sheet, can anyone recommend a library or any simpler solution?
Help would be much appreciated! I'm also happy to receive feedback on my coding as I think there is still a lot to learn for me.
Thank you
Moechl
Code:
#include "Arduino.h"
#include "Arduino_BHY2.h"
#include "Nicla_System.h"
#define SAMPLE_LENGTH 500 //number of samples recorded; 500 is about the maximum for this RAM
int sampleInterval = 1000; //microseconds
int l = SAMPLE_LENGTH;
int16_t datalines[SAMPLE_LENGTH][4]; // array where the data is stored
SensorXYZ accel(SENSOR_ID_ACC);
void setup()
{
Serial.begin(115200);
while (!Serial);
BHY2.begin(); //methods are defined in Arduino_BHY2.cpp
accel.begin(1000, 0); //sample rate 1000 Hz, 0 ms latency
nicla::begin();
nicla::leds.begin();
BHY2.delay(50);
static auto sampleTime = micros();
static auto startTime = millis();
/* record values to datalines */
for (int i = 0; i <= l; i++) {
sampleTime = micros();
while (micros() - sampleTime < sampleInterval) {
BHY2.update(); // Update function should be continuously polled
}
datalines[i][0] = millis() - startTime; //does not give a good timestamp
datalines[i][1] = accel.x();
datalines[i][2] = accel.y();
datalines[i][3] = accel.z();
}
BHY2.delay(50);
/* write the data to PC over serial */
for (int k = 0; k <= l; k++) {
Serial.println(String(datalines[k][0]) + "," + String(datalines[k][1]) + "," + String(datalines[k][2]) + "," + String(datalines[k][3]) + ";");
delay(30);
}
}
void loop()
{
// blink so I know it is done
nicla::leds.setColor(green);
delay(1000);
nicla::leds.setColor(off);
delay(1000);
}
Output Excerpt:
14:03:15.948 -> 1,1836,-816,3702;
14:03:15.988 -> 33,1830,-823,3720;
14:03:16.029 -> 34,1845,-824,3710;
14:03:16.069 -> 39,1843,-822,3732;
14:03:16.069 -> 41,1841,-812,3723;
14:03:16.109 -> 42,1834,-816,3735;
14:03:16.149 -> 43,1834,-816,3735;
14:03:16.190 -> 44,1829,-826,3724;
14:03:16.230 -> 49,1846,-819,3708;
14:03:16.230 -> 50,1845,-821,3719;
14:03:16.270 -> 51,1829,-820,3727;
14:03:16.310 -> 52,1829,-820,3727;
Solved! Go to Solution.
05-05-2022 03:15 PM
May I ask for your help @BSTRobin ?🙏
05-06-2022 03:25 AM
Hi Moechl,
We are checking now, and will give you feedback later.
05-10-2022 09:47 AM
Hi Moechl,
Current FW supports max ODR 400HZ.
Regarding the timestamp of sensor you could refer to function BoschParser::parseGeneric in BoschParser.cpp, It print timestamp from FIFO for each sensor.
05-11-2022 12:54 PM
Hi BSTRobin,
thanks for the answer.
As for the ODR, you are right I already noticed some temporal quantization when exceeding the 400 Hz. This is a bit unfortunate, as Arduino links this datasheet which on page 149 states a maximum ODR of 1600 Hz for the accelerometer and even the default sampling rate for the sensor seems to be 1000 Hz, when looking into SensorClass.h. Is there a way around this, maybe by using the "bhy2.h" directly?
Using the BoschParser::parseGeneric function cannot work directly on arduino, because it does not support the printf function, if I am not mistaken. Is there a workaround? Maybe the bhy2_get_hw_timestamp_ns function in bhy2.c will help in the future. But getting a timestamp is not important if I cannot reach my desired sampling rate.