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