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 Bob Giesberts

Committer:
bobgiesberts
Date:
Tue Feb 16 15:55:19 2016 +0000
Revision:
8:8cc1960467ae
Parent:
7:6c4cac1ec122
Child:
9:47f1b1c0ef8b
Included more features from the LDC1101.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bobgiesberts 0:e81b68888268 1 #include "mbed.h"
bobgiesberts 0:e81b68888268 2 #include "LDC1101.h"
bobgiesberts 1:22c272515015 3 #include "SDFileSystem.h"
bobgiesberts 2:1a203732fc95 4 #include "Bob.h"
bobgiesberts 0:e81b68888268 5 #include <iostream>
bobgiesberts 5:736a81a59f3c 6 #include <vector>
bobgiesberts 4:ae441c5727b9 7 #include <string>
bobgiesberts 0:e81b68888268 8 using namespace std;
bobgiesberts 0:e81b68888268 9
bobgiesberts 2:1a203732fc95 10 /**
bobgiesberts 2:1a203732fc95 11 * @file main.cpp
bobgiesberts 2:1a203732fc95 12 * @brief This file programs the processor for the inductive force sensor
bobgiesberts 2:1a203732fc95 13 * using the library LDC1101.h and LDC1101.cpp.
bobgiesberts 2:1a203732fc95 14 * - Red Led: processing communication with LDC1101
bobgiesberts 2:1a203732fc95 15 * - Green Led: processing SD card
bobgiesberts 4:ae441c5727b9 16 *
bobgiesberts 4:ae441c5727b9 17 * Log protocol:
bobgiesberts 4:ae441c5727b9 18 * - 1 minute at 20 Hz
bobgiesberts 4:ae441c5727b9 19 * - 29 minutes rest
bobgiesberts 2:1a203732fc95 20 *
bobgiesberts 2:1a203732fc95 21 * @author Bob Giesberts
bobgiesberts 2:1a203732fc95 22 *
bobgiesberts 2:1a203732fc95 23 * @date 2015-12-17
bobgiesberts 2:1a203732fc95 24 */
bobgiesberts 0:e81b68888268 25
bobgiesberts 5:736a81a59f3c 26 // SETTINGS
bobgiesberts 6:ff39d60061ca 27 bool DEBUG = false;
bobgiesberts 6:ff39d60061ca 28 float C = 120E-12; // pF
bobgiesberts 8:8cc1960467ae 29 int INTERVAL_ON = 1; // 30 = 30 sec
bobgiesberts 8:8cc1960467ae 30 int INTERVAL_OFF = 60; // 1770 = 29*60+30 sec
bobgiesberts 0:e81b68888268 31
bobgiesberts 5:736a81a59f3c 32 // load libraries
bobgiesberts 5:736a81a59f3c 33 Bob bob(PTB0, PTB1, PTC3, PTE0, PTC2, PTE30); // red led, green led, sd_enable, sd_present, batt, 3V3_DET
bobgiesberts 5:736a81a59f3c 34 Serial pc(USBTX, USBRX);
bobgiesberts 4:ae441c5727b9 35
bobgiesberts 4:ae441c5727b9 36 // timer variables
bobgiesberts 6:ff39d60061ca 37 uint32_t now = 0, next = 0, prev = 0;
bobgiesberts 8:8cc1960467ae 38 uint8_t t_high = 0;
bobgiesberts 6:ff39d60061ca 39 uint32_t lost = 0, lost_prev = 0;
bobgiesberts 8:8cc1960467ae 40 uint8_t lost_high = 0;
bobgiesberts 8:8cc1960467ae 41 uint16_t t_sleep = 0;
bobgiesberts 4:ae441c5727b9 42
bobgiesberts 4:ae441c5727b9 43 // file variables
bobgiesberts 4:ae441c5727b9 44 FILE *fp;
bobgiesberts 4:ae441c5727b9 45 string filename = "/sd/data00.txt";
bobgiesberts 4:ae441c5727b9 46 const char *fn;
bobgiesberts 4:ae441c5727b9 47
bobgiesberts 5:736a81a59f3c 48 // temporal storage for data samples
bobgiesberts 7:6c4cac1ec122 49 vector < uint32_t > Svector;
bobgiesberts 7:6c4cac1ec122 50 uint32_t S;
bobgiesberts 5:736a81a59f3c 51 vector < uint32_t > Lvector;
bobgiesberts 6:ff39d60061ca 52 uint32_t L;
bobgiesberts 5:736a81a59f3c 53 float batt;
bobgiesberts 5:736a81a59f3c 54
bobgiesberts 8:8cc1960467ae 55 float f;
bobgiesberts 6:ff39d60061ca 56
bobgiesberts 0:e81b68888268 57 int main(void){
bobgiesberts 5:736a81a59f3c 58
bobgiesberts 7:6c4cac1ec122 59 if( DEBUG ){
bobgiesberts 7:6c4cac1ec122 60 LDC1101 *ldc = new LDC1101(PTC6, PTC7, PTC5, PTC4, C, 16E6);
bobgiesberts 7:6c4cac1ec122 61
bobgiesberts 8:8cc1960467ae 62 wait(1);
bobgiesberts 8:8cc1960467ae 63 f = ldc->get_fsensor();
bobgiesberts 8:8cc1960467ae 64
bobgiesberts 7:6c4cac1ec122 65 while(1){
bobgiesberts 7:6c4cac1ec122 66 while( !ldc->is_New_LHR_data() ){ } // wait until data is ready
bobgiesberts 7:6c4cac1ec122 67
bobgiesberts 8:8cc1960467ae 68 L = ldc->get_LHR_Data();
bobgiesberts 8:8cc1960467ae 69 pc.printf( "%d", L );
bobgiesberts 8:8cc1960467ae 70 //pc.printf( "%.3f MHz", ldc->get_fsensor()/1000000 );
bobgiesberts 8:8cc1960467ae 71 //pc.printf( "%.3f KHz", (f - ldc->get_fsensor())/1000 );
bobgiesberts 8:8cc1960467ae 72
bobgiesberts 8:8cc1960467ae 73 pc.printf( "\r\n" );
bobgiesberts 8:8cc1960467ae 74 wait( 10 ); // 20 Hz
bobgiesberts 7:6c4cac1ec122 75 }
bobgiesberts 7:6c4cac1ec122 76 }
bobgiesberts 7:6c4cac1ec122 77
bobgiesberts 7:6c4cac1ec122 78
bobgiesberts 7:6c4cac1ec122 79
bobgiesberts 6:ff39d60061ca 80 bob.flash(2);
bobgiesberts 8:8cc1960467ae 81 pc.printf( "check sd: %d\r\n", bob.checkSD() );
bobgiesberts 6:ff39d60061ca 82 // Load SD File system
bobgiesberts 8:8cc1960467ae 83
bobgiesberts 8:8cc1960467ae 84
bobgiesberts 8:8cc1960467ae 85 // bob.wakeup();
bobgiesberts 6:ff39d60061ca 86 bob.SDon();
bobgiesberts 6:ff39d60061ca 87 SDFileSystem *sd = new SDFileSystem(PTD6, PTD7, PTD5, PTD4, "sd");
bobgiesberts 5:736a81a59f3c 88
bobgiesberts 6:ff39d60061ca 89 // Create a new data file (data00.txt)
bobgiesberts 4:ae441c5727b9 90 mkdir("/sd", 0777);
bobgiesberts 4:ae441c5727b9 91 for(uint8_t i = 0; i < 100; i++) {
bobgiesberts 6:ff39d60061ca 92 filename[8] = i/10 + '0';
bobgiesberts 4:ae441c5727b9 93 filename[9] = i%10 + '0';
bobgiesberts 4:ae441c5727b9 94 fp = fopen( filename.c_str() , "r" );
bobgiesberts 4:ae441c5727b9 95 if( fp == NULL ) { // read failed so file does not exist
bobgiesberts 4:ae441c5727b9 96 fp = fopen( filename.c_str(), "w" ); // create it
bobgiesberts 4:ae441c5727b9 97 if( fp != NULL ){
bobgiesberts 4:ae441c5727b9 98 fn = filename.c_str();
bobgiesberts 6:ff39d60061ca 99 fclose( fp );
bobgiesberts 4:ae441c5727b9 100 break;
bobgiesberts 4:ae441c5727b9 101 }else{
bobgiesberts 6:ff39d60061ca 102 bob.red();
bobgiesberts 4:ae441c5727b9 103 }
bobgiesberts 4:ae441c5727b9 104 }else{ // file already exists
bobgiesberts 4:ae441c5727b9 105 fclose( fp );
bobgiesberts 4:ae441c5727b9 106 }
bobgiesberts 1:22c272515015 107 }
bobgiesberts 8:8cc1960467ae 108
bobgiesberts 6:ff39d60061ca 109 // Unload SD File system
bobgiesberts 6:ff39d60061ca 110 delete sd; sd = NULL;
bobgiesberts 5:736a81a59f3c 111
bobgiesberts 5:736a81a59f3c 112
bobgiesberts 6:ff39d60061ca 113 while(1)
bobgiesberts 6:ff39d60061ca 114 {
bobgiesberts 6:ff39d60061ca 115 // SD on + sensor on
bobgiesberts 6:ff39d60061ca 116 bob.SDon();
bobgiesberts 5:736a81a59f3c 117
bobgiesberts 6:ff39d60061ca 118 // Sensor on
bobgiesberts 6:ff39d60061ca 119 LDC1101 *ldc = new LDC1101(PTC6, PTC7, PTC5, PTC4, C, 16E6);
bobgiesberts 6:ff39d60061ca 120
bobgiesberts 6:ff39d60061ca 121 // SD on
bobgiesberts 6:ff39d60061ca 122 SDFileSystem *sd = new SDFileSystem(PTD6, PTD7, PTD5, PTD4, "sd");
bobgiesberts 6:ff39d60061ca 123 mkdir("/sd", 0777); // select folder
bobgiesberts 6:ff39d60061ca 124
bobgiesberts 8:8cc1960467ae 125 // time
bobgiesberts 8:8cc1960467ae 126 prev = now; // 0 -> 429 496 --> (4 294,96 s) (71 min)
bobgiesberts 8:8cc1960467ae 127 now = (uint32_t) clock(); // 0 -> 429 496 --> (4 294,96 s) (71 min)
bobgiesberts 8:8cc1960467ae 128 if( now < prev ) t_high++; // 0 -> 255 --> (255*4 294,96 s) (12 days)
bobgiesberts 8:8cc1960467ae 129 S = now + 429496.7296*t_high + lost + 429496.7296*lost_high; // 0 -> 219 901 952 --> (2 199 019,52 s) (25 days)
bobgiesberts 8:8cc1960467ae 130
bobgiesberts 8:8cc1960467ae 131 // first time take a comfortably long period
bobgiesberts 8:8cc1960467ae 132 if( next == 0 ){
bobgiesberts 8:8cc1960467ae 133 next = S + (INTERVAL_ON + INTERVAL_OFF)*100;
bobgiesberts 8:8cc1960467ae 134 }else{
bobgiesberts 8:8cc1960467ae 135 next = S + INTERVAL_ON*100; // 0 -> 219 904 952 --> (2 199 049,52 s) (25 days)
bobgiesberts 8:8cc1960467ae 136 }
bobgiesberts 6:ff39d60061ca 137
bobgiesberts 6:ff39d60061ca 138 // Take samples for INTERVAL_ON seconds
bobgiesberts 8:8cc1960467ae 139 while( S < next )
bobgiesberts 6:ff39d60061ca 140 {
bobgiesberts 6:ff39d60061ca 141 // Collect a package 16 data points
bobgiesberts 6:ff39d60061ca 142 while( Lvector.size() < 16 ) // Write is per 512 bits: 16 * 32 = 512
bobgiesberts 6:ff39d60061ca 143 {
bobgiesberts 6:ff39d60061ca 144 // wait for new data to be ready
bobgiesberts 7:6c4cac1ec122 145 while( !ldc->is_New_LHR_data() ) { }
bobgiesberts 6:ff39d60061ca 146
bobgiesberts 6:ff39d60061ca 147 if( !ldc->is_Oscillation_Error() ){ // sensor not overloaded
bobgiesberts 6:ff39d60061ca 148 // time
bobgiesberts 6:ff39d60061ca 149 prev = now;
bobgiesberts 6:ff39d60061ca 150 now = (uint32_t) clock();
bobgiesberts 6:ff39d60061ca 151 if( now < prev ) t_high++;
bobgiesberts 7:6c4cac1ec122 152 S = now + 429496.7296*t_high + lost + 429496.7296*lost_high;
bobgiesberts 6:ff39d60061ca 153
bobgiesberts 6:ff39d60061ca 154 // induction
bobgiesberts 6:ff39d60061ca 155 L = ldc->get_LHR_Data();
bobgiesberts 8:8cc1960467ae 156
bobgiesberts 8:8cc1960467ae 157 // Store data in temporal memory
bobgiesberts 8:8cc1960467ae 158 Svector.push_back( S );
bobgiesberts 7:6c4cac1ec122 159 Lvector.push_back( L );
bobgiesberts 6:ff39d60061ca 160 }
bobgiesberts 6:ff39d60061ca 161 }
bobgiesberts 8:8cc1960467ae 162
bobgiesberts 8:8cc1960467ae 163 // pc.printf("%d; %02x\r\n", L, ldc->get_LHR_status() );
bobgiesberts 8:8cc1960467ae 164
bobgiesberts 6:ff39d60061ca 165 // battery level
bobgiesberts 6:ff39d60061ca 166 batt = bob.battery();
bobgiesberts 5:736a81a59f3c 167
bobgiesberts 6:ff39d60061ca 168 // Store the package of 16 data points
bobgiesberts 7:6c4cac1ec122 169 bob.green();
bobgiesberts 6:ff39d60061ca 170 fp = fopen( fn, "a" ); // open file
bobgiesberts 6:ff39d60061ca 171 for( int i = 0; i < Lvector.size(); i++ )
bobgiesberts 7:6c4cac1ec122 172 fprintf( fp, "%.2f;%d;%.4f\r\n", (float) Svector.at(i)/100.0, Lvector.at(i), batt ); // write to file
bobgiesberts 6:ff39d60061ca 173 fclose( fp ); // close file
bobgiesberts 7:6c4cac1ec122 174 bob.greenoff();
bobgiesberts 8:8cc1960467ae 175
bobgiesberts 8:8cc1960467ae 176
bobgiesberts 6:ff39d60061ca 177 // Release data
bobgiesberts 6:ff39d60061ca 178 Lvector.clear();
bobgiesberts 6:ff39d60061ca 179 Svector.clear();
bobgiesberts 6:ff39d60061ca 180
bobgiesberts 6:ff39d60061ca 181 }
bobgiesberts 6:ff39d60061ca 182
bobgiesberts 6:ff39d60061ca 183 // SD off
bobgiesberts 6:ff39d60061ca 184 delete sd; sd = NULL;
bobgiesberts 6:ff39d60061ca 185 bob.SDoff();
bobgiesberts 6:ff39d60061ca 186 DigitalOut *sdP2 = new DigitalOut(PTD4); sdP2->write(0); delete sdP2; sdP2 = NULL;// cs
bobgiesberts 6:ff39d60061ca 187 DigitalOut *sdP3 = new DigitalOut(PTD6); sdP3->write(0); delete sdP3; sdP3 = NULL;// mosi
bobgiesberts 6:ff39d60061ca 188 DigitalOut *sdP5 = new DigitalOut(PTD5); sdP5->write(0); delete sdP5; sdP5 = NULL;// sck
bobgiesberts 6:ff39d60061ca 189 DigitalOut *sdP7 = new DigitalOut(PTD7); sdP7->write(0); delete sdP7; sdP7 = NULL;// miso
bobgiesberts 6:ff39d60061ca 190
bobgiesberts 6:ff39d60061ca 191 // Sensor off
bobgiesberts 6:ff39d60061ca 192 delete ldc; ldc = NULL;
bobgiesberts 6:ff39d60061ca 193 DigitalOut *senP2 = new DigitalOut(PTC7); senP2->write(0); delete senP2; senP2 = NULL; // miso
bobgiesberts 6:ff39d60061ca 194 DigitalOut *senP3 = new DigitalOut(PTC5); senP3->write(0); delete senP3; senP3 = NULL; // sck
bobgiesberts 6:ff39d60061ca 195 DigitalOut *senP4 = new DigitalOut(PTC6); senP4->write(0); delete senP4; senP4 = NULL; // mosi
bobgiesberts 6:ff39d60061ca 196 DigitalOut *senP5 = new DigitalOut(PTC4); senP5->write(0); delete senP5; senP5 = NULL; // cs
bobgiesberts 6:ff39d60061ca 197
bobgiesberts 8:8cc1960467ae 198
bobgiesberts 7:6c4cac1ec122 199
bobgiesberts 8:8cc1960467ae 200 // Calculate sleeping time (correction to INTERVAL_OFF)
bobgiesberts 8:8cc1960467ae 201 prev = now;
bobgiesberts 8:8cc1960467ae 202 now = (uint32_t) clock();
bobgiesberts 8:8cc1960467ae 203 if( now < prev ) t_high++;
bobgiesberts 8:8cc1960467ae 204 S = now + 429496.7296*t_high + lost + 429496.7296*lost_high;
bobgiesberts 6:ff39d60061ca 205
bobgiesberts 8:8cc1960467ae 206 t_sleep = ( INTERVAL_OFF*100 - (S - next) );
bobgiesberts 8:8cc1960467ae 207
bobgiesberts 6:ff39d60061ca 208 // Add lost time to the counter
bobgiesberts 6:ff39d60061ca 209 lost_prev = lost;
bobgiesberts 8:8cc1960467ae 210 lost += t_sleep;
bobgiesberts 6:ff39d60061ca 211 if( lost < lost_prev ) lost_high++;
bobgiesberts 8:8cc1960467ae 212
bobgiesberts 8:8cc1960467ae 213 // Sleep now...
bobgiesberts 8:8cc1960467ae 214 // pc.printf( "zzz... (%f) \r\n\r\n", (float) (t_sleep / 100.0) );
bobgiesberts 8:8cc1960467ae 215 bob.sleep( (float) ( t_sleep / 100.0) );
bobgiesberts 8:8cc1960467ae 216
bobgiesberts 0:e81b68888268 217 }
bobgiesberts 6:ff39d60061ca 218
bobgiesberts 0:e81b68888268 219 }