for Danillo

Dependencies:   MBed_Adafruit-GPS-Library SDFileSystem mbed

Revision:
2:abcf77d0e77d
Parent:
1:f5770d9636b4
Child:
3:0cc40383d016
--- a/realtimeQAM.cpp	Tue Nov 18 08:50:58 2014 +0000
+++ b/realtimeQAM.cpp	Thu Dec 11 01:23:24 2014 +0000
@@ -2,14 +2,20 @@
 #include "mbed.h"
 #include "math.h"
 #include "MBed_Adafruit_GPS.h"
+#include "SDFileSystem.h"
 
 #include "QAM.h"
+#include "dataPoint.h"
 
-#define SAMPLE_LENGTH       512 
-#define SAMPLE_RATE         16666
+#define SAMPLE_LENGTH       2000 
+#define SAMPLE_RATE         10000
 #define SIN_LENGTH          500
-#define OUTAVG_LENGTH       1000
+#define OUTAVG_LENGTH       100
 #define PI                  3.14159265
+
+SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS
+FILE *fp;
+char buffer[1024];
  
 Serial * gps_Serial;
 
@@ -17,12 +23,13 @@
 Ticker tick2;
 AnalogIn AnLong(A0);
 AnalogIn AnShort(A1);
-AnalogIn AnRef(A2);
+AnalogIn AnRefLong(A2);
+AnalogIn AnRefShort(A3);
 AnalogOut dac0(DAC0_OUT);
 DigitalOut led_red(LED_RED);
 Serial pc(USBTX, USBRX);
 
-int sinRes = 74; // resolution of sinWave 74 for 225 hz
+int sinRes = (int)1/(CARRIER_FREQ*TIME_CONST); 
 int freq = 1;
 
 float sinWave[SIN_LENGTH] = {};
@@ -30,40 +37,56 @@
 
 float samplesLong[SAMPLE_LENGTH] = {};
 float samplesShort[SAMPLE_LENGTH] = {};
-float samplesRef[SAMPLE_LENGTH] = {};
+float samplesRefLong[SAMPLE_LENGTH] = {};
+float samplesRefShort[SAMPLE_LENGTH] = {};
 int sampleIndex = 0;
 
 float sI[SAMPLE_LENGTH] = {};
 float sQ[SAMPLE_LENGTH] = {};
 
-float filtered[OUTAVG_LENGTH] = {};
-float filteredRef[OUTAVG_LENGTH] = {};
+float decimateLong[4] = {};
+float decimateShort[4] = {};
+float decimateRef[4] = {};
 
 float totalAVG = 0;
 
-bool newValue = false;
 bool isSampling = true;
 int avgIndex = 0;
 
-void print_array(float *bar,int length){
+
+void print_array(float *bar,int length, FILE *file){
+    int i =0;
+    for(i = 0; i < length; i++){
+        fprintf(file, "%f, ", bar[i]);
+    }
+}
+
+void print_array_serial(float *bar,int length){
     int i =0;
     for(i = 0; i < length; i++){
         pc.printf("%f, ", bar[i]);
     }
-    pc.printf("\n\n\n\n");
+}
+
+float avg_array(float *array, int length){
+    int i = 0;
+    float total = 0;
+    for(i = 0; i < length; i++){
+        total += array[i];
+    }   
+    return total/length;
 }
 
 void tick_out(){
     if(isSampling){
-        //read
         samplesLong[sampleIndex] = AnLong.read();
-        samplesShort[sampleIndex] = AnLong.read();
-        samplesRef[sampleIndex] = AnRef.read();
+        samplesShort[sampleIndex] = AnShort.read();
+        samplesRefLong[sampleIndex] = AnRefLong.read();
+        samplesRefShort[sampleIndex] = AnRefShort.read();
         sampleIndex++;
         if(sampleIndex+1 > SAMPLE_LENGTH){
-            sampleIndex = 0;
+            sampleIndex--;
         }
-        //sampleIndex = (sampleIndex+1)&(SAMPLE_LENGTH-1);
         //write
         dac0 = sinWave[sinIndex];
         sinIndex++;
@@ -71,7 +94,6 @@
             sinIndex = 0;
         }
         
-        newValue = true;
     }
 
 }
