I am trying to built my gesture recognition project using Arduino UNO and MPU9250 sensor. I am trying to acquire the data of Accelerometer and Gyroscope in a format so that I could convert the output data into a .csv file for putting the data into the machine learning model for train the particular TinyML model. But ever after using the adequate library I am not able to Aquire the data from the sensor. I have used the following code and used the MPU9250 library by hideakitai
#include <Wire.h>
#include "MPU9250.h"
#include <Adafruit_Sensor.h>
/* Private define ------------------------------------------------------------*/
#define NUM_SAMPLES 50
#define NUM_GESTURES 30
#define G 9.80665f
#define ACC_THRESHOLD (2.5f*G)
#define GESTURE_0 0
#define GESTURE_1 1
#define GESTURE_TARGET GESTURE_0
//#define GESTURE_TARGET GESTURE_1
/* Private variables ---------------------------------------------------------*/
int samplesRead = NUM_SAMPLES;
int gesturesRead = 0;
MPU9250 IMU(Wire,0x68);
int status;
void setup() {
// init serial port
Serial.begin(115200);
while (!Serial) {
delay(10);
}
// init IMU sensor
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) {
delay(10);
}
}
// configure IMU sensor
IMU.setAccelRange(MPU9250::ACCEL_RANGE_8G);
IMU.setGyroRange(MPU9250::GYRO_RANGE_500DPS);
IMU.setDlpfBandwidth(MPU9250::DLPF_BANDWIDTH_20HZ);
IMU.setSrd(19);
// print the CSV header (ax0,ay0,az0,...,gx49,gy49,gz49,target)
for (int i=0; i<NUM_SAMPLES; i++) {
Serial.print("aX");
Serial.print(i);
Serial.print(",aY");
Serial.print(i);
Serial.print(",aZ");
Serial.print(i);
Serial.print(",gX");
Serial.print(i);
Serial.print(",gY");
Serial.print(i);
Serial.print(",gZ");
Serial.print(i);
Serial.print(",");
}
Serial.println("target");
}
void loop() {
while(gesturesRead < NUM_GESTURES) {
// wait for significant motion
while (samplesRead == NUM_SAMPLES) {
// read the acceleration data
IMU.readSensor();
// sum up the absolutes
float aSum = fabs(IMU.getAccelX_mss()) + fabs(IMU.getAccelY_mss()) + fabs(IMU.getAccelZ_mss());
// check if it's above the threshold
if (aSum >= ACC_THRESHOLD) {
// reset the sample read count
samplesRead = 0;
break;
}
}
// read samples of the detected motion
while (samplesRead < NUM_SAMPLES) {
// read the acceleration and gyroscope data
IMU.readSensor();
samplesRead++;
// print the sensor data in CSV format
Serial.print(IMU.getAccelX_mss(), 6);
Serial.print(',');
Serial.print(IMU.getAccelY_mss(), 6);
Serial.print(',');
Serial.print(IMU.getAccelZ_mss(), 6);
Serial.print(',');
Serial.print(IMU.getGyroX_rads(), 6);
Serial.print(',');
Serial.print(IMU.getGyroY_rads(), 6);
Serial.print(',');
Serial.print(IMU.getGyroZ_rads(), 6);
Serial.print(',');
// print target at the end of samples acquisition
if (samplesRead == NUM_SAMPLES) {
Serial.println(GESTURE_TARGET);
}
delay(10);
}
gesturesRead++;
}
}
Please feel to give to suggestion to change the code.