gps

Dependencies:   mbed

Committer:
naruu
Date:
Thu Oct 15 08:22:47 2020 +0000
Revision:
3:c64909f39383
Parent:
0:77bff687ff05
s

Who changed what in which revision?

UserRevisionLine numberNew contents of line
naruu 0:77bff687ff05 1 #include "mbed.h"
naruu 0:77bff687ff05 2 #include "getGPS.h"
naruu 0:77bff687ff05 3
naruu 0:77bff687ff05 4 GPS::GPS(PinName gpstx,PinName gpsrx): _gps(gpstx,gpsrx)
naruu 0:77bff687ff05 5 {
naruu 0:77bff687ff05 6 latitude = 0;
naruu 0:77bff687ff05 7 longitude = 0;
naruu 0:77bff687ff05 8 _gps.baud(GPSBAUD);
naruu 0:77bff687ff05 9 _gps.printf("$PMTK314,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29");
naruu 0:77bff687ff05 10 }
naruu 0:77bff687ff05 11
naruu 0:77bff687ff05 12 bool GPS::getgps()
naruu 0:77bff687ff05 13 {
naruu 0:77bff687ff05 14 char gps_data[256];
naruu 0:77bff687ff05 15 int i;
naruu 0:77bff687ff05 16
naruu 0:77bff687ff05 17 do {
naruu 0:77bff687ff05 18 while(_gps.getc() != '$'); //$マークまで読み飛ばす
naruu 0:77bff687ff05 19
naruu 0:77bff687ff05 20 i = 0;
naruu 0:77bff687ff05 21
naruu 0:77bff687ff05 22 /*gpsデータを初期化*/
naruu 0:77bff687ff05 23 for(int j = 0; j < 256; j++){
naruu 0:77bff687ff05 24 gps_data[j] = '\0';
naruu 0:77bff687ff05 25 }
naruu 0:77bff687ff05 26
naruu 0:77bff687ff05 27 /* NMEAから一行読み込み */
naruu 0:77bff687ff05 28 while((gps_data[i] = _gps.getc()) != '\r')//受信データを格納、\rが出てくるまでループ
naruu 0:77bff687ff05 29 {
naruu 0:77bff687ff05 30 i++;
naruu 0:77bff687ff05 31 if(i == 256) {
naruu 0:77bff687ff05 32 i = 255;
naruu 0:77bff687ff05 33 break;
naruu 0:77bff687ff05 34 }
naruu 0:77bff687ff05 35 }
naruu 0:77bff687ff05 36 } while(strstr(gps_data, "GPGGA") == NULL); //GGAセンテンスまで一行ずつ読み込み続ける
naruu 0:77bff687ff05 37
naruu 0:77bff687ff05 38 int rlock;
naruu 0:77bff687ff05 39 char ns,ew;
naruu 0:77bff687ff05 40 double w_time, raw_longitude, raw_latitude;
naruu 0:77bff687ff05 41 int satnum;
naruu 0:77bff687ff05 42 double hdop;
naruu 0:77bff687ff05 43
naruu 0:77bff687ff05 44 if(sscanf(gps_data, "GPGGA, %lf, %lf, %c, %lf, %c, %d, %d, %lf", &w_time, &raw_latitude, &ns, &raw_longitude, &ew, &rlock, &satnum, &hdop) > 1) {
naruu 0:77bff687ff05 45 /*座標1(度成分)*/
naruu 0:77bff687ff05 46 double latitude_dd = (double)(raw_latitude / 100);
naruu 0:77bff687ff05 47 double longitude_dd = (double)(raw_longitude / 100);
naruu 0:77bff687ff05 48
naruu 0:77bff687ff05 49 /* 座標2(分成分 → 度)*/
naruu 0:77bff687ff05 50 double latitude_md = (raw_latitude - latitude_dd * 100) / 60;
naruu 0:77bff687ff05 51 double longitude_md = (raw_longitude - longitude_dd * 100) / 60;
naruu 0:77bff687ff05 52
naruu 0:77bff687ff05 53 /* 座標1+座標2 */
naruu 0:77bff687ff05 54 latitude = latitude_dd + latitude_md;
naruu 0:77bff687ff05 55 longitude = longitude_dd + longitude_md;
naruu 0:77bff687ff05 56
naruu 0:77bff687ff05 57 return true;
naruu 0:77bff687ff05 58 } else
naruu 0:77bff687ff05 59 return false; //GGAセンテンスの情報が欠けている時
naruu 0:77bff687ff05 60 }
naruu 0:77bff687ff05 61