mbed code Inductive_Sensor_Jasper for Bsc paper

Dependencies:   Bob DS1825 LDC1101 SDFileSystem mbed

Fork of Inductive_Sensor by Bob Giesberts

Revision:
4:ae441c5727b9
Parent:
2:1a203732fc95
Child:
5:736a81a59f3c
--- a/main.cpp	Fri Dec 18 15:52:32 2015 +0000
+++ b/main.cpp	Tue Jan 05 11:04:25 2016 +0000
@@ -3,6 +3,7 @@
 #include "SDFileSystem.h"
 #include "Bob.h"
 #include <iostream>
+#include <string>
 using namespace std;
 
 /**
@@ -11,6 +12,10 @@
 * using the library LDC1101.h and LDC1101.cpp.
 * - Red Led: processing communication with LDC1101
 * - Green Led: processing SD card
+* 
+* Log protocol:
+* -  1 minute  at 20 Hz
+* - 29 minutes rest
 *
 * @author Bob Giesberts
 *
@@ -20,6 +25,21 @@
 Bob bob(PTB0, PTB1, PTC3, PTE0); // red led, green led, sd_enable, sd_present
 Serial pc(USBTX, USBRX);
 
+// battery
+AnalogIn batt(PTC2);    // battery voltage: batt.read(); should be x.xV (min) < 3.7V (typ) < 4.22V (max)
+float battery;
+
+// timer variables
+clock_t t;
+uint32_t t_new = 0, t_old = 0;
+int t_high = 0;
+float seconds = 0;
+
+// file variables
+FILE *fp;
+string filename = "/sd/data00.txt";
+const char *fn;
+
 int main(void){
     
     /** --- Connection to LDC1101 --- */
@@ -41,6 +61,7 @@
     }
 
     /** --- Start measuring! --- */
+    /*
     while(1)
     {
         clock_t t = clock();
@@ -48,101 +69,106 @@
         pc.printf("%.2f;%d\n", t/100.0, L);
         wait(0.05); // 20 Hz
     }
-
-
+    */
 
