for Danillo

Dependencies:   MBed_Adafruit-GPS-Library SDFileSystem mbed

Committer:
ncfronk
Date:
Tue Nov 18 08:50:58 2014 +0000
Revision:
1:f5770d9636b4
Parent:
0:37d4e78b2076
Child:
2:abcf77d0e77d
gps does not work concurrently with sampling;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ncfronk 1:f5770d9636b4 1 #pragma once
ncfronk 0:37d4e78b2076 2 #include "mbed.h"
ncfronk 0:37d4e78b2076 3 #include "math.h"
ncfronk 1:f5770d9636b4 4 #include "MBed_Adafruit_GPS.h"
ncfronk 1:f5770d9636b4 5
ncfronk 1:f5770d9636b4 6 #include "QAM.h"
ncfronk 0:37d4e78b2076 7
ncfronk 0:37d4e78b2076 8 #define SAMPLE_LENGTH 512
ncfronk 0:37d4e78b2076 9 #define SAMPLE_RATE 16666
ncfronk 0:37d4e78b2076 10 #define SIN_LENGTH 500
ncfronk 0:37d4e78b2076 11 #define OUTAVG_LENGTH 1000
ncfronk 1:f5770d9636b4 12 #define PI 3.14159265
ncfronk 1:f5770d9636b4 13
ncfronk 1:f5770d9636b4 14 Serial * gps_Serial;
ncfronk 0:37d4e78b2076 15
ncfronk 0:37d4e78b2076 16 Ticker tick1;
ncfronk 0:37d4e78b2076 17 Ticker tick2;
ncfronk 1:f5770d9636b4 18 AnalogIn AnLong(A0);
ncfronk 1:f5770d9636b4 19 AnalogIn AnShort(A1);
ncfronk 1:f5770d9636b4 20 AnalogIn AnRef(A2);
ncfronk 0:37d4e78b2076 21 AnalogOut dac0(DAC0_OUT);
ncfronk 0:37d4e78b2076 22 DigitalOut led_red(LED_RED);
ncfronk 0:37d4e78b2076 23 Serial pc(USBTX, USBRX);
ncfronk 0:37d4e78b2076 24
ncfronk 0:37d4e78b2076 25 int sinRes = 74; // resolution of sinWave 74 for 225 hz
ncfronk 0:37d4e78b2076 26 int freq = 1;
ncfronk 0:37d4e78b2076 27
ncfronk 0:37d4e78b2076 28 float sinWave[SIN_LENGTH] = {};
ncfronk 0:37d4e78b2076 29 int sinIndex = 0;
ncfronk 0:37d4e78b2076 30
ncfronk 1:f5770d9636b4 31 float samplesLong[SAMPLE_LENGTH] = {};
ncfronk 1:f5770d9636b4 32 float samplesShort[SAMPLE_LENGTH] = {};
ncfronk 1:f5770d9636b4 33 float samplesRef[SAMPLE_LENGTH] = {};
ncfronk 0:37d4e78b2076 34 int sampleIndex = 0;
ncfronk 1:f5770d9636b4 35
ncfronk 0:37d4e78b2076 36 float sI[SAMPLE_LENGTH] = {};
ncfronk 0:37d4e78b2076 37 float sQ[SAMPLE_LENGTH] = {};
ncfronk 1:f5770d9636b4 38
ncfronk 1:f5770d9636b4 39 float filtered[OUTAVG_LENGTH] = {};
ncfronk 1:f5770d9636b4 40 float filteredRef[OUTAVG_LENGTH] = {};
ncfronk 1:f5770d9636b4 41
ncfronk 1:f5770d9636b4 42 float totalAVG = 0;
ncfronk 0:37d4e78b2076 43
ncfronk 0:37d4e78b2076 44 bool newValue = false;
ncfronk 0:37d4e78b2076 45 bool isSampling = true;
ncfronk 0:37d4e78b2076 46 int avgIndex = 0;
ncfronk 0:37d4e78b2076 47
ncfronk 1:f5770d9636b4 48 void print_array(float *bar,int length){
ncfronk 0:37d4e78b2076 49 int i =0;
ncfronk 1:f5770d9636b4 50 for(i = 0; i < length; i++){
ncfronk 0:37d4e78b2076 51 pc.printf("%f, ", bar[i]);
ncfronk 0:37d4e78b2076 52 }
ncfronk 0:37d4e78b2076 53 pc.printf("\n\n\n\n");
ncfronk 0:37d4e78b2076 54 }
ncfronk 0:37d4e78b2076 55
ncfronk 0:37d4e78b2076 56 void tick_out(){
ncfronk 0:37d4e78b2076 57 if(isSampling){
ncfronk 0:37d4e78b2076 58 //read
ncfronk 1:f5770d9636b4 59 samplesLong[sampleIndex] = AnLong.read();
ncfronk 1:f5770d9636b4 60 samplesShort[sampleIndex] = AnLong.read();
ncfronk 1:f5770d9636b4 61 samplesRef[sampleIndex] = AnRef.read();
ncfronk 0:37d4e78b2076 62 sampleIndex++;
ncfronk 1:f5770d9636b4 63 if(sampleIndex+1 > SAMPLE_LENGTH){
ncfronk 0:37d4e78b2076 64 sampleIndex = 0;
ncfronk 0:37d4e78b2076 65 }
ncfronk 0:37d4e78b2076 66 //sampleIndex = (sampleIndex+1)&(SAMPLE_LENGTH-1);
ncfronk 0:37d4e78b2076 67 //write
ncfronk 0:37d4e78b2076 68 dac0 = sinWave[sinIndex];
ncfronk 0:37d4e78b2076 69 sinIndex++;
ncfronk 0:37d4e78b2076 70 if((sinIndex+1) > sinRes){
ncfronk 0:37d4e78b2076 71 sinIndex = 0;
ncfronk 0:37d4e78b2076 72 }
ncfronk 0:37d4e78b2076 73
ncfronk 0:37d4e78b2076 74 newValue = true;
ncfronk 0:37d4e78b2076 75 }
ncfronk 0:37d4e78b2076 76
ncfronk 0:37d4e78b2076 77 }
ncfronk 0:37d4e78b2076 78
ncfronk 0:37d4e78b2076 79 void create_sinWave(){
ncfronk 0:37d4e78b2076 80 int i = 0;
ncfronk 0:37d4e78b2076 81 for(i = 0; i < sinRes; i++){
ncfronk 1:f5770d9636b4 82 sinWave[i] = 0.5 * sin(2.0*PI*i/sinRes) + 0.5;
ncfronk 0:37d4e78b2076 83 }
ncfronk 0:37d4e78b2076 84 }
ncfronk 0:37d4e78b2076 85
ncfronk 0:37d4e78b2076 86 void set_Values(int inFreq){
ncfronk 0:37d4e78b2076 87 freq = inFreq;
ncfronk 0:37d4e78b2076 88 create_sinWave();
ncfronk 0:37d4e78b2076 89 }
ncfronk 0:37d4e78b2076 90
ncfronk 0:37d4e78b2076 91 void print_values(){
ncfronk 0:37d4e78b2076 92 printf("\n\r--------------------------------------SAMPLES-----------------------------------\n\r \r\n");
ncfronk 1:f5770d9636b4 93 print_array(samplesLong, SAMPLE_LENGTH);
ncfronk 0:37d4e78b2076 94 printf("\n\r-------------------------------------------SI-------------------------------------\n\r \r\n");
ncfronk 1:f5770d9636b4 95 print_array(sI, SAMPLE_LENGTH);
ncfronk 0:37d4e78b2076 96 printf("\n\r-----------------------------------------SQ--------------------------------------\n\r \r\n");
ncfronk 1:f5770d9636b4 97 print_array(sQ, SAMPLE_LENGTH);
ncfronk 0:37d4e78b2076 98 }
ncfronk 0:37d4e78b2076 99
ncfronk 0:37d4e78b2076 100 int main(){
ncfronk 0:37d4e78b2076 101 pc.baud(115200);
ncfronk 0:37d4e78b2076 102
ncfronk 1:f5770d9636b4 103 gps_Serial = new Serial(D1,D0); //serial object for use w/ GPS
ncfronk 1:f5770d9636b4 104 Adafruit_GPS myGPS(gps_Serial); //object of Adafruit's GPS class
ncfronk 1:f5770d9636b4 105 char c; //when read via Adafruit_GPS::read(), the class returns single character stored here
ncfronk 1:f5770d9636b4 106 Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info?
ncfronk 1:f5770d9636b4 107 const int refresh_Time = 10000; //refresh time in ms
ncfronk 1:f5770d9636b4 108
ncfronk 1:f5770d9636b4 109 myGPS.begin(9600); //sets baud rate for GPS communication; note this may be changed via Adafruit_GPS::sendCommand(char *)
ncfronk 1:f5770d9636b4 110 //a list of GPS commands is available at http://www.adafruit.com/datasheets/PMTK_A08.pdf
ncfronk 1:f5770d9636b4 111
ncfronk 1:f5770d9636b4 112 myGPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); //these commands are defined in MBed_Adafruit_GPS.h; a link is provided there for command creation
ncfronk 1:f5770d9636b4 113 myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
ncfronk 1:f5770d9636b4 114 myGPS.sendCommand(PGCMD_ANTENNA);
ncfronk 1:f5770d9636b4 115
ncfronk 1:f5770d9636b4 116 tick1.attach(&tick_out, 0.06); // below 0.00005 the board can no longer output and read
ncfronk 0:37d4e78b2076 117 //tick2.attach(&print_values, 20);
ncfronk 0:37d4e78b2076 118
ncfronk 0:37d4e78b2076 119 set_Values(225);
ncfronk 0:37d4e78b2076 120 pc.printf("Set values\n");
ncfronk 0:37d4e78b2076 121
ncfronk 1:f5770d9636b4 122 float filteredLongTemp = 0;
ncfronk 1:f5770d9636b4 123 float filteredShortTemp = 0;
ncfronk 1:f5770d9636b4 124 float filteredRefTemp = 0;
ncfronk 1:f5770d9636b4 125
ncfronk 1:f5770d9636b4 126 refresh_Timer.start(); //starts the clock on the timer
ncfronk 1:f5770d9636b4 127
ncfronk 0:37d4e78b2076 128 while(1){
ncfronk 1:f5770d9636b4 129 //isSampling = false;
ncfronk 1:f5770d9636b4 130 c = myGPS.read(); //queries the GPS
ncfronk 1:f5770d9636b4 131
ncfronk 1:f5770d9636b4 132 //if (c) { pc.printf("%c", c); } //this line will echo the GPS data if not paused
ncfronk 1:f5770d9636b4 133
ncfronk 1:f5770d9636b4 134 //check if we recieved a new message from GPS, if so, attempt to parse it,
ncfronk 1:f5770d9636b4 135 if ( myGPS.newNMEAreceived() ) {
ncfronk 1:f5770d9636b4 136 if ( !myGPS.parse(myGPS.lastNMEA()) ) {
ncfronk 1:f5770d9636b4 137 continue;
ncfronk 1:f5770d9636b4 138 }
ncfronk 1:f5770d9636b4 139 }
ncfronk 1:f5770d9636b4 140
ncfronk 1:f5770d9636b4 141 //check if enough time has passed to warrant printing GPS info to screen
ncfronk 1:f5770d9636b4 142 //note if refresh_Time is too low or pc.baud is too low, GPS data may be lost during printing
ncfronk 1:f5770d9636b4 143 if (refresh_Timer.read_ms() >= refresh_Time) {
ncfronk 1:f5770d9636b4 144 refresh_Timer.reset();
ncfronk 1:f5770d9636b4 145 pc.printf("Time: %d:%d:%d.%u\n", myGPS.hour, myGPS.minute, myGPS.seconds, myGPS.milliseconds);
ncfronk 1:f5770d9636b4 146 pc.printf("Date: %d/%d/20%d\n", myGPS.day, myGPS.month, myGPS.year);
ncfronk 1:f5770d9636b4 147 //pc.printf("Fix: %d\n", (int) myGPS.fix);
ncfronk 1:f5770d9636b4 148 //pc.printf("Quality: %d\n", (int) myGPS.fixquality);
ncfronk 1:f5770d9636b4 149 if (myGPS.fix) {
ncfronk 1:f5770d9636b4 150 pc.printf("Location: %5.2f%c, %5.2f%c\n", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon);
ncfronk 1:f5770d9636b4 151 }
ncfronk 1:f5770d9636b4 152 }
ncfronk 1:f5770d9636b4 153 //isSampling = true;
ncfronk 1:f5770d9636b4 154 if(newValue){ //newValue){
ncfronk 1:f5770d9636b4 155 filteredLongTemp = qam_in(samplesLong, sampleIndex, sI, sQ);
ncfronk 1:f5770d9636b4 156 filtered[avgIndex] = filteredLongTemp;
ncfronk 1:f5770d9636b4 157 filteredRefTemp = qam_in(samplesRef, sampleIndex, sI, sQ);
ncfronk 1:f5770d9636b4 158 filteredRef[avgIndex] = filteredRefTemp;
ncfronk 1:f5770d9636b4 159 pc.printf("%f, ", filteredLongTemp);
ncfronk 0:37d4e78b2076 160 avgIndex++;
ncfronk 1:f5770d9636b4 161 if(avgIndex+1 > OUTAVG_LENGTH){
ncfronk 1:f5770d9636b4 162 isSampling = false;
ncfronk 1:f5770d9636b4 163 //print_values();
ncfronk 1:f5770d9636b4 164 print_array(filtered, OUTAVG_LENGTH);
ncfronk 0:37d4e78b2076 165 avgIndex = 0;
ncfronk 1:f5770d9636b4 166 isSampling = true;
ncfronk 1:f5770d9636b4 167 }
ncfronk 0:37d4e78b2076 168 newValue = false;
ncfronk 0:37d4e78b2076 169 }
ncfronk 0:37d4e78b2076 170 }
ncfronk 0:37d4e78b2076 171 }