Haruki Sashida
/
ARLISS_2018_ver2
ARLISS 2018 parachute board
main.cpp@4:565b7641b0dc, 2022-07-21 (annotated)
- Committer:
- sashida_h
- Date:
- Thu Jul 21 07:17:01 2022 +0000
- Revision:
- 4:565b7641b0dc
- Parent:
- 3:24a8901befb6
Nichrome ON after 20seconds
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mikawataru | 1:b9ea35d93329 | 1 | /* |
mikawataru | 1:b9ea35d93329 | 2 | 説明 |
mikawataru | 1:b9ea35d93329 | 3 | Nucleo-F303K8とBMP180を使った気温・気圧・高度計算のサンプルプログラム |
mikawataru | 1:b9ea35d93329 | 4 | |
mikawataru | 1:b9ea35d93329 | 5 | ライブラリ |
mikawataru | 1:b9ea35d93329 | 6 | https://developer.mbed.org/users/spiridion/code/BMP180/ |
mikawataru | 1:b9ea35d93329 | 7 | |
mikawataru | 1:b9ea35d93329 | 8 | 以下ピン配置 |
mikawataru | 1:b9ea35d93329 | 9 | Nucleo BMP180 |
mikawataru | 1:b9ea35d93329 | 10 | GND-----GND-------0V |
mikawataru | 1:b9ea35d93329 | 11 | +3V3----VIN |
mikawataru | 1:b9ea35d93329 | 12 | D4------SDA |
mikawataru | 1:b9ea35d93329 | 13 | D5------SCL |
mikawataru | 1:b9ea35d93329 | 14 | |
mikawataru | 1:b9ea35d93329 | 15 | */ |
mikawataru | 0:0ff20d8e9090 | 16 | #include "mbed.h" |
mikawataru | 0:0ff20d8e9090 | 17 | #include "math.h" |
mikawataru | 0:0ff20d8e9090 | 18 | #include "BMP180.h" |
mikawataru | 0:0ff20d8e9090 | 19 | #define p0 1013.25f//海面気圧 |
mikawataru | 0:0ff20d8e9090 | 20 | |
sashida_h | 4:565b7641b0dc | 21 | DigitalOut led(dp28); |
sashida_h | 4:565b7641b0dc | 22 | DigitalOut led2(LED2); |
sashida_h | 4:565b7641b0dc | 23 | DigitalIn flyp(dp1); |
sashida_h | 4:565b7641b0dc | 24 | DigitalOut cutp1(dp10); |
sashida_h | 4:565b7641b0dc | 25 | DigitalOut cutp2(dp6); |
sashida_h | 4:565b7641b0dc | 26 | BMP180 bmp(dp5, dp27); |
sashida_h | 4:565b7641b0dc | 27 | Serial dbg(dp16,dp15); |
mikawataru | 0:0ff20d8e9090 | 28 | Timer timer; |
mikawataru | 0:0ff20d8e9090 | 29 | |
mikawataru | 0:0ff20d8e9090 | 30 | float getAlt(float press, float temp); |
mikawataru | 0:0ff20d8e9090 | 31 | |
mikawataru | 0:0ff20d8e9090 | 32 | int main() { |
sashida_h | 4:565b7641b0dc | 33 | dbg.printf("Hello\r\n"); |
sashida_h | 4:565b7641b0dc | 34 | cutp1 = 0; |
sashida_h | 4:565b7641b0dc | 35 | cutp2 = 0; |
sashida_h | 4:565b7641b0dc | 36 | float pressure,temperature,alt_gnd,alt_now; |
sashida_h | 4:565b7641b0dc | 37 | float alt[30]; |
sashida_h | 4:565b7641b0dc | 38 | float time; |
sashida_h | 4:565b7641b0dc | 39 | int i = 0; |
sashida_h | 4:565b7641b0dc | 40 | bmp.Initialize(64,BMP180_OSS_ULTRA_LOW_POWER); //BMP180初期化 |
sashida_h | 4:565b7641b0dc | 41 | alt_gnd = 0.0; |
sashida_h | 4:565b7641b0dc | 42 | int time_cnt = 0; |
sashida_h | 4:565b7641b0dc | 43 | //地上高度取得(10回の平均値) |
sashida_h | 4:565b7641b0dc | 44 | for(i =0; i < 10; i++){ |
sashida_h | 4:565b7641b0dc | 45 | bmp.ReadData(&temperature,&pressure); |
sashida_h | 4:565b7641b0dc | 46 | alt_gnd = alt_gnd + getAlt(pressure,temperature); |
sashida_h | 4:565b7641b0dc | 47 | } |
sashida_h | 4:565b7641b0dc | 48 | alt_gnd = alt_gnd/10.0; |
sashida_h | 4:565b7641b0dc | 49 | dbg.printf("GND:%f\r\n",alt_gnd); |
sashida_h | 4:565b7641b0dc | 50 | led = 1; |
sashida_h | 4:565b7641b0dc | 51 | |
sashida_h | 4:565b7641b0dc | 52 | //フライトピン抜けてたらエラー |
sashida_h | 4:565b7641b0dc | 53 | if(flyp == 0){ |
sashida_h | 4:565b7641b0dc | 54 | while(1){ |
sashida_h | 4:565b7641b0dc | 55 | led = !led; |
sashida_h | 4:565b7641b0dc | 56 | led2 = !led2; |
sashida_h | 4:565b7641b0dc | 57 | wait(0.2); |
sashida_h | 4:565b7641b0dc | 58 | } |
sashida_h | 4:565b7641b0dc | 59 | } |
sashida_h | 4:565b7641b0dc | 60 | dbg.printf("standby\r\n"); |
sashida_h | 4:565b7641b0dc | 61 | while(flyp == 1); //フライトピン抜けるまで待つ |
sashida_h | 4:565b7641b0dc | 62 | dbg.printf("rerease\r\n"); |
mikawataru | 0:0ff20d8e9090 | 63 | timer.start(); |
sashida_h | 4:565b7641b0dc | 64 | wait(5.0); //落ち着くまで待つ |
sashida_h | 4:565b7641b0dc | 65 | led2 = 1; |
sashida_h | 4:565b7641b0dc | 66 | led = 0; |
sashida_h | 4:565b7641b0dc | 67 | i = 0; |
mikawataru | 0:0ff20d8e9090 | 68 | |
sashida_h | 4:565b7641b0dc | 69 | //高度が50m以下または10分たつ,になるまで着地判定はしない. |
sashida_h | 4:565b7641b0dc | 70 | while(1){ |
sashida_h | 4:565b7641b0dc | 71 | bmp.ReadData(&temperature,&pressure); |
sashida_h | 4:565b7641b0dc | 72 | alt_now = getAlt(pressure,temperature); |
sashida_h | 4:565b7641b0dc | 73 | dbg.printf("NOW:%f[m]\r\n",alt_now - alt_gnd); |
sashida_h | 4:565b7641b0dc | 74 | time = timer.read(); |
sashida_h | 4:565b7641b0dc | 75 | if(time > 10){ |
sashida_h | 4:565b7641b0dc | 76 | time_cnt ++; |
sashida_h | 4:565b7641b0dc | 77 | timer.reset(); |
sashida_h | 4:565b7641b0dc | 78 | wait(0.1); |
sashida_h | 4:565b7641b0dc | 79 | timer.start(); |
sashida_h | 4:565b7641b0dc | 80 | } |
sashida_h | 4:565b7641b0dc | 81 | if(time_cnt == 1) break; |
sashida_h | 4:565b7641b0dc | 82 | } |
sashida_h | 4:565b7641b0dc | 83 | dbg.printf("under50m\r\n"); |
sashida_h | 4:565b7641b0dc | 84 | float land = 0.0; |
sashida_h | 4:565b7641b0dc | 85 | int cnt = 0; |
sashida_h | 4:565b7641b0dc | 86 | led = 1; |
sashida_h | 4:565b7641b0dc | 87 | //30分たつ又は高度変化が1mいない,で着地判定 |
mikawataru | 0:0ff20d8e9090 | 88 | while(1) { |
mikawataru | 0:0ff20d8e9090 | 89 | bmp.ReadData(&temperature,&pressure); |
mikawataru | 0:0ff20d8e9090 | 90 | time = timer.read(); |
sashida_h | 4:565b7641b0dc | 91 | if(time > 10){ |
sashida_h | 4:565b7641b0dc | 92 | time_cnt ++; |
sashida_h | 4:565b7641b0dc | 93 | timer.reset(); |
sashida_h | 4:565b7641b0dc | 94 | wait(0.1); |
sashida_h | 4:565b7641b0dc | 95 | timer.start(); |
sashida_h | 4:565b7641b0dc | 96 | } |
sashida_h | 4:565b7641b0dc | 97 | alt[i] = getAlt(pressure,temperature); |
sashida_h | 4:565b7641b0dc | 98 | dbg.printf("alt[%d]:%f",i,alt[i]); |
sashida_h | 4:565b7641b0dc | 99 | if(i > 0){ |
sashida_h | 4:565b7641b0dc | 100 | land = alt[i-1] - alt[i]; |
sashida_h | 4:565b7641b0dc | 101 | if(land < 0) land = land * (-1); //-を+に |
sashida_h | 4:565b7641b0dc | 102 | if(land < 1.0) cnt++; //約1秒前との高度差が1m未満の場合,カウント |
sashida_h | 4:565b7641b0dc | 103 | } |
sashida_h | 4:565b7641b0dc | 104 | if(i == 30){ //30カウント(約30秒)でカウントリセット |
sashida_h | 4:565b7641b0dc | 105 | i = 0; |
sashida_h | 4:565b7641b0dc | 106 | cnt = 0; |
sashida_h | 4:565b7641b0dc | 107 | } |
sashida_h | 4:565b7641b0dc | 108 | dbg.printf("cnt:%d\r\n",cnt); |
sashida_h | 4:565b7641b0dc | 109 | if(time_cnt == 2) break; //30カウントのうち10カウント又は30分経過で着地判定 |
sashida_h | 4:565b7641b0dc | 110 | wait(1.0); |
sashida_h | 4:565b7641b0dc | 111 | i ++; |
sashida_h | 4:565b7641b0dc | 112 | } |
sashida_h | 4:565b7641b0dc | 113 | timer.stop(); |
sashida_h | 4:565b7641b0dc | 114 | //ニクロム線に10秒ずつ電流流すを繰り返す |
sashida_h | 4:565b7641b0dc | 115 | while(1){ |
sashida_h | 4:565b7641b0dc | 116 | cutp1 = 1; |
sashida_h | 4:565b7641b0dc | 117 | led = 0; |
sashida_h | 4:565b7641b0dc | 118 | wait(60); |
sashida_h | 4:565b7641b0dc | 119 | cutp1 = 0; |
mikawataru | 0:0ff20d8e9090 | 120 | wait(1); |
sashida_h | 4:565b7641b0dc | 121 | cutp2 = 1; |
sashida_h | 4:565b7641b0dc | 122 | led = 1; |
sashida_h | 4:565b7641b0dc | 123 | wait(60); |
sashida_h | 4:565b7641b0dc | 124 | led2 = !led2; |
sashida_h | 4:565b7641b0dc | 125 | cutp2 = 0; |
sashida_h | 4:565b7641b0dc | 126 | wait(1); |
sashida_h | 4:565b7641b0dc | 127 | while(flyp == 1){ |
sashida_h | 4:565b7641b0dc | 128 | dbg.printf("stop\r\n"); |
sashida_h | 4:565b7641b0dc | 129 | wait(1); |
sashida_h | 4:565b7641b0dc | 130 | } |
mikawataru | 0:0ff20d8e9090 | 131 | } |
mikawataru | 0:0ff20d8e9090 | 132 | } |
mikawataru | 0:0ff20d8e9090 | 133 | |
mikawataru | 0:0ff20d8e9090 | 134 | float getAlt(float press, float temp){ |
mikawataru | 0:0ff20d8e9090 | 135 | return (pow((p0/press), (1.0f/5.257f))-1.0f)*(temp+273.15f)/0.0065f; |
mikawataru | 0:0ff20d8e9090 | 136 | } |