-    
-    
-    
-    /* let's leave this part out while calibrating the sensor
 
     // --- Connection to SD card --- 
     pc.printf("Connect to SD card...");
     if(bob.checkSD() == 1){
         pc.printf("success!\r\n");
+        bob.flash_green(2);
     }else{
         pc.printf("failed!\r\n");
+        bob.green();
     }
      
     // Load SD File system
     SDFileSystem SD(PTD6, PTD7, PTD5, PTD4, "sd");  // mosi, miso, sclk, cs, sd_name
 
-    // Open or create data file
-    pc.printf("Opening data file...");
-    mkdir("/sd/mydir", 0777);
-    FILE *fp = fopen("/sd/data.txt", "w");
-    if(fp != NULL) {
-        pc.printf("success!\r\n");
-    }else{
-        pc.printf("failed!\r\n");
+    // Create a new data file
+    pc.printf("Creating data file...");
+    mkdir("/sd", 0777);   
+    for(uint8_t i = 0; i < 100; i++) {
+        filename[8] = i/10 + '0';    // arrays count from 0 so the number is 16 and 17
+        filename[9] = i%10 + '0';
+        fp = fopen( filename.c_str() , "r" );
+        if( fp == NULL ) { // read failed so file does not exist
+            fp = fopen( filename.c_str(), "w" ); // create it
+            if( fp != NULL ){
+                fn = filename.c_str();
+                pc.printf("success! Filename: %s\r\n", fn);
+                break;
+            }else{
+                pc.printf("failed!\r\n");
+            }
+        }else{ // file already exists
+            fclose( fp );
+        }
     }
     
     // Write current settings to data file
-    fprintf(fp, "DIVIDER       :  %d\r\n", ldc.get_divider());
-    fprintf(fp, "RESPONSETIME  :  %d\r\n", ldc.get_responsetime());
-    fprintf(fp, "RP_MIN        :  %f\r\n", ldc.get_RPmin());
-    fprintf(fp, "LHR_RCOUNT    :  %d\r\n", ldc.get_Rcount());
+    fp = fopen( fn, "w" );
+    fprintf(fp, "DIVIDER       :  %d\r\n",   ldc.get_divider());
+    fprintf(fp, "RESPONSETIME  :  %d\r\n",   ldc.get_responsetime());
+    fprintf(fp, "RP_MIN        :  %.2f\r\n", ldc.get_RPmin());
+    fprintf(fp, "LHR_RCOUNT    :  %d\r\n",   ldc.get_Rcount());
     fprintf(fp, "\r\n\r\n");
-    fclose(fp); 
-
-
-
+    fclose(fp);
+    
+    // initialise timer
+    t = clock();
+    t_new = (uint32_t) t;
+     
     while(1)
-    {       
-        clock_t t = clock();
-             
-        // get value from sensor
-        int L = ldc.get_LHR_Data();
+    {
+        // Store one continuous stream of data
+        for( int i = 0; i < 1200; i++  ) { // FOR LATER: record 60 seconds --> 60 * 20 Hz = 1200
         
-        // if the sensor is connected
-        if(L != 16777215) {
+            // Set the timer
+            t_old = t_new;
+            t = clock();
+            t_new = (uint32_t) t;
+            if( t_new < t_old ) t_high++; // clock_t overflows at 2^32 = 4294.967296 sec = 71 min
+            seconds = ((float) t_new/100.0) + 4294.967296*(float)t_high;
             
-            // settings
-            pc.printf("ResponseTime: %d", ldc.get_responsetime());
-            pc.printf(", Divider: %d", ldc.get_divider());
-            pc.printf(", RP_MIN: %.0f", ldc.get_RPmin());
-            pc.printf(", R_COUNT: %d", ldc.get_Rcount());
-            pc.printf(", Q: %.2f", ldc.get_Q());
-            pc.printf("\r\n");
+            // get the sensor value
+            int L = ldc.get_LHR_Data();
             
-            // results
-            pc.printf("Time = %.2f", t/100.0);
-            pc.printf(", L_DATA = %d", L);
-            pc.printf(", f_sensor = %.2f MHz", ldc.get_fsensor()/1000000);
-            pc.printf(", L = %.2f uH", 1000000*ldc.get_Inductance());
-            pc.printf("\r\n");
-            pc.printf("\r\n");
-            
-            
-            // feedback: it's working
-            bob.flash_red(1);
-            
-            
-            // Store data on SD card
-            if( bob.checkSD() == 1 ) {
-                fp = fopen("/sd/data.txt", "a");
-                //for(int i = 0; i < 100; i++){     Store one continuous stream of data, 30 seconds
-                    fprintf(fp, "%.2f, ", t/100.0);
-                    fprintf(fp, "%d\r\n", L);
-                //}
-                fclose(fp);
+            // get the current battery level
+            battery = (float)batt.read()*3.0*2.0;
+        
+            // if the sensor is connected
+            if( L != 16777215 ) {
+                           
+                // Store data on SD card
+                if( bob.checkSD() == 1 ) {
+                    fp = fopen( fn, "a" );
+                    fprintf( fp, "%.2f;%d;%.6f\r\n", seconds, L, battery );
+                    fclose(fp);
+                    
+                    // pc.printf( "Time: %.2f sec; LHR_DATA: %d; Battery: %f V\r\n", seconds, L, battery );
+                    
+                    wait( 0.03 ); // Write takes about 0.02 --> 20 Hz --> 0.05 - 0.02 = 0.03s
+                }else{
+                    pc.printf("Error: SD card disconnected, please reinsert.\r\n");
+                    bob.green();    
+                }
+              
             }else{
-                pc.printf("Error: SD card disconnected, please reinsert.\r\n");
-                bob.green();    
+                pc.printf("Error: Sensor disconnected, please reconnect.\r\n");
+                bob.red();
             }
-          
-        }else{
-            pc.printf("Error: Sensor disconnected, please reconnect.\r\n");
-            bob.red();
         }
-
         
-        // Telkens een periode van 10 - 30 seconden continue meten, dan een half uur pauze.
-        // In die pauze alles op laagste vermogen (LDC1101 in shutdown modus, SD uit, leds uit)
-        // ldc.sleep(); sd_pin = 0;
-        // wait(1800); // FOR LATER: wait half an hour, 60 * 30 = 1800
-        // ldc.wakeup(); sd_pin = 1;
+        // After measuring for a period of 60 seconds, wait for 29 minutes
+        // While waiting, put every thing in lowest power modus
+        bob.sleep();
+        ldc.sleep(); 
         
+        wait(1740); // FOR LATER: wait half an hour, 60 * 29 = 1740
+        
+        // Now wake up.
+        ldc.wakeup();
+        bob.wakeup();
     }
-    */
 }