ニドキング

Dependencies:   mbed BMP180

Committer:
naruu
Date:
Sun Dec 20 04:24:53 2020 +0000
Revision:
12:df7a452c641a
Parent:
11:f84e10ae2a3b
a

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