gps

Dependencies:   TextLCD

Dependents:   RUCHE2-CODES RUCHE2-CODES_correctiondepoids RUCHE2-CODES_correction_de_poids

Committer:
mohayonor
Date:
Mon Jan 28 17:04:37 2019 +0000
Revision:
0:0c2f0ae3493e
gps actu

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mohayonor 0:0c2f0ae3493e 1
mohayonor 0:0c2f0ae3493e 2 #include "gps.h"
mohayonor 0:0c2f0ae3493e 3
mohayonor 0:0c2f0ae3493e 4 int dateRef = 90318;
mohayonor 0:0c2f0ae3493e 5
mohayonor 0:0c2f0ae3493e 6
mohayonor 0:0c2f0ae3493e 7 Serial gps(D1, D0);
mohayonor 0:0c2f0ae3493e 8 //TextLCD lcd(p11, p12, p15, p16, p29, p30); // rs, e, d4, d5, d6, d7
mohayonor 0:0c2f0ae3493e 9
mohayonor 0:0c2f0ae3493e 10 void Init()
mohayonor 0:0c2f0ae3493e 11 {
mohayonor 0:0c2f0ae3493e 12 gps.baud(9600);
mohayonor 0:0c2f0ae3493e 13 }
mohayonor 0:0c2f0ae3493e 14
mohayonor 0:0c2f0ae3493e 15 int getGPS(char* cDataBuffer,float *lat,float *lon,float *tmf,float *spd, int *dat, char *ns, char *ew, char *stat)
mohayonor 0:0c2f0ae3493e 16 {
mohayonor 0:0c2f0ae3493e 17 char nortSouth, eastWest, status;
mohayonor 0:0c2f0ae3493e 18 int date,fq, nst; // fix quality, Number of satellites being tracked, 3D fix
mohayonor 0:0c2f0ae3493e 19 float latitude, longitude, timefix, speed,altitude;
mohayonor 0:0c2f0ae3493e 20
mohayonor 0:0c2f0ae3493e 21 if(strncmp(cDataBuffer,"$GPRMC", 6) == 0)
mohayonor 0:0c2f0ae3493e 22 {
mohayonor 0:0c2f0ae3493e 23 sscanf(cDataBuffer, "$GPRMC,%f,%c,%f,%c,%f,%c,%f,,%d", &timefix, &status, &latitude, &nortSouth, &longitude, &eastWest, &speed, &date);
mohayonor 0:0c2f0ae3493e 24
mohayonor 0:0c2f0ae3493e 25 if (latitude !=0)
mohayonor 0:0c2f0ae3493e 26 *lat = latitude*0.01;
mohayonor 0:0c2f0ae3493e 27 if (longitude != 0)
mohayonor 0:0c2f0ae3493e 28 *lon = longitude*0.01;
mohayonor 0:0c2f0ae3493e 29
mohayonor 0:0c2f0ae3493e 30 *tmf = timefix;
mohayonor 0:0c2f0ae3493e 31
mohayonor 0:0c2f0ae3493e 32 *spd = speed;
mohayonor 0:0c2f0ae3493e 33 if (date >= dateRef)
mohayonor 0:0c2f0ae3493e 34 *dat = date;
mohayonor 0:0c2f0ae3493e 35
mohayonor 0:0c2f0ae3493e 36 if (nortSouth == 'N' | nortSouth == 'S')
mohayonor 0:0c2f0ae3493e 37 *ns = nortSouth;
mohayonor 0:0c2f0ae3493e 38 if (eastWest == 'W' | eastWest == 'E')
mohayonor 0:0c2f0ae3493e 39 *ew = eastWest;
mohayonor 0:0c2f0ae3493e 40 *stat = status;
mohayonor 0:0c2f0ae3493e 41 // pc.printf("GPRMC Fix: %f, Status: %c, Latitude: %f %c, Longitude: %f %c, Speed: %f, Date: %d\n", timefix, status, latitude, nortSouth, longitude, eastWest, speed, date);
mohayonor 0:0c2f0ae3493e 42 // pc.printf("GPRMC Fix: %f, Status: %c, Latitude: %f %c, Longitude: %f %c, Speed: %f, Date: %d\n", *tmf, *stat, *lat, *ns, *lon, *ew, *spd, *dat);
mohayonor 0:0c2f0ae3493e 43 return 0;
mohayonor 0:0c2f0ae3493e 44 }
mohayonor 0:0c2f0ae3493e 45 if(strncmp(cDataBuffer,"$GPGLL", 6) == 0)
mohayonor 0:0c2f0ae3493e 46 {
mohayonor 0:0c2f0ae3493e 47 sscanf(cDataBuffer, "$GPGLL,%f,%c,%f,%c,%f", &latitude, &nortSouth, &longitude, &eastWest, &timefix);
mohayonor 0:0c2f0ae3493e 48 //pc.printf("%s",cDataBuffer);
mohayonor 0:0c2f0ae3493e 49 if (latitude !=0)
mohayonor 0:0c2f0ae3493e 50 *lat = latitude * 0.01;
mohayonor 0:0c2f0ae3493e 51
mohayonor 0:0c2f0ae3493e 52 if (longitude != 0)
mohayonor 0:0c2f0ae3493e 53 *lon = longitude * 0.01;
mohayonor 0:0c2f0ae3493e 54
mohayonor 0:0c2f0ae3493e 55 *tmf = timefix;
mohayonor 0:0c2f0ae3493e 56 *spd = speed;
mohayonor 0:0c2f0ae3493e 57
mohayonor 0:0c2f0ae3493e 58 if (nortSouth == 'N' | nortSouth == 'S')
mohayonor 0:0c2f0ae3493e 59 *ns = nortSouth;
mohayonor 0:0c2f0ae3493e 60
mohayonor 0:0c2f0ae3493e 61 if (eastWest == 'W' | eastWest == 'E')
mohayonor 0:0c2f0ae3493e 62 *ew = eastWest;
mohayonor 0:0c2f0ae3493e 63 return 0;
mohayonor 0:0c2f0ae3493e 64 }
mohayonor 0:0c2f0ae3493e 65
mohayonor 0:0c2f0ae3493e 66 if(strncmp(cDataBuffer,"$GPGGA", 6) == 0)
mohayonor 0:0c2f0ae3493e 67 {
mohayonor 0:0c2f0ae3493e 68 sscanf(cDataBuffer, "$GPGGA,%f,%f,%c,%f,%c,%d,%d,%*f,%f", &timefix, &latitude, &nortSouth, &longitude, &eastWest, &fq, &nst, &altitude);
mohayonor 0:0c2f0ae3493e 69 if (latitude !=0)
mohayonor 0:0c2f0ae3493e 70 *lat = latitude * 0.01;
mohayonor 0:0c2f0ae3493e 71
mohayonor 0:0c2f0ae3493e 72 if (longitude != 0)
mohayonor 0:0c2f0ae3493e 73 *lon = longitude * 0.01;
mohayonor 0:0c2f0ae3493e 74
mohayonor 0:0c2f0ae3493e 75 //*alt = altitude;
mohayonor 0:0c2f0ae3493e 76 *tmf = timefix;
mohayonor 0:0c2f0ae3493e 77
mohayonor 0:0c2f0ae3493e 78 if (nortSouth == 'N' | nortSouth == 'S')
mohayonor 0:0c2f0ae3493e 79 *ns = nortSouth;
mohayonor 0:0c2f0ae3493e 80
mohayonor 0:0c2f0ae3493e 81 if (eastWest == 'W' | eastWest == 'E')
mohayonor 0:0c2f0ae3493e 82 *ew = eastWest;
mohayonor 0:0c2f0ae3493e 83 return 0;
mohayonor 0:0c2f0ae3493e 84
mohayonor 0:0c2f0ae3493e 85 }
mohayonor 0:0c2f0ae3493e 86 return -1;
mohayonor 0:0c2f0ae3493e 87
mohayonor 0:0c2f0ae3493e 88
mohayonor 0:0c2f0ae3493e 89 }
mohayonor 0:0c2f0ae3493e 90
mohayonor 0:0c2f0ae3493e 91
mohayonor 0:0c2f0ae3493e 92
mohayonor 0:0c2f0ae3493e 93 int getLatLong(float &lat , float &lon)
mohayonor 0:0c2f0ae3493e 94 {
mohayonor 0:0c2f0ae3493e 95 Init();
mohayonor 0:0c2f0ae3493e 96 char Buffer[128],str[7];
mohayonor 0:0c2f0ae3493e 97 char ns, ew, status;
mohayonor 0:0c2f0ae3493e 98 int date,gpsACK; // fix quality, Number of satellites being tracked, 3D fix
mohayonor 0:0c2f0ae3493e 99 float latitude, longitude, timefix, speed;
mohayonor 0:0c2f0ae3493e 100 memset(Buffer,0,sizeof(Buffer));
mohayonor 0:0c2f0ae3493e 101 date = 0;
mohayonor 0:0c2f0ae3493e 102 latitude = longitude = timefix = speed = 0;
mohayonor 0:0c2f0ae3493e 103 ns = ew = status =0;
mohayonor 0:0c2f0ae3493e 104
mohayonor 0:0c2f0ae3493e 105 if(gps.readable())
mohayonor 0:0c2f0ae3493e 106 {
mohayonor 0:0c2f0ae3493e 107 gps.gets(Buffer,90);
mohayonor 0:0c2f0ae3493e 108 if(Buffer[0] == '$'){
mohayonor 0:0c2f0ae3493e 109 memcpy(str,Buffer, 6);
mohayonor 0:0c2f0ae3493e 110 //printf("\nrecv: %s\n",str);
mohayonor 0:0c2f0ae3493e 111 gpsACK = getGPS(Buffer, &latitude, &longitude, &timefix, &speed, &date, &ns, &ew, &status);
mohayonor 0:0c2f0ae3493e 112
mohayonor 0:0c2f0ae3493e 113 }
mohayonor 0:0c2f0ae3493e 114 }
mohayonor 0:0c2f0ae3493e 115
mohayonor 0:0c2f0ae3493e 116 if (gpsACK == 0)
mohayonor 0:0c2f0ae3493e 117 {
mohayonor 0:0c2f0ae3493e 118 lat = latitude;
mohayonor 0:0c2f0ae3493e 119 lon = longitude;
mohayonor 0:0c2f0ae3493e 120 }
mohayonor 0:0c2f0ae3493e 121
mohayonor 0:0c2f0ae3493e 122
mohayonor 0:0c2f0ae3493e 123
mohayonor 0:0c2f0ae3493e 124 }
mohayonor 0:0c2f0ae3493e 125
mohayonor 0:0c2f0ae3493e 126