🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    Store and reopen IMU data on SD card

    Scheduled Pinned Locked Moved Units
    3 Posts 2 Posters 6.3k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J Offline
      jsh
      last edited by

      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);
                   }
            }
        }
      

      }
      }

      1 Reply Last reply Reply Quote 0
      • felmueF Offline
        felmue
        last edited by felmue

        Hi @jsh

        in your readFile() function try something like this to sum up accX into testX:

          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

        GPIO translation table M5Stack / M5Core2
        Information about various M5Stack products.
        Code examples

        1 Reply Last reply Reply Quote 0
        • J Offline
          jsh
          last edited by

          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 :)

          1 Reply Last reply Reply Quote 0

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          • First post
            Last post