00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef __WPROGRAM_H__
00029 #include <WaspClasses.h>
00030 #endif
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 WaspGPS::WaspGPS()
00041 {
00042 _baudRate = 4800;
00043
00044 _uart=1;
00045
00046
00047 reboot=COLD;
00048 flag = ACK;
00049 commMode = GPS_NMEA;
00050 pwrMode = GPS_ON;
00051 wakeMode = COLD;
00052 clkOffset= (char*) "96000";
00053 timeOfWeek= (char*) "497260";
00054 weekNo= (char*) "921";
00055 channel= (char*) "12";
00056 resetCfg= (char*) "1";
00057 coordinateLat = (char*) "4140.8217";
00058 coordinateLon = (char*) "00053.1736";
00059 coordinateAl = (char*) "198";
00060 checksum=0;
00061 }
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 void WaspGPS::extractDate(void)
00085 {
00086
00087 uint16_t currentSentences = commMode;
00088 long previous=0;
00089
00090
00091
00092 serialFlush(1);
00093 previous=millis();
00094 while(!setCommMode(GPS_NMEA_RMC) && (millis()-previous)<3000);
00095 Utils.strExplode(Utils.inBuffer, ',');
00096 if( !strcmp(Utils.arguments[0],"$GPRMC") )
00097 {
00098 for(int i=0;i<MAX_ARGS;i++) dateGPS[i]=Utils.arguments[9][i];
00099 }
00100 else
00101 {
00102 flag |=GPS_INVALID;
00103 }
00104
00105
00106 previous=millis();
00107 if (currentSentences == GPS_BINARY_OFF ) while( !setCommMode(GPS_BINARY) && (millis()-previous)<3000);
00108 delay(100);
00109 previous=millis();
00110 while(!setCommMode(currentSentences) && (millis()-previous)<3000);
00111 }
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124 void WaspGPS::extractTime(void)
00125 {
00126
00127 uint16_t currentSentences = commMode;
00128 long previous=0;
00129
00130
00131 serialFlush(1);
00132 previous=millis();
00133 while(!setCommMode(GPS_NMEA_GGA) && (millis()-previous)<3000);
00134 Utils.strExplode(Utils.inBuffer, ',');
00135 if( !strcmp(Utils.arguments[0],"$GPGGA") )
00136 {
00137 for(int i=0;i<MAX_ARGS;i++) timeGPS[i]=Utils.arguments[1][i];
00138 }
00139 else
00140 {
00141 flag |= GPS_INVALID;
00142 }
00143
00144
00145 previous=millis();
00146 if (currentSentences == GPS_BINARY_OFF ) while( !setCommMode(GPS_BINARY) && (millis()-previous)<3000);
00147 delay(100);
00148 previous=millis();
00149 while(!setCommMode(currentSentences) && (millis()-previous)<3000);
00150 }
00151
00152
00153
00154
00155
00156
00157
00158 long WaspGPS::parse_decimal(char *str)
00159 {
00160 bool isneg = *str == '-';
00161 if (isneg) *str++;
00162 unsigned long ret = 100UL * gpsatol(str);
00163 while (gpsisdigit(*str)) ++str;
00164 if (*str == '.')
00165 {
00166 if (gpsisdigit(str[1]))
00167 {
00168 ret += 10 * (str[1] - '0');
00169 if (gpsisdigit(str[2]))
00170 ret += str[2] - '0';
00171 }
00172 }
00173 return isneg ? -ret : ret;
00174 }
00175
00176
00177
00178
00179
00180
00181 unsigned long WaspGPS::parse_degrees(char *str)
00182 {
00183 unsigned long left = gpsatol(str);
00184 unsigned long tenk_minutes = (left % 100UL) * 10000UL;
00185 while (gpsisdigit(*str)) ++str;
00186 if (*str == '.')
00187 {
00188 unsigned long mult = 1000;
00189 while (gpsisdigit(*++str))
00190 {
00191 tenk_minutes += mult * (*str - '0');
00192 mult /= 10;
00193 }
00194 }
00195 return (left / 100) * 100000 + tenk_minutes / 6;
00196 }
00197
00198
00199
00200
00201
00202
00203 long WaspGPS::gpsatol(char *str)
00204 {
00205 long ret = 0;
00206 while (gpsisdigit(*str))
00207 ret = 10 * ret + *str++ - '0';
00208 return ret;
00209 }
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222 void WaspGPS::ON()
00223 {
00224 setMode(GPS_ON);
00225 begin();
00226 init();
00227 }
00228
00229
00230
00231
00232
00233
00234
00235 void WaspGPS::OFF()
00236 {
00237 close();
00238 setMode(GPS_OFF);
00239 }
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252 void WaspGPS::init()
00253 {
00254 init(coordinateLat, coordinateLon, coordinateAl, clkOffset, timeOfWeek, weekNo, channel, resetCfg);
00255 }
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267 void WaspGPS::init(const char* _coordinateLat, const char* _coordinateLon, const char* _coordinateAl, const char* _clkOffset, const char* _timeOfWeek, const char* _weekNo, const char* _channel, const char* _resetCfg)
00268 {
00269 long previous=0;
00270
00271 if(reboot)
00272 {
00273 begin();
00274 delay(3000);
00275 }
00276 sprintf(Utils.inBuffer,"$PSRF104,%s,%s,%s,%s,%s,%s,%s,%s*00",_coordinateLat,_coordinateLon,_coordinateAl,_clkOffset,_timeOfWeek,_weekNo,_channel,_resetCfg);
00277
00278 setChecksum();
00279 serialFlush(1);
00280 printString(Utils.inBuffer,1);
00281 printByte('\r',1);
00282 printByte('\n',1);
00283
00284
00285
00286 previous=millis();
00287 while( !setCommMode(GPS_BINARY) && (millis()-previous)<3000);
00288 previous=millis();
00289 while(!setCommMode(GPS_BINARY_OFF) && (millis()-previous)<3000);
00290 serialFlush(1);
00291 reboot=HOT;
00292 }
00293
00294
00295
00296
00297
00298
00299 uint8_t WaspGPS::setCommMode(uint16_t mode)
00300 {
00301 uint8_t tempBuffer[32] =
00302 {0xA0,0xA2,0x00,0x18,0x81,0x02,0x01,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,
00303 0x00,0x01,0x00,0x01,0x00,0x01,0x12,0xC0,0x01,0x60,0xB0,0xB3};
00304 uint8_t tempBuffer2[16] ={0xA0,0xA2,0x00,0x08,0xA6,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0xAA,0xB0,0xB3};
00305 uint8_t byteIN[10];
00306 commMode = mode;
00307 uint8_t valid=0;
00308
00309 switch(commMode)
00310 {
00311 case GPS_BINARY: sprintf(Utils.inBuffer,"$PSRF100,0,%u,8,1,0*0F",_baudRate);
00312 serialFlush(_uart);
00313 printString(Utils.inBuffer,_uart);
00314 printByte('\r',_uart);
00315 printByte('\n',_uart);
00316 delay(10);
00317 serialFlush(_uart);
00318 delay(10);
00319 if(serialAvailable(_uart)){
00320 byteIN[0]=serialRead(_uart);
00321 if( (byteIN[0]=='$') || ( (byteIN[0]>'0') && (byteIN[0]<'Z') ) ) valid=0;
00322 else valid=1;
00323 }
00324 break;
00325 case GPS_BINARY_OFF: serialFlush(_uart);
00326 for(int a=0;a<16;a++)
00327 {
00328 printByte(tempBuffer2[a],_uart);
00329
00330 }
00331 delay(100);
00332 if(serialAvailable(_uart))
00333 {
00334 for(int a=0;a<10;a++)
00335 {
00336 byteIN[a]=serialRead(_uart);
00337 }
00338 }
00339 if(byteIN[5]==0xA6) valid=1;
00340 else valid=0;
00341 break;
00342 case GPS_NMEA: tempBuffer[8]=0x01;
00343 tempBuffer[10]=0x01;
00344 tempBuffer[12]=0x01;
00345 tempBuffer[14]=0x01;
00346 tempBuffer[16]=0x01;
00347 tempBuffer[29]=0x65;
00348 for(int b=0;b<32;b++)
00349 {
00350 printByte(tempBuffer[b],1);
00351 }
00352 delay(10);
00353 getRaw(100);
00354 Utils.strExplode(Utils.inBuffer, ',');
00355 if( strcmp(Utils.arguments[0],"$GPGGA") )
00356 {
00357 valid=0;
00358 }
00359 else valid=1;
00360 break;
00361 case GPS_NMEA_GGA: for(int c=0;c<32;c++)
00362 {
00363 printByte(tempBuffer[c],1);
00364 }
00365 delay(10);
00366 getRaw(100);
00367 Utils.strExplode(Utils.inBuffer, ',');
00368 if( strcmp(Utils.arguments[0],"$GPGGA") )
00369 {
00370 valid=0;
00371 }
00372 else valid=1;
00373 break;
00374 case GPS_NMEA_GLL: tempBuffer[6]=0x00;
00375 tempBuffer[8]=0x01;
00376 for(int d=0;d<32;d++)
00377 {
00378 printByte(tempBuffer[d],1);
00379 }
00380 getRaw(100);
00381 Utils.strExplode(Utils.inBuffer, ',');
00382 if( strcmp(Utils.arguments[0],"$GPGLL") )
00383 {
00384 valid=0;
00385 }
00386 else valid=1;
00387 break;
00388 case GPS_NMEA_GSA: tempBuffer[6]=0x00;
00389 tempBuffer[10]=0x01;
00390 for(int e=0;e<32;e++)
00391 {
00392 printByte(tempBuffer[e],1);
00393 }
00394 getRaw(100);
00395 Utils.strExplode(Utils.inBuffer, ',');
00396 if( strcmp(Utils.arguments[0],"$GPGSA") )
00397 {
00398 valid=0;
00399 }
00400 else valid=1;
00401 break;
00402 case GPS_NMEA_GSV: tempBuffer[6]=0x00;
00403 tempBuffer[12]=0x01;
00404 for(int f=0;f<32;f++)
00405 {
00406 printByte(tempBuffer[f],1);
00407 }
00408 getRaw(100);
00409 Utils.strExplode(Utils.inBuffer, ',');
00410 if( strcmp(Utils.arguments[0],"$GPGSV") )
00411 {
00412 valid=0;
00413 }
00414 else valid=1;
00415 break;
00416 case GPS_NMEA_RMC: tempBuffer[6]=0x00;
00417 tempBuffer[14]=0x01;
00418 for(int g=0;g<32;g++)
00419 {
00420 printByte(tempBuffer[g],1);
00421 }
00422 getRaw(100);
00423 Utils.strExplode(Utils.inBuffer, ',');
00424 if( strcmp(Utils.arguments[0],"$GPRMC") )
00425 {
00426 valid=0;
00427 }
00428 else valid=1;
00429 break;
00430 case GPS_NMEA_VTG: tempBuffer[6]=0x00;
00431 tempBuffer[16]=0x01;
00432 for(int h=0;h<32;h++)
00433 {
00434 printByte(tempBuffer[h],1);
00435 }
00436 getRaw(100);
00437 Utils.strExplode(Utils.inBuffer, ',');
00438 if( strcmp(Utils.arguments[0],"$GPVTG") )
00439 {
00440 valid=0;
00441 }
00442 else valid=1;
00443 break;
00444 }
00445 return valid;
00446 }
00447
00448
00449
00450
00451
00452 uint8_t WaspGPS::getCommMode(void)
00453 {
00454 return commMode;
00455 }
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469 void WaspGPS::setMode(uint8_t mode)
00470 {
00471 pwrMode = mode;
00472 pinMode(GPS_PW,OUTPUT);
00473
00474
00475 switch (pwrMode)
00476 {
00477 case GPS_ON:
00478 digitalWrite(GPS_PW,HIGH);
00479 break;
00480
00481 case GPS_OFF:
00482 digitalWrite(GPS_PW,LOW);
00483 break;
00484 }
00485 }
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495 uint8_t WaspGPS::getMode(void)
00496 {
00497 return pwrMode;
00498 }
00499
00500
00501
00502
00503
00504
00505
00506
00507 bool WaspGPS::check()
00508 {
00509 uint16_t currentSentences = commMode;
00510 bool connection=0;
00511 long previous=0;
00512
00513 serialFlush(1);
00514 while(!setCommMode(GPS_NMEA_GGA) && (millis()-previous)<3000);
00515
00516
00517 Utils.strExplode(Utils.inBuffer, ',');
00518
00519
00520 if( !strcmp(Utils.arguments[0],"$GPGGA") )
00521 {
00522 connection = (Utils.arguments[6][0] - '0');
00523 }
00524 else connection=0;
00525
00526
00527 previous=millis();
00528 if (currentSentences == GPS_BINARY_OFF ) while( !setCommMode(GPS_BINARY) && (millis()-previous)<3000);
00529 delay(100);
00530 previous=millis();
00531 while(!setCommMode(currentSentences) && (millis()-previous)<3000);
00532
00533 return connection;
00534 }
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545 char* WaspGPS::getTime(void)
00546 {
00547 extractTime();
00548 return timeGPS;
00549 }
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560 char* WaspGPS::getDate(void)
00561 {
00562 extractDate();
00563 return dateGPS;
00564 }
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576 char* WaspGPS::getLatitude(void)
00577 {
00578 flag &= ~(GPS_INVALID);
00579
00580
00581 uint16_t currentSentences = commMode;
00582 long previous=0;
00583
00584
00585 while(!setCommMode(GPS_NMEA_GGA) && (millis()-previous)<3000);
00586
00587
00588 Utils.strExplode(Utils.inBuffer, ',');
00589
00590 if( !strcmp(Utils.arguments[0],"$GPGGA") )
00591 {
00592 for(int i=0;i<MAX_ARGS;i++) latitude[i]=Utils.arguments[2][i];
00593 }
00594 else
00595 {
00596 flag |=GPS_INVALID;
00597 }
00598
00599
00600
00601 fixValid = (Utils.arguments[6][0] - '0');
00602 if (!fixValid) flag |= GPS_INVALID;
00603
00604
00605 previous=millis();
00606 if (currentSentences == GPS_BINARY_OFF ) while( !setCommMode(GPS_BINARY) && (millis()-previous)<3000);
00607 delay(100);
00608 previous=millis();
00609 while(!setCommMode(currentSentences) && (millis()-previous)<3000);
00610
00611 return latitude;
00612 }
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623 char* WaspGPS::getLongitude(void)
00624 {
00625 flag &= ~(GPS_INVALID);
00626
00627
00628 uint16_t currentSentences = commMode;
00629 long previous=0;
00630
00631
00632 while(!setCommMode(GPS_NMEA_GGA) && (millis()-previous)<3000);
00633
00634
00635 Utils.strExplode(Utils.inBuffer, ',');
00636
00637 if( !strcmp(Utils.arguments[0],"$GPGGA") )
00638 {
00639 for(int i=0;i<MAX_ARGS;i++) longitude[i]=Utils.arguments[4][i];
00640 }
00641 else
00642 {
00643 flag |=GPS_INVALID;
00644 }
00645
00646
00647 fixValid = (Utils.arguments[6][0] - '0');
00648 if (!fixValid) flag |= GPS_INVALID;
00649
00650
00651 previous=millis();
00652 if (currentSentences == GPS_BINARY_OFF ) while( !setCommMode(GPS_BINARY) && (millis()-previous)<3000);
00653 delay(100);
00654 previous=millis();
00655 while(!setCommMode(currentSentences) && (millis()-previous)<3000);
00656
00657 return longitude;
00658 }
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675 char* WaspGPS::getSpeed(void)
00676 {
00677
00678 uint16_t currentSentences = commMode;
00679 long previous=0;
00680
00681
00682 while(!setCommMode(GPS_NMEA_VTG) && (millis()-previous)<3000);
00683
00684
00685 Utils.strExplode(Utils.inBuffer, ',');
00686
00687 if( !strcmp(Utils.arguments[0],"$GPVTG") )
00688 {
00689 for(int i=0;i<MAX_ARGS;i++) speed[i]=Utils.arguments[7][i];
00690 }
00691 else
00692 {
00693 flag |=GPS_INVALID;
00694 }
00695
00696
00697 previous=millis();
00698 if (currentSentences == GPS_BINARY_OFF ) while( !setCommMode(GPS_BINARY) && (millis()-previous)<3000);
00699 delay(100);
00700 previous=millis();
00701 while(!setCommMode(currentSentences) && (millis()-previous)<3000);
00702
00703 return speed;
00704 }
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715 char* WaspGPS::getAltitude(void)
00716 {
00717 flag &= ~(GPS_INVALID);
00718
00719
00720 uint16_t currentSentences = commMode;
00721 long previous=0;
00722
00723
00724 while(!setCommMode(GPS_NMEA_GGA) && (millis()-previous)<3000);
00725
00726
00727 Utils.strExplode(Utils.inBuffer, ',');
00728
00729 if( !strcmp(Utils.arguments[0],"$GPGGA") )
00730 {
00731 for(int i=0;i<MAX_ARGS;i++) altitude[i]=Utils.arguments[9][i];
00732 }
00733 else
00734 {
00735 flag |=GPS_INVALID;
00736 }
00737
00738
00739 fixValid = (Utils.arguments[6][0] - '0');
00740 if (!fixValid) flag |= GPS_INVALID;
00741
00742
00743 previous=millis();
00744 if (currentSentences == GPS_BINARY_OFF ) while( !setCommMode(GPS_BINARY) && (millis()-previous)<3000);
00745 delay(100);
00746 previous=millis();
00747 while(!setCommMode(currentSentences) && (millis()-previous)<3000);
00748
00749 return altitude;
00750 }
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766 char* WaspGPS::getCourse(void)
00767 {
00768
00769 uint16_t currentSentences = commMode;
00770 long previous=0;
00771
00772
00773 while(!setCommMode(GPS_NMEA_VTG) && (millis()-previous)<3000);
00774
00775
00776 Utils.strExplode(Utils.inBuffer, ',');
00777
00778 if( !strcmp(Utils.arguments[0],"$GPVTG") )
00779 {
00780 for(int i=0;i<MAX_ARGS;i++) course[i]=Utils.arguments[1][i];
00781 }
00782 else
00783 {
00784 flag |=GPS_INVALID;
00785 }
00786
00787
00788 previous=millis();
00789 if (currentSentences == GPS_BINARY_OFF ) while( !setCommMode(GPS_BINARY) && (millis()-previous)<3000);
00790 delay(100);
00791 previous=millis();
00792 while(!setCommMode(currentSentences) && (millis()-previous)<3000);
00793
00794 return course;
00795 }
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811 char* WaspGPS::getRaw(int byteAmount)
00812 {
00813 flag &= ~(GPS_TIMEOUT);
00814
00815 uint8_t byteGPS = 0;
00816 int i = 0;
00817 uint32_t timeout = 1000;
00818
00819 if (byteAmount == 0) byteAmount = GPS_BUFFER_SIZE;
00820
00821 serialFlush(1);
00822 Utils.clearBuffer();
00823
00824 while(!serialAvailable(_uart) && timeout > 0) { delay(1); timeout--; };
00825
00826 if (timeout <= 0)
00827 {
00828 flag |= GPS_TIMEOUT;
00829 return GPS_TIMEOUT_em;
00830 }
00831
00832 byteGPS = serialRead(_uart);
00833
00834 while(byteGPS != '$')
00835 {
00836 if(serialAvailable(_uart) > 0)
00837 byteGPS = serialRead(_uart);
00838 }
00839 Utils.inBuffer[i]=byteGPS;
00840 i++;
00841 while(i <= 1 || (byteGPS != '*' && byteGPS != '$' && i < byteAmount)){
00842
00843 if(serialAvailable(_uart) > 0)
00844 {
00845 byteGPS = serialRead(_uart);
00846 if (byteGPS != '*' && byteGPS != '$' && i < byteAmount) Utils.inBuffer[i]=byteGPS;
00847
00848 i++;
00849 }
00850 }
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879 if (byteGPS == '\n' || i == byteAmount) Utils.inBuffer[i-1] = '\0';
00880 else Utils.inBuffer[i] = '\0';
00881
00882 return Utils.inBuffer;
00883 }
00884
00885
00886
00887
00888
00889
00890
00891
00892 void WaspGPS::getChecksum(uint8_t* buffer)
00893 {
00894 int a=4;
00895 int check=0;
00896 uint8_t aux=0, aux2=0;
00897 while( (buffer[aux]!=0xB0) || (buffer[aux+1]!=0xB3) )
00898 {
00899 aux++;
00900 }
00901 buffer[aux-1]=0x00;
00902 buffer[aux-2]=0x00;
00903 aux=0;
00904 while( (buffer[a]!=0xB0) || (buffer[a+1]!=0xB3) )
00905 {
00906 check+=buffer[a];
00907 check &= 0x7FFF;
00908 a++;
00909 }
00910 if(check>255)
00911 {
00912 aux=check/256;
00913 aux2=check-(aux*256);
00914 checkSUM[0]=aux;
00915 checkSUM[1]=aux2;
00916 }
00917 else
00918 {
00919 checkSUM[0]=0x00;
00920 checkSUM[1]=check;
00921 }
00922 }
00923
00924
00925
00926
00927
00928
00929
00930 uint8_t WaspGPS::saveEphems()
00931 {
00932 return saveEphems(FILE_EPHEMERIS);
00933 }
00934
00935
00936
00937
00938
00939
00940
00941
00942 uint8_t WaspGPS::saveEphems(const char* filename)
00943 {
00944 uint8_t tempBuffer[11] ={0xA0,0xA2,0x00,0x03,0x93,0x00,0x00,0x00,0x00,0xB0,0xB3};
00945 uint8_t* ByteIN = (uint8_t*) calloc(110,sizeof(uint8_t));
00946 uint8_t* tempData = (uint8_t*) calloc(92,sizeof(uint8_t));
00947 uint8_t endFile[7] ={0xAA,0xBB,0xCC,0xCC,0xBB,0xAA,0xAA};
00948 uint8_t counter3=0;
00949 uint8_t end=0;
00950 uint16_t interval=1000;
00951 long previous=millis();
00952 int8_t error=2;
00953 uint8_t est=1;
00954
00955 flag = 0; SD.flag = 0;
00956 uint8_t aux=0;
00957
00958 if (SD.isFile(filename)) SD.del(filename);
00959 SD.create(filename);
00960
00961 previous=millis();
00962 while(!setCommMode(GPS_BINARY_OFF) && (millis()-previous)<3000);
00963 delay(100);
00964 while(serialAvailable(_uart)>0)
00965 {
00966 serialRead(_uart);
00967 }
00968
00969 for(int a=1;a<33;a++)
00970 {
00971 tempBuffer[5]=a;
00972 getChecksum(tempBuffer);
00973 tempBuffer[7]=checkSUM[0];
00974 tempBuffer[8]=checkSUM[1];
00975 for(int b=0;b<11;b++)
00976 {
00977 printByte(tempBuffer[b],_uart);
00978 }
00979
00980 while(end==0)
00981 {
00982 if(serialAvailable(_uart)>0)
00983 {
00984 ByteIN[counter3]=serialRead(_uart);
00985 counter3++;
00986 previous=millis();
00987 }
00988 if( (millis()-previous) > interval )
00989 {
00990 end=1;
00991 serialFlush(_uart);
00992 }
00993 }
00994 if( counter3>100 )
00995 {
00996 counter3=6;
00997 if( (ByteIN[0]!=0xA0) || (ByteIN[1]!=0xA2) ) break;
00998 while( counter3<96 )
00999 {
01000 tempData[counter3-6]=ByteIN[counter3];
01001 counter3++;
01002 }
01003 tempData[counter3]=0xAA;
01004 tempData[counter3+1]=0xAA;
01005 if(SD.writeSD(filename,tempData,aux*90)) error=1;
01006 else error=0;
01007 aux++;
01008 }
01009 counter3=0;
01010 end=0;
01011 previous=millis();
01012 }
01013 if (error==1) if(SD.writeSD(filename,endFile,aux*90)) error=1;
01014 free(ByteIN);
01015 free(tempData);
01016 ByteIN=NULL;
01017 tempData=NULL;
01018 return error;
01019 }
01020
01021
01022
01023
01024
01025
01026
01027 uint8_t WaspGPS::loadEphems()
01028 {
01029 return loadEphems(FILE_EPHEMERIS);
01030 }
01031
01032
01033
01034
01035
01036
01037
01038
01039 uint8_t WaspGPS::loadEphems(const char* filename)
01040 {
01041 uint8_t* tempData = (uint8_t*) calloc(99,sizeof(uint8_t));
01042 uint8_t* answer = (uint8_t*) calloc(10,sizeof(uint8_t));
01043 uint8_t endFile=0;
01044 uint16_t offset=0;
01045 uint8_t counter3=0;
01046 uint8_t end=0;
01047 uint16_t interval=1000;
01048 long previous=millis();
01049 int8_t error=2;
01050 SD.flag = 0;
01051
01052
01053 while(!setCommMode(GPS_BINARY_OFF) && (millis()-previous)<3000);
01054 delay(100);
01055 while(serialAvailable(_uart)>0)
01056 {
01057 serialRead(_uart);
01058 }
01059 while( !endFile )
01060 {
01061 SD.catBin(filename,offset,5);
01062
01063 if( (SD.bufferBin[0]==0xAA) && (SD.bufferBin[1]==0xBB) && (SD.bufferBin[2]==0xCC) &&
01064 (SD.bufferBin[3]==0xCC) && (SD.bufferBin[4]==0xBB) ) endFile=1;
01065
01066 if (!endFile)
01067 {
01068 for(int a=0;a<5;a++)
01069 {
01070 tempData[a+5]=SD.bufferBin[a];
01071 }
01072 offset+=5;
01073 SD.catBin(filename,offset,85);
01074
01075 tempData[0]=0xA0;
01076 tempData[1]=0xA2;
01077 tempData[2]=0x00;
01078 tempData[3]=0x5B;
01079 tempData[4]=0x95;
01080 for(int b=10;b<95;b++)
01081 {
01082 tempData[b]=SD.bufferBin[b-10];
01083 }
01084 tempData[95]=0x00;
01085 tempData[96]=0x00;
01086 tempData[97]=0xB0;
01087 tempData[98]=0xB3;
01088 getChecksum(tempData);
01089 tempData[95]=checkSUM[0];
01090 tempData[96]=checkSUM[1];
01091 for(int c=0;c<99;c++)
01092 {
01093 printByte(tempData[c],_uart);
01094 }
01095 delay(100);
01096 while(end==0)
01097 {
01098 if(serialAvailable(_uart)>0)
01099 {
01100 answer[counter3]=serialRead(_uart);
01101 counter3++;
01102 previous=millis();
01103 }
01104 if( (millis()-previous) > interval )
01105 {
01106 end=1;
01107 serialFlush(_uart);
01108 }
01109 }
01110 counter3=0;
01111 end=0;
01112 previous=millis();
01113 if( (answer[0]==0xA0) && (answer[1]==0xA2) && (answer[2]==0x00) && (answer[3]==0x02) &&
01114 (answer[4]==0x0B) && (answer[5]==0x95) && (answer[6]==0x00) &&
01115 (answer[7]==0xA0) && (answer[8]==0xB0) && (answer[9]==0xB3) ) error=1;
01116 else error=0;
01117 offset+=85;
01118 }
01119 }
01120 free(answer);
01121 free(tempData);
01122 answer=NULL;
01123 tempData=NULL;
01124 return error;
01125
01126 }
01127
01128
01129
01130
01131
01132
01133
01134
01135 uint8_t WaspGPS::getPosition()
01136 {
01137 flag &= ~(GPS_INVALID);
01138
01139 uint16_t currentSentences = commMode;
01140 long previous=0;
01141 uint8_t complete = 0;
01142
01143 uint8_t byteGPS = 0;
01144 int i = 0;
01145 uint32_t timeout = 1000;
01146
01147
01148 while(!setCommMode(GPS_NMEA) && (millis()-previous)<3000);
01149
01150
01151 Utils.strExplode(Utils.inBuffer, ',');
01152
01153
01154 previous = millis();
01155 while( complete<3 && (millis()-previous)<5000 )
01156 {
01157 if( !strcmp(Utils.arguments[0],"$GPGGA") )
01158 {
01159 for(int i=0;i<MAX_ARGS;i++) timeGPS[i]=Utils.arguments[1][i];
01160 for(int i=0;i<MAX_ARGS;i++) latitude[i]=Utils.arguments[2][i];
01161 for(int i=0;i<MAX_ARGS;i++) longitude[i]=Utils.arguments[4][i];
01162 for(int i=0;i<MAX_ARGS;i++) altitude[i]=Utils.arguments[9][i];
01163 complete++;
01164 }
01165 else if( !strcmp(Utils.arguments[0],"$GPRMC") )
01166 {
01167 for(int i=0;i<MAX_ARGS;i++) dateGPS[i]=Utils.arguments[9][i];
01168 for(int i=0;i<MAX_ARGS;i++) timeGPS[i]=Utils.arguments[1][i];
01169 complete++;
01170 }
01171 else if( !strcmp(Utils.arguments[0],"$GPVTG") )
01172 {
01173 for(int i=0;i<MAX_ARGS;i++) speed[i]=Utils.arguments[7][i];
01174 for(int i=0;i<MAX_ARGS;i++) course[i]=Utils.arguments[1][i];
01175 complete++;
01176 }
01177
01178 while(!serialAvailable(_uart) && timeout > 0) { delay(1); timeout--; };
01179
01180 if (timeout <= 0)
01181 {
01182 flag |= GPS_TIMEOUT;
01183 return 1;
01184 }
01185
01186 byteGPS = serialRead(_uart);
01187
01188 while(byteGPS != '$')
01189 {
01190 if(serialAvailable(_uart) > 0){
01191 byteGPS = serialRead(_uart);
01192 }
01193 }
01194 Utils.inBuffer[i]=byteGPS;
01195 i++;
01196 while(i <= 1 || (byteGPS != '*' && byteGPS != '$' && i < 100)){
01197 if(serialAvailable(_uart) > 0)
01198 {
01199 byteGPS = serialRead(_uart);
01200 if (byteGPS != '*' && byteGPS != '$' && i < 100) Utils.inBuffer[i]=byteGPS;
01201 i++;
01202 }
01203 }
01204
01205 if (byteGPS == '\n' || i == 100) Utils.inBuffer[i-1] = '\0';
01206 else Utils.inBuffer[i] = '\0';
01207
01208 i=0;
01209
01210 Utils.strExplode(Utils.inBuffer, ',');
01211 }
01212
01213
01214 previous=millis();
01215 if (currentSentences == GPS_BINARY_OFF ) while( !setCommMode(GPS_BINARY) && (millis()-previous)<3000);
01216 delay(100);
01217 previous=millis();
01218 while(!setCommMode(currentSentences) && (millis()-previous)<3000);
01219
01220 if( flag & GPS_INVALID ) return 0;
01221 else return 1;
01222 }
01223
01224
01225
01226
01227
01228
01229
01230
01231
01232
01233
01234
01235
01236
01237
01238
01239
01240
01241
01242
01243
01244
01245
01246
01247
01248
01249
01250
01251
01252
01253
01254
01255 void WaspGPS::begin(void)
01256 {
01257 Utils.setMuxGPS();
01258 beginSerial(_baudRate,_uart);
01259
01260 }
01261
01262
01263
01264
01265
01266
01267
01268
01269
01270
01271
01272
01273
01274
01275 void WaspGPS::close()
01276 {
01277 closeSerial(_uart);
01278 Utils.setMux(MUX_TO_LOW,MUX_TO_LOW);
01279 reboot=COLD;
01280 }
01281
01282
01283
01284
01285
01286
01287
01288
01289 uint8_t WaspGPS::checkSum(const char* gpsString)
01290 {
01291
01292 flag &= ~(GPS_BAD_CHECKSUM);
01293
01294
01295 if (gpsString[Utils.sizeOf(gpsString)-3] != '*') return 0;
01296
01297 char check = 0;
01298
01299 for (int c = 1; c < Utils.sizeOf(gpsString) - 3; c++) {
01300 check = char(check ^ gpsString[c]);
01301 }
01302
01303
01304 char stringCheckSum = (gpsString[Utils.sizeOf(gpsString)-2] - '0') << 4 | (gpsString[Utils.sizeOf(gpsString)-1] - '0');
01305
01306 uint8_t result = (check == stringCheckSum);
01307
01308 if (!result) flag |= GPS_BAD_CHECKSUM;
01309
01310
01311 return result;
01312 }
01313
01314
01315
01316
01317
01318
01319
01320
01321 uint8_t WaspGPS::getChecksum(const char* gpsString)
01322 {
01323 char check = 0;
01324
01325 for (int c = 1; c < Utils.sizeOf(gpsString) - 3; c++) {
01326 check = char(check ^ gpsString[c]);
01327 }
01328 return check;
01329 }
01330
01331
01332
01333
01334
01335
01336 void WaspGPS::setChecksum()
01337 {
01338 checksum= getChecksum(Utils.inBuffer);
01339 int aux=Utils.sizeOf(Utils.inBuffer);
01340 float aux2 = checksum%16;
01341 if(aux2==0.0)
01342 {
01343 Utils.inBuffer[aux-1]=48;
01344 Utils.inBuffer[aux-2]=(checksum/16)+48;
01345 }
01346 else
01347 {
01348 Utils.inBuffer[aux-2]=(int) (checksum/16) + 48;
01349 aux2 = checksum/16;
01350 Utils.inBuffer[aux-1]= (checksum-aux2*16)+48;
01351
01352 }
01353 }
01354
01355
01356
01357 WaspGPS GPS = WaspGPS();
01358