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:
Wed Feb 24 16:26:37 2016 +0000
Revision:
10:3cab80866536
Parent:
9:47f1b1c0ef8b
Child:
11:599ca9982e45
Thermometer (DS1825) included, ready for first run, all code in main.cpp will tidy up after first trial

Who changed what in which revision?

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