Version 3.0: Switching to newer LDC1614 which is placed on the same PCB.

Dependencies:   Bob DS1825 LDC1614 LDC1101 SDFileSystem mbed

Fork of Inductive_Sensor by Bob Giesberts

Revision:
6:ff39d60061ca
Parent:
5:736a81a59f3c
Child:
7:6c4cac1ec122
--- a/main.cpp	Sat Jan 16 09:14:21 2016 +0000
+++ b/main.cpp	Mon Jan 18 15:43:07 2016 +0000
@@ -24,23 +24,20 @@
 */
 
 // SETTINGS
-bool DEBUG_MODE = false;
-float C = 120E-12;          // pF
-int INTERVAL_ON  = 2;       //   30 =       30 sec
-int INTERVAL_OFF = 2;       // 1770 = 29*60+30 sec
+bool DEBUG = false;
+float C = 120E-12;      // pF
+int INTERVAL_ON  = 30;  //   30 =       30 sec
+int INTERVAL_OFF = 1770;  // 1770 = 29*60+30 sec
 
 // load libraries
 Bob bob(PTB0, PTB1, PTC3, PTE0, PTC2, PTE30); // red led, green led, sd_enable, sd_present, batt, 3V3_DET
 Serial pc(USBTX, USBRX);
 
 // timer variables
-clock_t t;
-uint32_t t_new = 0, t_old = 0; // real time in PEE modus (48 MHz)
+uint32_t now = 0, next = 0, prev = 0;
 int t_high = 0;
-uint32_t t_lost_new = 0, t_lost_old = 0; // lost time in BLPI modus (16 kHz)
-int t_lost_high = 0;
-float seconds = (float) 2.0 * INTERVAL_ON;
-float t_period = 0;
+uint32_t lost = 0, lost_prev = 0;
+int lost_high = 0;
 
 // file variables
 FILE *fp;
@@ -48,83 +45,35 @@
 const char *fn;
 
 // temporal storage for data samples
-vector < float > secvector;
+vector < float > Svector;
+float S;
 vector < uint32_t > Lvector;
+uint32_t L;
 float batt;
 
+
 int main(void){
 
-
-
-    /** --- Connection to LDC1101 --- */
-    pc.printf("Connect to LDC1101...");
-    LDC1101* ldc = new LDC1101(PTC6, PTC7, PTC5, PTC4, C, 16E6);
-    pc.printf("success!\r\n");
-    bob.flash(1);
-
-    /** --- Connection to sensor --- */
-    pc.printf("Connect to sensor...");
-    while( !ldc->is_New_LHR_data() ){
-       pc.printf(".");
-    }
-    // while( ldc.get_LHR_Data() == 0 ) { pc.printf(":"); bob.wakeup(); }        // sometimes this gives an error! TODO: fix the error!
-    if( ldc->get_LHR_Data() != 16777215 ) {
-        pc.printf(" success!\r\n");
-        bob.flash_red(1);
-    }else{
-        pc.printf(" failed!\r\n");
-        bob.red();
-    }
-    
+    bob.flash(2);
 
-    /**********************************/
-    /*********** DEBUG MODUS **********/
-    /**********************************/
-    if( DEBUG_MODE )
-    {
-        while(1)
-        {
-            if( ldc->is_Oscillation_Error() ){
-                pc.printf("Oscillation Error! \r\n");
-                
-            }else{
-                while( !ldc->is_New_LHR_data() ) {
-                    // pc.printf(".");
-                }
-                pc.printf("LHR_Data: %d, battery: %.4f\r\n", ldc->get_LHR_Data(), bob.battery());
-            }
-            wait( 0.1 );
-        }
-    }
-    /**********************************/
-    /**********************************/
-
+    // Load SD File system
+    bob.SDon();
+    SDFileSystem *sd = new SDFileSystem(PTD6, PTD7, PTD5, PTD4, "sd");
     
-    /** --- Connection to SD card --- */
-    pc.printf("Connect to SD card...");
-    while( bob.checkSD() != 1 ) { pc.printf("."); bob.green(); bob.wakeup(); }
-    pc.printf("success!\r\n");
-    bob.flash_green(1);
-         
-    // Load SD File system
-    // SDFileSystem SD(PTD6, PTD7, PTD5, PTD4, "sd");  // mosi, miso, sclk, cs, sd_name
-    SDFileSystem* sd = new SDFileSystem(PTD6, PTD7, PTD5, PTD4, "sd");
-    
-    // Create a new data file
-    pc.printf("Creating data file...");
+    // Create a new data file (data00.txt)
     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[8] = i/10 + '0';
         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);
