201903_14ISEで実際に使用した開放用プログラム. 使用マイコンがNUCLES-F303K8なので注意

Dependencies:   mbed Madgwick MPU6050 Kalman BMP180

Committer:
sashida_h
Date:
Wed Mar 13 15:12:17 2019 +0000
Revision:
19:1e1a9f1829a9
Parent:
18:37d747f51c0a
Child:
20:89fdec3731f8
2019_0314_0012;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mikawataru 0:0ff20d8e9090 1 #include "mbed.h"
Yukina 4:4b3ae90ec778 2 #include "MPU6050.h"
mikawataru 0:0ff20d8e9090 3 #include "BMP180.h"
mikawataru 0:0ff20d8e9090 4
Yukina 5:f6e956e8a060 5 /*しきい値など*/
sashida_h 13:3e6411bfd581 6 #define CNT_LAUNCH 10 //発射判定するときのしきい値
sashida_h 11:c90db1500720 7 #define ACC_JUDGE_LAUNCH 3.0 //発射判定の合成加速度のしきい値
sashida_h 8:15a1b22df82f 8 #define TIME_BURNING 6 //開放判定しない時間(燃焼時間)
sashida_h 19:1e1a9f1829a9 9 #define ALT_JUDGE_OPEN 5 //落下判定のカウントを1増やす高度差
sashida_h 8:15a1b22df82f 10 #define TIME_OPEN 25 //強制的に開放させる時間
sashida_h 11:c90db1500720 11 #define TIME_SEND 1.0 //無線送信する間隔
sashida_h 19:1e1a9f1829a9 12 #define CNT_JUDGE 10 //頂点判定する時に落下のカウント数
sashida_h 11:c90db1500720 13 #define NUM_CNT_MEDIAN 10 //中央値をとる個数
sashida_h 11:c90db1500720 14 #define RATE_GPS 1.0 //GPSのTickerを動作させるタイミング
sashida_h 11:c90db1500720 15 #define p0 1013.25f //海面気圧
Yukina 5:f6e956e8a060 16
sashida_h 7:9953d922499d 17 MPU6050 mpu(PB_7,PB_6);
sashida_h 7:9953d922499d 18 BMP180 bmp(PB_7,PB_6);
sashida_h 7:9953d922499d 19 Serial pc(PA_2, PA_3);
sashida_h 7:9953d922499d 20 Serial gps(PA_9, PA_10);
sashida_h 9:42b4d337d4cc 21 DigitalOut myled(PA_15);
sashida_h 10:1a626929850e 22 DigitalOut ES920_RST(PA_5);
sashida_h 11:c90db1500720 23 PwmOut servo1(PB_4);
sashida_h 11:c90db1500720 24 PwmOut servo2(PB_5);
sashida_h 11:c90db1500720 25 DigitalOut servo1_signal(PA_0);
sashida_h 11:c90db1500720 26 DigitalOut servo2_signal(PA_1);
Yukina 5:f6e956e8a060 27 Timer timer_open;
Yukina 5:f6e956e8a060 28 Timer timer_data;
Yukina 5:f6e956e8a060 29 Ticker tic_gps;
mikawataru 0:0ff20d8e9090 30
Yukina 5:f6e956e8a060 31 /*自作関数*/
Yukina 5:f6e956e8a060 32 float _getAlt();
Yukina 5:f6e956e8a060 33 float _median(float data[],int num);
Yukina 5:f6e956e8a060 34 void _SendGPS();
Yukina 5:f6e956e8a060 35 float _DMS2DEG(float raw_data);
Yukina 4:4b3ae90ec778 36
Yukina 5:f6e956e8a060 37 enum PHASE{STANDBY=0,LAUNCH=1,RISE=3,FIRE=7,OPEN=15,RECOVERY=9,SEA=6} Phase;
Yukina 4:4b3ae90ec778 38
Yukina 5:f6e956e8a060 39 /*グローバル変数*/
sashida_h 7:9953d922499d 40 //地上高度
sashida_h 7:9953d922499d 41 float alt_gnd;
sashida_h 9:42b4d337d4cc 42 float alt_max;
sashida_h 13:3e6411bfd581 43 char c[516];
sashida_h 9:42b4d337d4cc 44 int i = 0;
sashida_h 13:3e6411bfd581 45 int cnt_gps=0;
sashida_h 9:42b4d337d4cc 46 void main(){
sashida_h 10:1a626929850e 47 ES920_RST = 0;
sashida_h 10:1a626929850e 48 myled =0;
Yukina 5:f6e956e8a060 49 /*ローカル変数*/
Yukina 5:f6e956e8a060 50 float acc[3],acc_buff[10],gyro[3],gyro_buff[10],acc_abs;
sashida_h 9:42b4d337d4cc 51 float alt_buff[10],alt_md;
Yukina 5:f6e956e8a060 52 float time_judge;
Yukina 5:f6e956e8a060 53 int cnt_data=0,cnt_judge=0;
sashida_h 11:c90db1500720 54 float t = 0;
sashida_h 14:f932a8a297ff 55 float t_hour = 0;
sashida_h 14:f932a8a297ff 56 float t_send = 0;
Yukina 5:f6e956e8a060 57 /*センサの初期化等*/
sashida_h 7:9953d922499d 58 pc.baud(38400);
Yukina 5:f6e956e8a060 59 mpu.setAcceleroRange(3);
mikawataru 0:0ff20d8e9090 60 bmp.Initialize(64,BMP180_OSS_ULTRA_LOW_POWER);
Yukina 5:f6e956e8a060 61
Yukina 5:f6e956e8a060 62 /*初期位置の設定*/
Yukina 5:f6e956e8a060 63 mpu.getAccelero(acc);
Yukina 5:f6e956e8a060 64 mpu.getGyro(gyro);
Yukina 5:f6e956e8a060 65
Yukina 5:f6e956e8a060 66 Phase = STANDBY;
sashida_h 7:9953d922499d 67 for(cnt_data=0;cnt_data<NUM_CNT_MEDIAN;cnt_data++){
sashida_h 7:9953d922499d 68 alt_buff[cnt_data] = _getAlt();
sashida_h 7:9953d922499d 69 }
sashida_h 7:9953d922499d 70 alt_gnd = _median(alt_buff,NUM_CNT_MEDIAN);
sashida_h 7:9953d922499d 71 wait(2.0);
sashida_h 12:6a3c0e9075eb 72 pc.printf("0,0,0,0,0\r\n");
sashida_h 8:15a1b22df82f 73 wait(1.0);
sashida_h 9:42b4d337d4cc 74 timer_data.start();
sashida_h 11:c90db1500720 75 servo1_signal = 1;
sashida_h 11:c90db1500720 76 servo2_signal = 1;
sashida_h 13:3e6411bfd581 77 servo1.pulsewidth(0.0015);//close
sashida_h 13:3e6411bfd581 78 servo2.pulsewidth(0.0006);//close
Yukina 5:f6e956e8a060 79 while(1){
Yukina 5:f6e956e8a060 80 switch(Phase){
Yukina 5:f6e956e8a060 81 case STANDBY:
sashida_h 10:1a626929850e 82 /*サーボ入力待ち*/
sashida_h 10:1a626929850e 83 //servo();
sashida_h 14:f932a8a297ff 84 if(pc.readable()){
sashida_h 14:f932a8a297ff 85 c[0]=pc.getc();
sashida_h 14:f932a8a297ff 86 }
sashida_h 14:f932a8a297ff 87 t = timer_data.read();
sashida_h 14:f932a8a297ff 88 if(t > 1800){
sashida_h 18:37d747f51c0a 89 t_hour += 1800;
sashida_h 14:f932a8a297ff 90 timer_data.reset();
sashida_h 14:f932a8a297ff 91 }
sashida_h 14:f932a8a297ff 92 t = t + t_hour;
sashida_h 14:f932a8a297ff 93 t = t/60.0;
sashida_h 14:f932a8a297ff 94
sashida_h 11:c90db1500720 95 if(c[0] == 'o'){
sashida_h 11:c90db1500720 96 myled = 1;
sashida_h 13:3e6411bfd581 97 servo1.pulsewidth(0.0006);//open
sashida_h 13:3e6411bfd581 98 servo2.pulsewidth(0.0015);//open
sashida_h 11:c90db1500720 99 mpu.getAccelero(acc);
sashida_h 12:6a3c0e9075eb 100 //pc.printf("OPEN:%f\r\n",sqrt(pow(acc[0]/9.81,2.0)+pow(acc[1]/9.81,2.0)+pow(acc[2]/9.81,2.0)));
sashida_h 16:6395de1ad89c 101 pc.printf("0,%f,0,%f\r\n",sqrt(pow(acc[0]/9.81,2.0)+pow(acc[1]/9.81,2.0)+pow(acc[2]/9.81,2.0)),t);
sashida_h 11:c90db1500720 102 }
sashida_h 11:c90db1500720 103 if(c[0] == 'l'){
sashida_h 11:c90db1500720 104 myled = 0;
sashida_h 13:3e6411bfd581 105 servo1.pulsewidth(0.0015);
sashida_h 13:3e6411bfd581 106 servo2.pulsewidth(0.0006);
sashida_h 11:c90db1500720 107 mpu.getAccelero(acc);
sashida_h 12:6a3c0e9075eb 108 //pc.printf("LOCK:%f\r\n",sqrt(pow(acc[0]/9.81,2.0)+pow(acc[1]/9.81,2.0)+pow(acc[2]/9.81,2.0)));
sashida_h 16:6395de1ad89c 109 pc.printf("0,%f,0,%f\r\n",sqrt(pow(acc[0]/9.81,2.0)+pow(acc[1]/9.81,2.0)+pow(acc[2]/9.81,2.0)),t);
sashida_h 11:c90db1500720 110 }
sashida_h 10:1a626929850e 111 if(c[0] == 'f'){
sashida_h 10:1a626929850e 112 //pc.printf("flight_mode\r\n");
sashida_h 10:1a626929850e 113 Phase = LAUNCH;
sashida_h 11:c90db1500720 114 servo1_signal = 0;
sashida_h 11:c90db1500720 115 servo2_signal = 0;
sashida_h 18:37d747f51c0a 116 t = 0;
sashida_h 18:37d747f51c0a 117 timer_data.reset();
sashida_h 10:1a626929850e 118 }
sashida_h 13:3e6411bfd581 119 if(c[0] == 'g'){
sashida_h 13:3e6411bfd581 120 pc.printf("9,0,0,0,0\r\n");
sashida_h 13:3e6411bfd581 121 wait(0.5);
sashida_h 13:3e6411bfd581 122 tic_gps.attach(&_SendGPS, 1.0/RATE_GPS);
sashida_h 13:3e6411bfd581 123 Phase = RECOVERY;
sashida_h 13:3e6411bfd581 124 }
Yukina 5:f6e956e8a060 125 break;
sashida_h 8:15a1b22df82f 126
Yukina 5:f6e956e8a060 127 case LAUNCH:
Yukina 5:f6e956e8a060 128 for(cnt_data=0;cnt_data<NUM_CNT_MEDIAN;cnt_data++){
Yukina 5:f6e956e8a060 129 mpu.getAccelero(acc);
Yukina 5:f6e956e8a060 130 acc_buff[cnt_data] = sqrt(pow(acc[0]/9.81,2.0)+pow(acc[1]/9.81,2.0)+pow(acc[2]/9.81,2.0));
Yukina 5:f6e956e8a060 131 }
Yukina 5:f6e956e8a060 132 acc_abs = _median(acc_buff,NUM_CNT_MEDIAN);
sashida_h 8:15a1b22df82f 133 if(timer_data.read() - t > TIME_SEND){
sashida_h 12:6a3c0e9075eb 134 //pc.printf("LAUNCH,acc:%f,time:%3f,cnt:%d\r\n",acc_abs,timer_data.read(),cnt_judge);
sashida_h 16:6395de1ad89c 135 pc.printf("1,%f,0,%3f\r\n",acc_abs,timer_data.read());
sashida_h 13:3e6411bfd581 136 cnt_judge = 0;
sashida_h 8:15a1b22df82f 137 t = timer_data.read();
sashida_h 7:9953d922499d 138 }
Yukina 5:f6e956e8a060 139 /*加速度判定*/
Yukina 5:f6e956e8a060 140 if(acc_abs>ACC_JUDGE_LAUNCH){
Yukina 5:f6e956e8a060 141 cnt_judge++;
Yukina 4:4b3ae90ec778 142 }
sashida_h 13:3e6411bfd581 143 if(cnt_judge==CNT_LAUNCH){
sashida_h 11:c90db1500720 144 servo1_signal = 1;
sashida_h 11:c90db1500720 145 servo1_signal = 1;
Yukina 5:f6e956e8a060 146 cnt_judge=0;
Yukina 5:f6e956e8a060 147 timer_open.start();
sashida_h 7:9953d922499d 148 Phase = RISE;
sashida_h 18:37d747f51c0a 149 timer_data.stop();
Yukina 5:f6e956e8a060 150 }
Yukina 5:f6e956e8a060 151 break;
sashida_h 8:15a1b22df82f 152
Yukina 5:f6e956e8a060 153 case RISE:
Yukina 5:f6e956e8a060 154 while(timer_open.read() < TIME_BURNING){
sashida_h 12:6a3c0e9075eb 155 for(cnt_data=0;cnt_data<NUM_CNT_MEDIAN;cnt_data++){
sashida_h 12:6a3c0e9075eb 156 alt_buff[cnt_data] = _getAlt();
sashida_h 12:6a3c0e9075eb 157 }
sashida_h 12:6a3c0e9075eb 158 alt_md = _median(alt_buff,NUM_CNT_MEDIAN);
sashida_h 12:6a3c0e9075eb 159 alt_md = alt_md - alt_gnd;
sashida_h 12:6a3c0e9075eb 160 for(cnt_data=0;cnt_data<NUM_CNT_MEDIAN;cnt_data++){
sashida_h 12:6a3c0e9075eb 161 mpu.getAccelero(acc);
sashida_h 12:6a3c0e9075eb 162 acc_buff[cnt_data] = sqrt(pow(acc[0]/9.81,2.0)+pow(acc[1]/9.81,2.0)+pow(acc[2]/9.81,2.0));
sashida_h 12:6a3c0e9075eb 163 }
sashida_h 12:6a3c0e9075eb 164 acc_abs = _median(acc_buff,NUM_CNT_MEDIAN);
sashida_h 12:6a3c0e9075eb 165 //pc.printf("RISE,time from launch:%f\r\n",timer_open.read());
sashida_h 16:6395de1ad89c 166 pc.printf("3,%f,%f,%3f\r\n",acc_abs,alt_md,timer_open.read());
sashida_h 12:6a3c0e9075eb 167 wait(0.9);
sashida_h 7:9953d922499d 168 i=0;
sashida_h 18:37d747f51c0a 169 //timer_data.reset();
Yukina 5:f6e956e8a060 170 }
sashida_h 8:15a1b22df82f 171 Phase = OPEN;
sashida_h 8:15a1b22df82f 172 t = 0.0;
Yukina 5:f6e956e8a060 173 break;
Yukina 5:f6e956e8a060 174 case FIRE:
Yukina 5:f6e956e8a060 175 break;
Yukina 5:f6e956e8a060 176 case OPEN:
Yukina 5:f6e956e8a060 177 for(cnt_data=0;cnt_data<NUM_CNT_MEDIAN;cnt_data++){
Yukina 5:f6e956e8a060 178 alt_buff[cnt_data] = _getAlt();
Yukina 5:f6e956e8a060 179 }
Yukina 5:f6e956e8a060 180 alt_md = _median(alt_buff,NUM_CNT_MEDIAN);
sashida_h 7:9953d922499d 181 alt_md = alt_md - alt_gnd;
sashida_h 8:15a1b22df82f 182 if(timer_open.read() - t > TIME_SEND){
sashida_h 12:6a3c0e9075eb 183 //pc.printf("OPEN,alt:%f,time:%3f,cnt:%d\r\n",alt_md,timer_open.read(),cnt_judge);
sashida_h 16:6395de1ad89c 184 pc.printf("15,%f,%f,%3f\r\n",acc_abs,alt_md,timer_open.read());
sashida_h 8:15a1b22df82f 185 t = timer_open.read();
sashida_h 7:9953d922499d 186 }
Yukina 5:f6e956e8a060 187 if(alt_md > alt_max){
Yukina 5:f6e956e8a060 188 alt_max = alt_md;
Yukina 5:f6e956e8a060 189 cnt_judge = 0;
Yukina 5:f6e956e8a060 190 }
Yukina 5:f6e956e8a060 191 else if((alt_max-alt_md) > ALT_JUDGE_OPEN){
Yukina 5:f6e956e8a060 192 cnt_judge++;
Yukina 5:f6e956e8a060 193 }
Yukina 4:4b3ae90ec778 194
sashida_h 7:9953d922499d 195 //if((timer_open.read()-time_judge) - TIME_JUDGE_CNT > 0) cnt_judge=0;
sashida_h 8:15a1b22df82f 196 if(cnt_judge == CNT_JUDGE || timer_open.read() > TIME_OPEN){
Yukina 5:f6e956e8a060 197 Phase = RECOVERY;
sashida_h 13:3e6411bfd581 198 servo1_signal = 1;
sashida_h 13:3e6411bfd581 199 servo2_signal = 1;
sashida_h 13:3e6411bfd581 200 wait(0.1);
sashida_h 13:3e6411bfd581 201 servo1.pulsewidth(0.0006);//open
sashida_h 13:3e6411bfd581 202 servo2.pulsewidth(0.0015);//open
sashida_h 14:f932a8a297ff 203 pc.printf("9,0,0,0,%d\r\n",cnt_judge);
sashida_h 12:6a3c0e9075eb 204 wait(0.5);
sashida_h 9:42b4d337d4cc 205 tic_gps.attach(&_SendGPS, 1.0/RATE_GPS);
Yukina 5:f6e956e8a060 206 }
Yukina 5:f6e956e8a060 207 break;
Yukina 5:f6e956e8a060 208 case RECOVERY:
Yukina 5:f6e956e8a060 209 break;
Yukina 5:f6e956e8a060 210 case SEA:
Yukina 5:f6e956e8a060 211 break;
Yukina 4:4b3ae90ec778 212 }
Yukina 4:4b3ae90ec778 213 }
Yukina 4:4b3ae90ec778 214 }
Yukina 4:4b3ae90ec778 215
Yukina 4:4b3ae90ec778 216 float _getAlt(){
Yukina 4:4b3ae90ec778 217 float altitude,pressure,temperature;
Yukina 4:4b3ae90ec778 218 bmp.ReadData(&temperature,&pressure);
Yukina 4:4b3ae90ec778 219 altitude = (pow((p0/pressure), (1.0f/5.257f))-1.0f)*(temperature+273.15f)/0.0065f;
Yukina 4:4b3ae90ec778 220 return altitude;
Yukina 4:4b3ae90ec778 221 }
Yukina 4:4b3ae90ec778 222
sashida_h 9:42b4d337d4cc 223 float _DMS2DEG(float raw_data){
sashida_h 9:42b4d337d4cc 224 int d=(int)(raw_data/100);
sashida_h 9:42b4d337d4cc 225 float m=(raw_data-(float)d*100);
sashida_h 9:42b4d337d4cc 226 return (float)d+m/60;
sashida_h 9:42b4d337d4cc 227 }
sashida_h 9:42b4d337d4cc 228
sashida_h 9:42b4d337d4cc 229
sashida_h 9:42b4d337d4cc 230 float _median(float data[], int num){
sashida_h 9:42b4d337d4cc 231 float *data_cpy, ans;
sashida_h 9:42b4d337d4cc 232 data_cpy = new float[num];
sashida_h 9:42b4d337d4cc 233 memcpy(data_cpy,data,sizeof(float)*num);
sashida_h 9:42b4d337d4cc 234
sashida_h 9:42b4d337d4cc 235 for(i=0; i<num; i++){
sashida_h 9:42b4d337d4cc 236 for(int j=0; j<num-i-1; j++){
sashida_h 9:42b4d337d4cc 237 if(data_cpy[j]>data_cpy[j+1]){
sashida_h 9:42b4d337d4cc 238 float buff = data_cpy[j+1];
sashida_h 9:42b4d337d4cc 239 data_cpy[j+1] = data_cpy[j];
sashida_h 9:42b4d337d4cc 240 data_cpy[j] = buff;
sashida_h 9:42b4d337d4cc 241 }
sashida_h 9:42b4d337d4cc 242 }
sashida_h 9:42b4d337d4cc 243 }
sashida_h 9:42b4d337d4cc 244
sashida_h 9:42b4d337d4cc 245 if(num%2!=0) ans = data_cpy[num/2];
sashida_h 9:42b4d337d4cc 246 else ans = (data_cpy[num/2-1]+data_cpy[num/2])/2.0;
sashida_h 9:42b4d337d4cc 247 delete[] data_cpy;
sashida_h 9:42b4d337d4cc 248 return ans;
sashida_h 9:42b4d337d4cc 249 }
sashida_h 9:42b4d337d4cc 250
Yukina 5:f6e956e8a060 251 void _SendGPS(){
sashida_h 7:9953d922499d 252 char gps_data[256];
sashida_h 7:9953d922499d 253 while(1){
sashida_h 7:9953d922499d 254 if(gps.readable()){
sashida_h 7:9953d922499d 255 gps_data[cnt_gps] = gps.getc();
sashida_h 7:9953d922499d 256 if(gps_data[cnt_gps] == '$' || cnt_gps ==256){
sashida_h 7:9953d922499d 257 cnt_gps = 0;
sashida_h 7:9953d922499d 258 memset(gps_data,'\0',256);
sashida_h 7:9953d922499d 259 }else if(gps_data[cnt_gps] == '\r'){
sashida_h 7:9953d922499d 260 float world_time, lon_east, lat_north;
sashida_h 7:9953d922499d 261 int rlock, sat_num;
sashida_h 7:9953d922499d 262 char lat,lon;
sashida_h 7:9953d922499d 263 if(sscanf(gps_data,"GPGGA,%f,%f,%c,%f,%c,%d,%d",&world_time,&lat_north,&lat,&lon_east,&lon,&rlock,&sat_num)>=1){
sashida_h 7:9953d922499d 264 if(rlock==1){
sashida_h 7:9953d922499d 265 lat_north = _DMS2DEG(lat_north);
sashida_h 7:9953d922499d 266 lon_east = _DMS2DEG(lon_east);
sashida_h 7:9953d922499d 267 //pc.printf("%s\r\n",gps_data);
sashida_h 7:9953d922499d 268 //pc.printf("Lat:%f,Lon:%f\r\ntime:%f,sat_num:%d\r\n",lat_north,lon_east,world_time,sat_num);
sashida_h 7:9953d922499d 269 int japan_time = int(world_time) - 9;
sashida_h 13:3e6411bfd581 270 pc.printf("Lat:%f,Lon:%f,MAX_ALT:%f\r\n",lat_north,lon_east,alt_max);
sashida_h 10:1a626929850e 271 for(i=0;i<2;i++){
sashida_h 10:1a626929850e 272 c[i]=pc.getc();
sashida_h 10:1a626929850e 273 }
sashida_h 10:1a626929850e 274 //pc.printf("%c",c[1]);
sashida_h 10:1a626929850e 275 if(c[1]!='O'){
sashida_h 10:1a626929850e 276 ES920_RST = 1;
sashida_h 10:1a626929850e 277 wait(0.1);
sashida_h 10:1a626929850e 278 ES920_RST = 0;
sashida_h 10:1a626929850e 279 wait(1.0);
sashida_h 10:1a626929850e 280 myled = 1;
sashida_h 10:1a626929850e 281 }else{
sashida_h 10:1a626929850e 282 myled = 0;
sashida_h 10:1a626929850e 283 }
sashida_h 10:1a626929850e 284
sashida_h 7:9953d922499d 285 break;
sashida_h 7:9953d922499d 286 }else{
sashida_h 7:9953d922499d 287 //pc.printf("%s\r\n",gps_data);
sashida_h 17:9fc09af2f37a 288 pc.printf("NoGPSSignal\r\n");
sashida_h 7:9953d922499d 289 break;
sashida_h 7:9953d922499d 290 }
Yukina 5:f6e956e8a060 291 }else{
sashida_h 13:3e6411bfd581 292 //pc.printf("No_Satellite_signal\r\n");
Yukina 5:f6e956e8a060 293 }
sashida_h 7:9953d922499d 294 }else{
sashida_h 7:9953d922499d 295 cnt_gps++;
Yukina 5:f6e956e8a060 296 }
Yukina 5:f6e956e8a060 297 }
Yukina 4:4b3ae90ec778 298 }
Yukina 4:4b3ae90ec778 299
sashida_h 7:9953d922499d 300 }