narumi tatsuya
/
motor_sample
ルンパッパ
getGPS.cpp@10:c96e83e35ed2, 2020-12-19 (annotated)
- Committer:
- naruu
- Date:
- Sat Dec 19 16:22:46 2020 +0000
- Revision:
- 10:c96e83e35ed2
- Parent:
- 5:e922c07f1b61
ou;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
naruu | 1:1dab888e1f3c | 1 | #include "mbed.h" |
naruu | 1:1dab888e1f3c | 2 | #include "getGPS.h" |
naruu | 1:1dab888e1f3c | 3 | |
naruu | 1:1dab888e1f3c | 4 | GPS::GPS(PinName gpstx,PinName gpsrx): _gps(gpstx,gpsrx) |
naruu | 1:1dab888e1f3c | 5 | { |
naruu | 1:1dab888e1f3c | 6 | latitude = 0; |
naruu | 5:e922c07f1b61 | 7 | longitude = 0; //getGPS.hで宣言した値を初期化 |
naruu | 1:1dab888e1f3c | 8 | _gps.baud(GPSBAUD); |
naruu | 1:1dab888e1f3c | 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 | 1:1dab888e1f3c | 10 | } |
naruu | 1:1dab888e1f3c | 11 | |
naruu | 5:e922c07f1b61 | 12 | bool GPS::getgps() //GPSのデータを取得する関数の中身 |
naruu | 1:1dab888e1f3c | 13 | { |
naruu | 1:1dab888e1f3c | 14 | char gps_data[256]; |
naruu | 1:1dab888e1f3c | 15 | int i; |
naruu | 1:1dab888e1f3c | 16 | |
naruu | 1:1dab888e1f3c | 17 | do { |
naruu | 5:e922c07f1b61 | 18 | while(_gps.getc() != '$');//$マークまで読み飛ばし |
naruu | 5:e922c07f1b61 | 19 | /*送ったリンクのサイトに『1つのセンテンスは、「$」で始まり、「(改行(\r\n))」で終わります。』 |
naruu | 5:e922c07f1b61 | 20 | とあるので、_gps.getc()が取得する値が$以外の場合は無視して、$が始まったところを探すためにループしている。*/ |
naruu | 1:1dab888e1f3c | 21 | i = 0; |
naruu | 1:1dab888e1f3c | 22 | |
naruu | 5:e922c07f1b61 | 23 | /* gpa_data初期化 */ |
naruu | 1:1dab888e1f3c | 24 | for(int j = 0; j < 256; j++) |
naruu | 1:1dab888e1f3c | 25 | gps_data[j] = '\0'; |
naruu | 1:1dab888e1f3c | 26 | |
naruu | 1:1dab888e1f3c | 27 | /* NMEAから一行読み込み */ |
naruu | 5:e922c07f1b61 | 28 | while((gps_data[i] = _gps.getc()) != '\r') {//受信したデータを配列に格納し、'\r'でない場合ループ |
naruu | 1:1dab888e1f3c | 29 | i++; |
naruu | 1:1dab888e1f3c | 30 | if(i == 256) { |
naruu | 1:1dab888e1f3c | 31 | i = 255; |
naruu | 5:e922c07f1b61 | 32 | return 0;; |
naruu | 1:1dab888e1f3c | 33 | } |
naruu | 1:1dab888e1f3c | 34 | } |
naruu | 1:1dab888e1f3c | 35 | } while(strstr(gps_data, "GPGGA") == NULL); //GGAセンテンスまで一行ずつ読み込み続ける |
naruu | 1:1dab888e1f3c | 36 | |
naruu | 1:1dab888e1f3c | 37 | int rlock; |
naruu | 1:1dab888e1f3c | 38 | char ns,ew; |
naruu | 1:1dab888e1f3c | 39 | double w_time, raw_longitude, raw_latitude; |
naruu | 1:1dab888e1f3c | 40 | int satnum; |
naruu | 1:1dab888e1f3c | 41 | double hdop; |
naruu | 1:1dab888e1f3c | 42 | |
naruu | 1:1dab888e1f3c | 43 | 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 | 5:e922c07f1b61 | 44 | //取得したデータを読み込んでいる 詳しくはリンクを送ります |
naruu | 1:1dab888e1f3c | 45 | /* 座標1(度部分) */ |
naruu | 1:1dab888e1f3c | 46 | double latitude_dd = (double)(raw_latitude / 100); |
naruu | 1:1dab888e1f3c | 47 | double longitude_dd = (double)(raw_longitude / 100); |
naruu | 1:1dab888e1f3c | 48 | |
naruu | 1:1dab888e1f3c | 49 | /* 座標2(分部分 → 度) */ |
naruu | 1:1dab888e1f3c | 50 | double latitude_md = (raw_latitude - latitude_dd * 100) / 60; |
naruu | 1:1dab888e1f3c | 51 | double longitude_md = (raw_longitude - longitude_dd * 100) / 60; |
naruu | 1:1dab888e1f3c | 52 | |
naruu | 1:1dab888e1f3c | 53 | /* 座標1 + 2 */ |
naruu | 1:1dab888e1f3c | 54 | latitude = latitude_dd + latitude_md; |
naruu | 1:1dab888e1f3c | 55 | longitude = longitude_dd + longitude_md; |
naruu | 1:1dab888e1f3c | 56 | |
naruu | 1:1dab888e1f3c | 57 | return true; |
naruu | 5:e922c07f1b61 | 58 | //ここまでgpsが受信する値は単位が度になっていないので、度に直している |
naruu | 1:1dab888e1f3c | 59 | } else |
naruu | 1:1dab888e1f3c | 60 | return false; //GGAセンテンスの情報が欠けている時 |
naruu | 5:e922c07f1b61 | 61 | } |