Uses NXP tutorial over 6 Axis Sensor and extends it to writing to csv files on the onboard sd-card.
Dependencies: FXOS8700Q SDFileSystem mbed
Fork of FRDMK64_SDCard by
Diff: main.cpp
- Revision:
- 3:98c49df25889
- Parent:
- 0:e1c215fe231c
- Child:
- 4:e68880ae8048
diff -r df0ff4f12345 -r 98c49df25889 main.cpp --- a/main.cpp Mon Apr 07 20:33:11 2014 +0000 +++ b/main.cpp Sat Oct 29 06:58:28 2016 +0000 @@ -1,137 +1,82 @@ #include "mbed.h" #include "SDFileSystem.h" +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "mbed.h" +#include "FXOS8700Q.h" + + +//FXOS8700Q acc( A4, A5, FXOS8700CQ_SLAVE_ADDR0); // Proper Ports and I2C address for Freescale Multi Axis shield +//FXOS8700Q mag( A4, A5, FXOS8700CQ_SLAVE_ADDR0); // Proper Ports and I2C address for Freescale Multi Axis shield +FXOS8700Q_acc acc( PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // Proper Ports and I2C Address for K64F Freedom board +FXOS8700Q_mag mag( PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // Proper Ports and I2C Address for K64F Freedom board + +Serial pc(USBTX, USBRX); + +MotionSensorDataUnits mag_data; +MotionSensorDataUnits acc_data; + +MotionSensorDataCounts mag_raw; +MotionSensorDataCounts acc_raw; SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS -Serial pc(USBTX, USBRX); + FILE *fp; -int file_copy(const char *src, const char *dst) -{ - int retval = 0; - int ch; - - FILE *fpsrc = fopen(src, "r"); // src file - FILE *fpdst = fopen(dst, "w"); // dest file - - while (1) { // Copy src to dest - ch = fgetc(fpsrc); // until src EOF read. - if (ch == EOF) break; - fputc(ch, fpdst); - } - fclose(fpsrc); - fclose(fpdst); - - fpdst = fopen(dst, "r"); // Reopen dest to insure - if (fpdst == NULL) { // that it was created. - retval = -1; // Return error. - } else { - fclose(fpdst); - retval = 0; // Return success. - } - return retval; -} - -uint32_t do_list(const char *fsrc) -{ - DIR *d = opendir(fsrc); - struct dirent *p; - uint32_t counter = 0; - - while ((p = readdir(d)) != NULL) { - counter++; - printf("%s\n", p->d_name); - } - closedir(d); - return counter; -} - -// bool is_folder(const char *fdir) -// { -// DIR *dir = opendir(fdir); -// if (dir) { -// closedir(dir); -// } -// return (dir != NULL); - -// } - -// bool is_file(const char *ffile) -// { -// FILE *fp = fopen(ffile, "r"); -// if (fp) { -// fclose(fp); -// } -// return (fp != NULL); -// } - -void do_remove(const char *fsrc) -{ - DIR *d = opendir(fsrc); - struct dirent *p; - char path[30] = {0}; - while((p = readdir(d)) != NULL) { - strcpy(path, fsrc); - strcat(path, "/"); - strcat(path, p->d_name); - remove(path); - } - closedir(d); - remove(fsrc); -} - int main() { + float faX, faY, faZ; + float fmX, fmY, fmZ; + int16_t raX, raY, raZ; + int16_t rmX, rmY, rmZ; + acc.enable(); pc.printf("Initializing \n"); - wait(2); - - do_remove("/sd/test1"); /* clean up from the previous Lab 5 if was executed */ - if (do_list("/sd") == 0) { - printf("No files/directories on the sd card."); - } - - printf("\nCreating two folders. \n"); - mkdir("/sd/test1", 0777); - mkdir("/sd/test2", 0777); - - fp = fopen("/sd/test1/1.txt", "w"); - if (fp == NULL) { - pc.printf("Unable to write the file \n"); - } else { - fprintf(fp, "1.txt in test 1"); - fclose(fp); - } - - fp = fopen("/sd/test2/2.txt", "w"); + mkdir("/sd/test", 0777); + + fp = fopen("/sd/test/testing.txt", "a+"); if (fp == NULL) { pc.printf("Unable to write the file \n"); } else { - fprintf(fp, "2.txt in test 2"); + fprintf(fp, "\nTesting Reset _________________________________\n"); + fprintf(fp, "Data goes here\n"); fclose(fp); + int count = 0; + while(true){ + count =0; + fp = fopen("/sd/test/testing.txt", "a+"); + while (count <1000) { + acc.getAxis(acc_data); + mag.getAxis(mag_data); + fprintf(fp,"FXOS8700Q ACC:__ X=%1.4f Y=%1.4f Z=%1.4f ", acc_data.x, acc_data.y, acc_data.z); + fprintf(fp," MAG: X=%4.1f Y=%4.1f Z=%4.1f\r\n", mag_data.x, mag_data.y, mag_data.z); + acc.getX(&faX); + acc.getY(&faY); + acc.getZ(&faZ); + mag.getX(&fmX); + mag.getY(&fmY); + mag.getZ(&fmZ); + fprintf(fp,"FXOS8700Q ACC:__ X=%1.4f Y=%1.4f Z=%1.4f ", faX, faY, faZ); + fprintf(fp," MAG: X=%4.1f Y=%4.1f Z=%4.1f\r\n", fmX, fmY, fmZ); + acc.getAxis(acc_raw); + mag.getAxis(mag_raw); + fprintf(fp,"FXOS8700Q ACC: X=%d Y=%d Z=%d ", acc_raw.x, acc_raw.y, acc_raw.z); + fprintf(fp," MAG: X=%d Y=%d Z=%d\r\n", mag_raw.x, mag_raw.y, mag_raw.z); + acc.getX(&raX); + acc.getY(&raY); + acc.getZ(&raZ); + mag.getX(&rmX); + mag.getY(&rmY); + mag.getZ(&rmZ); + fprintf(fp,"FXOS8700Q ACC: X=%d Y=%d Z=%d ", raX, raY, raZ); + fprintf(fp," MAG: X=%d Y=%d Z=%d\r\n\n", rmX, rmY, rmZ); + wait(.001); + count = count +1; + } + fclose(fp); + } + } - - printf("\nList all directories/files /sd.\n"); - do_list("/sd"); - - printf("\nList all files within /sd/test1.\n"); - do_list("/sd/test1"); - - - printf("\nList all files within /sd/test2.\n"); - do_list("/sd/test2"); + - int status = file_copy("/sd/test2/2.txt", "/sd/test1/2_copy.txt"); - if (status == -1) { - printf("Error, file was not copied.\n"); - } - printf("Removing test2 folder and 2.txt file inside."); - remove("/sd/test2/2.txt"); - remove("/sd/test2"); - - printf("\nList all directories/files /sd.\n"); - do_list("/sd"); - - printf("\nList all files within /sd/test1.\n"); - do_list("/sd/test1"); - - printf("\nEnd of complete Lab 5. \n"); }