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