Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@2:19c8f66ab10f, 2020-12-19 (annotated)
- Committer:
- aihara
- Date:
- Sat Dec 19 09:57:10 2020 +0000
- Revision:
- 2:19c8f66ab10f
- Parent:
- 1:fb92cf1c694b
a;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sugihara0312 | 0:b54d41dacf49 | 1 | #include "mbed.h" |
sugihara0312 | 0:b54d41dacf49 | 2 | #include "getGPS.h" |
sugihara0312 | 0:b54d41dacf49 | 3 | #include <stdio.h> |
sugihara0312 | 0:b54d41dacf49 | 4 | #include <math.h> |
sugihara0312 | 0:b54d41dacf49 | 5 | |
sugihara0312 | 0:b54d41dacf49 | 6 | #define PIN_SDA D4 |
sugihara0312 | 0:b54d41dacf49 | 7 | #define PIN_SCL D5 |
sugihara0312 | 0:b54d41dacf49 | 8 | |
sugihara0312 | 0:b54d41dacf49 | 9 | Serial pc(SERIAL_TX,SERIAL_RX,921600);//通信 |
sugihara0312 | 0:b54d41dacf49 | 10 | Serial xbee(D1,D0);//Xbeeのピン |
aihara | 1:fb92cf1c694b | 11 | //GPS gps (D1,D0); |
sugihara0312 | 0:b54d41dacf49 | 12 | |
sugihara0312 | 0:b54d41dacf49 | 13 | |
sugihara0312 | 0:b54d41dacf49 | 14 | int main() |
sugihara0312 | 0:b54d41dacf49 | 15 | { |
sugihara0312 | 0:b54d41dacf49 | 16 | double a; |
sugihara0312 | 0:b54d41dacf49 | 17 | double b; |
sugihara0312 | 0:b54d41dacf49 | 18 | double distance; |
sugihara0312 | 0:b54d41dacf49 | 19 | |
sugihara0312 | 0:b54d41dacf49 | 20 | pc.printf("GPS begin\n"); |
aihara | 2:19c8f66ab10f | 21 | wait(2); |
aihara | 2:19c8f66ab10f | 22 | GPS gps (D1,D0); |
aihara | 2:19c8f66ab10f | 23 | while(1){ |
sugihara0312 | 0:b54d41dacf49 | 24 | if(gps.getgps()){ |
sugihara0312 | 0:b54d41dacf49 | 25 | /*a,bを緯度経度の初期値で初期化*/ |
sugihara0312 | 0:b54d41dacf49 | 26 | a = gps.latitude; |
sugihara0312 | 0:b54d41dacf49 | 27 | b = gps.longitude; |
aihara | 2:19c8f66ab10f | 28 | wait(2); |
aihara | 2:19c8f66ab10f | 29 | Serial xbee(D1,D0);//Xbeeのピン |
sugihara0312 | 0:b54d41dacf49 | 30 | pc.printf("get1"); |
sugihara0312 | 0:b54d41dacf49 | 31 | break; |
sugihara0312 | 0:b54d41dacf49 | 32 | }else{ |
aihara | 2:19c8f66ab10f | 33 | wait(2); |
aihara | 2:19c8f66ab10f | 34 | Serial xbee(D1,D0);//Xbeeのピン |
sugihara0312 | 0:b54d41dacf49 | 35 | pc.printf("Fault_No_Data1\r\n"); |
sugihara0312 | 0:b54d41dacf49 | 36 | wait(1); |
sugihara0312 | 0:b54d41dacf49 | 37 | } |
aihara | 2:19c8f66ab10f | 38 | } |
aihara | 2:19c8f66ab10f | 39 | //wait(2); |
aihara | 2:19c8f66ab10f | 40 | //GPS gps (D1,D0); |
aihara | 2:19c8f66ab10f | 41 | while(1){ |
sugihara0312 | 0:b54d41dacf49 | 42 | if(gps.getgps()){ |
aihara | 2:19c8f66ab10f | 43 | wait(2); |
aihara | 1:fb92cf1c694b | 44 | Serial xbee(D1,D0);//Xbeeのピン |
sugihara0312 | 0:b54d41dacf49 | 45 | printf("get2"); |
sugihara0312 | 0:b54d41dacf49 | 46 | |
sugihara0312 | 0:b54d41dacf49 | 47 | /*ここから距離の計算*/ |
sugihara0312 | 0:b54d41dacf49 | 48 | /*c、dを得た緯度経度の値で初期化*/ |
sugihara0312 | 0:b54d41dacf49 | 49 | double c; |
sugihara0312 | 0:b54d41dacf49 | 50 | double d; |
sugihara0312 | 0:b54d41dacf49 | 51 | c = gps.latitude; |
sugihara0312 | 0:b54d41dacf49 | 52 | d = gps.longitude; |
sugihara0312 | 0:b54d41dacf49 | 53 | |
sugihara0312 | 0:b54d41dacf49 | 54 | const double pi = 3.14159265359;//円周率 |
sugihara0312 | 0:b54d41dacf49 | 55 | |
sugihara0312 | 0:b54d41dacf49 | 56 | /*ラジアンに変換*/ |
sugihara0312 | 0:b54d41dacf49 | 57 | double theta_a = a * pi /180; |
sugihara0312 | 0:b54d41dacf49 | 58 | double theta_b = b * pi / 180; |
sugihara0312 | 0:b54d41dacf49 | 59 | double theta_c = c * pi / 180; |
sugihara0312 | 0:b54d41dacf49 | 60 | double theta_d = d * pi / 180; |
sugihara0312 | 0:b54d41dacf49 | 61 | |
sugihara0312 | 0:b54d41dacf49 | 62 | double e = sin(theta_a) * sin(theta_c) + cos(theta_a) * cos(theta_c) * cos(theta_b-theta_d);//2点間のなす角を求める |
sugihara0312 | 0:b54d41dacf49 | 63 | double theta_r = acos(e); |
sugihara0312 | 0:b54d41dacf49 | 64 | |
sugihara0312 | 0:b54d41dacf49 | 65 | const double earth_radius = 6378140;//赤道半径 |
sugihara0312 | 0:b54d41dacf49 | 66 | |
sugihara0312 | 0:b54d41dacf49 | 67 | distance = earth_radius*theta_r;//距離の計算 |
sugihara0312 | 0:b54d41dacf49 | 68 | |
aihara | 1:fb92cf1c694b | 69 | /*距離が25m以上なら表示、通信*/ |
sugihara0312 | 0:b54d41dacf49 | 70 | if(distance>=30){ |
sugihara0312 | 0:b54d41dacf49 | 71 | pc.printf("run over 20m"); |
sugihara0312 | 0:b54d41dacf49 | 72 | break; |
sugihara0312 | 0:b54d41dacf49 | 73 | } |
sugihara0312 | 0:b54d41dacf49 | 74 | |
sugihara0312 | 0:b54d41dacf49 | 75 | }else { |
sugihara0312 | 0:b54d41dacf49 | 76 | pc.printf("False_No_Data2\r\n"); |
sugihara0312 | 0:b54d41dacf49 | 77 | wait(1); |
sugihara0312 | 0:b54d41dacf49 | 78 | }//データ取得失敗を表示、通信、1秒待機 |
sugihara0312 | 0:b54d41dacf49 | 79 | |
sugihara0312 | 0:b54d41dacf49 | 80 | } |
aihara | 2:19c8f66ab10f | 81 | |
sugihara0312 | 0:b54d41dacf49 | 82 | pc.printf("成功\n"); |
sugihara0312 | 0:b54d41dacf49 | 83 | return 0; |
sugihara0312 | 0:b54d41dacf49 | 84 | } |