ARLISS 2018 parachute board

Dependencies:   mbed BMP180

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

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 "math.h"
00018 #include "BMP180.h"
00019 #define p0 1013.25f//海面気圧
00020 
00021 DigitalOut led(dp28);
00022 DigitalOut led2(LED2);
00023 DigitalIn flyp(dp1);
00024 DigitalOut cutp1(dp10);
00025 DigitalOut cutp2(dp6);
00026 BMP180  bmp(dp5, dp27);
00027 Serial  dbg(dp16,dp15);
00028 Timer timer;
00029 
00030 float getAlt(float press, float temp);
00031 
00032 int main() {
00033     dbg.printf("Hello\r\n");
00034     cutp1 = 0;
00035     cutp2 = 0;
00036     float pressure,temperature,alt_gnd,alt_now;
00037     float alt[30];
00038     float time;
00039     int i = 0;   
00040     bmp.Initialize(64,BMP180_OSS_ULTRA_LOW_POWER);  //BMP180初期化
00041     alt_gnd = 0.0;
00042     int time_cnt = 0;
00043     //地上高度取得(10回の平均値)
00044     for(i =0; i < 10; i++){
00045         bmp.ReadData(&temperature,&pressure);
00046         alt_gnd = alt_gnd + getAlt(pressure,temperature);
00047     }
00048     alt_gnd = alt_gnd/10.0;
00049     dbg.printf("GND:%f\r\n",alt_gnd);
00050     led = 1;
00051     
00052     //フライトピン抜けてたらエラー
00053     if(flyp == 0){
00054         while(1){
00055             led = !led;
00056             led2 = !led2;
00057             wait(0.2);
00058         }
00059     }
00060     dbg.printf("standby\r\n");
00061     while(flyp == 1);   //フライトピン抜けるまで待つ
00062     dbg.printf("rerease\r\n");
00063     timer.start();
00064     wait(5.0);  //落ち着くまで待つ
00065     led2 = 1;
00066     led = 0;
00067     i = 0;
00068     
00069     //高度が50m以下または10分たつ,になるまで着地判定はしない.
00070     while(1){
00071          bmp.ReadData(&temperature,&pressure);
00072          alt_now = getAlt(pressure,temperature);
00073          dbg.printf("NOW:%f[m]\r\n",alt_now - alt_gnd);
00074          time = timer.read();
00075          if(time > 10){
00076              time_cnt ++;
00077              timer.reset();
00078              wait(0.1);
00079              timer.start();
00080         }
00081          if(time_cnt == 1) break;
00082     }
00083     dbg.printf("under50m\r\n");
00084     float land = 0.0;
00085     int cnt = 0;
00086     led = 1;
00087     //30分たつ又は高度変化が1mいない,で着地判定
00088     while(1) {
00089         bmp.ReadData(&temperature,&pressure);
00090         time = timer.read();
00091         if(time > 10){
00092              time_cnt ++;
00093              timer.reset();
00094              wait(0.1);
00095              timer.start();
00096         }
00097         alt[i] = getAlt(pressure,temperature);
00098         dbg.printf("alt[%d]:%f",i,alt[i]);
00099         if(i > 0){
00100             land = alt[i-1] - alt[i];
00101             if(land < 0) land = land * (-1);    //-を+に
00102             if(land < 1.0) cnt++;   //約1秒前との高度差が1m未満の場合,カウント
00103         }
00104         if(i == 30){    //30カウント(約30秒)でカウントリセット
00105             i = 0;
00106             cnt = 0;
00107         }
00108         dbg.printf("cnt:%d\r\n",cnt);
00109         if(time_cnt == 2) break; //30カウントのうち10カウント又は30分経過で着地判定
00110         wait(1.0);
00111         i ++;
00112     }
00113     timer.stop();
00114     //ニクロム線に10秒ずつ電流流すを繰り返す
00115     while(1){
00116         cutp1 = 1;
00117         led = 0;
00118         wait(60);
00119         cutp1 = 0;
00120         wait(1);
00121         cutp2 = 1;
00122         led = 1;
00123         wait(60);
00124         led2 = !led2;
00125         cutp2 = 0;
00126         wait(1);
00127         while(flyp == 1){
00128             dbg.printf("stop\r\n");
00129             wait(1);
00130             }
00131     }
00132 }
00133 
00134 float getAlt(float press, float temp){
00135     return (pow((p0/press), (1.0f/5.257f))-1.0f)*(temp+273.15f)/0.0065f;
00136 }