Example of using the SDFileSystem library on a K64F to write files from LSM6DS33 (accel and gyro)
Dependencies: FXOS8700CQ LSM6DS3 SDFileSystem mbed
Fork of 2545_SD_Card by
Diff: main.cpp
- Revision:
- 1:23691b50336d
- Parent:
- 0:5448330e1a33
diff -r 5448330e1a33 -r 23691b50336d main.cpp --- a/main.cpp Fri Mar 11 16:07:41 2016 +0000 +++ b/main.cpp Wed Jul 11 00:59:13 2018 +0000 @@ -1,173 +1,60 @@ -/* 2545_SD_Card Example - -Example of writing data to SD card. - -Based on FTF2014_lab4 Example - -https://developer.mbed.org/teams/Freescale/wiki/FTF2014_workshop - -Craig A. Evans, University of Leeds, Mar 2016 - -*/ - #include "mbed.h" #include "SDFileSystem.h" +#include "FXOS8700CQ.h" +#include "LSM6DS3.h" // Connections to SD card holder on K64F (SPI interface) SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS -Serial serial(USBTX, USBRX); // for PC debug +Timer t; +FXOS8700CQ fxos(PTE25,PTE24); +LSM6DS3 LSM6DS3(PTE25,PTE24); +Serial pc(USBTX, USBRX); +Data fxos_acc; +unsigned long int start_time = 0, end_time = 0; +uint16_t adc_value; +AnalogIn pot0(A0), pot1(A1), pot2(A2), pot3(A3); -void delete_file(char filename[]); + int main() { - serial.baud(115200); // full-speed! - serial.printf("#### SD Card Example #####\n"); + LSM6DS3.begin(); + fxos.init(); + double t0, t1; + pc.baud(115200); // full-speed! + pc.printf("#### Example #####\n"); FILE *fp; // this is our file pointer - wait(1); - - // Various examples below - can comment out ones you don't need - - /////////////////////// Deleting file example //////////////////////// - - // comment this line out if you don't want to delete the file! - delete_file("/sd/test.txt"); - - ////////////////////// Simple writing example ////////////////////////// - - // open file for writing ('w') - creates file if it doesn't exist and overwrites - // if it does. If you wish to add a score onto a list, then you can - // append instead 'a'. This will open the file if it exists and start - // writing at the end. It will create the file if it doesn't exist. - fp = fopen("/sd/topscore.txt", "w"); - int top_score = 56; // random example - - if (fp == NULL) { // if it can't open the file then print error message - serial.printf("Error! Unable to open file!\n"); - } else { // opened file so can write - serial.printf("Writing to file...."); - fprintf(fp, "%d",top_score); // ensure data type matches - serial.printf("Done.\n"); - fclose(fp); // ensure you close the file after writing - } - - ////////////////////// Simple reading example ////////////////////////// - - // now open file for reading - fp = fopen("/sd/topscore.txt", "r"); - int stored_top_score = -1; // -1 to demonstrate it has changed after reading - - if (fp == NULL) { // if it can't open the file then print error message - serial.printf("Error! Unable to open file!\n"); - } else { // opened file so can write - fscanf(fp, "%d",&stored_top_score); // ensure data type matches - note address operator (&) - serial.printf("Read %d from file.\n",stored_top_score); - fclose(fp); // ensure you close the file after reading - } - - ///////////////////// Writing list to file example ////////////////////// + t.start(); + t0 = t.read_us(); + fp = fopen("/sd/Teste1.txt", "wb"); + //read Accel & Gyro + LSM6DS3.readAccel(); + LSM6DS3.readGyro(); + start_time = t.read_us(); + fxos_acc = fxos.get_values(); + end_time = t.read_us(); + pc.printf("\nPass time: %d\n", end_time - start_time); + //serial send Accel (board) + pc.printf("BoardAccelerX[%f]\n",fxos_acc.ax); + pc.printf("BoardAccelerY[%f]\n",fxos_acc.ay); + pc.printf("BoardAccelerZ[%f]\n",fxos_acc.az); + //serial send Gyro + pc.printf("GyroX[%f]\n",LSM6DS3.gx); + pc.printf("GyroY[%f]\n",LSM6DS3.gy); + pc.printf("GyroZ[%f]\n",LSM6DS3.gz); + //serial send Accel (lsm6ds33) + pc.printf("AccelerX[%f]\n",LSM6DS3.ax); + pc.printf("AccelerY[%f]\n",LSM6DS3.ay); + pc.printf("AccelerZ[%f]\n",LSM6DS3.az); - // for this example, I'll create some numbers to write to file in a big list - // a data logger for example will usually append to a file - at a reading - // at the end rather than creating a new file - fp = fopen("/sd/test.txt", "a"); - - if (fp == NULL) { // if it can't open the file then print error message - serial.printf("Error! Unable to open file!\n"); - } else { // opened file so can write - serial.printf("Writing to file...."); - for(int i = 1; i <= 50; i++) { - float dummy = 1000.0F/i; // dummy variable - fprintf(fp, "%d,%f\n",i,dummy); // print formatted string to file (CSV) - } - serial.printf("Done.\n"); - fclose(fp); // ensure you close the file after writing - } - // you can comment out the writing example to check that the writing has - // worked - when you run it after commenting, it should still open the - // file that exists on the SD card - assuming you didn't delete it! - - /////////////////////// Reading from file example //////////////////////// - - // now open file for reading...note the 'r' - fp = fopen("/sd/test.txt", "r"); if (fp == NULL) { // if it can't open the file then print error message - serial.printf("Error! Unable to open file!\n"); - } else { - serial.printf("Reading file....\n"); - int i; // create suitable variables to store the data in the file - float value; - - // in this example, we keep reading (using fscanf) until we reach - // the 'end of file'. Note we use the address operator & to write - // to the variables. Also the format of the string must match what - // is in the file - while (fscanf(fp, "%d,%f", &i, &value) != EOF) { - serial.printf("%d,%f\n",i,value); - } - serial.printf("Done.\n"); - fclose(fp); // ensure you close the file after reading + pc.printf("Error! Unable to open file!\n"); + } else { // opened file so can write + fprintf(fp, "%f %f %f \n %f %f %f \n %f %f %f \n", fxos_acc.ax,fxos_acc.ay,fxos_acc.az,LSM6DS3.gx,LSM6DS3.gy,LSM6DS3.gz,LSM6DS3.ax,LSM6DS3.ay,LSM6DS3.az); + fclose(fp); // ensure you close the file after writing + t1 = t.read_us(); + pc.printf("The time taken was %lf u-seconds\n", t1-t0); + t.stop(); } - - ///////////////// Advanced Reading from file example /////////////////// - - // the previous example just read the values into variables and printed to - // serial, we'll now read files into an array. - - // now open file for reading...note the 'r' - fp = fopen("/sd/test.txt", "r"); - - int n=0; // going to store the number of lines in the file - int *index_array; // pointers to create dynamic arrays later - float *value_array; // note memory will be in heap rather than on the stack - - if (fp == NULL) { // if it can't open the file then print error message - serial.printf("Error! Unable to open file!\n"); - } else { - serial.printf("Counting lines in file....\n"); - //Since we may not know the - // number of lines in the files ahead of time, we'll first count them - // * means scan but don't save - while (fscanf(fp, "%*d,%*f") != EOF) { - n++; // increment counter when read a line - } - - - serial.printf("Read %d lines\n",n); - serial.printf("Creating dynamic arrays...\n"); - // calloc creates an array and initilises to 0 - // malloc returns unitialised array - diffrent syntax - index_array = (int *)calloc(n, sizeof (int)); - value_array = (float *)calloc(n, sizeof (float)); - - int i=0; - rewind(fp); // 'scrolled' to end of file, so go back to beginning - serial.printf("Reading into arrays...\n"); - while (fscanf(fp, "%d,%f",&index_array[i],&value_array[i]) != EOF) { - i++; // read data into array and increment index - } - serial.printf("Done.\n"); - fclose(fp); // ensure you close the file after reading - } - - // we should now have the data in the arrays, will print to serial to check - for(int i=0; i<n ; i++) { - serial.printf("[%d] %d,%f\n",i,index_array[i],value_array[i]); - } - - /////////////////////////////////////////////////// - serial.printf("End of SD card example\n"); } - -void delete_file(char filename[]) -{ - serial.printf("Deleting file '%s'...",filename); - FILE *fp = fopen(filename, "r"); // try and open file - if (fp != NULL) { // if it does open... - fclose(fp); // close it - remove(filename); // and then delete - serial.printf("Done!\n"); - } - // if we can't open it, it doesn't exist and so we can't delete it -}