The official code that runs on the FRDM board for the chlorine sensor.
Dependencies: MBed_Adafruit-GPS-Library SDFileSystem mbed GSM_Library
Fork of DCS by
Sensor.cpp@4:35d92f290cfe, 2015-03-07 (annotated)
- Committer:
- danilob
- Date:
- Sat Mar 07 23:34:49 2015 +0000
- Revision:
- 4:35d92f290cfe
- Parent:
- 2:5c0513ab856e
- Child:
- 5:f0946f8ed23a
lalla
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bjcrofts | 0:d7b2716c5a4f | 1 | #pragma once |
bjcrofts | 0:d7b2716c5a4f | 2 | #include "mbed.h" |
bjcrofts | 0:d7b2716c5a4f | 3 | #include "math.h" |
bjcrofts | 0:d7b2716c5a4f | 4 | #include "MBed_Adafruit_GPS.h" |
bjcrofts | 0:d7b2716c5a4f | 5 | #include "SDFileSystem.h" |
bjcrofts | 0:d7b2716c5a4f | 6 | #include "QAM.h" |
bjcrofts | 0:d7b2716c5a4f | 7 | #include "param.h" |
danilob | 2:5c0513ab856e | 8 | #include "GSMLibrary.h" |
bjcrofts | 0:d7b2716c5a4f | 9 | |
bjcrofts | 0:d7b2716c5a4f | 10 | DigitalOut led_red(LED_RED); |
bjcrofts | 0:d7b2716c5a4f | 11 | Serial pc(USBTX, USBRX); |
danilob | 2:5c0513ab856e | 12 | Serial gsm(PTC17,PTC16); |
bjcrofts | 0:d7b2716c5a4f | 13 | Timer t; |
bjcrofts | 0:d7b2716c5a4f | 14 | |
bjcrofts | 0:d7b2716c5a4f | 15 | /************************************************** |
bjcrofts | 0:d7b2716c5a4f | 16 | ** SD FILE SYSTEM ** |
bjcrofts | 0:d7b2716c5a4f | 17 | **************************************************/ |
bjcrofts | 0:d7b2716c5a4f | 18 | SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); |
bjcrofts | 0:d7b2716c5a4f | 19 | FILE *fp; |
bjcrofts | 0:d7b2716c5a4f | 20 | |
bjcrofts | 0:d7b2716c5a4f | 21 | /************************************************** |
bjcrofts | 0:d7b2716c5a4f | 22 | ** GPS ** |
bjcrofts | 0:d7b2716c5a4f | 23 | **************************************************/ |
bjcrofts | 0:d7b2716c5a4f | 24 | Serial * gps_Serial; |
bjcrofts | 0:d7b2716c5a4f | 25 | |
bjcrofts | 0:d7b2716c5a4f | 26 | /************************************************** |
bjcrofts | 0:d7b2716c5a4f | 27 | ** SENSOR INPUTS ** |
bjcrofts | 0:d7b2716c5a4f | 28 | **************************************************/ |
bjcrofts | 0:d7b2716c5a4f | 29 | AnalogIn AnLong(A0); |
bjcrofts | 0:d7b2716c5a4f | 30 | AnalogIn AnShort(A1); |
bjcrofts | 0:d7b2716c5a4f | 31 | AnalogIn AnRefLong(A2); |
bjcrofts | 0:d7b2716c5a4f | 32 | AnalogIn AnRefShort(A3); |
bjcrofts | 0:d7b2716c5a4f | 33 | Ticker sample_tick; |
bjcrofts | 0:d7b2716c5a4f | 34 | bool takeSample = false; |
bjcrofts | 0:d7b2716c5a4f | 35 | void tick(){takeSample = true;} |
bjcrofts | 0:d7b2716c5a4f | 36 | |
bjcrofts | 0:d7b2716c5a4f | 37 | /************************************************** |
bjcrofts | 0:d7b2716c5a4f | 38 | ** SIN OUTPUT ** |
bjcrofts | 0:d7b2716c5a4f | 39 | **************************************************/ |
bjcrofts | 0:d7b2716c5a4f | 40 | AnalogOut dac0(DAC0_OUT); |
bjcrofts | 0:d7b2716c5a4f | 41 | int sinRes = (int)1/(CARRIER_FREQ*TIME_CONST); |
bjcrofts | 0:d7b2716c5a4f | 42 | float sinWave[SIN_LENGTH] = {}; |
bjcrofts | 0:d7b2716c5a4f | 43 | int sinIndex = 0; |
bjcrofts | 0:d7b2716c5a4f | 44 | |
bjcrofts | 0:d7b2716c5a4f | 45 | |
bjcrofts | 0:d7b2716c5a4f | 46 | /************************************************** |
bjcrofts | 0:d7b2716c5a4f | 47 | ** QAM ** |
bjcrofts | 0:d7b2716c5a4f | 48 | **************************************************/ |
bjcrofts | 0:d7b2716c5a4f | 49 | float sLQ[SAMPLE_LENGTH] = {}; |
bjcrofts | 0:d7b2716c5a4f | 50 | float sLI[SAMPLE_LENGTH] = {}; |
bjcrofts | 0:d7b2716c5a4f | 51 | float sSQ[SAMPLE_LENGTH] = {}; |
bjcrofts | 0:d7b2716c5a4f | 52 | float sSI[SAMPLE_LENGTH] = {}; |
bjcrofts | 0:d7b2716c5a4f | 53 | float sRefLQ[SAMPLE_LENGTH] = {}; |
bjcrofts | 0:d7b2716c5a4f | 54 | float sRefLI[SAMPLE_LENGTH] = {}; |
bjcrofts | 0:d7b2716c5a4f | 55 | float sRefSQ[SAMPLE_LENGTH] = {}; |
bjcrofts | 0:d7b2716c5a4f | 56 | float sRefSI[SAMPLE_LENGTH] = {}; |
bjcrofts | 0:d7b2716c5a4f | 57 | |
bjcrofts | 0:d7b2716c5a4f | 58 | float Iarray[SAMPLE_LENGTH] = {}; |
bjcrofts | 0:d7b2716c5a4f | 59 | float Qarray[SAMPLE_LENGTH] = {}; |
bjcrofts | 0:d7b2716c5a4f | 60 | int sampleIndex = 0; |
bjcrofts | 0:d7b2716c5a4f | 61 | float I = 0; |
bjcrofts | 0:d7b2716c5a4f | 62 | float Q = 0; |
bjcrofts | 0:d7b2716c5a4f | 63 | float lon = 0; |
bjcrofts | 0:d7b2716c5a4f | 64 | float lonRef = 0; |
bjcrofts | 0:d7b2716c5a4f | 65 | float shor = 0; |
bjcrofts | 0:d7b2716c5a4f | 66 | float shorRef = 0; |
bjcrofts | 0:d7b2716c5a4f | 67 | |
bjcrofts | 0:d7b2716c5a4f | 68 | void buildIQ(){ |
bjcrofts | 0:d7b2716c5a4f | 69 | for(int i = 0; i < SAMPLE_LENGTH; i++){ |
bjcrofts | 0:d7b2716c5a4f | 70 | Iarray[i] = cos(2*PI*CARRIER_FREQ*i*TIME_CONST); |
bjcrofts | 0:d7b2716c5a4f | 71 | Qarray[i] = -sin(2*PI*CARRIER_FREQ*i*TIME_CONST); |
bjcrofts | 0:d7b2716c5a4f | 72 | } |
bjcrofts | 0:d7b2716c5a4f | 73 | } |
bjcrofts | 0:d7b2716c5a4f | 74 | |
bjcrofts | 0:d7b2716c5a4f | 75 | void create_sinWave(){ |
bjcrofts | 0:d7b2716c5a4f | 76 | int i = 0; |
bjcrofts | 0:d7b2716c5a4f | 77 | for(i = 0; i < sinRes; i++){ |
bjcrofts | 0:d7b2716c5a4f | 78 | sinWave[i] = 0.25 * sin(2.0*PI*i/sinRes) + 0.75; |
bjcrofts | 0:d7b2716c5a4f | 79 | } |
bjcrofts | 0:d7b2716c5a4f | 80 | } |
bjcrofts | 0:d7b2716c5a4f | 81 | |
bjcrofts | 0:d7b2716c5a4f | 82 | int main () { |
bjcrofts | 0:d7b2716c5a4f | 83 | |
bjcrofts | 0:d7b2716c5a4f | 84 | pc.baud(115200); |
danilob | 2:5c0513ab856e | 85 | gsm.baud(115200); |
bjcrofts | 0:d7b2716c5a4f | 86 | pc.printf("hello\r\n"); |
bjcrofts | 0:d7b2716c5a4f | 87 | |
danilob | 2:5c0513ab856e | 88 | //GSM INITIALIZATION /////////////////////////////// |
danilob | 2:5c0513ab856e | 89 | gsm_initialize(); |
bjcrofts | 0:d7b2716c5a4f | 90 | // GPS INITIALIZATION ////////////////////////////// |
danilob | 2:5c0513ab856e | 91 | //gps_Serial = new Serial(D1,D0); |
danilob | 2:5c0513ab856e | 92 | //Adafruit_GPS myGPS(gps_Serial); |
danilob | 2:5c0513ab856e | 93 | //char c; |
danilob | 2:5c0513ab856e | 94 | //myGPS.begin(9600); |
danilob | 2:5c0513ab856e | 95 | //myGPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); |
danilob | 2:5c0513ab856e | 96 | //myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); |
danilob | 2:5c0513ab856e | 97 | //myGPS.sendCommand(PGCMD_ANTENNA); |
bjcrofts | 0:d7b2716c5a4f | 98 | //////////////////////////////////////////////////// |
bjcrofts | 0:d7b2716c5a4f | 99 | |
bjcrofts | 0:d7b2716c5a4f | 100 | buildIQ(); |
bjcrofts | 0:d7b2716c5a4f | 101 | create_sinWave(); |
bjcrofts | 0:d7b2716c5a4f | 102 | |
bjcrofts | 0:d7b2716c5a4f | 103 | float filteredLong = 0; |
bjcrofts | 0:d7b2716c5a4f | 104 | float filteredShort = 0; |
bjcrofts | 0:d7b2716c5a4f | 105 | float filteredLongRef = 0; |
bjcrofts | 0:d7b2716c5a4f | 106 | float filteredShortRef = 0; |
bjcrofts | 0:d7b2716c5a4f | 107 | |
bjcrofts | 0:d7b2716c5a4f | 108 | sample_tick.attach(&tick, 0.0001); |
bjcrofts | 0:d7b2716c5a4f | 109 | |
bjcrofts | 0:d7b2716c5a4f | 110 | t.start(); |
bjcrofts | 0:d7b2716c5a4f | 111 | |
bjcrofts | 0:d7b2716c5a4f | 112 | while(1){ |
bjcrofts | 0:d7b2716c5a4f | 113 | |
bjcrofts | 0:d7b2716c5a4f | 114 | if(takeSample){ //3.46 us per loop |
bjcrofts | 0:d7b2716c5a4f | 115 | |
bjcrofts | 0:d7b2716c5a4f | 116 | takeSample = false; |
bjcrofts | 0:d7b2716c5a4f | 117 | dac0 = sinWave[sinIndex]; |
bjcrofts | 0:d7b2716c5a4f | 118 | sinIndex++; |
bjcrofts | 0:d7b2716c5a4f | 119 | if((sinIndex+1) > sinRes){ |
bjcrofts | 0:d7b2716c5a4f | 120 | sinIndex = 0; |
bjcrofts | 0:d7b2716c5a4f | 121 | } |
bjcrofts | 0:d7b2716c5a4f | 122 | |
bjcrofts | 0:d7b2716c5a4f | 123 | |
bjcrofts | 0:d7b2716c5a4f | 124 | lon = AnLong.read(); |
bjcrofts | 0:d7b2716c5a4f | 125 | lonRef = AnRefLong.read(); |
bjcrofts | 0:d7b2716c5a4f | 126 | shor = AnShort.read(); |
bjcrofts | 0:d7b2716c5a4f | 127 | shorRef = AnRefShort.read(); |
bjcrofts | 0:d7b2716c5a4f | 128 | |
bjcrofts | 0:d7b2716c5a4f | 129 | I = Iarray[sampleIndex]; |
bjcrofts | 0:d7b2716c5a4f | 130 | Q = Qarray[sampleIndex]; |
bjcrofts | 0:d7b2716c5a4f | 131 | sLI[sampleIndex] = lon*I; |
bjcrofts | 0:d7b2716c5a4f | 132 | sLQ[sampleIndex] = lon*Q; |
bjcrofts | 0:d7b2716c5a4f | 133 | sSI[sampleIndex] = shor*I; |
bjcrofts | 0:d7b2716c5a4f | 134 | sSQ[sampleIndex] = shor*Q; |
bjcrofts | 0:d7b2716c5a4f | 135 | sRefLI[sampleIndex] = lonRef*I; |
bjcrofts | 0:d7b2716c5a4f | 136 | sRefLQ[sampleIndex] = lonRef*Q; |
bjcrofts | 0:d7b2716c5a4f | 137 | sRefSI[sampleIndex] = shorRef*I; |
bjcrofts | 0:d7b2716c5a4f | 138 | sRefSQ[sampleIndex] = shorRef*Q; |
bjcrofts | 0:d7b2716c5a4f | 139 | |
bjcrofts | 0:d7b2716c5a4f | 140 | sampleIndex++; |
bjcrofts | 0:d7b2716c5a4f | 141 | if(sampleIndex+1 > SAMPLE_LENGTH){ |
bjcrofts | 0:d7b2716c5a4f | 142 | sampleIndex--; |
bjcrofts | 0:d7b2716c5a4f | 143 | } |
bjcrofts | 0:d7b2716c5a4f | 144 | } |
bjcrofts | 0:d7b2716c5a4f | 145 | |
danilob | 4:35d92f290cfe | 146 | |
danilob | 2:5c0513ab856e | 147 | |
bjcrofts | 0:d7b2716c5a4f | 148 | if(sampleIndex+2 > SAMPLE_LENGTH){ //0.50 seconds |
bjcrofts | 0:d7b2716c5a4f | 149 | |
bjcrofts | 0:d7b2716c5a4f | 150 | sampleIndex = 0; |
bjcrofts | 0:d7b2716c5a4f | 151 | filteredLong = QAM(sLI, sLQ, &pc); |
bjcrofts | 0:d7b2716c5a4f | 152 | filteredLongRef = QAM(sRefLI, sRefLQ, &pc); |
bjcrofts | 0:d7b2716c5a4f | 153 | filteredShort = QAM(sSI, sSQ, &pc); |
bjcrofts | 0:d7b2716c5a4f | 154 | filteredShortRef = QAM(sRefSI, sRefSQ, &pc); |
bjcrofts | 0:d7b2716c5a4f | 155 | } |
bjcrofts | 0:d7b2716c5a4f | 156 | |
bjcrofts | 0:d7b2716c5a4f | 157 | c = myGPS.read(); |
bjcrofts | 0:d7b2716c5a4f | 158 | if ( myGPS.newNMEAreceived() ) { |
bjcrofts | 0:d7b2716c5a4f | 159 | if ( !myGPS.parse(myGPS.lastNMEA()) ) { |
bjcrofts | 0:d7b2716c5a4f | 160 | continue; |
bjcrofts | 0:d7b2716c5a4f | 161 | } |
bjcrofts | 0:d7b2716c5a4f | 162 | } |
bjcrofts | 0:d7b2716c5a4f | 163 | |
bjcrofts | 0:d7b2716c5a4f | 164 | |
bjcrofts | 0:d7b2716c5a4f | 165 | if(t.read_ms()>1000){ |
bjcrofts | 0:d7b2716c5a4f | 166 | led_red = !led_red; |
bjcrofts | 0:d7b2716c5a4f | 167 | |
bjcrofts | 0:d7b2716c5a4f | 168 | pc.printf("Long = %f\r\n", filteredLong); |
bjcrofts | 0:d7b2716c5a4f | 169 | pc.printf("LongRef = %f\r\n", filteredLongRef); |
bjcrofts | 0:d7b2716c5a4f | 170 | pc.printf("Short = %f\r\n", filteredShort); |
bjcrofts | 0:d7b2716c5a4f | 171 | pc.printf("ShortRef = %f\r\n", filteredShortRef); |
bjcrofts | 0:d7b2716c5a4f | 172 | pc.printf("Time: %d:%d:%d \r\n", myGPS.hour, myGPS.minute, myGPS.seconds); |
bjcrofts | 0:d7b2716c5a4f | 173 | |
bjcrofts | 0:d7b2716c5a4f | 174 | |
bjcrofts | 0:d7b2716c5a4f | 175 | if (myGPS.fix) pc.printf("Location: %5.2f%c, %5.2f%c\r\n", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon); |
bjcrofts | 0:d7b2716c5a4f | 176 | else pc.printf("No GPS fix\r\n"); |
bjcrofts | 0:d7b2716c5a4f | 177 | pc.printf("--------------------------------\r\n"); |
bjcrofts | 0:d7b2716c5a4f | 178 | |
bjcrofts | 1:8614e190908b | 179 | fp = fopen("/sd/data.txt", "w"); |
bjcrofts | 0:d7b2716c5a4f | 180 | if (fp != NULL){ |
bjcrofts | 0:d7b2716c5a4f | 181 | |
bjcrofts | 0:d7b2716c5a4f | 182 | fprintf(fp, "Long = %f\r\n", filteredLong); |
bjcrofts | 0:d7b2716c5a4f | 183 | fprintf(fp, "LongRef = %f\r\n", filteredLongRef); |
bjcrofts | 0:d7b2716c5a4f | 184 | fprintf(fp, "Short = %f\r\n", filteredShort); |
bjcrofts | 0:d7b2716c5a4f | 185 | fprintf(fp, "ShortRef = %f\r\n", filteredShortRef); |
bjcrofts | 0:d7b2716c5a4f | 186 | fprintf(fp, "Time: %d:%d:%d \r\n", myGPS.hour, myGPS.minute, myGPS.seconds); |
bjcrofts | 0:d7b2716c5a4f | 187 | if (myGPS.fix) fprintf(fp, "Location: %5.2f%c, %5.2f%c\r\n", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon); |
bjcrofts | 0:d7b2716c5a4f | 188 | else fprintf(fp, "No GPS fix\r\n"); |
bjcrofts | 0:d7b2716c5a4f | 189 | |
bjcrofts | 1:8614e190908b | 190 | fclose(fp); |
bjcrofts | 0:d7b2716c5a4f | 191 | } |
bjcrofts | 0:d7b2716c5a4f | 192 | |
bjcrofts | 0:d7b2716c5a4f | 193 | t.reset(); |
bjcrofts | 0:d7b2716c5a4f | 194 | } |
bjcrofts | 0:d7b2716c5a4f | 195 | |
bjcrofts | 0:d7b2716c5a4f | 196 | |
bjcrofts | 0:d7b2716c5a4f | 197 | |
bjcrofts | 1:8614e190908b | 198 | |
bjcrofts | 1:8614e190908b | 199 | |
bjcrofts | 0:d7b2716c5a4f | 200 | /* |
bjcrofts | 0:d7b2716c5a4f | 201 | if (sampleIndex+2 > SAMPLE_LENGTH) { // 0.25 seconds |
bjcrofts | 0:d7b2716c5a4f | 202 | |
bjcrofts | 0:d7b2716c5a4f | 203 | sampleIndex = 0; |
bjcrofts | 0:d7b2716c5a4f | 204 | pc.printf("Time: %d:%d:%d \r\n", myGPS.hour, myGPS.minute, myGPS.seconds); |
bjcrofts | 0:d7b2716c5a4f | 205 | fp = fopen("/sd/data.txt", "w"); |
bjcrofts | 0:d7b2716c5a4f | 206 | if (fp != NULL){ |
bjcrofts | 0:d7b2716c5a4f | 207 | fprintf(fp,"Time: %d:%d:%d.%u \r\n", myGPS.hour, myGPS.minute, myGPS.seconds, myGPS.milliseconds); |
bjcrofts | 0:d7b2716c5a4f | 208 | fclose(fp); |
bjcrofts | 0:d7b2716c5a4f | 209 | } |
bjcrofts | 0:d7b2716c5a4f | 210 | |
bjcrofts | 0:d7b2716c5a4f | 211 | if (myGPS.fix) { |
bjcrofts | 0:d7b2716c5a4f | 212 | pc.printf("Location: %5.2f%c, %5.2f%c\r\n", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon); |
bjcrofts | 0:d7b2716c5a4f | 213 | led_red = !led_red; |
bjcrofts | 0:d7b2716c5a4f | 214 | fp = fopen("/sd/data.txt", "w"); |
bjcrofts | 0:d7b2716c5a4f | 215 | if (fp != NULL){ |
bjcrofts | 0:d7b2716c5a4f | 216 | fprintf(fp,"Location: %5.2f%c, %5.2f%c\r\n", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon); |
bjcrofts | 0:d7b2716c5a4f | 217 | fclose(fp); |
bjcrofts | 0:d7b2716c5a4f | 218 | } |
bjcrofts | 0:d7b2716c5a4f | 219 | }else{ |
bjcrofts | 0:d7b2716c5a4f | 220 | fprintf(fp,"no gps fix\r\n"); |
bjcrofts | 0:d7b2716c5a4f | 221 | fclose(fp); |
bjcrofts | 0:d7b2716c5a4f | 222 | } |
bjcrofts | 0:d7b2716c5a4f | 223 | } |
bjcrofts | 0:d7b2716c5a4f | 224 | */ |
bjcrofts | 0:d7b2716c5a4f | 225 | } |
bjcrofts | 0:d7b2716c5a4f | 226 | } |