for Danillo

Dependencies:   MBed_Adafruit-GPS-Library SDFileSystem mbed

Committer:
ncfronk
Date:
Sat Jan 17 19:12:28 2015 +0000
Revision:
3:0cc40383d016
Parent:
2:abcf77d0e77d
publishing;

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 2:abcf77d0e77d 5 #include "SDFileSystem.h"
ncfronk 1:f5770d9636b4 6
ncfronk 1:f5770d9636b4 7 #include "QAM.h"
ncfronk 2:abcf77d0e77d 8 #include "dataPoint.h"
ncfronk 0:37d4e78b2076 9
ncfronk 3:0cc40383d016 10 #define SAMPLE_LENGTH 1000
ncfronk 2:abcf77d0e77d 11 #define SAMPLE_RATE 10000
ncfronk 0:37d4e78b2076 12 #define SIN_LENGTH 500
ncfronk 2:abcf77d0e77d 13 #define OUTAVG_LENGTH 100
ncfronk 3:0cc40383d016 14 #define CARRIER_FREQ 220
ncfronk 3:0cc40383d016 15 #define TIME_CONST 0.0001
ncfronk 1:f5770d9636b4 16 #define PI 3.14159265
ncfronk 2:abcf77d0e77d 17
ncfronk 2:abcf77d0e77d 18 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS
ncfronk 2:abcf77d0e77d 19 FILE *fp;
ncfronk 2:abcf77d0e77d 20 char buffer[1024];
ncfronk 1:f5770d9636b4 21
ncfronk 1:f5770d9636b4 22 Serial * gps_Serial;
ncfronk 0:37d4e78b2076 23
ncfronk 0:37d4e78b2076 24 Ticker tick1;
ncfronk 0:37d4e78b2076 25 Ticker tick2;
ncfronk 1:f5770d9636b4 26 AnalogIn AnLong(A0);
ncfronk 1:f5770d9636b4 27 AnalogIn AnShort(A1);
ncfronk 2:abcf77d0e77d 28 AnalogIn AnRefLong(A2);
ncfronk 2:abcf77d0e77d 29 AnalogIn AnRefShort(A3);
ncfronk 0:37d4e78b2076 30 AnalogOut dac0(DAC0_OUT);
ncfronk 0:37d4e78b2076 31 DigitalOut led_red(LED_RED);
ncfronk 0:37d4e78b2076 32 Serial pc(USBTX, USBRX);
ncfronk 0:37d4e78b2076 33
ncfronk 2:abcf77d0e77d 34 int sinRes = (int)1/(CARRIER_FREQ*TIME_CONST);
ncfronk 0:37d4e78b2076 35 int freq = 1;
ncfronk 0:37d4e78b2076 36
ncfronk 0:37d4e78b2076 37 float sinWave[SIN_LENGTH] = {};
ncfronk 0:37d4e78b2076 38 int sinIndex = 0;
ncfronk 0:37d4e78b2076 39
ncfronk 3:0cc40383d016 40 float sLQ[SAMPLE_LENGTH] = {};
ncfronk 3:0cc40383d016 41 float sLI[SAMPLE_LENGTH] = {};
ncfronk 3:0cc40383d016 42 float sSQ[SAMPLE_LENGTH] = {};
ncfronk 3:0cc40383d016 43 float sSI[SAMPLE_LENGTH] = {};
ncfronk 3:0cc40383d016 44 float sRefLQ[SAMPLE_LENGTH] = {};
ncfronk 3:0cc40383d016 45 float sRefLI[SAMPLE_LENGTH] = {};
ncfronk 3:0cc40383d016 46 float sRefSQ[SAMPLE_LENGTH] = {};
ncfronk 3:0cc40383d016 47 float sRefSI[SAMPLE_LENGTH] = {};
ncfronk 3:0cc40383d016 48
ncfronk 3:0cc40383d016 49 float Iarray[SAMPLE_LENGTH] = {};
ncfronk 3:0cc40383d016 50 float Qarray[SAMPLE_LENGTH] = {};
ncfronk 0:37d4e78b2076 51 int sampleIndex = 0;
ncfronk 3:0cc40383d016 52 float I = 0;
ncfronk 3:0cc40383d016 53 float Q = 0;
ncfronk 3:0cc40383d016 54 float lon = 0;
ncfronk 3:0cc40383d016 55 float lonRef = 0;
ncfronk 3:0cc40383d016 56 float shor = 0;
ncfronk 3:0cc40383d016 57 float shorRef = 0;
ncfronk 1:f5770d9636b4 58
ncfronk 2:abcf77d0e77d 59 float decimateLong[4] = {};
ncfronk 2:abcf77d0e77d 60 float decimateShort[4] = {};
ncfronk 2:abcf77d0e77d 61 float decimateRef[4] = {};
ncfronk 1:f5770d9636b4 62
ncfronk 1:f5770d9636b4 63 float totalAVG = 0;
ncfronk 0:37d4e78b2076 64
ncfronk 0:37d4e78b2076 65 bool isSampling = true;
ncfronk 0:37d4e78b2076 66 int avgIndex = 0;
ncfronk 0:37d4e78b2076 67
ncfronk 3:0cc40383d016 68 char * timeDatestamp = "";
ncfronk 3:0cc40383d016 69
ncfronk 2:abcf77d0e77d 70
ncfronk 2:abcf77d0e77d 71 void print_array(float *bar,int length, FILE *file){
ncfronk 2:abcf77d0e77d 72 int i =0;
ncfronk 2:abcf77d0e77d 73 for(i = 0; i < length; i++){
ncfronk 2:abcf77d0e77d 74 fprintf(file, "%f, ", bar[i]);
ncfronk 2:abcf77d0e77d 75 }
ncfronk 2:abcf77d0e77d 76 }
ncfronk 2:abcf77d0e77d 77
ncfronk 2:abcf77d0e77d 78 void print_array_serial(float *bar,int length){
ncfronk 0:37d4e78b2076 79 int i =0;
ncfronk 1:f5770d9636b4 80 for(i = 0; i < length; i++){
ncfronk 0:37d4e78b2076 81 pc.printf("%f, ", bar[i]);
ncfronk 0:37d4e78b2076 82 }
ncfronk 2:abcf77d0e77d 83 }
ncfronk 2:abcf77d0e77d 84
ncfronk 2:abcf77d0e77d 85 float avg_array(float *array, int length){
ncfronk 2:abcf77d0e77d 86 int i = 0;
ncfronk 2:abcf77d0e77d 87 float total = 0;
ncfronk 2:abcf77d0e77d 88 for(i = 0; i < length; i++){
ncfronk 2:abcf77d0e77d 89 total += array[i];
ncfronk 2:abcf77d0e77d 90 }
ncfronk 2:abcf77d0e77d 91 return total/length;
ncfronk 0:37d4e78b2076 92 }
ncfronk 0:37d4e78b2076 93
ncfronk 3:0cc40383d016 94 void buildIQ(){
ncfronk 3:0cc40383d016 95 for(int i = 0; i < SAMPLE_LENGTH; i++){
ncfronk 3:0cc40383d016 96 Iarray[i] = cos(2*PI*CARRIER_FREQ*i*TIME_CONST);
ncfronk 3:0cc40383d016 97 Qarray[i] = -sin(2*PI*CARRIER_FREQ*i*TIME_CONST);
ncfronk 3:0cc40383d016 98 }
ncfronk 3:0cc40383d016 99 }
ncfronk 3:0cc40383d016 100
ncfronk 0:37d4e78b2076 101 void tick_out(){
ncfronk 0:37d4e78b2076 102 if(isSampling){
ncfronk 0:37d4e78b2076 103 dac0 = sinWave[sinIndex];
ncfronk 0:37d4e78b2076 104 sinIndex++;
ncfronk 0:37d4e78b2076 105 if((sinIndex+1) > sinRes){
ncfronk 0:37d4e78b2076 106 sinIndex = 0;
ncfronk 0:37d4e78b2076 107 }
ncfronk 0:37d4e78b2076 108
ncfronk 3:0cc40383d016 109 lon = AnLong.read();
ncfronk 3:0cc40383d016 110 lonRef = AnRefLong.read();
ncfronk 3:0cc40383d016 111 shor = AnShort.read();
ncfronk 3:0cc40383d016 112 shorRef = AnRefShort.read();
ncfronk 3:0cc40383d016 113
ncfronk 3:0cc40383d016 114 I = Iarray[sampleIndex];
ncfronk 3:0cc40383d016 115 Q = Qarray[sampleIndex];
ncfronk 3:0cc40383d016 116 sLI[sampleIndex] = lon*I;
ncfronk 3:0cc40383d016 117 sLQ[sampleIndex] = lon*Q;
ncfronk 3:0cc40383d016 118 sSI[sampleIndex] = shor*I;
ncfronk 3:0cc40383d016 119 sSQ[sampleIndex] = shor*Q;
ncfronk 3:0cc40383d016 120 sRefLI[sampleIndex] = lonRef*I;
ncfronk 3:0cc40383d016 121 sRefLQ[sampleIndex] = lonRef*Q;
ncfronk 3:0cc40383d016 122 sRefSI[sampleIndex] = shorRef*I;
ncfronk 3:0cc40383d016 123 sRefSQ[sampleIndex] = shorRef*Q;
ncfronk 3:0cc40383d016 124
ncfronk 3:0cc40383d016 125 sampleIndex++;
ncfronk 3:0cc40383d016 126 if(sampleIndex+1 > SAMPLE_LENGTH){
ncfronk 3:0cc40383d016 127 sampleIndex--;
ncfronk 3:0cc40383d016 128 }
ncfronk 3:0cc40383d016 129
ncfronk 3:0cc40383d016 130
ncfronk 0:37d4e78b2076 131 }
ncfronk 0:37d4e78b2076 132
ncfronk 0:37d4e78b2076 133 }
ncfronk 0:37d4e78b2076 134
ncfronk 0:37d4e78b2076 135 void create_sinWave(){
ncfronk 0:37d4e78b2076 136 int i = 0;
ncfronk 0:37d4e78b2076 137 for(i = 0; i < sinRes; i++){
ncfronk 2:abcf77d0e77d 138 sinWave[i] = 0.25 * sin(2.0*PI*i/sinRes) + 0.75;
ncfronk 0:37d4e78b2076 139 }
ncfronk 0:37d4e78b2076 140 }
ncfronk 0:37d4e78b2076 141
ncfronk 0:37d4e78b2076 142 void set_Values(int inFreq){
ncfronk 0:37d4e78b2076 143 freq = inFreq;
ncfronk 0:37d4e78b2076 144 create_sinWave();
ncfronk 0:37d4e78b2076 145 }
ncfronk 0:37d4e78b2076 146
ncfronk 0:37d4e78b2076 147 void print_values(){
ncfronk 3:0cc40383d016 148 fp = fopen("/sd/data.txt", "r");
ncfronk 2:abcf77d0e77d 149 if (fp != NULL) {
ncfronk 2:abcf77d0e77d 150 fclose(fp);
ncfronk 2:abcf77d0e77d 151 remove("/sd/hello.txt");
ncfronk 2:abcf77d0e77d 152 pc.printf("Remove an existing file with the same name \n");
ncfronk 2:abcf77d0e77d 153 }
ncfronk 2:abcf77d0e77d 154
ncfronk 2:abcf77d0e77d 155 printf("\nWriting data to the sd card \n");
ncfronk 2:abcf77d0e77d 156 fp = fopen("/sd/hello.txt", "w");
ncfronk 2:abcf77d0e77d 157 if (fp == NULL) {
ncfronk 2:abcf77d0e77d 158 pc.printf("Unable to write the file \n");
ncfronk 2:abcf77d0e77d 159 }
ncfronk 2:abcf77d0e77d 160 fclose(fp);
ncfronk 2:abcf77d0e77d 161 }
ncfronk 2:abcf77d0e77d 162
ncfronk 3:0cc40383d016 163 void save_point(char * c){
ncfronk 3:0cc40383d016 164 fp = fopen("/sd/data.txt", "r");
ncfronk 3:0cc40383d016 165 if (fp != NULL) {
ncfronk 3:0cc40383d016 166 fclose(fp);
ncfronk 3:0cc40383d016 167 remove("/sd/data.txt");
ncfronk 3:0cc40383d016 168 pc.printf("Remove an existing file with the same name \n");
ncfronk 3:0cc40383d016 169 }
ncfronk 3:0cc40383d016 170
ncfronk 3:0cc40383d016 171 printf("\nWriting data to the sd card \n");
ncfronk 3:0cc40383d016 172 fp = fopen("/sd/data.txt", "w");
ncfronk 3:0cc40383d016 173 if (fp == NULL) {
ncfronk 3:0cc40383d016 174 pc.printf("Unable to write the file \n");
ncfronk 3:0cc40383d016 175 } else {
ncfronk 3:0cc40383d016 176 fprintf(fp, c);
ncfronk 3:0cc40383d016 177 fprintf(fp, "\n");
ncfronk 3:0cc40383d016 178 fclose(fp);
ncfronk 3:0cc40383d016 179 }
ncfronk 0:37d4e78b2076 180 }
ncfronk 0:37d4e78b2076 181
ncfronk 0:37d4e78b2076 182 int main(){
ncfronk 0:37d4e78b2076 183 pc.baud(115200);
ncfronk 0:37d4e78b2076 184
ncfronk 1:f5770d9636b4 185 gps_Serial = new Serial(D1,D0); //serial object for use w/ GPS
ncfronk 1:f5770d9636b4 186 Adafruit_GPS myGPS(gps_Serial); //object of Adafruit's GPS class
ncfronk 1:f5770d9636b4 187 char c; //when read via Adafruit_GPS::read(), the class returns single character stored here
ncfronk 1:f5770d9636b4 188 Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info?
ncfronk 2:abcf77d0e77d 189 Timer sampling_Toggle;
ncfronk 2:abcf77d0e77d 190 Timer t;
ncfronk 3:0cc40383d016 191 const int refresh_Time = 1000; //refresh time in ms
ncfronk 3:0cc40383d016 192 const int sample_Time = 250;
ncfronk 3:0cc40383d016 193 const int filter_Time = 2000;
ncfronk 1:f5770d9636b4 194
ncfronk 1:f5770d9636b4 195 myGPS.begin(9600); //sets baud rate for GPS communication; note this may be changed via Adafruit_GPS::sendCommand(char *)
ncfronk 1:f5770d9636b4 196 //a list of GPS commands is available at http://www.adafruit.com/datasheets/PMTK_A08.pdf
ncfronk 1:f5770d9636b4 197
ncfronk 1:f5770d9636b4 198 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 199 myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
ncfronk 1:f5770d9636b4 200 myGPS.sendCommand(PGCMD_ANTENNA);
ncfronk 1:f5770d9636b4 201
ncfronk 3:0cc40383d016 202 buildIQ();
ncfronk 3:0cc40383d016 203
ncfronk 2:abcf77d0e77d 204 tick1.attach(&tick_out, 0.0001); // below 0.00005 the board can no longer output and read
ncfronk 0:37d4e78b2076 205 //tick2.attach(&print_values, 20);
ncfronk 0:37d4e78b2076 206
ncfronk 2:abcf77d0e77d 207 set_Values(220);
ncfronk 2:abcf77d0e77d 208 pc.printf("sinres = %i\n", sinRes);
ncfronk 0:37d4e78b2076 209
ncfronk 1:f5770d9636b4 210 float filteredLongTemp = 0;
ncfronk 1:f5770d9636b4 211 float filteredShortTemp = 0;
ncfronk 2:abcf77d0e77d 212 float filteredRefLongTemp = 0;
ncfronk 2:abcf77d0e77d 213 float filteredRefShortTemp = 0;
ncfronk 1:f5770d9636b4 214
ncfronk 2:abcf77d0e77d 215 refresh_Timer.start();
ncfronk 2:abcf77d0e77d 216 sampling_Toggle.start();
ncfronk 1:f5770d9636b4 217
ncfronk 2:abcf77d0e77d 218 while(1){
ncfronk 3:0cc40383d016 219 if(isSampling){
ncfronk 3:0cc40383d016 220 if (sampling_Toggle.read_ms() >= sample_Time) {
ncfronk 3:0cc40383d016 221 isSampling = false;
ncfronk 3:0cc40383d016 222 sampling_Toggle.reset();
ncfronk 3:0cc40383d016 223
ncfronk 3:0cc40383d016 224 filteredLongTemp = qam_in(sLI, sLQ, &pc);
ncfronk 3:0cc40383d016 225 filteredRefLongTemp = qam_in(sRefLI, sRefLQ, &pc);
ncfronk 3:0cc40383d016 226 filteredShortTemp = qam_in(sSI, sSQ, &pc);
ncfronk 3:0cc40383d016 227 filteredRefShortTemp = qam_in(sRefSI, sRefSQ, &pc);
ncfronk 3:0cc40383d016 228 pc.printf("Long = %f, Short = %f ",filteredLongTemp, filteredShortTemp);
ncfronk 3:0cc40383d016 229
ncfronk 3:0cc40383d016 230 fp = fopen("/sd/data.txt", "w");
ncfronk 3:0cc40383d016 231 if (fp == NULL) {
ncfronk 3:0cc40383d016 232 pc.printf("Unable to write the file \n");
ncfronk 3:0cc40383d016 233 } else {
ncfronk 3:0cc40383d016 234 fprintf(fp,"Long = %f, Short = %f ",filteredLongTemp/filteredRefShortTemp, filteredShortTemp/filteredRefShortTemp);
ncfronk 3:0cc40383d016 235 fclose(fp);
ncfronk 3:0cc40383d016 236 }
ncfronk 2:abcf77d0e77d 237 }
ncfronk 3:0cc40383d016 238 }else{
ncfronk 3:0cc40383d016 239 if (sampling_Toggle.read_ms() >= filter_Time) {
ncfronk 3:0cc40383d016 240 isSampling = true;
ncfronk 3:0cc40383d016 241 sampling_Toggle.reset();
ncfronk 3:0cc40383d016 242
ncfronk 3:0cc40383d016 243 sampleIndex = 0;
ncfronk 3:0cc40383d016 244 }
ncfronk 3:0cc40383d016 245 }
ncfronk 2:abcf77d0e77d 246
ncfronk 2:abcf77d0e77d 247 if(!isSampling){
ncfronk 2:abcf77d0e77d 248 c = myGPS.read();
ncfronk 2:abcf77d0e77d 249 if ( myGPS.newNMEAreceived() ) {
ncfronk 2:abcf77d0e77d 250 if ( !myGPS.parse(myGPS.lastNMEA()) ) {
ncfronk 2:abcf77d0e77d 251 continue;
ncfronk 2:abcf77d0e77d 252 }
ncfronk 1:f5770d9636b4 253 }
ncfronk 2:abcf77d0e77d 254
ncfronk 2:abcf77d0e77d 255 //check if enough time has passed to warrant printing GPS info to screen
ncfronk 2:abcf77d0e77d 256 //note if refresh_Time is too low or pc.baud is too low, GPS data may be lost during printing
ncfronk 2:abcf77d0e77d 257 if (refresh_Timer.read_ms() >= refresh_Time) {
ncfronk 2:abcf77d0e77d 258 refresh_Timer.reset();
ncfronk 3:0cc40383d016 259 pc.printf("Time: %d:%d:%d.%u \n", myGPS.hour, myGPS.minute, myGPS.seconds, myGPS.milliseconds);
ncfronk 3:0cc40383d016 260 fp = fopen("/sd/data.txt", "w");
ncfronk 3:0cc40383d016 261 if (fp == NULL) {
ncfronk 3:0cc40383d016 262 pc.printf("Unable to write the file \n");
ncfronk 3:0cc40383d016 263 } else {
ncfronk 3:0cc40383d016 264 fprintf(fp,"Time: %d:%d:%d.%u \n", myGPS.hour, myGPS.minute, myGPS.seconds, myGPS.milliseconds);
ncfronk 3:0cc40383d016 265 fclose(fp);
ncfronk 3:0cc40383d016 266 }
ncfronk 2:abcf77d0e77d 267 if (myGPS.fix) {
ncfronk 3:0cc40383d016 268 pc.printf("Location: %5.2f%c, %5.2f%c\n", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon);
ncfronk 3:0cc40383d016 269 fp = fopen("/sd/data.txt", "w");
ncfronk 3:0cc40383d016 270 if (fp == NULL) {
ncfronk 3:0cc40383d016 271 pc.printf("Unable to write the file \n");
ncfronk 3:0cc40383d016 272 } else {
ncfronk 3:0cc40383d016 273 fprintf(fp,"Location: %5.2f%c, %5.2f%c\n", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon);
ncfronk 3:0cc40383d016 274 fclose(fp);
ncfronk 3:0cc40383d016 275 }
ncfronk 2:abcf77d0e77d 276 }
ncfronk 2:abcf77d0e77d 277 }
ncfronk 3:0cc40383d016 278 }
ncfronk 0:37d4e78b2076 279 }
ncfronk 0:37d4e78b2076 280 }