for Danillo

Dependencies:   MBed_Adafruit-GPS-Library SDFileSystem mbed

realtimeQAM.cpp

Committer:
ncfronk
Date:
2014-11-18
Revision:
1:f5770d9636b4
Parent:
0:37d4e78b2076
Child:
2:abcf77d0e77d

File content as of revision 1:f5770d9636b4:

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

#include "QAM.h"

#define SAMPLE_LENGTH       512 
#define SAMPLE_RATE         16666
#define SIN_LENGTH          500
#define OUTAVG_LENGTH       1000
#define PI                  3.14159265
 
Serial * gps_Serial;

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

int sinRes = 74; // resolution of sinWave 74 for 225 hz
int freq = 1;

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

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

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

float filtered[OUTAVG_LENGTH] = {};
float filteredRef[OUTAVG_LENGTH] = {};

float totalAVG = 0;

bool newValue = false;
bool isSampling = true;
int avgIndex = 0;

void print_array(float *bar,int length){
    int i =0;
    for(i = 0; i < length; i++){
        pc.printf("%f, ", bar[i]);
    }
    pc.printf("\n\n\n\n");
}

void tick_out(){
    if(isSampling){
        //read
        samplesLong[sampleIndex] = AnLong.read();
        samplesShort[sampleIndex] = AnLong.read();
        samplesRef[sampleIndex] = AnRef.read();
        sampleIndex++;
        if(sampleIndex+1 > SAMPLE_LENGTH){
            sampleIndex = 0;
        }
        //sampleIndex = (sampleIndex+1)&(SAMPLE_LENGTH-1);
        //write
        dac0 = sinWave[sinIndex];
        sinIndex++;
        if((sinIndex+1) > sinRes){
            sinIndex = 0;
        }
        
        newValue = true;
    }

}

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

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

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

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?
    const int refresh_Time = 10000; //refresh time in ms
    
    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.06); // below 0.00005 the board can no longer output and read
    //tick2.attach(&print_values, 20);
    
    set_Values(225);
    pc.printf("Set values\n");
    
    float filteredLongTemp = 0;
    float filteredShortTemp = 0;
    float filteredRefTemp = 0;
    
    refresh_Timer.start();  //starts the clock on the timer
    
    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;   
            }    
        }
        
        //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);
            }
        }
        //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;
        }
    }
}