gps

Dependencies:   mbed

Committer:
naruu
Date:
Thu Oct 15 08:22:47 2020 +0000
Revision:
3:c64909f39383
Parent:
1:6165e8067d7a
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 #include"math.h"
naruu 0:77bff687ff05 4
naruu 0:77bff687ff05 5 Serial pc(USBTX,USBRX);
naruu 3:c64909f39383 6 GPS gps(D0,D1);
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 3:c64909f39383 21 printf("------------\r\n");
naruu 3:c64909f39383 22 pc.printf("(北緯:%lf,東経%lf)\r\n",gps.latitude,gps.longitude);//緯度と経度を表示
naruu 0:77bff687ff05 23 break;
naruu 0:77bff687ff05 24 }else{
naruu 1:6165e8067d7a 25 pc.printf("Fault_No_Data\r\n");
naruu 0:77bff687ff05 26 wait(1);
naruu 0:77bff687ff05 27 }
naruu 0:77bff687ff05 28 } while(1){
naruu 0:77bff687ff05 29 if(gps.getgps()){
naruu 3:c64909f39383 30 printf("------------\r\n");
naruu 3:c64909f39383 31 pc.printf("(北緯%lf,東経%lf)\n\r",gps.latitude,gps.longitude);//緯度と経度を表示
naruu 0:77bff687ff05 32
naruu 0:77bff687ff05 33 /*ここから距離の計算*/
naruu 0:77bff687ff05 34 /*c、dを得た緯度経度の値で初期化*/
naruu 1:6165e8067d7a 35 double c;
naruu 0:77bff687ff05 36 double d;
naruu 0:77bff687ff05 37 c=gps.latitude;
naruu 0:77bff687ff05 38 d=gps.longitude;
naruu 0:77bff687ff05 39
naruu 0:77bff687ff05 40 const double pi=3.14159265359;//円周率
naruu 0:77bff687ff05 41
naruu 0:77bff687ff05 42 /*ラジアンに変換*/
naruu 0:77bff687ff05 43 double theta_a=a*pi/180;
naruu 0:77bff687ff05 44 double theta_b=b*pi/180;
naruu 0:77bff687ff05 45 double theta_c=c*pi/180;
naruu 0:77bff687ff05 46 double theta_d=d*pi/180;
naruu 0:77bff687ff05 47
naruu 0:77bff687ff05 48 double e=sin(theta_a)*sin(theta_c)+cos(theta_a)*cos(theta_c)*cos(theta_b-theta_d);//2点間のなす角を求める
naruu 0:77bff687ff05 49 double theta_r=acos(e);
naruu 0:77bff687ff05 50
naruu 0:77bff687ff05 51 const double earth_radius=6378140;//赤道半径
naruu 0:77bff687ff05 52
naruu 0:77bff687ff05 53 distance=earth_radius*theta_r;//距離の計算
naruu 0:77bff687ff05 54
naruu 0:77bff687ff05 55 /*距離が20m以上なら表示、通信*/
naruu 0:77bff687ff05 56 if(distance>=20){
naruu 0:77bff687ff05 57 pc.printf("run 20m");
naruu 0:77bff687ff05 58 xbee.printf("run 20m");
naruu 0:77bff687ff05 59 break;
naruu 0:77bff687ff05 60 }
naruu 0:77bff687ff05 61
naruu 0:77bff687ff05 62 }else {
naruu 1:6165e8067d7a 63 // xbee.print("False_No_Data\r\n");
naruu 3:c64909f39383 64 pc.printf("False_No_Data\r\n");
naruu 0:77bff687ff05 65 wait(1);
naruu 0:77bff687ff05 66 }//データ取得失敗を表示、通信、1秒待機
naruu 0:77bff687ff05 67 }
naruu 0:77bff687ff05 68 return 0;
naruu 0:77bff687ff05 69 }