2022ARLISS_TMU
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 /* 00002 説明 00003 Nucleo-F303K8とBMP180を使った気温・気圧・高度計算のサンプルプログラム 00004 00005 ライブラリ 00006 https://developer.mbed.org/users/spiridion/code/BMP180/ 00007 00008 以下ピン配置 00009 Nucleo BMP180 00010 GND-----GND-------0V 00011 +3V3----VIN 00012 D4------SDA 00013 D5------SCL 00014 00015 */ 00016 #include "mbed.h" 00017 #include "BMP180.h" 00018 #define p0 1013.25f//海面気圧 00019 00020 DigitalOut led(PA_8); 00021 DigitalIn flyp(PF_0); 00022 DigitalOut cutp1(PB_0); 00023 DigitalOut cutp2(PB_1); 00024 BMP180 bmp(PB_7, PB_6); 00025 Serial dbg(PA_9, PA_10); 00026 Timer timer; 00027 00028 float getAlt(float press, float temp); 00029 00030 #define GUDGE_WAIT_TIME 600 //[sec] 60*10 00031 #define GUDGE_LIMIT_TIME 1800 00032 #define CUT_ON_TIME 50 //ニクロム線の電源ON時間 [sec] 00033 #define GUDGE_HIGHT 2 //[m] 00034 00035 int main() { 00036 led = 1; 00037 dbg.printf("Hello\r\n"); 00038 cutp1 = 0; 00039 cutp2 = 0; 00040 float pressure,temperature,alt_gnd,alt_now; 00041 float alt[30]; 00042 float time; 00043 int i = 0; 00044 bmp.Initialize(64,BMP180_OSS_ULTRA_LOW_POWER); //BMP180初期化 00045 alt_gnd = 0.0; 00046 int time_cnt = 0; 00047 //地上高度取得(10回の平均値) 00048 for(i =0; i < 10; i++){ 00049 bmp.ReadData(&temperature,&pressure); 00050 alt_gnd = alt_gnd + getAlt(pressure,temperature); 00051 } 00052 alt_gnd = alt_gnd/10.0; 00053 dbg.printf("GND:%f",alt_gnd); 00054 dbg.printf("standby\r\n"); 00055 led = 0; 00056 00057 i=0; 00058 00059 while(i < 10){ //フライトピン抜けるまで待つ 00060 if(flyp == 0){ 00061 i++; 00062 }else{ 00063 i = 0; 00064 } 00065 wait(0.5); 00066 dbg.printf("%d",i); 00067 led = !led; 00068 } 00069 00070 dbg.printf("rerease\r\n"); 00071 timer.start(); 00072 wait(5.0); //落ち着くまで待つ 00073 led = 0; 00074 i = 1; 00075 int cnt = 0; 00076 00077 //高度が50m以下または10分たつ,になるまで着地判定はしない. 00078 while(1){ 00079 wait(0.1); 00080 bmp.ReadData(&temperature,&pressure); 00081 alt_now = getAlt(pressure,temperature) - alt_gnd; 00082 dbg.printf("NOW:%f\r\n",alt_now); 00083 time = timer.read(); 00084 00085 if(alt_now < GUDGE_HIGHT){ 00086 cnt++; 00087 } 00088 00089 if(time > GUDGE_WAIT_TIME && cnt > 10){ 00090 time_cnt ++; 00091 timer.reset(); 00092 wait(0.1); 00093 timer.start(); 00094 } 00095 if(time_cnt == 1) break; 00096 } 00097 dbg.printf("under10m\r\n"); 00098 led = 0; 00099 wait(2.0); 00100 float land = 0.0; 00101 cnt = 0; 00102 led = 1; 00103 //30分たつ又は高度変化が1mいない,で着地判定 00104 while(1) { 00105 bmp.ReadData(&temperature,&pressure); 00106 time = timer.read(); 00107 if(time > GUDGE_LIMIT_TIME){ 00108 time_cnt ++; 00109 timer.reset(); 00110 wait(0.1); 00111 timer.start(); 00112 } 00113 alt[i] = getAlt(pressure,temperature); 00114 dbg.printf("alt[%d]:%f",i,alt[i]); 00115 if(i > 0){ 00116 land = alt[i-1] - alt[i]; 00117 if(land < 0.0f) land = land * (-1.0f); //-を+に 00118 if(land < 1.0f) cnt++; //約1秒前との高度差が1m未満の場合,カウント 00119 } 00120 if(i == 30){ //30カウント(約30秒)でカウントリセット 00121 i = 0; 00122 cnt = 0; 00123 } 00124 dbg.printf("cnt:%d\r\n",cnt); 00125 if(time_cnt == 2 || cnt == 10) break; //30カウントのうち10カウント又は30分経過で着地判定 00126 wait(1.0); 00127 i ++; 00128 } 00129 timer.stop(); 00130 //ニクロム線に10秒ずつ電流流すを繰り返す 00131 while(1){ 00132 cutp1 = 1; 00133 //for(i = 0;i) 00134 led = 0; 00135 wait(CUT_ON_TIME); 00136 cutp1 = 0; 00137 wait(1); 00138 cutp2 = 1; 00139 led = 1; 00140 wait(CUT_ON_TIME); 00141 cutp2 = 0; 00142 wait(1); 00143 while(flyp == 1){ 00144 dbg.printf("stop\r\n"); 00145 wait(1); 00146 } 00147 } 00148 } 00149 00150 float getAlt(float press, float temp){ 00151 return (pow((p0/press), (1.0f/5.257f))-1.0f)*(temp+273.15f)/0.0065f; 00152 }
Generated on Thu Sep 8 2022 16:45:44 by
1.7.2