@@ -79,7 +101,7 @@
 void create_sinWave(){
     int i = 0;
     for(i = 0; i < sinRes; i++){
-        sinWave[i] = 0.5 * sin(2.0*PI*i/sinRes) + 0.5;
+        sinWave[i] = 0.25 * sin(2.0*PI*i/sinRes) + 0.75;
     }
 }
 
@@ -89,12 +111,48 @@
 }
 
 void print_values(){
-    printf("\n\r--------------------------------------SAMPLES-----------------------------------\n\r \r\n");
-    print_array(samplesLong, SAMPLE_LENGTH);
-    printf("\n\r-------------------------------------------SI-------------------------------------\n\r \r\n");
-    print_array(sI, SAMPLE_LENGTH);
-    printf("\n\r-----------------------------------------SQ--------------------------------------\n\r \r\n");
-    print_array(sQ, SAMPLE_LENGTH);
+    fp = fopen("/sd/hello.txt", "r");
+    if (fp != NULL) {
+        fclose(fp);
+        remove("/sd/hello.txt");
+        pc.printf("Remove an existing file with the same name \n");
+    }
+
+    printf("\nWriting data to the sd card \n");
+    fp = fopen("/sd/hello.txt", "w");
+    if (fp == NULL) {
+        pc.printf("Unable to write the file \n");
+    }
+    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;
 }
 
 int main(){
@@ -104,7 +162,10 @@
     Adafruit_GPS myGPS(gps_Serial); //object of Adafruit's GPS class
     char c; //when read via Adafruit_GPS::read(), the class returns single character stored here
     Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info?
-    const int refresh_Time = 10000; //refresh time in ms
+    Timer sampling_Toggle;
+    Timer t; 
+    const int refresh_Time = 700; //refresh time in ms
+    const int toggle_Time = 1000;
     
     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
@@ -113,59 +174,56 @@
     myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
     myGPS.sendCommand(PGCMD_ANTENNA);
     
-    tick1.attach(&tick_out, 0.06); // below 0.00005 the board can no longer output and read
+    tick1.attach(&tick_out, 0.0001); // below 0.00005 the board can no longer output and read
     //tick2.attach(&print_values, 20);
     
-    set_Values(225);
-    pc.printf("Set values\n");
+    set_Values(220);
+    pc.printf("sinres = %i\n", sinRes);
     
     float filteredLongTemp = 0;
     float filteredShortTemp = 0;
-    float filteredRefTemp = 0;
+    float filteredRefLongTemp = 0;
+    float filteredRefShortTemp = 0;
     
-    refresh_Timer.start();  //starts the clock on the timer
+    refresh_Timer.start();  
+    sampling_Toggle.start();
     
-    while(1){
-        //isSampling = false;
-         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;   
-            }    
+    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;                
+            }
+            isSampling = !isSampling;
+            sampling_Toggle.reset();            
         }
         
-        //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.read_ms() >= 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);
+        
+        if(!isSampling){  
+            c = myGPS.read();   
+            if ( myGPS.newNMEAreceived() ) {
+                if ( !myGPS.parse(myGPS.lastNMEA()) ) {
+                    continue;   
+                }    
             }
-        }
-        //isSampling = true;
-        if(newValue){ //newValue){ 
-           filteredLongTemp = qam_in(samplesLong, sampleIndex, sI, sQ); 
-           filtered[avgIndex] = filteredLongTemp;
-           filteredRefTemp = qam_in(samplesRef, sampleIndex, sI, sQ);
-           filteredRef[avgIndex] = filteredRefTemp;
-           pc.printf("%f, ", filteredLongTemp);
-           avgIndex++;
-           if(avgIndex+1 > OUTAVG_LENGTH){
-               isSampling = false;
-               //print_values();
-               print_array(filtered,  OUTAVG_LENGTH);
-               avgIndex = 0;
-               isSampling = true;
-           }
-            newValue = false;
-        }
+        
+            //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.read_ms() >= refresh_Time) {
+                refresh_Timer.reset();
+                //pc.printf("Time: %d:%d:%d.%u----", myGPS.hour, myGPS.minute, myGPS.seconds, myGPS.milliseconds);   
+                if (myGPS.fix) {
+                    //pc.printf("Location: %5.2f%c, %5.2f%c\n", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon);
+                }
+            }
+        }        
     }
 }
\ No newline at end of file