DCS_TEAM / Mbed 2 deprecated DCS

Dependencies:   MBed_Adafruit-GPS-Library SDFileSystem mbed GSM_Library

Fork of DCS by Brandon Crofts

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Sensor.cpp Source File

Sensor.cpp

00001 #pragma once
00002 #include "mbed.h"
00003 #include "math.h"
00004 #include "MBed_Adafruit_GPS.h"
00005 #include "SDFileSystem.h"
00006 #include "QAM.h"
00007 #include "param.h"
00008 #include "GSMLibrary.h"
00009 #include "stdlib.h"
00010 
00011 DigitalOut led_red(LED_RED);
00012 Serial pc(USBTX, USBRX);
00013 Timer t;
00014 bool run = 0;
00015 bool fil = 0;
00016 Serial gsm(D1,D0);
00017 
00018 
00019 /**************************************************
00020  **          SD FILE SYSTEM                       **
00021  **************************************************/
00022 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd");
00023 FILE *fp;
00024 
00025 /**************************************************
00026  **          GPS                                 **
00027  **************************************************/
00028 Serial * gps_Serial;
00029 
00030 /**************************************************
00031  **          SENSOR INPUTS                       **
00032  **************************************************/
00033 AnalogIn AnLong(A0);
00034 AnalogIn AnShort(A1);
00035 AnalogIn AnRefLong(A2);
00036 AnalogIn AnRefShort(A3);
00037 Ticker sample_tick;
00038 bool takeSample = false;
00039 void tick(){takeSample = true;}
00040 
00041 /**************************************************
00042  **          SIN OUTPUT                          **
00043  **************************************************/
00044 AnalogOut dac0(DAC0_OUT);
00045 int sinRes = (int)1/(CARRIER_FREQ*TIME_CONST);
00046 float sinWave[SIN_LENGTH] = {};
00047 int sinIndex = 0;
00048 
00049 
00050 /**************************************************
00051  **          QAM                                 **
00052  **************************************************/
00053 float sLQ[SAMPLE_LENGTH] = {};
00054 float sLI[SAMPLE_LENGTH] = {};
00055 float sSQ[SAMPLE_LENGTH] = {};
00056 float sSI[SAMPLE_LENGTH] = {};
00057 float sRefLQ[SAMPLE_LENGTH] = {};
00058 float sRefLI[SAMPLE_LENGTH] = {};
00059 float sRefSQ[SAMPLE_LENGTH] = {};
00060 float sRefSI[SAMPLE_LENGTH] = {};
00061 
00062 float Iarray[SAMPLE_LENGTH] = {};
00063 float Qarray[SAMPLE_LENGTH] = {};
00064 int sampleIndex = 0;
00065 float I = 0;
00066 float Q = 0;
00067 float lon = 0;
00068 float lonRef = 0;
00069 float shor = 0;
00070 float shorRef = 0;
00071 
00072 void buildIQ(){
00073     for(int i = 0; i < SAMPLE_LENGTH; i++){
00074         Iarray[i] = cos(2*PI*CARRIER_FREQ*i*TIME_CONST);
00075         Qarray[i] = -sin(2*PI*CARRIER_FREQ*i*TIME_CONST);
00076     }
00077 }
00078 
00079 void create_sinWave(){
00080     int i = 0;
00081     for(i = 0; i < SIN_LENGTH; i++){
00082         sinWave[i] = 0.25 * sin(2.0*PI*i/sinRes) + 0.75;
00083     }
00084 }
00085 
00086 int main () {
00087     
00088     pc.baud(115200);
00089     gsm.baud(115200);
00090     pc.printf("hello\r\n");
00091     
00092     //GSM INITIALIZATION ///////////////////////////////
00093     gsm_initialize();
00094     
00095     // GPS INITIALIZATION //////////////////////////////
00096     gps_Serial = new Serial(PTC4,PTC3); ////Serial gsm(D1,D0);
00097     Adafruit_GPS myGPS(gps_Serial);
00098     char c;
00099     myGPS.begin(9600);
00100     myGPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
00101     myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
00102     myGPS.sendCommand(PGCMD_ANTENNA);
00103     ////////////////////////////////////////////////////
00104     
00105     buildIQ();
00106     create_sinWave();
00107     
00108     float filteredLong = 0;
00109     float filteredShort = 0;
00110     float filteredLongRef = 0;
00111     float filteredShortRef = 0;
00112     
00113     fp = fopen("/sd/data.txt", "w");
00114     if (fp != NULL){
00115         fprintf(fp, "--------------- DCS ------------------");
00116     }
00117     
00118     
00119     void gsm_initialize();
00120     
00121     sample_tick.attach(&tick, 0.0001);
00122     
00123     t.start();
00124     
00125     while(1){
00126         
00127         if(takeSample){
00128             
00129             dac0 = sinWave[sinIndex];
00130             
00131             lon = AnLong.read();
00132             lonRef = AnRefLong.read();
00133             shor = AnShort.read();
00134             shorRef = AnRefShort.read();
00135             
00136             I = Iarray[sampleIndex];
00137             Q = Qarray[sampleIndex];
00138             sLI[sampleIndex] = lon*I;
00139             sLQ[sampleIndex] = lon*Q;
00140             sSI[sampleIndex] = shor*I;
00141             sSQ[sampleIndex] = shor*Q;
00142             sRefLI[sampleIndex] = lonRef*I;
00143             sRefLQ[sampleIndex] = lonRef*Q;
00144             sRefSI[sampleIndex] = shorRef*I;
00145             sRefSQ[sampleIndex] = shorRef*Q;
00146             
00147             takeSample = false;
00148             
00149             sinIndex++;
00150             if((sinIndex+1) > SIN_LENGTH){
00151                 sinIndex = 0;
00152             }
00153             
00154             
00155             sampleIndex++;
00156             if(sampleIndex+1 > SAMPLE_LENGTH){
00157                 fil = 1;
00158             }
00159             
00160         
00161         }
00162         
00163         
00164 
00165         if(fil==1){
00166             
00167             fil = 0;
00168             run = 1;
00169             
00170             sampleIndex = 0;
00171             
00172             gsm_tick();
00173             
00174             filteredLong = 10*QAM(sLI, sLQ);
00175             filteredLongRef = QAM(sRefLI, sRefLQ);
00176             filteredShort = 25*QAM(sSI, sSQ);
00177             filteredShortRef = QAM(sRefSI, sRefSQ);
00178             
00179             gsm_tick();
00180             
00181         }  
00182         
00183         c = myGPS.read();
00184         if ( myGPS.newNMEAreceived() ) {
00185             if ( !myGPS.parse(myGPS.lastNMEA()) ) {
00186                 continue;
00187             }
00188         }
00189 
00190 
00191         if(run){
00192             led_red = !led_red;
00193             run = 0;
00194             
00195             pc.printf("%f, ", filteredLong);
00196             pc.printf("%f, ", filteredLongRef);
00197             pc.printf("%f, ", filteredShort);
00198             pc.printf("%f\r\n", filteredShortRef);
00199             pc.printf("%f, ", filteredLong/filteredLongRef);
00200             pc.printf("%f\r\n", filteredShort/filteredShortRef);
00201             pc.printf("%d:%d:%d \r\n", myGPS.hour+6, myGPS.minute, myGPS.seconds);
00202             
00203             if (myGPS.fix) pc.printf("%5.2f%c, %5.2f%c\r\n", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon);
00204             else           pc.printf("No GPS fix\r\n");
00205             pc.printf("--------------------------------\r\n");
00206             
00207             fp = fopen("/sd/data.txt", "a");
00208             if (fp != NULL){
00209                 
00210                 fprintf(fp, "%f, ", filteredLong);
00211                 fprintf(fp, "%f, ", filteredLongRef);
00212                 fprintf(fp, "%f, ", filteredShort);
00213                 fprintf(fp, "%f\r\n", filteredShortRef);
00214                 fprintf(fp, "%d:%d:%d\r\n", myGPS.hour+6, myGPS.minute, myGPS.seconds);
00215                 if (myGPS.fix) fprintf(fp, "%5.2f%c, %5.2f%c\r\n", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon);
00216                 else           fprintf(fp, "No_GPS_fix\r\n");
00217                 
00218                 fclose(fp);    
00219             }
00220         
00221             if(myGPS.fix)
00222                 gsm_send_data(filteredLong, filteredLongRef, filteredShort, filteredShortRef, myGPS.hour+6, myGPS.minute, myGPS.seconds, myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon);
00223             else
00224                 gsm_send_data(filteredLong, filteredLongRef, filteredShort, filteredShortRef, 0, 0, 0, 0, 0, 0, 0);
00225 
00226         }
00227         
00228     }
00229 }