Store and reopen IMU data on SD card
-
Hello everyone,
I am programming a M5Stack in the Arduino IDE. I want to read the acceleration data of the installed IMU, save it on the SD card, open it again and edit it. Using the example sketches of Arduino I can do all this. But my problem is that I can't edit the data further. I would like to sum up all recorded accX, accY and accZ values one by one. I have tested this for the accX values, but nothing comes out right. My goal is to integrate the data to get a total value for the speed of one-dimensional linear movements.
Can someone tell me how I can only access the saved and reopened values of my SD card?
Here is my code:
<
//© by Jan-Soeren Henning
#define M5STACK_MPU6886
#include <M5Stack.h>
#include "FS.h"
#include "SD.h"
#include "SPI.h"
#include "time.h"
#include "math.h"float accX = 0.0F;
float accY = 0.0F;
float accZ = 0.0F;
float zeit = 0.0F;
float testX = 0.0F;
char msg[100];
unsigned long zeit_start = 0.0;
unsigned long zeit_end = 0.0;
boolean button_a = false;
boolean measurement = false;
boolean start = true;void writeFile(fs::FS &fs, const char * path, const char * message){
Serial.printf("Writing file: %s\n", path);File file = fs.open(path, FILE_WRITE); if(!file){ Serial.println("Failed to open file for writing"); return; } if(file.print(message)){ Serial.println("File written"); } else { Serial.println("Write failed"); } file.close();
}
void appendFile(fs::FS &fs, const char * path, const char * message){
Serial.printf("Appending to file: %s\n", path);File file = fs.open(path, FILE_APPEND); if(!file){ Serial.println("Failed to open file for appending"); return; } if(file.print(message)){ Serial.println("Message appended"); } else { Serial.println("Append failed"); } file.close();
}
void readFile(fs::FS &fs, const char * path){
Serial.printf("Reading file: %s\n", path);
File file = fs.open(path);
if(!file){
Serial.println("Failed to open file for reading");
return;
}
Serial.print("Read from file:\n ");
while(file.available()){
Serial.write(file.read());
testX = testX + (abs(accX));
}
file.close();
}
void setup() {
M5.begin();
M5.Power.begin();
M5.IMU.Init();
M5.Lcd.println("Press button A to\nstart measurement");Serial.begin(115200);
if(!SD.begin()){
Serial.println("Card Mount Failed");
return;
}
uint8_t cardType = SD.cardType();if(cardType == CARD_NONE){ Serial.println("No SD card attached"); return; } Serial.print("SD Card Type: "); if(cardType == CARD_MMC){ Serial.println("MMC"); } else if(cardType == CARD_SD){ Serial.println("SDSC"); } else if(cardType == CARD_SDHC){ Serial.println("SDHC"); } else { Serial.println("UNKNOWN"); } writeFile(SD, "/acceleration.txt", " Time aX aY aZ"); while (start){ zeit_start = millis(); if (M5.BtnA.wasReleasefor(70)) { button_a = (!(button_a)); measurement = (!(measurement)); } if (button_a) { while (measurement) { zeit_end = millis()-zeit_start; M5.IMU.getAccelData(&accX,&accY,&accZ); snprintf(msg, 100, "\r\n %5.2lf %5.2lf %5.2lf %5.2lf",zeit, accX, accY, accZ); // => Daten lesen mit sscanf????? appendFile(SD, "/acceleration.txt", msg); if (M5.BtnA.wasReleasefor(70)) { button_a = (!(button_a)); measurement = (!(measurement)); M5.Lcd.println("Press button A to\nstart measurement"); readFile(SD, "/acceleration.txt"); Serial.println(" "); Serial.println(testX); } } }
}
} -
Hi @jsh
in your
readFile()
function try something like this to sum upaccX
intotestX
:testX = 0.0; while(file.available()) { // read zeit - skip over file.parseFloat(); // read accX - sum up testX = testX + file.parseFloat(); // read accY - skip over file.parseFloat(); // read accZ - skip over file.parseFloat(); }
instead of
while(file.available()) { Serial.write(file.read()); testX = testX + (abs(accX)); }
Good luck!
Cheers
Felix -
Hallo @felmue
I have tried it and it works, thanks for the quick help!!!
I am glad that I can finally continue working on my program :)