Version 3.0: Switching to newer LDC1614 which is placed on the same PCB.
Dependencies: Bob DS1825 LDC1614 LDC1101 SDFileSystem mbed
Fork of Inductive_Sensor by
main.cpp@19:c0707d20bada, 2016-09-10 (annotated)
- Committer:
- bobgiesberts
- Date:
- Sat Sep 10 12:53:23 2016 +0000
- Revision:
- 19:c0707d20bada
- Parent:
- 17:a4700b1c3c37
- Child:
- 20:1c5ea04a3b2a
Working version with the new LDC1614
Who changed what in which revision?
User | Revision | Line number | New 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 | 15:5de8394fdb3c | 4 | * using the library LDC1614.h and LDC1614.cpp. |
bobgiesberts | 15:5de8394fdb3c | 5 | * - Green Led: processing communication with LDC1614 |
bobgiesberts | 15:5de8394fdb3c | 6 | * - Red Led: error |
bobgiesberts | 10:3cab80866536 | 7 | * |
bobgiesberts | 4:ae441c5727b9 | 8 | * Log protocol: |
bobgiesberts | 15:5de8394fdb3c | 9 | * - 0:30 minutes at 13 Hz |
bobgiesberts | 13:2caedc56b863 | 10 | * - 14:30 minutes rest |
bobgiesberts | 2:1a203732fc95 | 11 | * |
bobgiesberts | 2:1a203732fc95 | 12 | * @author Bob Giesberts |
bobgiesberts | 2:1a203732fc95 | 13 | * |
bobgiesberts | 15:5de8394fdb3c | 14 | * @date 2016-08-23 |
bobgiesberts | 2:1a203732fc95 | 15 | */ |
bobgiesberts | 10:3cab80866536 | 16 | #include "mbed.h" |
bobgiesberts | 15:5de8394fdb3c | 17 | #include "LDC1614.h" // inductive force sensor |
bobgiesberts | 11:599ca9982e45 | 18 | #include "SDFileSystem.h" // control the SD card |
bobgiesberts | 11:599ca9982e45 | 19 | #include "Bob.h" // processorboard |
bobgiesberts | 11:599ca9982e45 | 20 | #include "DS1825.h" // thermometer |
bobgiesberts | 10:3cab80866536 | 21 | |
bobgiesberts | 10:3cab80866536 | 22 | #include <iostream> |
bobgiesberts | 10:3cab80866536 | 23 | #include <vector> |
bobgiesberts | 10:3cab80866536 | 24 | #include <string> |
bobgiesberts | 10:3cab80866536 | 25 | using namespace std; |
bobgiesberts | 10:3cab80866536 | 26 | |
bobgiesberts | 17:a4700b1c3c37 | 27 | |
bobgiesberts | 17:a4700b1c3c37 | 28 | |
bobgiesberts | 17:a4700b1c3c37 | 29 | bool DEBUG = true; |
bobgiesberts | 17:a4700b1c3c37 | 30 | |
bobgiesberts | 17:a4700b1c3c37 | 31 | |
bobgiesberts | 17:a4700b1c3c37 | 32 | |
bobgiesberts | 5:736a81a59f3c | 33 | // SETTINGS |
bobgiesberts | 12:cceece4f3afb | 34 | float C = 120E-12; // Capacitor in F |
bobgiesberts | 19:c0707d20bada | 35 | int sensors = 1; // number of attached sensors |
bobgiesberts | 13:2caedc56b863 | 36 | uint16_t INTERVAL_FIRST = 3600; // First sampling time in seconds. 60:00 minutes = 3600 sec |
bobgiesberts | 12:cceece4f3afb | 37 | uint16_t INTERVAL_OFF = 870; // Waiting interval in seconds. 14:30 minutes = 870 sec (14*60+30) |
bobgiesberts | 12:cceece4f3afb | 38 | uint16_t INTERVAL_ON = 30; // Sampling interval in seconds. 0:30 minutes = 30 sec |
bobgiesberts | 12:cceece4f3afb | 39 | |
bobgiesberts | 15:5de8394fdb3c | 40 | // Leds |
bobgiesberts | 15:5de8394fdb3c | 41 | PinName _LED_PROCESS = PTB1; // Green led (PTB0 for the first sensor) // old = new: PTB1 |
bobgiesberts | 15:5de8394fdb3c | 42 | PinName _LED_ERROR = PTB0; // Red led // new: PTB0; |
bobgiesberts | 15:5de8394fdb3c | 43 | |
bobgiesberts | 15:5de8394fdb3c | 44 | // Thermometer |
bobgiesberts | 15:5de8394fdb3c | 45 | PinName _Tpin = PTC1; // OneWire system (PTB1 for the first sensor) // old = PTB0, new: PTC1 |
bobgiesberts | 15:5de8394fdb3c | 46 | |
bobgiesberts | 15:5de8394fdb3c | 47 | // LDC1614 |
bobgiesberts | 15:5de8394fdb3c | 48 | PinName _LDC_SDA = PTC5; // I2C: SDA |
bobgiesberts | 15:5de8394fdb3c | 49 | PinName _LDC_SCL = PTC4; // I2C: SCL |
bobgiesberts | 15:5de8394fdb3c | 50 | PinName _LDC_SD = PTC6; // Shutdown |
bobgiesberts | 15:5de8394fdb3c | 51 | |
bobgiesberts | 15:5de8394fdb3c | 52 | // SD File system |
bobgiesberts | 15:5de8394fdb3c | 53 | PinName _SD_PRESENT = PTE0; // card detect // old = new: PTE0 |
bobgiesberts | 15:5de8394fdb3c | 54 | PinName _SD_MOSI = PTD6; // mosi // old = new: PTD6 |
bobgiesberts | 15:5de8394fdb3c | 55 | PinName _SD_MISO = PTD7; // miso // old = new: PTD7 |
bobgiesberts | 15:5de8394fdb3c | 56 | PinName _SD_SCLK = PTD5; // sclk // old = new: PTD5 |
bobgiesberts | 15:5de8394fdb3c | 57 | PinName _SD_CS = PTD4; // cs // old = new: PTD4 |
bobgiesberts | 15:5de8394fdb3c | 58 | |
bobgiesberts | 15:5de8394fdb3c | 59 | // Other components |
bobgiesberts | 17:a4700b1c3c37 | 60 | PinName _ENABLE = PTC3; // enables SD card and Crystal |
bobgiesberts | 15:5de8394fdb3c | 61 | PinName _BATTERY = PTC2; // voltage // old = new: PTC2 |
bobgiesberts | 15:5de8394fdb3c | 62 | PinName _BUTTON = PTA4; |
bobgiesberts | 12:cceece4f3afb | 63 | |
bobgiesberts | 0:e81b68888268 | 64 | |
bobgiesberts | 5:736a81a59f3c | 65 | // load libraries |
bobgiesberts | 17:a4700b1c3c37 | 66 | Bob bob( _LED_PROCESS, _LED_ERROR, _BUTTON, _ENABLE, _SD_PRESENT, _BATTERY ); |
bobgiesberts | 17:a4700b1c3c37 | 67 | Serial pc( USBTX, USBRX ); |
bobgiesberts | 4:ae441c5727b9 | 68 | |
bobgiesberts | 4:ae441c5727b9 | 69 | // timer variables |
bobgiesberts | 6:ff39d60061ca | 70 | uint32_t now = 0, next = 0, prev = 0; |
bobgiesberts | 8:8cc1960467ae | 71 | uint8_t t_high = 0; |
bobgiesberts | 11:599ca9982e45 | 72 | uint32_t t_lost = 0, lost_prev = 0; |
bobgiesberts | 8:8cc1960467ae | 73 | uint8_t lost_high = 0; |
bobgiesberts | 11:599ca9982e45 | 74 | uint32_t t_sleep = 0; |
bobgiesberts | 12:cceece4f3afb | 75 | uint32_t t; |
bobgiesberts | 4:ae441c5727b9 | 76 | |
bobgiesberts | 4:ae441c5727b9 | 77 | // file variables |
bobgiesberts | 4:ae441c5727b9 | 78 | FILE *fp; |
bobgiesberts | 4:ae441c5727b9 | 79 | string filename = "/sd/data00.txt"; |
bobgiesberts | 4:ae441c5727b9 | 80 | const char *fn; |
bobgiesberts | 4:ae441c5727b9 | 81 | |
bobgiesberts | 11:599ca9982e45 | 82 | // temperature variables |
bobgiesberts | 11:599ca9982e45 | 83 | uint8_t Tdata[9]; |
bobgiesberts | 11:599ca9982e45 | 84 | int16_t T_bin; |
bobgiesberts | 11:599ca9982e45 | 85 | float T; |
bobgiesberts | 11:599ca9982e45 | 86 | |
bobgiesberts | 5:736a81a59f3c | 87 | // temporal storage for data samples |
bobgiesberts | 12:cceece4f3afb | 88 | const uint8_t package_size = 60; // ±12-13 Hz, so 60 would take about 5 sec. |
bobgiesberts | 12:cceece4f3afb | 89 | struct mydata { |
bobgiesberts | 12:cceece4f3afb | 90 | uint32_t t[package_size]; // time (s) |
bobgiesberts | 12:cceece4f3afb | 91 | uint32_t L[package_size]; // LHR_DATA |
bobgiesberts | 12:cceece4f3afb | 92 | } collected; |
bobgiesberts | 12:cceece4f3afb | 93 | uint8_t counter = 0; |
bobgiesberts | 15:5de8394fdb3c | 94 | uint8_t status; |
bobgiesberts | 12:cceece4f3afb | 95 | |
bobgiesberts | 12:cceece4f3afb | 96 | // function to write all data to the SD card |
bobgiesberts | 12:cceece4f3afb | 97 | void storeit( float T ) |
bobgiesberts | 12:cceece4f3afb | 98 | { |
bobgiesberts | 13:2caedc56b863 | 99 | // TODO: writing to SD card more efficient! |
bobgiesberts | 13:2caedc56b863 | 100 | // t 32-bit | 0.00 2000000.00 3-10x8-bit | FFFFFFFF 8x8 |
bobgiesberts | 13:2caedc56b863 | 101 | // L 24-bit | 0 1677216 6- 7x8-bit | FFFFFF 6x8 |
bobgiesberts | 13:2caedc56b863 | 102 | // V 8-bit | 3.10 4.20 4x8-bit | FF 2x8 |
bobgiesberts | 13:2caedc56b863 | 103 | // T 16-bit | -20.0000 85.0000 7x8-bit | FFFF 4x8 |
bobgiesberts | 13:2caedc56b863 | 104 | |
bobgiesberts | 13:2caedc56b863 | 105 | |
bobgiesberts | 13:2caedc56b863 | 106 | // construct data |
bobgiesberts | 13:2caedc56b863 | 107 | /* |
bobgiesberts | 17:a4700b1c3c37 | 108 | bob.processing(); |
bobgiesberts | 13:2caedc56b863 | 109 | // fp = fopen( fn, "a" ); // open file (append) |
bobgiesberts | 13:2caedc56b863 | 110 | char buffer[16]; memset(buffer, 0, sizeof(buffer)); |
bobgiesberts | 13:2caedc56b863 | 111 | char towrite[64]; memset(towrite, 0, sizeof(towrite)); |
bobgiesberts | 13:2caedc56b863 | 112 | int n = 0, m = 0; |
bobgiesberts | 13:2caedc56b863 | 113 | for( int i = 0; i < counter; i++ ) |
bobgiesberts | 13:2caedc56b863 | 114 | { |
bobgiesberts | 13:2caedc56b863 | 115 | for( int j = 0; j < 4; j++ ) |
bobgiesberts | 13:2caedc56b863 | 116 | { |
bobgiesberts | 13:2caedc56b863 | 117 | // build up next 'word' |
bobgiesberts | 13:2caedc56b863 | 118 | switch(j) |
bobgiesberts | 13:2caedc56b863 | 119 | { |
bobgiesberts | 13:2caedc56b863 | 120 | case 0: n = sprintf(buffer, "%.2f;", (float) collected.t[i]/100.0); break; |
bobgiesberts | 13:2caedc56b863 | 121 | case 1: n = sprintf(buffer, "%d;", collected.L[i]); break; |
bobgiesberts | 13:2caedc56b863 | 122 | case 2: n = sprintf(buffer, "%.2f;", bob.battery()); break; |
bobgiesberts | 13:2caedc56b863 | 123 | case 3: n = sprintf(buffer, "%.4f\r\n", T); break; |
bobgiesberts | 13:2caedc56b863 | 124 | } |
bobgiesberts | 13:2caedc56b863 | 125 | |
bobgiesberts | 13:2caedc56b863 | 126 | // calculate if there is still room left for that word |
bobgiesberts | 13:2caedc56b863 | 127 | if( strlen(towrite) + n > 64 ) |
bobgiesberts | 13:2caedc56b863 | 128 | { |
bobgiesberts | 13:2caedc56b863 | 129 | |
bobgiesberts | 13:2caedc56b863 | 130 | // pc.printf( "%s", towrite); |
bobgiesberts | 13:2caedc56b863 | 131 | fp = fopen( fn, "a" ); // open file (append) |
bobgiesberts | 13:2caedc56b863 | 132 | fprintf( fp, "%s", towrite); |
bobgiesberts | 13:2caedc56b863 | 133 | fclose( fp ); // close file |
bobgiesberts | 13:2caedc56b863 | 134 | |
bobgiesberts | 13:2caedc56b863 | 135 | memset(towrite, 0, sizeof(towrite)); |
bobgiesberts | 13:2caedc56b863 | 136 | } |
bobgiesberts | 13:2caedc56b863 | 137 | |
bobgiesberts | 13:2caedc56b863 | 138 | // build up letter with the next word |
bobgiesberts | 13:2caedc56b863 | 139 | m = sprintf(towrite, "%s%s", towrite, buffer); |
bobgiesberts | 13:2caedc56b863 | 140 | } |
bobgiesberts | 13:2caedc56b863 | 141 | } |
bobgiesberts | 13:2caedc56b863 | 142 | if(m > 0) |
bobgiesberts | 13:2caedc56b863 | 143 | { |
bobgiesberts | 13:2caedc56b863 | 144 | fp = fopen( fn, "a" ); // open file (append) |
bobgiesberts | 13:2caedc56b863 | 145 | fprintf( fp, "%s", towrite); |
bobgiesberts | 13:2caedc56b863 | 146 | fclose( fp ); // close file |
bobgiesberts | 13:2caedc56b863 | 147 | //pc.printf( "%s", towrite); |
bobgiesberts | 13:2caedc56b863 | 148 | } |
bobgiesberts | 13:2caedc56b863 | 149 | //fclose( fp ); // close file |
bobgiesberts | 17:a4700b1c3c37 | 150 | bob.no_processing(); |
bobgiesberts | 15:5de8394fdb3c | 151 | */ |
bobgiesberts | 13:2caedc56b863 | 152 | |
bobgiesberts | 12:cceece4f3afb | 153 | // write data to SD card |
bobgiesberts | 17:a4700b1c3c37 | 154 | bob.processing(); |
bobgiesberts | 12:cceece4f3afb | 155 | fp = fopen( fn, "a" ); // open file (append) |
bobgiesberts | 12:cceece4f3afb | 156 | for( int i = 0; i < counter; i++ ) |
bobgiesberts | 13:2caedc56b863 | 157 | fprintf( fp, "%.2f;%d;%.2f;%.4f\r\n", (float) collected.t[i]/100.0, collected.L[i], bob.battery(), T ); // write to file |
bobgiesberts | 12:cceece4f3afb | 158 | fclose( fp ); // close file |
bobgiesberts | 13:2caedc56b863 | 159 | |
bobgiesberts | 13:2caedc56b863 | 160 | // write to screen |
bobgiesberts | 13:2caedc56b863 | 161 | //pc.printf( "%.2f;%d;%.2f;%.4f\r\n", (float) collected.t[counter-1]/100.0, collected.L[counter-1], bob.battery(), T ); // write to screen |
bobgiesberts | 13:2caedc56b863 | 162 | |
bobgiesberts | 17:a4700b1c3c37 | 163 | bob.no_processing(); |
bobgiesberts | 12:cceece4f3afb | 164 | |
bobgiesberts | 12:cceece4f3afb | 165 | // Reset data |
bobgiesberts | 12:cceece4f3afb | 166 | memset(collected.t, 0, counter); |
bobgiesberts | 12:cceece4f3afb | 167 | memset(collected.L, 0, counter); |
bobgiesberts | 12:cceece4f3afb | 168 | counter = 0; |
bobgiesberts | 12:cceece4f3afb | 169 | } |
bobgiesberts | 5:736a81a59f3c | 170 | |
bobgiesberts | 10:3cab80866536 | 171 | int main(void) |
bobgiesberts | 10:3cab80866536 | 172 | { |
bobgiesberts | 15:5de8394fdb3c | 173 | if( DEBUG ) |
bobgiesberts | 17:a4700b1c3c37 | 174 | { |
bobgiesberts | 17:a4700b1c3c37 | 175 | bob.wakeup_periphery( ); |
bobgiesberts | 19:c0707d20bada | 176 | LDC1614 ldc( _LDC_SDA, _LDC_SCL, _LDC_SD, 16E6, sensors, C ); |
bobgiesberts | 19:c0707d20bada | 177 | |
bobgiesberts | 19:c0707d20bada | 178 | while( 1 ) { |
bobgiesberts | 19:c0707d20bada | 179 | |
bobgiesberts | 19:c0707d20bada | 180 | for(int sensor = 0; sensor < sensors; sensor++) |
bobgiesberts | 19:c0707d20bada | 181 | { |
bobgiesberts | 19:c0707d20bada | 182 | // wait for a new conversion to be ready |
bobgiesberts | 19:c0707d20bada | 183 | while( !ldc.is_ready( sensor ) ) { } |
bobgiesberts | 19:c0707d20bada | 184 | |
bobgiesberts | 19:c0707d20bada | 185 | // get error information from ldc.get_Data because |
bobgiesberts | 19:c0707d20bada | 186 | // it is not stored in ldc.get_status as soon as |
bobgiesberts | 19:c0707d20bada | 187 | // ldc.is_ready == true |
bobgiesberts | 15:5de8394fdb3c | 188 | |
bobgiesberts | 19:c0707d20bada | 189 | // write data to screen |
bobgiesberts | 19:c0707d20bada | 190 | pc.printf( "%d\r\n", ldc.get_Data( sensor ) ); |
bobgiesberts | 19:c0707d20bada | 191 | } |
bobgiesberts | 19:c0707d20bada | 192 | // wait(0.5); |
bobgiesberts | 15:5de8394fdb3c | 193 | |
bobgiesberts | 15:5de8394fdb3c | 194 | } |
bobgiesberts | 17:a4700b1c3c37 | 195 | } |
bobgiesberts | 17:a4700b1c3c37 | 196 | |
bobgiesberts | 13:2caedc56b863 | 197 | |
bobgiesberts | 13:2caedc56b863 | 198 | // TODO: |
bobgiesberts | 13:2caedc56b863 | 199 | // - implement this data conversion in writing to SD!! |
bobgiesberts | 13:2caedc56b863 | 200 | // - use T as uint16_t, not float! |
bobgiesberts | 13:2caedc56b863 | 201 | // - use V as uint16_t, not float! |
bobgiesberts | 13:2caedc56b863 | 202 | // - write blocks of 512 bit (4x 32 + 32 + 16 + 16) |
bobgiesberts | 13:2caedc56b863 | 203 | // |
bobgiesberts | 13:2caedc56b863 | 204 | // float temp_a = 127.9375; |
bobgiesberts | 13:2caedc56b863 | 205 | // pc.printf( "A: %.4f (%08x); \r\n", temp_a, * (uint32_t *) &temp_a); |
bobgiesberts | 13:2caedc56b863 | 206 | |
bobgiesberts | 6:ff39d60061ca | 207 | // Load SD File system |
bobgiesberts | 12:cceece4f3afb | 208 | // - the Raspberry Pie SD cards give a warning here that might just be ignored: |
bobgiesberts | 12:cceece4f3afb | 209 | // "Not in idle state after sending CMD8 (not an SD card?) |
bobgiesberts | 12:cceece4f3afb | 210 | // Didn't get a response from the disk |
bobgiesberts | 12:cceece4f3afb | 211 | // Set 512-byte block timed out" |
bobgiesberts | 12:cceece4f3afb | 212 | // TODO: buy 'better' SD cards from IAPC / the Stores (Kingston ...) |
bobgiesberts | 17:a4700b1c3c37 | 213 | |
bobgiesberts | 17:a4700b1c3c37 | 214 | // This turns enable on, i.e. it powers the SD card system, the crystal and the LDC |
bobgiesberts | 17:a4700b1c3c37 | 215 | bob.wakeup_periphery(); |
bobgiesberts | 17:a4700b1c3c37 | 216 | |
bobgiesberts | 17:a4700b1c3c37 | 217 | // load SD File System |
bobgiesberts | 15:5de8394fdb3c | 218 | SDFileSystem *sd = new SDFileSystem( _SD_MOSI, _SD_MISO, _SD_SCLK, _SD_CS, "sd" ); |
bobgiesberts | 10:3cab80866536 | 219 | |
bobgiesberts | 6:ff39d60061ca | 220 | // Create a new data file (data00.txt) |
bobgiesberts | 10:3cab80866536 | 221 | mkdir("/sd", 0777); |
bobgiesberts | 11:599ca9982e45 | 222 | for(uint8_t i = 0; i < 100; i++) |
bobgiesberts | 11:599ca9982e45 | 223 | { |
bobgiesberts | 6:ff39d60061ca | 224 | filename[8] = i/10 + '0'; |
bobgiesberts | 4:ae441c5727b9 | 225 | filename[9] = i%10 + '0'; |
bobgiesberts | 4:ae441c5727b9 | 226 | fp = fopen( filename.c_str() , "r" ); |
bobgiesberts | 11:599ca9982e45 | 227 | if( fp == NULL ) // read failed so file does not exist |
bobgiesberts | 11:599ca9982e45 | 228 | { |
bobgiesberts | 4:ae441c5727b9 | 229 | fp = fopen( filename.c_str(), "w" ); // create it |
bobgiesberts | 11:599ca9982e45 | 230 | if( fp != NULL ) |
bobgiesberts | 11:599ca9982e45 | 231 | { |
bobgiesberts | 4:ae441c5727b9 | 232 | fn = filename.c_str(); |
bobgiesberts | 6:ff39d60061ca | 233 | fclose( fp ); |
bobgiesberts | 4:ae441c5727b9 | 234 | break; |
bobgiesberts | 10:3cab80866536 | 235 | } else { |
bobgiesberts | 17:a4700b1c3c37 | 236 | bob.error(); // error: unable to create new file |
bobgiesberts | 4:ae441c5727b9 | 237 | } |
bobgiesberts | 10:3cab80866536 | 238 | } else { // file already exists |
bobgiesberts | 4:ae441c5727b9 | 239 | fclose( fp ); |
bobgiesberts | 4:ae441c5727b9 | 240 | } |
bobgiesberts | 1:22c272515015 | 241 | } |
bobgiesberts | 11:599ca9982e45 | 242 | if( fp == NULL ) |
bobgiesberts | 11:599ca9982e45 | 243 | { |
bobgiesberts | 17:a4700b1c3c37 | 244 | bob.error(); // error: file 00 - 99 already exists |
bobgiesberts | 12:cceece4f3afb | 245 | // pc.printf( "/sd/data00.txt t/m /sd/data99.txt already exist!\n" ); |
bobgiesberts | 11:599ca9982e45 | 246 | } |
bobgiesberts | 10:3cab80866536 | 247 | |
bobgiesberts | 6:ff39d60061ca | 248 | // Unload SD File system |
bobgiesberts | 10:3cab80866536 | 249 | delete sd; |
bobgiesberts | 10:3cab80866536 | 250 | sd = NULL; |
bobgiesberts | 17:a4700b1c3c37 | 251 | |
bobgiesberts | 12:cceece4f3afb | 252 | |
bobgiesberts | 11:599ca9982e45 | 253 | while(1) |
bobgiesberts | 11:599ca9982e45 | 254 | { |
bobgiesberts | 11:599ca9982e45 | 255 | // what time is it now? |
bobgiesberts | 8:8cc1960467ae | 256 | prev = now; // 0 -> 429 496 --> (4 294,96 s) (71 min) |
bobgiesberts | 8:8cc1960467ae | 257 | now = (uint32_t) clock(); // 0 -> 429 496 --> (4 294,96 s) (71 min) |
bobgiesberts | 11:599ca9982e45 | 258 | if( now < prev ) |
bobgiesberts | 11:599ca9982e45 | 259 | t_high++; // 0 -> 255 --> (255*4 294,96 s) (12 days) |
bobgiesberts | 11:599ca9982e45 | 260 | t = now + 429496.7296*t_high + t_lost + 429496.7296*lost_high; // 0 -> 219 901 952 --> (2 199 019,52 s) (25 days) |
bobgiesberts | 17:a4700b1c3c37 | 261 | |
bobgiesberts | 17:a4700b1c3c37 | 262 | // How long should we takes samples? |
bobgiesberts | 17:a4700b1c3c37 | 263 | next = t + ( (next == 0) ? INTERVAL_FIRST : INTERVAL_ON )*100; // 0 -> 219 904 952 --> (2 199 049,52 s) (25 days) |
bobgiesberts | 17:a4700b1c3c37 | 264 | |
bobgiesberts | 17:a4700b1c3c37 | 265 | |
bobgiesberts | 17:a4700b1c3c37 | 266 | // Wakeup the periphery (SD, crystal and LDC1614 on) |
bobgiesberts | 17:a4700b1c3c37 | 267 | bob.wakeup_periphery(); |
bobgiesberts | 11:599ca9982e45 | 268 | |
bobgiesberts | 15:5de8394fdb3c | 269 | // load libraries to take control over the communication |
bobgiesberts | 17:a4700b1c3c37 | 270 | LDC1614 *ldc = new LDC1614(_LDC_SDA, _LDC_SCL, _LDC_SD, 16E6, sensors, C); // load LDC1614 libray |
bobgiesberts | 15:5de8394fdb3c | 271 | DS1825 *thermometer = new DS1825( _Tpin ); // load thermometer (DS1825) library |
bobgiesberts | 17:a4700b1c3c37 | 272 | SDFileSystem *sd = new SDFileSystem(_SD_MOSI, _SD_MISO, _SD_SCLK, _SD_CS, "sd"); // load SD File System library |
bobgiesberts | 11:599ca9982e45 | 273 | mkdir("/sd", 0777); // select folder |
bobgiesberts | 10:3cab80866536 | 274 | |
bobgiesberts | 10:3cab80866536 | 275 | |
bobgiesberts | 6:ff39d60061ca | 276 | // Take samples for INTERVAL_ON seconds |
bobgiesberts | 11:599ca9982e45 | 277 | while( t < next ) |
bobgiesberts | 11:599ca9982e45 | 278 | { |
bobgiesberts | 12:cceece4f3afb | 279 | // what time is it now? |
bobgiesberts | 12:cceece4f3afb | 280 | prev = now; |
bobgiesberts | 12:cceece4f3afb | 281 | now = (uint32_t) clock(); |
bobgiesberts | 12:cceece4f3afb | 282 | if( now < prev ) |
bobgiesberts | 12:cceece4f3afb | 283 | t_high++; |
bobgiesberts | 12:cceece4f3afb | 284 | t = now + 429496.7296*t_high + t_lost + 429496.7296*lost_high; |
bobgiesberts | 10:3cab80866536 | 285 | |
bobgiesberts | 15:5de8394fdb3c | 286 | status = ldc->get_status(); |
bobgiesberts | 15:5de8394fdb3c | 287 | |
bobgiesberts | 15:5de8394fdb3c | 288 | // Is there any error? |
bobgiesberts | 15:5de8394fdb3c | 289 | if( ldc->is_error(status) ) |
bobgiesberts | 12:cceece4f3afb | 290 | { |
bobgiesberts | 15:5de8394fdb3c | 291 | // red led on |
bobgiesberts | 17:a4700b1c3c37 | 292 | bob.error(); |
bobgiesberts | 15:5de8394fdb3c | 293 | }else{ |
bobgiesberts | 15:5de8394fdb3c | 294 | // red led off |
bobgiesberts | 17:a4700b1c3c37 | 295 | bob.no_error(); |
bobgiesberts | 15:5de8394fdb3c | 296 | |
bobgiesberts | 15:5de8394fdb3c | 297 | if( ldc->is_ready(status) ) // data from all sensors is ready |
bobgiesberts | 15:5de8394fdb3c | 298 | { |
bobgiesberts | 15:5de8394fdb3c | 299 | // Store data in temporal memory |
bobgiesberts | 15:5de8394fdb3c | 300 | collected.t[counter] = t; |
bobgiesberts | 15:5de8394fdb3c | 301 | collected.L[counter] = ldc->get_Data( 0 ); |
bobgiesberts | 15:5de8394fdb3c | 302 | counter++; |
bobgiesberts | 15:5de8394fdb3c | 303 | |
bobgiesberts | 15:5de8394fdb3c | 304 | // Write a full package of data points to the SD card |
bobgiesberts | 15:5de8394fdb3c | 305 | if( counter >= package_size-1 ) |
bobgiesberts | 15:5de8394fdb3c | 306 | storeit( thermometer->getTemperature() ); |
bobgiesberts | 15:5de8394fdb3c | 307 | } |
bobgiesberts | 6:ff39d60061ca | 308 | } |
bobgiesberts | 15:5de8394fdb3c | 309 | |
bobgiesberts | 15:5de8394fdb3c | 310 | |
bobgiesberts | 10:3cab80866536 | 311 | } |
bobgiesberts | 12:cceece4f3afb | 312 | |
bobgiesberts | 12:cceece4f3afb | 313 | // Write remaining data to the SD card |
bobgiesberts | 12:cceece4f3afb | 314 | if( counter > 0 ) |
bobgiesberts | 12:cceece4f3afb | 315 | storeit( thermometer->getTemperature() ); |
bobgiesberts | 10:3cab80866536 | 316 | |
bobgiesberts | 17:a4700b1c3c37 | 317 | // prepare for sleep: delete and power down the periphery (SD, crystal and LDC1614) |
bobgiesberts | 12:cceece4f3afb | 318 | delete sd; sd = NULL; // unload library to be able to power down completely |
bobgiesberts | 12:cceece4f3afb | 319 | delete ldc; ldc = NULL; // unload library to be able to power down completely |
bobgiesberts | 12:cceece4f3afb | 320 | delete thermometer; thermometer = NULL; // unload library to be able to power down completely |
bobgiesberts | 17:a4700b1c3c37 | 321 | bob.shutdown_periphery(); |
bobgiesberts | 12:cceece4f3afb | 322 | |
bobgiesberts | 16:b53721840a38 | 323 | // if the battery is almost empty (Vbatt < 3.10 V), the thermometer stops |
bobgiesberts | 12:cceece4f3afb | 324 | // working well and L can not be corrected. So just shut down... |
bobgiesberts | 12:cceece4f3afb | 325 | if( bob.battery() < 3.10f ) |
bobgiesberts | 12:cceece4f3afb | 326 | { |
bobgiesberts | 17:a4700b1c3c37 | 327 | bob.error(); // error: battery empty |
bobgiesberts | 12:cceece4f3afb | 328 | // pc.printf( "Battery empty (%.1f V), shutting down.\n", bob.battery() ); |
bobgiesberts | 12:cceece4f3afb | 329 | exit(0); |
bobgiesberts | 12:cceece4f3afb | 330 | } |
bobgiesberts | 10:3cab80866536 | 331 | |
bobgiesberts | 11:599ca9982e45 | 332 | // what time is it now? |
bobgiesberts | 11:599ca9982e45 | 333 | prev = now; |
bobgiesberts | 11:599ca9982e45 | 334 | now = (uint32_t) clock(); |
bobgiesberts | 11:599ca9982e45 | 335 | if( now < prev ) |
bobgiesberts | 11:599ca9982e45 | 336 | t_high++; |
bobgiesberts | 11:599ca9982e45 | 337 | t = now + 429496.7296*t_high + t_lost + 429496.7296*lost_high; |
bobgiesberts | 10:3cab80866536 | 338 | |
bobgiesberts | 8:8cc1960467ae | 339 | // Calculate sleeping time (correction to INTERVAL_OFF) |
bobgiesberts | 12:cceece4f3afb | 340 | // t has passed the limit of next. Those few ms are substracted from INTERVAL_OFF. |
bobgiesberts | 11:599ca9982e45 | 341 | t_sleep = INTERVAL_OFF*100 - (t - next); |
bobgiesberts | 8:8cc1960467ae | 342 | |
bobgiesberts | 12:cceece4f3afb | 343 | // Calculate time that will be lost during sleep |
bobgiesberts | 11:599ca9982e45 | 344 | lost_prev = t_lost; |
bobgiesberts | 11:599ca9982e45 | 345 | t_lost += t_sleep; |
bobgiesberts | 11:599ca9982e45 | 346 | if( t_lost < lost_prev ) |
bobgiesberts | 11:599ca9982e45 | 347 | lost_high++; |
bobgiesberts | 13:2caedc56b863 | 348 | |
bobgiesberts | 11:599ca9982e45 | 349 | // Sleep now (ms), enter low power mode |
bobgiesberts | 11:599ca9982e45 | 350 | bob.sleep( t_sleep * 10 ); |
bobgiesberts | 10:3cab80866536 | 351 | |
bobgiesberts | 0:e81b68888268 | 352 | } |
bobgiesberts | 6:ff39d60061ca | 353 | |
bobgiesberts | 11:599ca9982e45 | 354 | |
bobgiesberts | 11:599ca9982e45 | 355 | |
bobgiesberts | 11:599ca9982e45 | 356 | |
bobgiesberts | 12:cceece4f3afb | 357 | } |
bobgiesberts | 12:cceece4f3afb | 358 |