+                fclose( fp );
                 break;
             }else{
-                pc.printf("failed!\r\n");
+                bob.red();
             }
         }else{ // file already exists
             fclose( fp );
@@ -132,218 +81,104 @@
     }
     
     // Write current settings to data file
-    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, "C             :  %f\r\n",   C);
-    fprintf(fp, "\r\n\r\n");
-    fclose(fp);
-    
-    delete ldc;
-    delete sd;
-
-   
-    // initialise timer
-    t = clock();
-    t_new = (uint32_t) t;
-     
-
+    // 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, "C             :  %f\r\n",   C);
+    // fprintf(fp, "\r\n\r\n");
+    // fclose(fp);
     
-    // Take samples for a period of INTERVAL_ON sec, then shut down everything and
-    // sleep for a period of INTERVAL_OFF seconds.    
-    while(1)
-    {
-        
-        // wake up.
-        pc.printf("waking up!\r\n\r\n");
-        
-        bob.wakeup();       // SD + Sensor on
-        
-        pc.printf("Connect to SD card...");
-        while( bob.checkSD() != 1 ) { pc.printf("."); bob.green(); bob.wakeup(); }
-        pc.printf("success!\r\n");
-        bob.greenoff();
-        
-        // connection to LDC1101
-        pc.printf("Connect to LDC1101...");
-        LDC1101* ldc = new LDC1101(PTC6, PTC7, PTC5, PTC4, C, 16E6);
-        pc.printf("succes!\r\n");
-        
-        // SD-card
-        // BIG FAT ERROR RIGHT HERE!!!
-        //                               //
-        //                               //
-        //       SDFileSystem gives an   //
-        //       error: after power off  //
-        //       everything seems wrong  //
-        //                               //
-        //                               //
-        // BIG FAT ERROR RIGHT HERE!!!
-        // SDFileSystem* sd = new SDFileSystem(PTD6, PTD7, PTD5, PTD4, "sd");
-        // mkdir("/sd", 0777);
-        // fp = fopen(fn, "a");
-        pc.printf("Let's begin\r\n\r\n");
-        // fprintf(fp, "Let's begin\r\n\r\n");
-        // fclose(fp);
-        
-        
-                
-        // Add lost time to the counter
-        t_lost_old = t_lost_new;
-        t_lost_new += (INTERVAL_OFF);
-        t_period = seconds + INTERVAL_OFF;
-        
-        while( seconds < t_period + INTERVAL_ON )
-        {
-            
-            // Set the timer
-            t_old = t_new;
-            t = clock();
-            t_new = (uint32_t) t;
-            if( t_new < t_old ) t_high++; // uint32_t <clock_t> overflows at 2^32 = 4294.967296 sec = 71 min
-            if( t_lost_new < t_lost_old ) t_lost_high++; // uint32_t <t_lost_new> overflows at 2^32 = 4294.967296 sec = 71 min
-            
-            seconds = ((float) t_new/100.0) + 4294.967296*(float)t_high + t_lost_new + 4294.967296*(float)t_lost_high;
-            
-            // check if sensor is not overloaded
-            if(ldc->is_Oscillation_Error()){
-                // Error: sensor overloaded
-                
-            }else{
-                // wait until the next value is ready, this takes about 0.2 s?
-                while(!ldc->is_New_LHR_data()) {
-                    // pc.printf(".");
-                    bob.red();
-                }
-                bob.redoff();
-                        
-                // get the sensor value
-                int L = ldc->get_LHR_Data();
-                
-                // if the sensor is connected
-                if( L != 0 && L != 16777215 ) {
-                    
-                    bob.green();
-                    
-                    // store sample data in RAM / flash memory
-                    secvector.push_back( seconds );               
-                    Lvector.push_back( L ); 
-                    
-                    // RAM reaches memory limit when vector size = 64
-                    // 64 * 32bit * 2 = 512 bytes | KL25Z: 128 kb flash + 16 kb RAM
-                    if( Lvector.size() > 31 )
-                    {
-                        // If SD-carddetect fails, try to wake it up setting enable to 1 and wait
-                        while( bob.checkSD() != 1 ) { pc.printf("."); bob.green(); bob.wakeup(); }
-                        
-                        if( bob.checkSD() == 1 )
-                        {
-                            // no need to have accurate timestamp for battery level, just one sample for this period
-                            batt = bob.battery();
-                            
-                            bob.red();
-                            
-                            // BIG FAT ERROR RIGHT HERE!!!
-                            //                               //
-                            //                               //
-                            //       fopen gives a stupid    //
-                            //              error            //
-                            //                               //
-                            //                               //
-                            //                               //
-                            // BIG FAT ERROR RIGHT HERE!!!
-                            
-                            /* debugging...
-                            fp = fopen( fn, "r" );
-                            if(fp == NULL)
-                            {
-                                pc.printf("r werkt niet \r\n;");
-                            }else{
-                                pc.printf("r werkt\r\n;");
-                            }
-                            fclose(fp);
-                            wait (0.5);
-                            */
-                            
-                            pc.printf("Write RAM memory to SD card...");
-                            // fp = fopen( fn, "a" );
-                            // for(uint8_t i = 0; i < Lvector.size(); i++)
-                            // {
-                            //     fprintf( fp, "%.2f;%d;%.6f\r\n", secvector.at(i), Lvector.at(i), batt );
-                            // }
-                            // fclose(fp);
-                            pc.printf("success!\r\n");
-                            bob.redoff();
-                            
-                            secvector.clear();
-                            Lvector.clear();
-                        }
-                    }
-                    
-                    pc.printf( "[%d]: %.2f;%d;%.6f\r\n", secvector.size(), seconds, L, bob.battery() );
-                    
-                    bob.greenoff();
-                        
-                  
-                }
-                    
-            }
+    // Unload SD File system
+    delete sd; sd = NULL;
 
-        }
-
-        // store remaining data before going to sleep
-        if( bob.checkSD() == 1 && Lvector.size() != 0)
-        {
-            // no need to have accurate timestamp for battery level, just one sample for this period
-            batt = bob.battery();
-            
-            bob.red();
-            pc.printf("Write RAM memory to SD card...");
-            // fp = fopen( fn, "a" );
-            // for(uint8_t i = 0; i < Lvector.size(); i++)
-            //     fprintf( fp, "%.2f;%d;%.6f\r\n", secvector[i], Lvector[i], batt );
-            // fclose(fp);
-            pc.printf("succes!\r\n");
-            bob.redoff();    
-
-        }
-        secvector.clear();
-        Lvector.clear();
-
-        
-        
-        // After measuring for a period of INTERVAL_ON seconds, wait for INTERVAL_OFF minutes
-        // While waiting, put everything in lowest power modus        
-        pc.printf("zzz...\r\n\r\n");
 
 
-        
-        // SD-card: Kill SPI to SD card
-        delete sd;
-        DigitalOut sdP2(PTD4); sdP2 = 0; // cs
-        DigitalOut sdP3(PTD6); sdP3 = 0; // mosi
-        DigitalOut sdP5(PTD5); sdP5 = 0; // sck
-        DigitalOut sdP7(PTD7); sdP7 = 0; // miso
-
-        // LDC in SHUTDOWN mode
-        ldc->sleep(); 
+    while(1)
+    {        
+        // SD on + sensor on
+        bob.SDon();
 
-        // Sensor: Kill SPI to sensor        
-        DigitalOut senP2(PTC7); senP2 = 0; // miso
-        DigitalOut senP3(PTC5); senP3 = 0; // sck
-        DigitalOut senP4(PTC6); senP4 = 0; // mosi
-        DigitalOut senP5(PTC4); senP5 = 0; // cs
-
-        // Power off SD + Sensor, KL25Z in deepsleep mode
-        bob.sleep( INTERVAL_OFF );
+        // Sensor on        
+        LDC1101 *ldc = new LDC1101(PTC6, PTC7, PTC5, PTC4, C, 16E6);
+        
+        // SD on
+        SDFileSystem *sd = new SDFileSystem(PTD6, PTD7, PTD5, PTD4, "sd"); 
+        mkdir("/sd", 0777); // select folder  
+        
+        // clock
+        now  = (uint32_t) clock(); 
+        next = (uint32_t) now + INTERVAL_ON*100;
+        
+        // Take samples for INTERVAL_ON seconds
+        while( (uint32_t) clock() < next )
+        {
+            // Collect a package 16 data points
+            bob.green();
+            while( Lvector.size() < 16 ) // Write is per 512 bits: 16 * 32 = 512
+            {
+                // wait for new data to be ready
+                while( !ldc->is_New_LHR_data() ) { } // does not seem to work?
+                
+                if( !ldc->is_Oscillation_Error() ){ // sensor not overloaded
+                    // time
+                    prev = now;
+                    now = (uint32_t) clock();
+                    if( now < prev ) t_high++;
+                    S = ((float) now/100.0) + 4294.967296*(float)t_high + lost + 4294.967296*(float)lost_high;
+                    Svector.push_back( S );
+                    
+                    // induction
+                    L = ldc->get_LHR_Data();
+                    Lvector.push_back( L );
+                    
+                    // set sample rate
+                    wait_ms(50);    // ~20 Hz
+                }
+            }
+            // battery level
+            batt = bob.battery();
+            bob.greenoff();
     
-            // RESULTS
-            // 0.101 mA (zonder sensor, zonder SD)
-            // 1.300 mA (zonder sensor, met SD)
-            // 2.574 mA (met sensor, zonder SD)
-            // 3.185 mA (met sensor, met SD)
+            // Store the package of 16 data points
+            // pc.printf( "[%d] Writing %dx data to SD card: %.2f;%d;%.4f\r\n", lost, Lvector.size(), Svector[0], Lvector[0], batt );
+            // bob.red();
+            fp = fopen( fn, "a" ); // open file
+            // if( fp == NULL ) { pc.printf("error (append)...\r\n"); }
+            for( int i = 0; i < Lvector.size(); i++ )
+                fprintf( fp, "%.2f;%d;%.4f\r\n", Svector.at(i), Lvector.at(i), batt ); // write to file
+            fclose( fp ); // close file
+            // bob.redoff();
+            
+            // Release data
+            Lvector.clear();
+            Svector.clear();
+            
+        }               
+        
+        // SD off
+        delete sd; sd = NULL;
+        bob.SDoff();
+        DigitalOut *sdP2 = new DigitalOut(PTD4); sdP2->write(0); delete sdP2; sdP2 = NULL;// cs
+        DigitalOut *sdP3 = new DigitalOut(PTD6); sdP3->write(0); delete sdP3; sdP3 = NULL;// mosi
+        DigitalOut *sdP5 = new DigitalOut(PTD5); sdP5->write(0); delete sdP5; sdP5 = NULL;// sck
+        DigitalOut *sdP7 = new DigitalOut(PTD7); sdP7->write(0); delete sdP7; sdP7 = NULL;// miso 
+        
+        // Sensor off
+        delete ldc; ldc = NULL;
+        DigitalOut *senP2 = new DigitalOut(PTC7); senP2->write(0); delete senP2; senP2 = NULL; // miso
+        DigitalOut *senP3 = new DigitalOut(PTC5); senP3->write(0); delete senP3; senP3 = NULL; // sck
+        DigitalOut *senP4 = new DigitalOut(PTC6); senP4->write(0); delete senP4; senP4 = NULL; // mosi
+        DigitalOut *senP5 = new DigitalOut(PTC4); senP5->write(0); delete senP5; senP5 = NULL; // cs
+        
+        // Sleep for INTERVAL_OFF samples
+        bob.sleep( (float) INTERVAL_OFF - (clock() - next)/100.0 );
+        
+        // Add lost time to the counter
+        lost_prev = lost;
+        lost += (INTERVAL_OFF);
+        if( lost < lost_prev ) lost_high++;
     }
-    
+
 }