for Danillo

Dependencies:   MBed_Adafruit-GPS-Library SDFileSystem mbed

Revision:
3:0cc40383d016
Parent:
2:abcf77d0e77d
--- a/realtimeQAM.cpp	Thu Dec 11 01:23:24 2014 +0000
+++ b/realtimeQAM.cpp	Sat Jan 17 19:12:28 2015 +0000
@@ -7,10 +7,12 @@
 #include "QAM.h"
 #include "dataPoint.h"
 
-#define SAMPLE_LENGTH       2000 
+#define SAMPLE_LENGTH       1000 
 #define SAMPLE_RATE         10000
 #define SIN_LENGTH          500
 #define OUTAVG_LENGTH       100
+#define CARRIER_FREQ        220
+#define TIME_CONST          0.0001
 #define PI                  3.14159265
 
 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS
@@ -35,14 +37,24 @@
 float sinWave[SIN_LENGTH] = {};
 int sinIndex = 0;
 
-float samplesLong[SAMPLE_LENGTH] = {};
-float samplesShort[SAMPLE_LENGTH] = {};
-float samplesRefLong[SAMPLE_LENGTH] = {};
-float samplesRefShort[SAMPLE_LENGTH] = {};
+float sLQ[SAMPLE_LENGTH] = {};
+float sLI[SAMPLE_LENGTH] = {};
+float sSQ[SAMPLE_LENGTH] = {};
+float sSI[SAMPLE_LENGTH] = {};
+float sRefLQ[SAMPLE_LENGTH] = {};
+float sRefLI[SAMPLE_LENGTH] = {};
+float sRefSQ[SAMPLE_LENGTH] = {};
+float sRefSI[SAMPLE_LENGTH] = {};
+
+float Iarray[SAMPLE_LENGTH] = {};
+float Qarray[SAMPLE_LENGTH] = {};
 int sampleIndex = 0;
-
-float sI[SAMPLE_LENGTH] = {};
-float sQ[SAMPLE_LENGTH] = {};
+float I = 0;
+float Q = 0;
+float lon = 0;
+float lonRef = 0;
+float shor = 0;
+float shorRef = 0;
 
 float decimateLong[4] = {};
 float decimateShort[4] = {};
@@ -53,6 +65,8 @@
 bool isSampling = true;
 int avgIndex = 0;
 
+char * timeDatestamp = "";
+
 
 void print_array(float *bar,int length, FILE *file){
     int i =0;
@@ -77,23 +91,43 @@
     return total/length;
 }
 
+void buildIQ(){
+    for(int i = 0; i < SAMPLE_LENGTH; i++){
+        Iarray[i] = cos(2*PI*CARRIER_FREQ*i*TIME_CONST);
+        Qarray[i] = -sin(2*PI*CARRIER_FREQ*i*TIME_CONST);
+    }
+}
+
 void tick_out(){
     if(isSampling){
-        samplesLong[sampleIndex] = AnLong.read();
-        samplesShort[sampleIndex] = AnShort.read();
-        samplesRefLong[sampleIndex] = AnRefLong.read();
-        samplesRefShort[sampleIndex] = AnRefShort.read();
-        sampleIndex++;
-        if(sampleIndex+1 > SAMPLE_LENGTH){
-            sampleIndex--;
-        }
-        //write
         dac0 = sinWave[sinIndex];
         sinIndex++;
         if((sinIndex+1) > sinRes){
             sinIndex = 0;
         }
         
+        lon = AnLong.read();
+        lonRef = AnRefLong.read();
+        shor = AnShort.read();
+        shorRef = AnRefShort.read();
+        
+        I = Iarray[sampleIndex];
+        Q = Qarray[sampleIndex];
+        sLI[sampleIndex] = lon*I;
+        sLQ[sampleIndex] = lon*Q;
+        sSI[sampleIndex] = shor*I;
+        sSQ[sampleIndex] = shor*Q;
+        sRefLI[sampleIndex] = lonRef*I;
+        sRefLQ[sampleIndex] = lonRef*Q;
+        sRefSI[sampleIndex] = shorRef*I;
+        sRefSQ[sampleIndex] = shorRef*Q;
+        
+        sampleIndex++;
+        if(sampleIndex+1 > SAMPLE_LENGTH){
+            sampleIndex--;
+        }
+        
+        
     }
 
 }
