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