SparkFun_9DOF_SensorStick

Dependencies:   FatFileSystem mbed

Fork of 9DOF_SensorStick by hige dura

Revision:
3:5b192b38b3bb
Parent:
2:3ffce3e97527
Child:
4:cc2b1a6de723
--- a/main.cpp	Wed Jan 25 05:25:29 2012 +0000
+++ b/main.cpp	Wed Mar 14 17:03:49 2012 +0000
@@ -1,66 +1,48 @@
+#include "mbed.h"
 #include "ADXL345_I2C.h"
 #include "ITG3200.h"
 #include "HMC5883L.h"
+#include "SDFileSystem.h"
 
 ADXL345_I2C accelerometer(p9, p10);
 ITG3200 gyro(p9, p10);
 HMC5883L compass(p9, p10);
+SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
 Serial pc(USBTX, USBRX);
 
 #define N 3
 
+int preparing_acc();
+
 int main(){ 
     
-    int i   =  0;
-    float dt = 0.1;
+    float dt = 0.01;
     float t  = 0;    
-    pc.baud(9600);
+    pc.baud(921600);
     int    bitAcc[N]    =   {0};    // Buffer of the accelerometer
     int    getMag[N]    =   {0};    // Buffer of the compass
     double Acc  [N]    =   {0};
     double Gyro [N]    =   {0};
     int    Mag  [N]    =   {0};
     
-    // *** Setting up accelerometer ***
-    // These are here to test whether any of the initialization fails. It will print the failure.
-    if (accelerometer.setPowerControl(0x00)){
-        pc.printf("didn't intitialize power control\n\r"); 
-        return 0;
-    }
-    // Full resolution, +/-16g, 4mg/LSB.
-    wait(.001);
-     
-    if(accelerometer.setDataFormatControl(0x0B)){
-        pc.printf("didn't set data format\n\r");
-        return 0;  }
-    wait(.001);
+    // SD file system
+    mkdir("/sd/mydir", 0777);
      
-    // 3.2kHz data rate.
-    if(accelerometer.setDataRate(ADXL345_3200HZ)){
-        pc.printf("didn't set data rate\n\r");
-        return 0;
-    }
-    wait(.001);
-      
-    if(accelerometer.setPowerControl(MeasurementMode)) {
-        pc.printf("didn't set the power control to measurement\n\r"); 
-        return 0;
-    } 
-    // ***  Setting up accelerometer ***
+    FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
+    if( fp==NULL ){     error("Could not open file for write\n");   }
     
-    // ***  Setting up gyro ***
+    // ***  Setting up sensors ***    
+    int preparing_acc();
     gyro.setLpBandwidth(LPFBW_42HZ);
-
-    // ***  Setting up compass ***
     compass.setDefault();
-    
-    // Wait some time for all sensors (Need at least 5ms)
-    wait(0.1);
+    wait(0.1);                        // Wait some time for all sensors (Need at least 5ms)
     
     pc.printf("\n\rStarting ADXL345, ITG3200 and HMC5883L test...\n\r");
     pc.printf("   Time     AccX     AccY     AccZ    GyroX    GyroY    GyroZ   MagX   MagY   MagZ\n\r");
                 
-    while(1){
+    //while(1){
+    for( int i=0;i<10000;i++ ){
+    
         // Updating accelerometer and compass
         accelerometer.getOutput(bitAcc);
         compass.readData(getMag);
@@ -91,9 +73,50 @@
         //    if( -1.0<Gyro[i] && Gyro[i]<1.0 ){    Gyro[i]=0;  }
         //}
 
-        pc.printf("%7.2f, %7.3f, %7.3f, %7.3f, %7.1f, %7.1f, %7.1f, %5d, %5d, %5d\n\r", t, Acc[0], Acc[1], Acc[2], Gyro[0], Gyro[1], Gyro[2], Mag[0], Mag[1], Mag[2]);
+        //pc.printf("%7.2f, %7.3f, %7.3f, %7.3f, %7.1f, %7.1f, %7.1f, %5d, %5d, %5d\n\r", t, Acc[0], Acc[1], Acc[2], Gyro[0], Gyro[1], Gyro[2], Mag[0], Mag[1], Mag[2]);
+        pc.printf("%7.2f, %7.3f, %7.3f, %7.3f, %7.1f, %7.1f, %7.1f\n\r", t, Acc[0], Acc[1], Acc[2], Gyro[0], Gyro[1], Gyro[2]);
+        //pc.printf("%7.1f, %7.1f, %7.1f\n\r",Gyro[0], Gyro[1], Gyro[2]);
+        fprintf(fp, "%7.2f, %7.3f, %7.3f, %7.3f, %7.1f, %7.1f, %7.1f\n\r", t, Acc[0], Acc[1], Acc[2], Gyro[0], Gyro[1], Gyro[2]);
         
         t += dt;
         wait(dt);
+        
     }
+    
+    fclose(fp);
+    
  }
+ 
+int preparing_acc(){
+ 
+    // *** Setting up accelerometer ***
+    // These are here to test whether any of the initialization fails. It will print the failure.
+    if (accelerometer.setPowerControl(0x00)){
+        pc.printf("didn't intitialize power control\n\r"); 
+        return 0;
+    }
+    // Full resolution, +/-16g, 4mg/LSB.
+    wait(.001);
+     
+    if(accelerometer.setDataFormatControl(0x0B)){
+        pc.printf("didn't set data format\n\r");
+        return 0;
+    }
+    wait(.001);
+     
+    // 3.2kHz data rate.
+    if(accelerometer.setDataRate(ADXL345_3200HZ)){
+        pc.printf("didn't set data rate\n\r");
+        return 0;
+    }
+    wait(.001);
+      
+    if(accelerometer.setPowerControl(MeasurementMode)) {
+        pc.printf("didn't set the power control to measurement\n\r"); 
+        return 0;
+    } 
+    // ***  Setting up accelerometer ***
+ 
+    return 0;
+ 
+}