@@ -111,7 +145,7 @@
 }
 
 void print_values(){
-    fp = fopen("/sd/hello.txt", "r");
+    fp = fopen("/sd/data.txt", "r");
     if (fp != NULL) {
         fclose(fp);
         remove("/sd/hello.txt");
@@ -126,33 +160,23 @@
     fclose(fp);
 }
 
-//does not work right now
-bool run_gps(int refresh_Timer, Adafruit_GPS myGPS, int refresh_Time, char c){
-    c = myGPS.read();   //queries the GPS
-        
-     //if (c) { pc.printf("%c", c); } //this line will echo the GPS data if not paused
-       
-     //check if we recieved a new message from GPS, if so, attempt to parse it,
-     if ( myGPS.newNMEAreceived() ) {
-         if ( !myGPS.parse(myGPS.lastNMEA()) ) {
-             //continue;   
-         }    
-     }
-       
-     //check if enough time has passed to warrant printing GPS info to screen
-     //note if refresh_Time is too low or pc.baud is too low, GPS data may be lost during printing
-     if (refresh_Timer >= refresh_Time) {
-         //refresh_Timer.reset();
-         pc.printf("Time: %d:%d:%d.%u\n", myGPS.hour, myGPS.minute, myGPS.seconds, myGPS.milliseconds);   
-         pc.printf("Date: %d/%d/20%d\n", myGPS.day, myGPS.month, myGPS.year);
-         //pc.printf("Fix: %d\n", (int) myGPS.fix);
-         //pc.printf("Quality: %d\n", (int) myGPS.fixquality);
-         if (myGPS.fix) {
-             pc.printf("Location: %5.2f%c, %5.2f%c\n", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon);
-         }
-         return 1;
-     }
-     return 0;
+void save_point(char * c){
+    fp = fopen("/sd/data.txt", "r");
+    if (fp != NULL) {
+        fclose(fp);
+        remove("/sd/data.txt");
+        pc.printf("Remove an existing file with the same name \n");
+    }
+
+    printf("\nWriting data to the sd card \n");
+    fp = fopen("/sd/data.txt", "w");
+    if (fp == NULL) {
+        pc.printf("Unable to write the file \n");
+    } else {
+        fprintf(fp, c);
+        fprintf(fp, "\n");
+        fclose(fp);
+    }
 }
 
 int main(){
@@ -164,8 +188,9 @@
     Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info?
     Timer sampling_Toggle;
     Timer t; 
-    const int refresh_Time = 700; //refresh time in ms
-    const int toggle_Time = 1000;
+    const int refresh_Time = 1000; //refresh time in ms
+    const int sample_Time = 250;
+    const int filter_Time = 2000;
     
     myGPS.begin(9600);  //sets baud rate for GPS communication; note this may be changed via Adafruit_GPS::sendCommand(char *)
                         //a list of GPS commands is available at http://www.adafruit.com/datasheets/PMTK_A08.pdf
@@ -174,6 +199,8 @@
     myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
     myGPS.sendCommand(PGCMD_ANTENNA);
     
+    buildIQ();
+    
     tick1.attach(&tick_out, 0.0001); // below 0.00005 the board can no longer output and read
     //tick2.attach(&print_values, 20);
     
@@ -189,23 +216,33 @@
     sampling_Toggle.start();
     
     while(1){        
-        if (sampling_Toggle.read_ms() >= toggle_Time) {
-            if(isSampling){
-                t.start();
-                filteredLongTemp = qam_in(samplesLong, sI, sQ, &pc);
-                filteredRefLongTemp = qam_in(samplesRefLong, sI, sQ, &pc);
-                filteredShortTemp = qam_in(samplesShort, sI, sQ, &pc);
-                filteredRefShortTemp = qam_in(samplesRefShort, sI, sQ, &pc);
-                t.stop();
-                pc.printf("Long = %f, longref = %f, Short = %f, shortref = %f in time = %f \n",filteredLongTemp, filteredRefLongTemp, filteredShortTemp, filteredRefShortTemp, t.read());
-                t.reset();
-            }else{
-                sampleIndex = 0;                
+        if(isSampling){
+            if (sampling_Toggle.read_ms() >= sample_Time) {
+                isSampling = false;
+                sampling_Toggle.reset();
+                
+                filteredLongTemp = qam_in(sLI, sLQ, &pc);
+                filteredRefLongTemp = qam_in(sRefLI, sRefLQ, &pc);
+                filteredShortTemp = qam_in(sSI, sSQ, &pc);
+                filteredRefShortTemp = qam_in(sRefSI, sRefSQ, &pc);
+                pc.printf("Long = %f,  Short = %f ",filteredLongTemp, filteredShortTemp);
+                
+                fp = fopen("/sd/data.txt", "w");
+                if (fp == NULL) {
+                    pc.printf("Unable to write the file \n");
+                } else {
+                    fprintf(fp,"Long = %f,  Short = %f ",filteredLongTemp/filteredRefShortTemp, filteredShortTemp/filteredRefShortTemp);
+                    fclose(fp);
+                }
             }
-            isSampling = !isSampling;
-            sampling_Toggle.reset();            
-        }
-        
+        }else{
+            if (sampling_Toggle.read_ms() >= filter_Time) {
+                isSampling = true;
+                sampling_Toggle.reset();
+                
+                sampleIndex = 0;
+            }                
+        } 
         
         if(!isSampling){  
             c = myGPS.read();   
@@ -219,11 +256,25 @@
             //note if refresh_Time is too low or pc.baud is too low, GPS data may be lost during printing
             if (refresh_Timer.read_ms() >= refresh_Time) {
                 refresh_Timer.reset();
-                //pc.printf("Time: %d:%d:%d.%u----", myGPS.hour, myGPS.minute, myGPS.seconds, myGPS.milliseconds);   
+                pc.printf("Time: %d:%d:%d.%u \n", myGPS.hour, myGPS.minute, myGPS.seconds, myGPS.milliseconds);
+                fp = fopen("/sd/data.txt", "w");
+                if (fp == NULL) {
+                    pc.printf("Unable to write the file \n");
+                } else {
+                    fprintf(fp,"Time: %d:%d:%d.%u \n", myGPS.hour, myGPS.minute, myGPS.seconds, myGPS.milliseconds);
+                    fclose(fp);
+                }   
                 if (myGPS.fix) {
-                    //pc.printf("Location: %5.2f%c, %5.2f%c\n", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon);
+                    pc.printf("Location: %5.2f%c, %5.2f%c\n", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon);
+                    fp = fopen("/sd/data.txt", "w");
+                    if (fp == NULL) {
+                        pc.printf("Unable to write the file \n");
+                    } else {
+                        fprintf(fp,"Location: %5.2f%c, %5.2f%c\n", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon);
+                        fclose(fp);
+                    }
                 }
             }
-        }        
+        }                  
     }
 }
\ No newline at end of file