mbed code Inductive_Sensor_Jasper for Bsc paper
Dependencies: Bob DS1825 LDC1101 SDFileSystem mbed
Fork of Inductive_Sensor by
Diff: main.cpp
- Revision:
- 5:736a81a59f3c
- Parent:
- 4:ae441c5727b9
- Child:
- 6:ff39d60061ca
diff -r ae441c5727b9 -r 736a81a59f3c main.cpp --- a/main.cpp Tue Jan 05 11:04:25 2016 +0000 +++ b/main.cpp Sat Jan 16 09:14:21 2016 +0000 @@ -3,6 +3,7 @@ #include "SDFileSystem.h" #include "Bob.h" #include <iostream> +#include <vector> #include <string> using namespace std; @@ -22,69 +23,93 @@ * @date 2015-12-17 */ -Bob bob(PTB0, PTB1, PTC3, PTE0); // red led, green led, sd_enable, sd_present -Serial pc(USBTX, USBRX); +// 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 -// battery -AnalogIn batt(PTC2); // battery voltage: batt.read(); should be x.xV (min) < 3.7V (typ) < 4.22V (max) -float battery; +// 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; +uint32_t t_new = 0, t_old = 0; // real time in PEE modus (48 MHz) int t_high = 0; -float seconds = 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; // file variables FILE *fp; string filename = "/sd/data00.txt"; const char *fn; +// temporal storage for data samples +vector < float > secvector; +vector < uint32_t > Lvector; +float batt; + int main(void){ - + + + /** --- Connection to LDC1101 --- */ pc.printf("Connect to LDC1101..."); - LDC1101 ldc(PTC6, PTC7, PTC5, PTC4, 120E-12, 16E6); // mosi, miso, sck, cs, capacitor (F), f_CLKIN (Hz) + LDC1101* ldc = new LDC1101(PTC6, PTC7, PTC5, PTC4, C, 16E6); pc.printf("success!\r\n"); - bob.flash(2); - wait(0.2); + bob.flash(1); /** --- Connection to sensor --- */ pc.printf("Connect to sensor..."); - if(ldc.get_LHR_Data() != 16777215) - { - pc.printf("success!\r\n"); - bob.flash_red(2); + 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"); + pc.printf(" failed!\r\n"); bob.red(); } - - /** --- Start measuring! --- */ - /* - while(1) - { - clock_t t = clock(); - int L = ldc.get_LHR_Data(); - pc.printf("%.2f;%d\n", t/100.0, L); - wait(0.05); // 20 Hz - } - */ - + - // --- Connection to SD card --- + /**********************************/ + /*********** 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 ); + } + } + /**********************************/ + /**********************************/ + + + /** --- 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(); - } - + 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(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..."); mkdir("/sd", 0777); @@ -108,67 +133,217 @@ // 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, "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; + + + // Take samples for a period of INTERVAL_ON sec, then shut down everything and + // sleep for a period of INTERVAL_OFF seconds. while(1) { - // Store one continuous stream of data - for( int i = 0; i < 1200; i++ ) { // FOR LATER: record 60 seconds --> 60 * 20 Hz = 1200 + + // 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++; // clock_t overflows at 2^32 = 4294.967296 sec = 71 min - seconds = ((float) t_new/100.0) + 4294.967296*(float)t_high; + 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 - // get the sensor value - int L = ldc.get_LHR_Data(); + seconds = ((float) t_new/100.0) + 4294.967296*(float)t_high + t_lost_new + 4294.967296*(float)t_lost_high; - // 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); + // 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 ) { - // pc.printf( "Time: %.2f sec; LHR_DATA: %d; Battery: %f V\r\n", seconds, L, battery ); + bob.green(); + + // store sample data in RAM / flash memory + secvector.push_back( seconds ); + Lvector.push_back( L ); - 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(); + // 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(); + + } - - }else{ - pc.printf("Error: Sensor disconnected, please reconnect.\r\n"); - bob.red(); + } + } + + // 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 60 seconds, wait for 29 minutes - // While waiting, put every thing in lowest power modus - bob.sleep(); - ldc.sleep(); + + // 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"); + + - wait(1740); // FOR LATER: wait half an hour, 60 * 29 = 1740 - - // Now wake up. - ldc.wakeup(); - bob.wakeup(); + // 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(); + + // 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 ); + + // 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) } + }