narumi tatsuya
/
hokkahokkatrei
ウソッキー
Revision 0:326c07d635d7, committed 2020-12-19
- Comitter:
- naruu
- Date:
- Sat Dec 19 11:30:53 2020 +0000
- Commit message:
- GPS;
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/getGPS.cpp Sat Dec 19 11:30:53 2020 +0000 @@ -0,0 +1,61 @@ +#include "mbed.h" +#include "getGPS.h" + +GPS::GPS(PinName gpstx,PinName gpsrx): _gps(gpstx,gpsrx) +{ + latitude = 0; + longitude = 0; //getGPS.hで宣言した値を初期化 + _gps.baud(GPSBAUD); + _gps.printf("$PMTK314,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29"); +} + +bool GPS::getgps() //GPSのデータを取得する関数の中身 +{ + char gps_data[256]; + int i; + + do { + while(_gps.getc() != '$');//$マークまで読み飛ばし + /*送ったリンクのサイトに『1つのセンテンスは、「$」で始まり、「(改行(\r\n))」で終わります。』 + とあるので、_gps.getc()が取得する値が$以外の場合は無視して、$が始まったところを探すためにループしている。*/ + i = 0; + + /* gpa_data初期化 */ + for(int j = 0; j < 256; j++) + gps_data[j] = '\0'; + + /* NMEAから一行読み込み */ + while((gps_data[i] = _gps.getc()) != '\r') {//受信したデータを配列に格納し、'\r'でない場合ループ + i++; + if(i == 256) { + i = 255; + return 0;; + } + } + } while(strstr(gps_data, "GPGGA") == NULL); //GGAセンテンスまで一行ずつ読み込み続ける + + int rlock; + char ns,ew; + double w_time, raw_longitude, raw_latitude; + int satnum; + double hdop; + + 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) { + //取得したデータを読み込んでいる 詳しくはリンクを送ります + /* 座標1(度部分) */ + double latitude_dd = (double)(raw_latitude / 100); + double longitude_dd = (double)(raw_longitude / 100); + + /* 座標2(分部分 → 度) */ + double latitude_md = (raw_latitude - latitude_dd * 100) / 60; + double longitude_md = (raw_longitude - longitude_dd * 100) / 60; + + /* 座標1 + 2 */ + latitude = latitude_dd + latitude_md; + longitude = longitude_dd + longitude_md; + + return true; +//ここまでgpsが受信する値は単位が度になっていないので、度に直している + } else + return false; //GGAセンテンスの情報が欠けている時 +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/getGPS.h Sat Dec 19 11:30:53 2020 +0000 @@ -0,0 +1,20 @@ +#ifndef GPS_H +#define GPS_H + +#define GPSBAUD 9600 //ボーレート + +class GPS { +public: + GPS(PinName gpstx,PinName gpsrx); + + bool getgps(); + + double longitude; + double latitude; + +private: + Serial _gps; +}; + +#endif //GPS_H + \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sat Dec 19 11:30:53 2020 +0000 @@ -0,0 +1,76 @@ +#include "mbed.h" +#include "getGPS.h" +#include <stdio.h> +#include <math.h> + +#define PIN_SDA D4 +#define PIN_SCL D5 + +Serial pc(SERIAL_TX,SERIAL_RX,921600);//通信 +Serial xbee(D1,D0);//Xbeeのピン +GPS gps (D13,D12); + +int main(){ + double a; + double b; + double distance; + + xbee.printf("GPS begin\n"); + + while(1){ + if(gps.getgps()){ + /*a,bを緯度経度の初期値で初期化*/ + a=gps.latitude; + b=gps.longitude; + xbee.printf("(%lf,%lf)\r\n",a,b);//緯度と経度を表示 + xbee.printf("--------------------------------\n\r"); + break; + }else{ + xbee.printf("Fault_No_Data\r\n"); + wait(1); + } + } + while(1){ + if(gps.getgps()){ + xbee.printf("(%lf,%lf)\n\r",gps.latitude,gps.longitude);//緯度と経度を表示 + xbee.printf("--------------------------------\n\r"); + + /*ここから距離の計算*/ + /*c、dを得た緯度経度の値で初期化*/ + double c; + double d; + c=gps.latitude; + d=gps.longitude; + + const double pi=3.14159265359;//円周率 + + /*ラジアンに変換*/ + double theta_a=a*pi/180; + double theta_b=b*pi/180; + double theta_c=c*pi/180; + double theta_d=d*pi/180; + + double e=sin(theta_a)*sin(theta_c)+cos(theta_a)*cos(theta_c)*cos(theta_b-theta_d);//2点間のなす角を求める + double theta_r=acos(e); + + const double earth_radius=6378140;//赤道半径 + + distance=earth_radius*theta_r;//距離の計算 + + /*距離が30m以上なら表示、通信*/ + if(distance>=30){ + pc.printf("run over 20m"); + xbee.printf("run over 20m"); + break; + } + + }else { + xbee.printf("False_No_Data\r\n"); + pc.printf("False_No_Date\r\n"); + wait(1); + }//データ取得失敗を表示、通信、1秒待機 + + } + xbee.printf("成功\n"); + return 0; + } \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sat Dec 19 11:30:53 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400 \ No newline at end of file