for Danillo

Dependencies:   MBed_Adafruit-GPS-Library SDFileSystem mbed

realtimeQAM.cpp

Committer:
ncfronk
Date:
2014-12-11
Revision:
2:abcf77d0e77d
Parent:
1:f5770d9636b4
Child:
3:0cc40383d016

File content as of revision 2:abcf77d0e77d:

#pragma once
#include "mbed.h"
#include "math.h"
#include "MBed_Adafruit_GPS.h"
#include "SDFileSystem.h"

#include "QAM.h"
#include "dataPoint.h"

#define SAMPLE_LENGTH       2000 
#define SAMPLE_RATE         10000
#define SIN_LENGTH          500
#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;

Ticker tick1;
Ticker tick2;
AnalogIn AnLong(A0);
AnalogIn AnShort(A1);
AnalogIn AnRefLong(A2);
AnalogIn AnRefShort(A3);
AnalogOut dac0(DAC0_OUT);
DigitalOut led_red(LED_RED);
Serial pc(USBTX, USBRX);

int sinRes = (int)1/(CARRIER_FREQ*TIME_CONST); 
int freq = 1;

float sinWave[SIN_LENGTH] = {};
int sinIndex = 0;

float samplesLong[SAMPLE_LENGTH] = {};
float samplesShort[SAMPLE_LENGTH] = {};
float samplesRefLong[SAMPLE_LENGTH] = {};
float samplesRefShort[SAMPLE_LENGTH] = {};
int sampleIndex = 0;

float sI[SAMPLE_LENGTH] = {};
float sQ[SAMPLE_LENGTH] = {};

float decimateLong[4] = {};
float decimateShort[4] = {};
float decimateRef[4] = {};

float totalAVG = 0;

bool isSampling = true;
int avgIndex = 0;


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]);
    }
}

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){
        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;
        }
        
    }

}

void create_sinWave(){
    int i = 0;
    for(i = 0; i < sinRes; i++){
        sinWave[i] = 0.25 * sin(2.0*PI*i/sinRes) + 0.75;
    }
}

void set_Values(int inFreq){
    freq = inFreq;
    create_sinWave();
}

void print_values(){
    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(){
    pc.baud(115200);
    
    gps_Serial = new Serial(D1,D0); //serial object for use w/ GPS
    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?
    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
    
    myGPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); //these commands are defined in MBed_Adafruit_GPS.h; a link is provided there for command creation
    myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
    myGPS.sendCommand(PGCMD_ANTENNA);
    
    tick1.attach(&tick_out, 0.0001); // below 0.00005 the board can no longer output and read
    //tick2.attach(&print_values, 20);
    
    set_Values(220);
    pc.printf("sinres = %i\n", sinRes);
    
    float filteredLongTemp = 0;
    float filteredShortTemp = 0;
    float filteredRefLongTemp = 0;
    float filteredRefShortTemp = 0;
    
    refresh_Timer.start();  
    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;                
            }
            isSampling = !isSampling;
            sampling_Toggle.reset();            
        }
        
        
        if(!isSampling){  
            c = myGPS.read();   
            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.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);
                }
            }
        }        
    }
}