ルンパッパ

Dependencies:   mbed BMP180

Committer:
naruu
Date:
Thu Dec 17 13:32:46 2020 +0000
Revision:
2:20304b3cea67
Parent:
1:1dab888e1f3c
Child:
3:3a62c5ccfddb
koajfa;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
naruu 0:5cddbcb7193f 1 #include "mbed.h"
naruu 0:5cddbcb7193f 2 #include "TB6612.h"
naruu 1:1dab888e1f3c 3 #include "getGPS.h"
naruu 1:1dab888e1f3c 4 #include "BMP180.h"
naruu 1:1dab888e1f3c 5 #include <stdio.h>
naruu 2:20304b3cea67 6 #include <math.h>
naruu 1:1dab888e1f3c 7
naruu 1:1dab888e1f3c 8 #define PIN_SDA D4
naruu 1:1dab888e1f3c 9 #define PIN_SCL D5
naruu 2:20304b3cea67 10
naruu 1:1dab888e1f3c 11 Serial pc(SERIAL_TX,SERIAL_RX,921600);//通信
naruu 1:1dab888e1f3c 12 Serial xbee(D1,D0);//Xbeeのピン
naruu 1:1dab888e1f3c 13 DigitalOut FET(D9);//FETのピン
naruu 1:1dab888e1f3c 14 DigitalIn flight(D6); //フライトピンのピン
naruu 1:1dab888e1f3c 15 DigitalOut SW(D7);//フライトピンの電圧降下ピン
naruu 1:1dab888e1f3c 16 TB6612 motor(D7,D9,D11);//モータードライバーのピン
naruu 2:20304b3cea67 17 GPS gps (D1,D0);
naruu 1:1dab888e1f3c 18
naruu 1:1dab888e1f3c 19
naruu 1:1dab888e1f3c 20 int main(){
naruu 1:1dab888e1f3c 21 float x8;
naruu 1:1dab888e1f3c 22 FET=0;
naruu 1:1dab888e1f3c 23 SW=1;
naruu 1:1dab888e1f3c 24 flight==1;//フライトピンがついている
naruu 1:1dab888e1f3c 25
naruu 1:1dab888e1f3c 26
naruu 1:1dab888e1f3c 27 while(1) {
naruu 1:1dab888e1f3c 28 if(flight==1) {
naruu 1:1dab888e1f3c 29 wait(1);
naruu 1:1dab888e1f3c 30 }//フライトピンがついているとき1秒待機
naruu 1:1dab888e1f3c 31
naruu 1:1dab888e1f3c 32 else{
naruu 1:1dab888e1f3c 33 if(flight==1) {
naruu 1:1dab888e1f3c 34 wait(1);
naruu 1:1dab888e1f3c 35 }
naruu 1:1dab888e1f3c 36 else{
naruu 1:1dab888e1f3c 37
naruu 1:1dab888e1f3c 38 SW = 0;
naruu 1:1dab888e1f3c 39 xbee.printf("やったぞおおおおおおおおお!\n");
naruu 2:20304b3cea67 40
naruu 1:1dab888e1f3c 41 break;
naruu 1:1dab888e1f3c 42 }
naruu 1:1dab888e1f3c 43 }
naruu 1:1dab888e1f3c 44 }
naruu 1:1dab888e1f3c 45 BMP180 bmp180(PIN_SDA,PIN_SCL);
naruu 1:1dab888e1f3c 46 float pressure,temperature,altitude;//気圧,気温,高度
naruu 2:20304b3cea67 47 int n;
naruu 1:1dab888e1f3c 48 xbee.printf("\rstart!\n\r");//気圧センサースタート
naruu 1:1dab888e1f3c 49 bmp180.Initialize(27,BMP180_OSS_ULTRA_HIGH_RESOLUTION);//27は府大の海抜高度
naruu 1:1dab888e1f3c 50 xbee.printf("initialization complete!\n\r");//初期化完了
naruu 1:1dab888e1f3c 51
naruu 1:1dab888e1f3c 52 while(1){
naruu 1:1dab888e1f3c 53 if(bmp180.ReadData(&temperature,&pressure)){
naruu 1:1dab888e1f3c 54 float x4,x5,x6,x7,a,b;
naruu 1:1dab888e1f3c 55 a = pressure;
naruu 1:1dab888e1f3c 56 b = temperature;
naruu 1:1dab888e1f3c 57 x4 = 1019.11 / a; //海面気圧を気圧でわる
naruu 1:1dab888e1f3c 58 x5 = powf(x4, 0.1902225); //5.257ぶんの1
naruu 1:1dab888e1f3c 59 x6 = 273.15 + b; //絶対温度
naruu 1:1dab888e1f3c 60 x7 = (x5 - 1) * x6;
naruu 1:1dab888e1f3c 61 x8 = x7 / 0.0065;
naruu 1:1dab888e1f3c 62 altitude = x8;
naruu 1:1dab888e1f3c 63
naruu 1:1dab888e1f3c 64
naruu 1:1dab888e1f3c 65 xbee.printf("Altitude(m)\t:%.3f\n\r",altitude);
naruu 1:1dab888e1f3c 66 xbee.printf("--------------------------------\n\r");
naruu 2:20304b3cea67 67 wait(3);
naruu 1:1dab888e1f3c 68 break;
naruu 2:20304b3cea67 69 n=0;
naruu 1:1dab888e1f3c 70 }else{
naruu 1:1dab888e1f3c 71 xbee.printf("NO DATA\n\r");
naruu 1:1dab888e1f3c 72 xbee.printf("---------------------------\n\r");
naruu 1:1dab888e1f3c 73 wait(1);
naruu 1:1dab888e1f3c 74 }
naruu 1:1dab888e1f3c 75 }
naruu 1:1dab888e1f3c 76 while(1){
naruu 1:1dab888e1f3c 77 if(bmp180.ReadData(&temperature,&pressure)){
naruu 1:1dab888e1f3c 78 float y4,y5,y6,y7,y8,c,d;
naruu 1:1dab888e1f3c 79 float speed;
naruu 1:1dab888e1f3c 80
naruu 1:1dab888e1f3c 81 c = pressure;
naruu 1:1dab888e1f3c 82 d = temperature;
naruu 1:1dab888e1f3c 83 y4 = 1019.11 / c; //海面気圧を気圧でわる
naruu 1:1dab888e1f3c 84 y5 = powf(y4,0.1902225);
naruu 1:1dab888e1f3c 85 y6 = 273.15 + d;
naruu 1:1dab888e1f3c 86 y7 = (y5 - 1) * y6;
naruu 1:1dab888e1f3c 87 y8 = y7 / 0.0065;
naruu 1:1dab888e1f3c 88 altitude = y8;
naruu 2:20304b3cea67 89 speed = (x8 - y8)/(float)(3+n);//値が取得でた場合は,3秒間の速さをだし,値が取得できなかった場合は3+n秒(nは値が取得できなかった回数)の速さをだす
naruu 1:1dab888e1f3c 90
naruu 1:1dab888e1f3c 91 xbee.printf("Altitude(m)\t:%.3f\n\r",altitude);
naruu 1:1dab888e1f3c 92 xbee.printf("Speed(m/s)\t:%.3f\n\r",speed);
naruu 1:1dab888e1f3c 93 xbee.printf("--------------------------------\n\r");
naruu 1:1dab888e1f3c 94 x8 = y8;
naruu 2:20304b3cea67 95 n=0;
naruu 2:20304b3cea67 96 wait(3);
naruu 1:1dab888e1f3c 97 if(speed<=0){
naruu 1:1dab888e1f3c 98 break;
naruu 2:20304b3cea67 99 }
naruu 2:20304b3cea67 100 }else{
naruu 1:1dab888e1f3c 101 xbee.printf("NO DATA\n\r");
naruu 2:20304b3cea67 102 ++n;
naruu 1:1dab888e1f3c 103 wait(1);
naruu 1:1dab888e1f3c 104 }
naruu 1:1dab888e1f3c 105 }
naruu 2:20304b3cea67 106 /*speedが0以下になったらFETに20秒電流を流してその後電流を止める*/
naruu 1:1dab888e1f3c 107 FET=1;
naruu 1:1dab888e1f3c 108 wait(20);
naruu 1:1dab888e1f3c 109 FET=0;
naruu 2:20304b3cea67 110 motor = 100;
naruu 2:20304b3cea67 111 double a;
naruu 1:1dab888e1f3c 112 double b;
naruu 1:1dab888e1f3c 113 double distance;
naruu 1:1dab888e1f3c 114
naruu 2:20304b3cea67 115 xbee.printf("GPS begin\n");
naruu 1:1dab888e1f3c 116
naruu 1:1dab888e1f3c 117 while(1){
naruu 1:1dab888e1f3c 118 if(gps.getgps()){
naruu 1:1dab888e1f3c 119 /*a,bを緯度経度の初期値で初期化*/
naruu 1:1dab888e1f3c 120 a=gps.latitude;
naruu 1:1dab888e1f3c 121 b=gps.longitude;
naruu 2:20304b3cea67 122 xbee.printf("(%lf,%lf)\r\n",gps.latitude,gps.longitude);//緯度と経度を表示
naruu 2:20304b3cea67 123 xbee.printf("--------------------------------\n\r");
naruu 1:1dab888e1f3c 124 break;
naruu 1:1dab888e1f3c 125 }else{
naruu 2:20304b3cea67 126 xbee.printf("Fault_No_Data\r\n");
naruu 1:1dab888e1f3c 127 wait(1);
naruu 1:1dab888e1f3c 128 }
naruu 2:20304b3cea67 129 }
naruu 2:20304b3cea67 130 while(1){
naruu 1:1dab888e1f3c 131 if(gps.getgps()){
naruu 2:20304b3cea67 132 xbee.printf("(%lf,%lf)\n\r",gps.latitude,gps.longitude);//緯度と経度を表示
naruu 2:20304b3cea67 133 xbee.printf("--------------------------------\n\r");
naruu 1:1dab888e1f3c 134
naruu 1:1dab888e1f3c 135 /*ここから距離の計算*/
naruu 1:1dab888e1f3c 136 /*c、dを得た緯度経度の値で初期化*/
naruu 1:1dab888e1f3c 137 double c;
naruu 1:1dab888e1f3c 138 double d;
naruu 1:1dab888e1f3c 139 c=gps.latitude;
naruu 1:1dab888e1f3c 140 d=gps.longitude;
naruu 1:1dab888e1f3c 141
naruu 1:1dab888e1f3c 142 const double pi=3.14159265359;//円周率
naruu 1:1dab888e1f3c 143
naruu 1:1dab888e1f3c 144 /*ラジアンに変換*/
naruu 1:1dab888e1f3c 145 double theta_a=a*pi/180;
naruu 1:1dab888e1f3c 146 double theta_b=b*pi/180;
naruu 1:1dab888e1f3c 147 double theta_c=c*pi/180;
naruu 1:1dab888e1f3c 148 double theta_d=d*pi/180;
naruu 1:1dab888e1f3c 149
naruu 1:1dab888e1f3c 150 double e=sin(theta_a)*sin(theta_c)+cos(theta_a)*cos(theta_c)*cos(theta_b-theta_d);//2点間のなす角を求める
naruu 1:1dab888e1f3c 151 double theta_r=acos(e);
naruu 1:1dab888e1f3c 152
naruu 1:1dab888e1f3c 153 const double earth_radius=6378140;//赤道半径
naruu 1:1dab888e1f3c 154
naruu 1:1dab888e1f3c 155 distance=earth_radius*theta_r;//距離の計算
naruu 1:1dab888e1f3c 156
naruu 2:20304b3cea67 157 /*距離が25m以上なら表示、通信*/
naruu 2:20304b3cea67 158 if(distance>=30){
naruu 2:20304b3cea67 159 pc.printf("run over 20m");
naruu 1:1dab888e1f3c 160 motor=0;
naruu 2:20304b3cea67 161 xbee.printf("run over 20m");
naruu 1:1dab888e1f3c 162 break;
naruu 1:1dab888e1f3c 163 }
naruu 1:1dab888e1f3c 164
naruu 1:1dab888e1f3c 165 }else {
naruu 1:1dab888e1f3c 166 xbee.printf("False_No_Data\r\n");
naruu 1:1dab888e1f3c 167 pc.printf("False_No_Date\r\n");
naruu 1:1dab888e1f3c 168 wait(1);
naruu 1:1dab888e1f3c 169 }//データ取得失敗を表示、通信、1秒待機
naruu 1:1dab888e1f3c 170
naruu 1:1dab888e1f3c 171 }
naruu 2:20304b3cea67 172 xbee.printf("成功\n");
naruu 0:5cddbcb7193f 173 return 0;
naruu 0:5cddbcb7193f 174 }