narumi tatsuya
/
GPS_program
gps
gps.cpp@0:77bff687ff05, 2020-10-02 (annotated)
- Committer:
- naruu
- Date:
- Fri Oct 02 13:48:57 2020 +0000
- Revision:
- 0:77bff687ff05
- Child:
- 1:6165e8067d7a
GPS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
naruu | 0:77bff687ff05 | 1 | #include "mbed.h" |
naruu | 0:77bff687ff05 | 2 | #include "getGPS.h" |
naruu | 0:77bff687ff05 | 3 | #include"math.h" |
naruu | 0:77bff687ff05 | 4 | |
naruu | 0:77bff687ff05 | 5 | Serial pc(USBTX,USBRX); |
naruu | 0:77bff687ff05 | 6 | GPS gps(D13,D14); |
naruu | 0:77bff687ff05 | 7 | Serial xbee(D0,D1); |
naruu | 0:77bff687ff05 | 8 | |
naruu | 0:77bff687ff05 | 9 | int main(){ |
naruu | 0:77bff687ff05 | 10 | double a; |
naruu | 0:77bff687ff05 | 11 | double b; |
naruu | 0:77bff687ff05 | 12 | double distance; |
naruu | 0:77bff687ff05 | 13 | |
naruu | 0:77bff687ff05 | 14 | pc.printf("GPS begin\n"); |
naruu | 0:77bff687ff05 | 15 | |
naruu | 0:77bff687ff05 | 16 | while(1){ |
naruu | 0:77bff687ff05 | 17 | if(gps.getgps()){ |
naruu | 0:77bff687ff05 | 18 | /*a,bを緯度経度の初期値で初期化*/ |
naruu | 0:77bff687ff05 | 19 | a=gps.latitude; |
naruu | 0:77bff687ff05 | 20 | b=gps.longitude; |
naruu | 0:77bff687ff05 | 21 | pc.printf("(%lf,%lf)\r\n",gps.latitude,gps.longitude);//緯度と経度を表示 |
naruu | 0:77bff687ff05 | 22 | break; |
naruu | 0:77bff687ff05 | 23 | }else{ |
naruu | 0:77bff687ff05 | 24 | pc.printf("Fault_No_Data\r\n); |
naruu | 0:77bff687ff05 | 25 | wait(1); |
naruu | 0:77bff687ff05 | 26 | } |
naruu | 0:77bff687ff05 | 27 | } while(1){ |
naruu | 0:77bff687ff05 | 28 | if(gps.getgps()){ |
naruu | 0:77bff687ff05 | 29 | pc.printf("(%lf,%lf)\n\r",gps.latitude,gps.longitude);//緯度と経度を表示 |
naruu | 0:77bff687ff05 | 30 | |
naruu | 0:77bff687ff05 | 31 | /*ここから距離の計算*/ |
naruu | 0:77bff687ff05 | 32 | /*c、dを得た緯度経度の値で初期化*/ |
naruu | 0:77bff687ff05 | 33 | double c; |
naruu | 0:77bff687ff05 | 34 | double d; |
naruu | 0:77bff687ff05 | 35 | c=gps.latitude; |
naruu | 0:77bff687ff05 | 36 | d=gps.longitude; |
naruu | 0:77bff687ff05 | 37 | |
naruu | 0:77bff687ff05 | 38 | const double pi=3.14159265359;//円周率 |
naruu | 0:77bff687ff05 | 39 | |
naruu | 0:77bff687ff05 | 40 | /*ラジアンに変換*/ |
naruu | 0:77bff687ff05 | 41 | double theta_a=a*pi/180; |
naruu | 0:77bff687ff05 | 42 | double theta_b=b*pi/180; |
naruu | 0:77bff687ff05 | 43 | double theta_c=c*pi/180; |
naruu | 0:77bff687ff05 | 44 | double theta_d=d*pi/180; |
naruu | 0:77bff687ff05 | 45 | |
naruu | 0:77bff687ff05 | 46 | double e=sin(theta_a)*sin(theta_c)+cos(theta_a)*cos(theta_c)*cos(theta_b-theta_d);//2点間のなす角を求める |
naruu | 0:77bff687ff05 | 47 | double theta_r=acos(e); |
naruu | 0:77bff687ff05 | 48 | |
naruu | 0:77bff687ff05 | 49 | const double earth_radius=6378140;//赤道半径 |
naruu | 0:77bff687ff05 | 50 | |
naruu | 0:77bff687ff05 | 51 | distance=earth_radius*theta_r;//距離の計算 |
naruu | 0:77bff687ff05 | 52 | |
naruu | 0:77bff687ff05 | 53 | /*距離が20m以上なら表示、通信*/ |
naruu | 0:77bff687ff05 | 54 | if(distance>=20){ |
naruu | 0:77bff687ff05 | 55 | pc.printf("run 20m"); |
naruu | 0:77bff687ff05 | 56 | xbee.printf("run 20m"); |
naruu | 0:77bff687ff05 | 57 | break; |
naruu | 0:77bff687ff05 | 58 | } |
naruu | 0:77bff687ff05 | 59 | |
naruu | 0:77bff687ff05 | 60 | }else { |
naruu | 0:77bff687ff05 | 61 | xbee.print("False_No_Data\r\n"); |
naruu | 0:77bff687ff05 | 62 | pc.printf("False_No_Date\r\n"); |
naruu | 0:77bff687ff05 | 63 | wait(1); |
naruu | 0:77bff687ff05 | 64 | }//データ取得失敗を表示、通信、1秒待機 |
naruu | 0:77bff687ff05 | 65 | } |
naruu | 0:77bff687ff05 | 66 | return 0; |
naruu | 0:77bff687ff05 | 67 | } |