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