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:
Mon Jan 25 09:13:11 2016 +0000
Revision:
7:6c4cac1ec122
Parent:
6:ff39d60061ca
Child:
8:8cc1960467ae
Minor changes:;  - fixed ldc->is_New_LHR_data();  - changed time variable type (S and Svector) from float (23 bit) to uint32_t (32 bit) to prevent overflow after about 70 hours

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 7:6c4cac1ec122 29 int INTERVAL_ON = 5; // 30 = 30 sec
bobgiesberts 7:6c4cac1ec122 30 int INTERVAL_OFF = 5; // 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 4:ae441c5727b9 38 int t_high = 0;
bobgiesberts 6:ff39d60061ca 39 uint32_t lost = 0, lost_prev = 0;
bobgiesberts 6:ff39d60061ca 40 int lost_high = 0;
bobgiesberts 4:ae441c5727b9 41
bobgiesberts 4:ae441c5727b9 42 // file variables
bobgiesberts 4:ae441c5727b9 43 FILE *fp;
bobgiesberts 4:ae441c5727b9 44 string filename = "/sd/data00.txt";
bobgiesberts 4:ae441c5727b9 45 const char *fn;
bobgiesberts 4:ae441c5727b9 46
bobgiesberts 5:736a81a59f3c 47 // temporal storage for data samples
bobgiesberts 7:6c4cac1ec122 48 vector < uint32_t > Svector;
bobgiesberts 7:6c4cac1ec122 49 uint32_t S;
bobgiesberts 5:736a81a59f3c 50 vector < uint32_t > Lvector;
bobgiesberts 6:ff39d60061ca 51 uint32_t L;
bobgiesberts 5:736a81a59f3c 52 float batt;
bobgiesberts 5:736a81a59f3c 53
bobgiesberts 7:6c4cac1ec122 54 uint8_t bla;
bobgiesberts 6:ff39d60061ca 55
bobgiesberts 0:e81b68888268 56 int main(void){
bobgiesberts 5:736a81a59f3c 57
bobgiesberts 7:6c4cac1ec122 58 if( DEBUG ){
bobgiesberts 7:6c4cac1ec122 59 LDC1101 *ldc = new LDC1101(PTC6, PTC7, PTC5, PTC4, C, 16E6);
bobgiesberts 7:6c4cac1ec122 60
bobgiesberts 7:6c4cac1ec122 61 while(1){
bobgiesberts 7:6c4cac1ec122 62 while( !ldc->is_New_LHR_data() ){ } // wait until data is ready
bobgiesberts 7:6c4cac1ec122 63
bobgiesberts 7:6c4cac1ec122 64 pc.printf( "%d\r\n", ldc->get_LHR_Data() );
bobgiesberts 7:6c4cac1ec122 65 }
bobgiesberts 7:6c4cac1ec122 66 }
bobgiesberts 7:6c4cac1ec122 67
bobgiesberts 7:6c4cac1ec122 68
bobgiesberts 7:6c4cac1ec122 69
bobgiesberts 6:ff39d60061ca 70 bob.flash(2);
bobgiesberts 2:1a203732fc95 71
bobgiesberts 6:ff39d60061ca 72 // Load SD File system
bobgiesberts 6:ff39d60061ca 73 bob.SDon();
bobgiesberts 6:ff39d60061ca 74 SDFileSystem *sd = new SDFileSystem(PTD6, PTD7, PTD5, PTD4, "sd");
bobgiesberts 5:736a81a59f3c 75
bobgiesberts 6:ff39d60061ca 76 // Create a new data file (data00.txt)
bobgiesberts 4:ae441c5727b9 77 mkdir("/sd", 0777);
bobgiesberts 4:ae441c5727b9 78 for(uint8_t i = 0; i < 100; i++) {
bobgiesberts 6:ff39d60061ca 79 filename[8] = i/10 + '0';
bobgiesberts 4:ae441c5727b9 80 filename[9] = i%10 + '0';
bobgiesberts 4:ae441c5727b9 81 fp = fopen( filename.c_str() , "r" );
bobgiesberts 4:ae441c5727b9 82 if( fp == NULL ) { // read failed so file does not exist
bobgiesberts 4:ae441c5727b9 83 fp = fopen( filename.c_str(), "w" ); // create it
bobgiesberts 4:ae441c5727b9 84 if( fp != NULL ){
bobgiesberts 4:ae441c5727b9 85 fn = filename.c_str();
bobgiesberts 6:ff39d60061ca 86 fclose( fp );
bobgiesberts 4:ae441c5727b9 87 break;
bobgiesberts 4:ae441c5727b9 88 }else{
bobgiesberts 6:ff39d60061ca 89 bob.red();
bobgiesberts 4:ae441c5727b9 90 }
bobgiesberts 4:ae441c5727b9 91 }else{ // file already exists
bobgiesberts 4:ae441c5727b9 92 fclose( fp );
bobgiesberts 4:ae441c5727b9 93 }
bobgiesberts 1:22c272515015 94 }
bobgiesberts 2:1a203732fc95 95
bobgiesberts 2:1a203732fc95 96 // Write current settings to data file
bobgiesberts 6:ff39d60061ca 97 // fp = fopen( fn, "w" );
bobgiesberts 6:ff39d60061ca 98 // fprintf(fp, "DIVIDER : %d\r\n", ldc->get_divider());
bobgiesberts 6:ff39d60061ca 99 // fprintf(fp, "RESPONSETIME : %d\r\n", ldc->get_responsetime());
bobgiesberts 6:ff39d60061ca 100 // fprintf(fp, "RP_MIN : %.2f\r\n", ldc->get_RPmin());
bobgiesberts 6:ff39d60061ca 101 // fprintf(fp, "LHR_RCOUNT : %d\r\n", ldc->get_Rcount());
bobgiesberts 6:ff39d60061ca 102 // fprintf(fp, "C : %f\r\n", C);
bobgiesberts 6:ff39d60061ca 103 // fprintf(fp, "\r\n\r\n");
bobgiesberts 6:ff39d60061ca 104 // fclose(fp);
bobgiesberts 5:736a81a59f3c 105
bobgiesberts 6:ff39d60061ca 106 // Unload SD File system
bobgiesberts 6:ff39d60061ca 107 delete sd; sd = NULL;
bobgiesberts 5:736a81a59f3c 108
bobgiesberts 5:736a81a59f3c 109
bobgiesberts 5:736a81a59f3c 110
bobgiesberts 6:ff39d60061ca 111 while(1)
bobgiesberts 6:ff39d60061ca 112 {
bobgiesberts 6:ff39d60061ca 113 // SD on + sensor on
bobgiesberts 6:ff39d60061ca 114 bob.SDon();
bobgiesberts 5:736a81a59f3c 115
bobgiesberts 6:ff39d60061ca 116 // Sensor on
bobgiesberts 6:ff39d60061ca 117 LDC1101 *ldc = new LDC1101(PTC6, PTC7, PTC5, PTC4, C, 16E6);
bobgiesberts 6:ff39d60061ca 118
bobgiesberts 6:ff39d60061ca 119 // SD on
bobgiesberts 6:ff39d60061ca 120 SDFileSystem *sd = new SDFileSystem(PTD6, PTD7, PTD5, PTD4, "sd");
bobgiesberts 6:ff39d60061ca 121 mkdir("/sd", 0777); // select folder
bobgiesberts 6:ff39d60061ca 122
bobgiesberts 6:ff39d60061ca 123 // clock
bobgiesberts 7:6c4cac1ec122 124 now = (uint32_t) clock();
bobgiesberts 6:ff39d60061ca 125 next = (uint32_t) now + INTERVAL_ON*100;
bobgiesberts 6:ff39d60061ca 126
bobgiesberts 6:ff39d60061ca 127 // Take samples for INTERVAL_ON seconds
bobgiesberts 6:ff39d60061ca 128 while( (uint32_t) clock() < next )
bobgiesberts 6:ff39d60061ca 129 {
bobgiesberts 6:ff39d60061ca 130 // Collect a package 16 data points
bobgiesberts 6:ff39d60061ca 131 while( Lvector.size() < 16 ) // Write is per 512 bits: 16 * 32 = 512
bobgiesberts 6:ff39d60061ca 132 {
bobgiesberts 6:ff39d60061ca 133 // wait for new data to be ready
bobgiesberts 7:6c4cac1ec122 134 while( !ldc->is_New_LHR_data() ) { }
bobgiesberts 6:ff39d60061ca 135
bobgiesberts 6:ff39d60061ca 136 if( !ldc->is_Oscillation_Error() ){ // sensor not overloaded
bobgiesberts 6:ff39d60061ca 137 // time
bobgiesberts 6:ff39d60061ca 138 prev = now;
bobgiesberts 6:ff39d60061ca 139 now = (uint32_t) clock();
bobgiesberts 6:ff39d60061ca 140 if( now < prev ) t_high++;
bobgiesberts 7:6c4cac1ec122 141 S = now + 429496.7296*t_high + lost + 429496.7296*lost_high;
bobgiesberts 6:ff39d60061ca 142 Svector.push_back( S );
bobgiesberts 6:ff39d60061ca 143
bobgiesberts 6:ff39d60061ca 144 // induction
bobgiesberts 6:ff39d60061ca 145 L = ldc->get_LHR_Data();
bobgiesberts 7:6c4cac1ec122 146 Lvector.push_back( L );
bobgiesberts 6:ff39d60061ca 147 }
bobgiesberts 6:ff39d60061ca 148 }
bobgiesberts 6:ff39d60061ca 149 // battery level
bobgiesberts 6:ff39d60061ca 150 batt = bob.battery();
bobgiesberts 5:736a81a59f3c 151
bobgiesberts 6:ff39d60061ca 152 // Store the package of 16 data points
bobgiesberts 7:6c4cac1ec122 153 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 );
bobgiesberts 7:6c4cac1ec122 154
bobgiesberts 7:6c4cac1ec122 155 bob.green();
bobgiesberts 6:ff39d60061ca 156 fp = fopen( fn, "a" ); // open file
bobgiesberts 6:ff39d60061ca 157 // if( fp == NULL ) { pc.printf("error (append)...\r\n"); }
bobgiesberts 6:ff39d60061ca 158 for( int i = 0; i < Lvector.size(); i++ )
bobgiesberts 7:6c4cac1ec122 159 fprintf( fp, "%.2f;%d;%.4f\r\n", (float) Svector.at(i)/100.0, Lvector.at(i), batt ); // write to file
bobgiesberts 6:ff39d60061ca 160 fclose( fp ); // close file
bobgiesberts 7:6c4cac1ec122 161 bob.greenoff();
bobgiesberts 6:ff39d60061ca 162
bobgiesberts 6:ff39d60061ca 163 // Release data
bobgiesberts 6:ff39d60061ca 164 Lvector.clear();
bobgiesberts 6:ff39d60061ca 165 Svector.clear();
bobgiesberts 6:ff39d60061ca 166
bobgiesberts 6:ff39d60061ca 167 }
bobgiesberts 6:ff39d60061ca 168
bobgiesberts 6:ff39d60061ca 169 // SD off
bobgiesberts 6:ff39d60061ca 170 delete sd; sd = NULL;
bobgiesberts 6:ff39d60061ca 171 bob.SDoff();
bobgiesberts 6:ff39d60061ca 172 DigitalOut *sdP2 = new DigitalOut(PTD4); sdP2->write(0); delete sdP2; sdP2 = NULL;// cs
bobgiesberts 6:ff39d60061ca 173 DigitalOut *sdP3 = new DigitalOut(PTD6); sdP3->write(0); delete sdP3; sdP3 = NULL;// mosi
bobgiesberts 6:ff39d60061ca 174 DigitalOut *sdP5 = new DigitalOut(PTD5); sdP5->write(0); delete sdP5; sdP5 = NULL;// sck
bobgiesberts 6:ff39d60061ca 175 DigitalOut *sdP7 = new DigitalOut(PTD7); sdP7->write(0); delete sdP7; sdP7 = NULL;// miso
bobgiesberts 6:ff39d60061ca 176
bobgiesberts 6:ff39d60061ca 177 // Sensor off
bobgiesberts 6:ff39d60061ca 178 delete ldc; ldc = NULL;
bobgiesberts 6:ff39d60061ca 179 DigitalOut *senP2 = new DigitalOut(PTC7); senP2->write(0); delete senP2; senP2 = NULL; // miso
bobgiesberts 6:ff39d60061ca 180 DigitalOut *senP3 = new DigitalOut(PTC5); senP3->write(0); delete senP3; senP3 = NULL; // sck
bobgiesberts 6:ff39d60061ca 181 DigitalOut *senP4 = new DigitalOut(PTC6); senP4->write(0); delete senP4; senP4 = NULL; // mosi
bobgiesberts 6:ff39d60061ca 182 DigitalOut *senP5 = new DigitalOut(PTC4); senP5->write(0); delete senP5; senP5 = NULL; // cs
bobgiesberts 6:ff39d60061ca 183
bobgiesberts 7:6c4cac1ec122 184 pc.printf( "zzz...\r\n\r\n" );
bobgiesberts 7:6c4cac1ec122 185
bobgiesberts 6:ff39d60061ca 186 // Sleep for INTERVAL_OFF samples
bobgiesberts 6:ff39d60061ca 187 bob.sleep( (float) INTERVAL_OFF - (clock() - next)/100.0 );
bobgiesberts 6:ff39d60061ca 188
bobgiesberts 6:ff39d60061ca 189 // Add lost time to the counter
bobgiesberts 6:ff39d60061ca 190 lost_prev = lost;
bobgiesberts 7:6c4cac1ec122 191 lost += (INTERVAL_OFF)*100;
bobgiesberts 6:ff39d60061ca 192 if( lost < lost_prev ) lost_high++;
bobgiesberts 0:e81b68888268 193 }
bobgiesberts 6:ff39d60061ca 194
bobgiesberts 0:e81b68888268 195 }