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 NXP

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "SDFileSystem.h"
00003 #include "FXOS8700Q.h"
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <string.h>
00007 
00008 //Port Assignments________________________________________________________________________________________________
00009 FXOS8700Q_acc acc( PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // Proper Ports and I2C Address for K64F Freedom board
00010 FXOS8700Q_mag mag( PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // Proper Ports and I2C Address for K64F Freedom board
00011 
00012 
00013 Serial pc(USBTX, USBRX);
00014 //MotionSensor Data given in terms of gravity
00015 MotionSensorDataUnits mag_data;
00016 MotionSensorDataUnits acc_data;
00017 //MotionSensor Data Raw - Meaning that it gives in non 'Gs'
00018 MotionSensorDataCounts mag_raw;
00019 MotionSensorDataCounts acc_raw;
00020 //SD card initialization  "sd" is what the directory is called. to access it is "/sd"
00021 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS
00022 //File initialization
00023 FILE *fp_raw;
00024 FILE *fp_;
00025 FILE *fp_log;
00026 
00027 int main()
00028 {
00029 
00030     
00031     
00032     acc.enable(); //Enable Motion Sensor
00033     
00034     //pc.printf("Initializing \n");
00035     mkdir("/sd/test", 0777);
00036     fp_log = fopen("/sd/log.txt", "w");
00037     fp_= fopen("/sd/test/testing.csv", "a+");
00038     fp_raw = fopen("/sd/test/testing_raw.csv", "a+");
00039     if (fp_ == NULL) {
00040         fprintf(fp_log,"Unable to write the file \n");
00041     fclose(fp_log);
00042     } else {
00043         fprintf(fp_,"Begin Here _______________________________________________");
00044         fprintf(fp_raw, "Begin Here _____________________________________________");
00045         fclose(fp_);
00046         fclose(fp_raw);
00047         int count = 0;
00048         while(true){
00049             count =0;
00050             fp_ = fopen("/sd/test/testing.csv", "a+");
00051             fp_raw = fopen("/sd/test/testing_raw.csv","a+");
00052             while (count <100) {
00053                 acc.getAxis(acc_data);
00054                 mag.getAxis(mag_data);
00055                 fprintf(fp_,"%1.5f,%1.5f,%1.5f, ", acc_data.x, acc_data.y, acc_data.z);
00056                 fprintf(fp_,"%4.3f, %4.3f, %4.3f\r\n", mag_data.x, mag_data.y, mag_data.z);
00057                 acc.getAxis(acc_raw);
00058                 mag.getAxis(mag_raw);
00059                 fprintf(fp_raw,"%d,%d,%d,", acc_raw.x, acc_raw.y, acc_raw.z);
00060                 fprintf(fp_raw,"%d, %d,%d\r\n", mag_raw.x, mag_raw.y, mag_raw.z);
00061                 wait(.001);
00062                 count = count +1;
00063             }
00064             fclose(fp_);
00065             fclose(fp_raw);
00066         }
00067         
00068     }
00069     
00070 
00071 }