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