Communication with LDC1101. Working version, ready for extensive calibration tests for resolution, linearity, etc.
Dependencies: Bob DS1825 LDC1101 SDFileSystem mbed
Fork of Inductive_Sensor by
main.cpp
- Committer:
- bobgiesberts
- Date:
- 2016-01-18
- Revision:
- 6:ff39d60061ca
- Parent:
- 5:736a81a59f3c
- Child:
- 7:6c4cac1ec122
File content as of revision 6:ff39d60061ca:
#include "mbed.h" #include "LDC1101.h" #include "SDFileSystem.h" #include "Bob.h" #include <iostream> #include <vector> #include <string> using namespace std; /** * @file main.cpp * @brief This file programs the processor for the inductive force sensor * 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 * * @date 2015-12-17 */ // SETTINGS 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 uint32_t now = 0, next = 0, prev = 0; int t_high = 0; uint32_t lost = 0, lost_prev = 0; int lost_high = 0; // file variables FILE *fp; string filename = "/sd/data00.txt"; const char *fn; // temporal storage for data samples vector < float > Svector; float S; vector < uint32_t > Lvector; uint32_t L; float batt; int main(void){ bob.flash(2); // Load SD File system bob.SDon(); SDFileSystem *sd = new SDFileSystem(PTD6, PTD7, PTD5, PTD4, "sd"); // Create a new data file (data00.txt) mkdir("/sd", 0777); for(uint8_t i = 0; i < 100; i++) { 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(); fclose( fp ); break; }else{ bob.red(); } }else{ // file already exists 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 bob.SDon(); // 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(); // 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++; } }