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@17:a4700b1c3c37, 2016-09-07 (annotated)
- Committer:
- bobgiesberts
- Date:
- Wed Sep 07 09:59:16 2016 +0000
- Revision:
- 17:a4700b1c3c37
- Parent:
- 16:b53721840a38
- Child:
- 19:c0707d20bada
Blinky works!; Big troubles with I2C communication with our 32-pin KL25Z chip. Dynamic allocation needed...
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 | 15:5de8394fdb3c | 35 | int sensors = 2; // 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 | 17:a4700b1c3c37 | 176 | wait(1); |
bobgiesberts | 17:a4700b1c3c37 | 177 | // LDC1614 ldc( _LDC_SDA, _LDC_SCL, _LDC_SD, 16E6, sensors, C ); // error: pinmap not found for peripheral |
bobgiesberts | 17:a4700b1c3c37 | 178 | // LDC1614 ldc( PTC5, PTC4, PTC6, 16E6, sensors, C ); // error: pinmap not found for peripheral |
bobgiesberts | 17:a4700b1c3c37 | 179 | // I2C _i2c( PTC5, PTC4 ); // error: pinmap not found for peripheral |
bobgiesberts | 17:a4700b1c3c37 | 180 | // I2CX _i2c( I2C_0, PTC5, PTC4 ); // error: library doesn't work |
bobgiesberts | 15:5de8394fdb3c | 181 | |
bobgiesberts | 17:a4700b1c3c37 | 182 | // KL25Z has standard allocated I2C pins that are not present in our package (32-pins) |
bobgiesberts | 17:a4700b1c3c37 | 183 | // Dynamic allocation of pinnames... |
bobgiesberts | 17:a4700b1c3c37 | 184 | |
bobgiesberts | 15:5de8394fdb3c | 185 | while( 1 ) { |
bobgiesberts | 17:a4700b1c3c37 | 186 | |
bobgiesberts | 17:a4700b1c3c37 | 187 | pc.printf( "works!\r\n" ); |
bobgiesberts | 17:a4700b1c3c37 | 188 | |
bobgiesberts | 17:a4700b1c3c37 | 189 | wait(1); |
bobgiesberts | 17:a4700b1c3c37 | 190 | |
bobgiesberts | 17:a4700b1c3c37 | 191 | /* |
bobgiesberts | 15:5de8394fdb3c | 192 | // check the sensor |
bobgiesberts | 17:a4700b1c3c37 | 193 | status = ldc.get_status(); |
bobgiesberts | 15:5de8394fdb3c | 194 | |
bobgiesberts | 15:5de8394fdb3c | 195 | // if there is an error turn on the red led |
bobgiesberts | 15:5de8394fdb3c | 196 | if( ldc.is_error( status ) ) |
bobgiesberts | 15:5de8394fdb3c | 197 | { |
bobgiesberts | 15:5de8394fdb3c | 198 | // error |
bobgiesberts | 17:a4700b1c3c37 | 199 | bob.error(); |
bobgiesberts | 15:5de8394fdb3c | 200 | }else{ |
bobgiesberts | 15:5de8394fdb3c | 201 | if( ldc.is_ready( status ) ) |
bobgiesberts | 15:5de8394fdb3c | 202 | { |
bobgiesberts | 17:a4700b1c3c37 | 203 | bob.no_error(); |
bobgiesberts | 15:5de8394fdb3c | 204 | pc.printf( "sensor 1: %d | sensor 2: %d\r\n", ldc.get_Data( 0 ), ldc.get_Data( 1 ) ); |
bobgiesberts | 15:5de8394fdb3c | 205 | } |
bobgiesberts | 15:5de8394fdb3c | 206 | } |
bobgiesberts | 17:a4700b1c3c37 | 207 | */ |
bobgiesberts | 17:a4700b1c3c37 | 208 | |
bobgiesberts | 15:5de8394fdb3c | 209 | } |
bobgiesberts | 17:a4700b1c3c37 | 210 | } |
bobgiesberts | 17:a4700b1c3c37 | 211 | |
bobgiesberts | 13:2caedc56b863 | 212 | |
bobgiesberts | 13:2caedc56b863 | 213 | // TODO: |
bobgiesberts | 13:2caedc56b863 | 214 | // - implement this data conversion in writing to SD!! |
bobgiesberts | 13:2caedc56b863 | 215 | // - use T as uint16_t, not float! |
bobgiesberts | 13:2caedc56b863 | 216 | // - use V as uint16_t, not float! |
bobgiesberts | 13:2caedc56b863 | 217 | // - write blocks of 512 bit (4x 32 + 32 + 16 + 16) |
bobgiesberts | 13:2caedc56b863 | 218 | // |
bobgiesberts | 13:2caedc56b863 | 219 | // float temp_a = 127.9375; |
bobgiesberts | 13:2caedc56b863 | 220 | // pc.printf( "A: %.4f (%08x); \r\n", temp_a, * (uint32_t *) &temp_a); |
bobgiesberts | 13:2caedc56b863 | 221 | |
bobgiesberts | 6:ff39d60061ca | 222 | // Load SD File system |
bobgiesberts | 12:cceece4f3afb | 223 | // - the Raspberry Pie SD cards give a warning here that might just be ignored: |
bobgiesberts | 12:cceece4f3afb | 224 | // "Not in idle state after sending CMD8 (not an SD card?) |
bobgiesberts | 12:cceece4f3afb | 225 | // Didn't get a response from the disk |
bobgiesberts | 12:cceece4f3afb | 226 | // Set 512-byte block timed out" |
bobgiesberts | 12:cceece4f3afb | 227 | // TODO: buy 'better' SD cards from IAPC / the Stores (Kingston ...) |
bobgiesberts | 17:a4700b1c3c37 | 228 | |
bobgiesberts | 17:a4700b1c3c37 | 229 | // This turns enable on, i.e. it powers the SD card system, the crystal and the LDC |
bobgiesberts | 17:a4700b1c3c37 | 230 | bob.wakeup_periphery(); |
bobgiesberts | 17:a4700b1c3c37 | 231 | |
bobgiesberts | 17:a4700b1c3c37 | 232 | // load SD File System |
bobgiesberts | 15:5de8394fdb3c | 233 | SDFileSystem *sd = new SDFileSystem( _SD_MOSI, _SD_MISO, _SD_SCLK, _SD_CS, "sd" ); |
bobgiesberts | 10:3cab80866536 | 234 | |
bobgiesberts | 6:ff39d60061ca | 235 | // Create a new data file (data00.txt) |
bobgiesberts | 10:3cab80866536 | 236 | mkdir("/sd", 0777); |
bobgiesberts | 11:599ca9982e45 | 237 | for(uint8_t i = 0; i < 100; i++) |
bobgiesberts | 11:599ca9982e45 | 238 | { |
bobgiesberts | 6:ff39d60061ca | 239 | filename[8] = i/10 + '0'; |
bobgiesberts | 4:ae441c5727b9 | 240 | filename[9] = i%10 + '0'; |
bobgiesberts | 4:ae441c5727b9 | 241 | fp = fopen( filename.c_str() , "r" ); |
bobgiesberts | 11:599ca9982e45 | 242 | if( fp == NULL ) // read failed so file does not exist |
bobgiesberts | 11:599ca9982e45 | 243 | { |
bobgiesberts | 4:ae441c5727b9 | 244 | fp = fopen( filename.c_str(), "w" ); // create it |
bobgiesberts | 11:599ca9982e45 | 245 | if( fp != NULL ) |
bobgiesberts | 11:599ca9982e45 | 246 | { |
bobgiesberts | 4:ae441c5727b9 | 247 | fn = filename.c_str(); |
bobgiesberts | 6:ff39d60061ca | 248 | fclose( fp ); |
bobgiesberts | 4:ae441c5727b9 | 249 | break; |
bobgiesberts | 10:3cab80866536 | 250 | } else { |
bobgiesberts | 17:a4700b1c3c37 | 251 | bob.error(); // error: unable to create new file |
bobgiesberts | 4:ae441c5727b9 | 252 | } |
bobgiesberts | 10:3cab80866536 | 253 | } else { // file already exists |
bobgiesberts | 4:ae441c5727b9 | 254 | fclose( fp ); |
bobgiesberts | 4:ae441c5727b9 | 255 | } |
bobgiesberts | 1:22c272515015 | 256 | } |
bobgiesberts | 11:599ca9982e45 | 257 | if( fp == NULL ) |
bobgiesberts | 11:599ca9982e45 | 258 | { |
bobgiesberts | 17:a4700b1c3c37 | 259 | bob.error(); // error: file 00 - 99 already exists |
bobgiesberts | 12:cceece4f3afb | 260 | // pc.printf( "/sd/data00.txt t/m /sd/data99.txt already exist!\n" ); |
bobgiesberts | 11:599ca9982e45 | 261 | } |
bobgiesberts | 10:3cab80866536 | 262 | |
bobgiesberts | 6:ff39d60061ca | 263 | // Unload SD File system |
bobgiesberts | 10:3cab80866536 | 264 | delete sd; |
bobgiesberts | 10:3cab80866536 | 265 | sd = NULL; |
bobgiesberts | 17:a4700b1c3c37 | 266 | |
bobgiesberts | 12:cceece4f3afb | 267 | |
bobgiesberts | 11:599ca9982e45 | 268 | while(1) |
bobgiesberts | 11:599ca9982e45 | 269 | { |
bobgiesberts | 11:599ca9982e45 | 270 | // what time is it now? |
bobgiesberts | 8:8cc1960467ae | 271 | prev = now; // 0 -> 429 496 --> (4 294,96 s) (71 min) |
bobgiesberts | 8:8cc1960467ae | 272 | now = (uint32_t) clock(); // 0 -> 429 496 --> (4 294,96 s) (71 min) |
bobgiesberts | 11:599ca9982e45 | 273 | if( now < prev ) |
bobgiesberts | 11:599ca9982e45 | 274 | t_high++; // 0 -> 255 --> (255*4 294,96 s) (12 days) |
bobgiesberts | 11:599ca9982e45 | 275 | 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 | 276 | |
bobgiesberts | 17:a4700b1c3c37 | 277 | // How long should we takes samples? |
bobgiesberts | 17:a4700b1c3c37 | 278 | next = t + ( (next == 0) ? INTERVAL_FIRST : INTERVAL_ON )*100; // 0 -> 219 904 952 --> (2 199 049,52 s) (25 days) |
bobgiesberts | 17:a4700b1c3c37 | 279 | |
bobgiesberts | 17:a4700b1c3c37 | 280 | |
bobgiesberts | 17:a4700b1c3c37 | 281 | // Wakeup the periphery (SD, crystal and LDC1614 on) |
bobgiesberts | 17:a4700b1c3c37 | 282 | bob.wakeup_periphery(); |
bobgiesberts | 11:599ca9982e45 | 283 | |
bobgiesberts | 15:5de8394fdb3c | 284 | // load libraries to take control over the communication |
bobgiesberts | 17:a4700b1c3c37 | 285 | LDC1614 *ldc = new LDC1614(_LDC_SDA, _LDC_SCL, _LDC_SD, 16E6, sensors, C); // load LDC1614 libray |
bobgiesberts | 15:5de8394fdb3c | 286 | DS1825 *thermometer = new DS1825( _Tpin ); // load thermometer (DS1825) library |
bobgiesberts | 17:a4700b1c3c37 | 287 | SDFileSystem *sd = new SDFileSystem(_SD_MOSI, _SD_MISO, _SD_SCLK, _SD_CS, "sd"); // load SD File System library |
bobgiesberts | 11:599ca9982e45 | 288 | mkdir("/sd", 0777); // select folder |
bobgiesberts | 10:3cab80866536 | 289 | |
bobgiesberts | 10:3cab80866536 | 290 | |
bobgiesberts | 6:ff39d60061ca | 291 | // Take samples for INTERVAL_ON seconds |
bobgiesberts | 11:599ca9982e45 | 292 | while( t < next ) |
bobgiesberts | 11:599ca9982e45 | 293 | { |
bobgiesberts | 12:cceece4f3afb | 294 | // what time is it now? |
bobgiesberts | 12:cceece4f3afb | 295 | prev = now; |
bobgiesberts | 12:cceece4f3afb | 296 | now = (uint32_t) clock(); |
bobgiesberts | 12:cceece4f3afb | 297 | if( now < prev ) |
bobgiesberts | 12:cceece4f3afb | 298 | t_high++; |
bobgiesberts | 12:cceece4f3afb | 299 | t = now + 429496.7296*t_high + t_lost + 429496.7296*lost_high; |
bobgiesberts | 10:3cab80866536 | 300 | |
bobgiesberts | 15:5de8394fdb3c | 301 | status = ldc->get_status(); |
bobgiesberts | 15:5de8394fdb3c | 302 | |
bobgiesberts | 15:5de8394fdb3c | 303 | // Is there any error? |
bobgiesberts | 15:5de8394fdb3c | 304 | if( ldc->is_error(status) ) |
bobgiesberts | 12:cceece4f3afb | 305 | { |
bobgiesberts | 15:5de8394fdb3c | 306 | // red led on |
bobgiesberts | 17:a4700b1c3c37 | 307 | bob.error(); |
bobgiesberts | 15:5de8394fdb3c | 308 | }else{ |
bobgiesberts | 15:5de8394fdb3c | 309 | // red led off |
bobgiesberts | 17:a4700b1c3c37 | 310 | bob.no_error(); |
bobgiesberts | 15:5de8394fdb3c | 311 | |
bobgiesberts | 15:5de8394fdb3c | 312 | if( ldc->is_ready(status) ) // data from all sensors is ready |
bobgiesberts | 15:5de8394fdb3c | 313 | { |
bobgiesberts | 15:5de8394fdb3c | 314 | // Store data in temporal memory |
bobgiesberts | 15:5de8394fdb3c | 315 | collected.t[counter] = t; |
bobgiesberts | 15:5de8394fdb3c | 316 | collected.L[counter] = ldc->get_Data( 0 ); |
bobgiesberts | 15:5de8394fdb3c | 317 | counter++; |
bobgiesberts | 15:5de8394fdb3c | 318 | |
bobgiesberts | 15:5de8394fdb3c | 319 | // Write a full package of data points to the SD card |
bobgiesberts | 15:5de8394fdb3c | 320 | if( counter >= package_size-1 ) |
bobgiesberts | 15:5de8394fdb3c | 321 | storeit( thermometer->getTemperature() ); |
bobgiesberts | 15:5de8394fdb3c | 322 | } |
bobgiesberts | 6:ff39d60061ca | 323 | } |
bobgiesberts | 15:5de8394fdb3c | 324 | |
bobgiesberts | 15:5de8394fdb3c | 325 | |
bobgiesberts | 10:3cab80866536 | 326 | } |
bobgiesberts | 12:cceece4f3afb | 327 | |
bobgiesberts | 12:cceece4f3afb | 328 | // Write remaining data to the SD card |
bobgiesberts | 12:cceece4f3afb | 329 | if( counter > 0 ) |
bobgiesberts | 12:cceece4f3afb | 330 | storeit( thermometer->getTemperature() ); |
bobgiesberts | 10:3cab80866536 | 331 | |
bobgiesberts | 17:a4700b1c3c37 | 332 | // prepare for sleep: delete and power down the periphery (SD, crystal and LDC1614) |
bobgiesberts | 12:cceece4f3afb | 333 | delete sd; sd = NULL; // unload library to be able to power down completely |
bobgiesberts | 12:cceece4f3afb | 334 | delete ldc; ldc = NULL; // unload library to be able to power down completely |
bobgiesberts | 12:cceece4f3afb | 335 | delete thermometer; thermometer = NULL; // unload library to be able to power down completely |
bobgiesberts | 17:a4700b1c3c37 | 336 | bob.shutdown_periphery(); |
bobgiesberts | 12:cceece4f3afb | 337 | |
bobgiesberts | 16:b53721840a38 | 338 | // if the battery is almost empty (Vbatt < 3.10 V), the thermometer stops |
bobgiesberts | 12:cceece4f3afb | 339 | // working well and L can not be corrected. So just shut down... |
bobgiesberts | 12:cceece4f3afb | 340 | if( bob.battery() < 3.10f ) |
bobgiesberts | 12:cceece4f3afb | 341 | { |
bobgiesberts | 17:a4700b1c3c37 | 342 | bob.error(); // error: battery empty |
bobgiesberts | 12:cceece4f3afb | 343 | // pc.printf( "Battery empty (%.1f V), shutting down.\n", bob.battery() ); |
bobgiesberts | 12:cceece4f3afb | 344 | exit(0); |
bobgiesberts | 12:cceece4f3afb | 345 | } |
bobgiesberts | 10:3cab80866536 | 346 | |
bobgiesberts | 11:599ca9982e45 | 347 | // what time is it now? |
bobgiesberts | 11:599ca9982e45 | 348 | prev = now; |
bobgiesberts | 11:599ca9982e45 | 349 | now = (uint32_t) clock(); |
bobgiesberts | 11:599ca9982e45 | 350 | if( now < prev ) |
bobgiesberts | 11:599ca9982e45 | 351 | t_high++; |
bobgiesberts | 11:599ca9982e45 | 352 | t = now + 429496.7296*t_high + t_lost + 429496.7296*lost_high; |
bobgiesberts | 10:3cab80866536 | 353 | |
bobgiesberts | 8:8cc1960467ae | 354 | // Calculate sleeping time (correction to INTERVAL_OFF) |
bobgiesberts | 12:cceece4f3afb | 355 | // t has passed the limit of next. Those few ms are substracted from INTERVAL_OFF. |
bobgiesberts | 11:599ca9982e45 | 356 | t_sleep = INTERVAL_OFF*100 - (t - next); |
bobgiesberts | 8:8cc1960467ae | 357 | |
bobgiesberts | 12:cceece4f3afb | 358 | // Calculate time that will be lost during sleep |
bobgiesberts | 11:599ca9982e45 | 359 | lost_prev = t_lost; |
bobgiesberts | 11:599ca9982e45 | 360 | t_lost += t_sleep; |
bobgiesberts | 11:599ca9982e45 | 361 | if( t_lost < lost_prev ) |
bobgiesberts | 11:599ca9982e45 | 362 | lost_high++; |
bobgiesberts | 13:2caedc56b863 | 363 | |
bobgiesberts | 11:599ca9982e45 | 364 | // Sleep now (ms), enter low power mode |
bobgiesberts | 11:599ca9982e45 | 365 | bob.sleep( t_sleep * 10 ); |
bobgiesberts | 10:3cab80866536 | 366 | |
bobgiesberts | 0:e81b68888268 | 367 | } |
bobgiesberts | 6:ff39d60061ca | 368 | |
bobgiesberts | 11:599ca9982e45 | 369 | |
bobgiesberts | 11:599ca9982e45 | 370 | |
bobgiesberts | 11:599ca9982e45 | 371 | |
bobgiesberts | 12:cceece4f3afb | 372 | } |
bobgiesberts | 12:cceece4f3afb | 373 |