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 18 15:43:07 2016 +0000
Revision:
6:ff39d60061ca
Parent:
5:736a81a59f3c
Child:
7:6c4cac1ec122
Beautifully working version of the code. Current during rest reduced to 0.196 mA. During sampling approximately 15-20 mA. Measuring for 30 seconds and resting for half an hour results in a theoretic battery life of 14 days.

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