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
Diff: main.cpp
- Revision:
- 8:8cc1960467ae
- Parent:
- 7:6c4cac1ec122
- Child:
- 9:47f1b1c0ef8b
--- a/main.cpp Mon Jan 25 09:13:11 2016 +0000 +++ b/main.cpp Tue Feb 16 15:55:19 2016 +0000 @@ -26,8 +26,8 @@ // SETTINGS bool DEBUG = false; float C = 120E-12; // pF -int INTERVAL_ON = 5; // 30 = 30 sec -int INTERVAL_OFF = 5; // 1770 = 29*60+30 sec +int INTERVAL_ON = 1; // 30 = 30 sec +int INTERVAL_OFF = 60; // 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 @@ -35,9 +35,10 @@ // timer variables uint32_t now = 0, next = 0, prev = 0; -int t_high = 0; +uint8_t t_high = 0; uint32_t lost = 0, lost_prev = 0; -int lost_high = 0; +uint8_t lost_high = 0; +uint16_t t_sleep = 0; // file variables FILE *fp; @@ -51,25 +52,37 @@ uint32_t L; float batt; -uint8_t bla; +float f; int main(void){ if( DEBUG ){ LDC1101 *ldc = new LDC1101(PTC6, PTC7, PTC5, PTC4, C, 16E6); + wait(1); + f = ldc->get_fsensor(); + while(1){ while( !ldc->is_New_LHR_data() ){ } // wait until data is ready - pc.printf( "%d\r\n", ldc->get_LHR_Data() ); + L = ldc->get_LHR_Data(); + pc.printf( "%d", L ); + //pc.printf( "%.3f MHz", ldc->get_fsensor()/1000000 ); + //pc.printf( "%.3f KHz", (f - ldc->get_fsensor())/1000 ); + + pc.printf( "\r\n" ); + wait( 10 ); // 20 Hz } } bob.flash(2); - + pc.printf( "check sd: %d\r\n", bob.checkSD() ); // Load SD File system + + + // bob.wakeup(); bob.SDon(); SDFileSystem *sd = new SDFileSystem(PTD6, PTD7, PTD5, PTD4, "sd"); @@ -92,22 +105,11 @@ fclose( fp ); } } - - // 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); - + // Unload SD File system delete sd; sd = NULL; - while(1) { // SD on + sensor on @@ -120,12 +122,21 @@ 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; + // time + prev = now; // 0 -> 429 496 --> (4 294,96 s) (71 min) + now = (uint32_t) clock(); // 0 -> 429 496 --> (4 294,96 s) (71 min) + if( now < prev ) t_high++; // 0 -> 255 --> (255*4 294,96 s) (12 days) + S = now + 429496.7296*t_high + lost + 429496.7296*lost_high; // 0 -> 219 901 952 --> (2 199 019,52 s) (25 days) + + // first time take a comfortably long period + if( next == 0 ){ + next = S + (INTERVAL_ON + INTERVAL_OFF)*100; + }else{ + next = S + INTERVAL_ON*100; // 0 -> 219 904 952 --> (2 199 049,52 s) (25 days) + } // Take samples for INTERVAL_ON seconds - while( (uint32_t) clock() < next ) + while( S < next ) { // Collect a package 16 data points while( Lvector.size() < 16 ) // Write is per 512 bits: 16 * 32 = 512 @@ -139,27 +150,30 @@ now = (uint32_t) clock(); if( now < prev ) t_high++; S = now + 429496.7296*t_high + lost + 429496.7296*lost_high; - Svector.push_back( S ); // induction L = ldc->get_LHR_Data(); + + // Store data in temporal memory + Svector.push_back( S ); Lvector.push_back( L ); } } + + // pc.printf("%d; %02x\r\n", L, ldc->get_LHR_status() ); + // battery level batt = bob.battery(); // Store the package of 16 data points - pc.printf( "Writing %d samples of data to SD card: Time = %.2f; Inductance = %d; Battery = %.4f\r\n", Lvector.size(), (float) Svector.at(15)/100.0, Lvector.at(15), batt ); - bob.green(); 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", (float) Svector.at(i)/100.0, Lvector.at(i), batt ); // write to file fclose( fp ); // close file bob.greenoff(); - + + // Release data Lvector.clear(); Svector.clear(); @@ -181,15 +195,25 @@ 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 - pc.printf( "zzz...\r\n\r\n" ); + - // Sleep for INTERVAL_OFF samples - bob.sleep( (float) INTERVAL_OFF - (clock() - next)/100.0 ); + // Calculate sleeping time (correction to INTERVAL_OFF) + prev = now; + now = (uint32_t) clock(); + if( now < prev ) t_high++; + S = now + 429496.7296*t_high + lost + 429496.7296*lost_high; + t_sleep = ( INTERVAL_OFF*100 - (S - next) ); + // Add lost time to the counter lost_prev = lost; - lost += (INTERVAL_OFF)*100; + lost += t_sleep; if( lost < lost_prev ) lost_high++; + + // Sleep now... + // pc.printf( "zzz... (%f) \r\n\r\n", (float) (t_sleep / 100.0) ); + bob.sleep( (float) ( t_sleep / 100.0) ); + } }