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

Dependencies:   mbed Madgwick MPU6050 Kalman BMP180

Committer:
sashida_h
Date:
Wed Mar 13 07:28:08 2019 +0000
Revision:
14:f932a8a297ff
Parent:
13:3e6411bfd581
Child:
15:a47cf038d601
2019_0313

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 8:15a1b22df82f 9 #define ALT_JUDGE_OPEN 1 //落下判定のカウントを1増やす高度差
sashida_h 8:15a1b22df82f 10 #define TIME_OPEN 25 //強制的に開放させる時間
sashida_h 11:c90db1500720 11 #define TIME_SEND 1.0 //無線送信する間隔
sashida_h 11:c90db1500720 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 14:f932a8a297ff 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 14:f932a8a297ff 101 pc.printf("0,%f,%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)),(acc[1]/9.81),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 14:f932a8a297ff 109 pc.printf("0,%f,%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)),(acc[1]/9.81),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 10:1a626929850e 116 }
sashida_h 13:3e6411bfd581 117 if(c[0] == 'g'){
sashida_h 13:3e6411bfd581 118 pc.printf("9,0,0,0,0\r\n");
sashida_h 13:3e6411bfd581 119 wait(0.5);
sashida_h 13:3e6411bfd581 120 tic_gps.attach(&_SendGPS, 1.0/RATE_GPS);
sashida_h 13:3e6411bfd581 121 Phase = RECOVERY;
sashida_h 13:3e6411bfd581 122 }
Yukina 5:f6e956e8a060 123 break;
sashida_h 8:15a1b22df82f 124
Yukina 5:f6e956e8a060 125 case LAUNCH:
Yukina 5:f6e956e8a060 126 for(cnt_data=0;cnt_data<NUM_CNT_MEDIAN;cnt_data++){
Yukina 5:f6e956e8a060 127 mpu.getAccelero(acc);
Yukina 5:f6e956e8a060 128 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 129 }
Yukina 5:f6e956e8a060 130 acc_abs = _median(acc_buff,NUM_CNT_MEDIAN);
sashida_h 8:15a1b22df82f 131 if(timer_data.read() - t > TIME_SEND){
sashida_h 12:6a3c0e9075eb 132 //pc.printf("LAUNCH,acc:%f,time:%3f,cnt:%d\r\n",acc_abs,timer_data.read(),cnt_judge);
sashida_h 12:6a3c0e9075eb 133 pc.printf("1,%f,%f,0,%3f\r\n",acc_abs,acc_abs,timer_data.read());
sashida_h 13:3e6411bfd581 134 cnt_judge = 0;
sashida_h 8:15a1b22df82f 135 t = timer_data.read();
sashida_h 7:9953d922499d 136 }
Yukina 5:f6e956e8a060 137 /*加速度判定*/
Yukina 5:f6e956e8a060 138 if(acc_abs>ACC_JUDGE_LAUNCH){
Yukina 5:f6e956e8a060 139 cnt_judge++;
Yukina 4:4b3ae90ec778 140 }
sashida_h 13:3e6411bfd581 141 if(cnt_judge==CNT_LAUNCH){
sashida_h 11:c90db1500720 142 servo1_signal = 1;
sashida_h 11:c90db1500720 143 servo1_signal = 1;
Yukina 5:f6e956e8a060 144 cnt_judge=0;
Yukina 5:f6e956e8a060 145 timer_open.start();
sashida_h 7:9953d922499d 146 Phase = RISE;
Yukina 5:f6e956e8a060 147 }
Yukina 5:f6e956e8a060 148 break;
sashida_h 8:15a1b22df82f 149
Yukina 5:f6e956e8a060 150 case RISE:
Yukina 5:f6e956e8a060 151 while(timer_open.read() < TIME_BURNING){
sashida_h 12:6a3c0e9075eb 152 for(cnt_data=0;cnt_data<NUM_CNT_MEDIAN;cnt_data++){
sashida_h 12:6a3c0e9075eb 153 alt_buff[cnt_data] = _getAlt();
sashida_h 12:6a3c0e9075eb 154 }
sashida_h 12:6a3c0e9075eb 155 alt_md = _median(alt_buff,NUM_CNT_MEDIAN);
sashida_h 12:6a3c0e9075eb 156 alt_md = alt_md - alt_gnd;
sashida_h 12:6a3c0e9075eb 157 for(cnt_data=0;cnt_data<NUM_CNT_MEDIAN;cnt_data++){
sashida_h 12:6a3c0e9075eb 158 mpu.getAccelero(acc);
sashida_h 12:6a3c0e9075eb 159 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 160 }
sashida_h 12:6a3c0e9075eb 161 acc_abs = _median(acc_buff,NUM_CNT_MEDIAN);
sashida_h 12:6a3c0e9075eb 162 //pc.printf("RISE,time from launch:%f\r\n",timer_open.read());
sashida_h 12:6a3c0e9075eb 163 pc.printf("3,%f,%f,%f,%3f\r\n",acc_abs,acc_abs,alt_md,timer_open.read());
sashida_h 12:6a3c0e9075eb 164 wait(0.9);
sashida_h 7:9953d922499d 165 i=0;
sashida_h 8:15a1b22df82f 166 timer_data.reset();
Yukina 5:f6e956e8a060 167 }
sashida_h 8:15a1b22df82f 168 Phase = OPEN;
sashida_h 8:15a1b22df82f 169 t = 0.0;
Yukina 5:f6e956e8a060 170 break;
Yukina 5:f6e956e8a060 171 case FIRE:
Yukina 5:f6e956e8a060 172 break;
Yukina 5:f6e956e8a060 173 case OPEN:
Yukina 5:f6e956e8a060 174 for(cnt_data=0;cnt_data<NUM_CNT_MEDIAN;cnt_data++){
Yukina 5:f6e956e8a060 175 alt_buff[cnt_data] = _getAlt();
Yukina 5:f6e956e8a060 176 }
Yukina 5:f6e956e8a060 177 alt_md = _median(alt_buff,NUM_CNT_MEDIAN);
sashida_h 7:9953d922499d 178 alt_md = alt_md - alt_gnd;
sashida_h 8:15a1b22df82f 179 if(timer_open.read() - t > TIME_SEND){
sashida_h 12:6a3c0e9075eb 180 //pc.printf("OPEN,alt:%f,time:%3f,cnt:%d\r\n",alt_md,timer_open.read(),cnt_judge);
sashida_h 12:6a3c0e9075eb 181 pc.printf("15,%f,%f,%f,%3f\r\n",acc_abs,acc_abs,alt_md,timer_open.read());
sashida_h 8:15a1b22df82f 182 t = timer_open.read();
sashida_h 7:9953d922499d 183 }
Yukina 5:f6e956e8a060 184 if(alt_md > alt_max){
Yukina 5:f6e956e8a060 185 alt_max = alt_md;
Yukina 5:f6e956e8a060 186 cnt_judge = 0;
Yukina 5:f6e956e8a060 187 }
Yukina 5:f6e956e8a060 188 else if((alt_max-alt_md) > ALT_JUDGE_OPEN){
Yukina 5:f6e956e8a060 189 cnt_judge++;
Yukina 5:f6e956e8a060 190 }
Yukina 4:4b3ae90ec778 191
sashida_h 7:9953d922499d 192 //if((timer_open.read()-time_judge) - TIME_JUDGE_CNT > 0) cnt_judge=0;
sashida_h 8:15a1b22df82f 193 if(cnt_judge == CNT_JUDGE || timer_open.read() > TIME_OPEN){
Yukina 5:f6e956e8a060 194 Phase = RECOVERY;
sashida_h 13:3e6411bfd581 195 servo1_signal = 1;
sashida_h 13:3e6411bfd581 196 servo2_signal = 1;
sashida_h 13:3e6411bfd581 197 wait(0.1);
sashida_h 13:3e6411bfd581 198 servo1.pulsewidth(0.0006);//open
sashida_h 13:3e6411bfd581 199 servo2.pulsewidth(0.0015);//open
sashida_h 14:f932a8a297ff 200 pc.printf("9,0,0,0,%d\r\n",cnt_judge);
sashida_h 12:6a3c0e9075eb 201 wait(0.5);
sashida_h 9:42b4d337d4cc 202 tic_gps.attach(&_SendGPS, 1.0/RATE_GPS);
Yukina 5:f6e956e8a060 203 }
Yukina 5:f6e956e8a060 204 break;
Yukina 5:f6e956e8a060 205 case RECOVERY:
Yukina 5:f6e956e8a060 206 break;
Yukina 5:f6e956e8a060 207 case SEA:
Yukina 5:f6e956e8a060 208 break;
Yukina 4:4b3ae90ec778 209 }
Yukina 4:4b3ae90ec778 210 }
Yukina 4:4b3ae90ec778 211 }
Yukina 4:4b3ae90ec778 212
Yukina 4:4b3ae90ec778 213 float _getAlt(){
Yukina 4:4b3ae90ec778 214 float altitude,pressure,temperature;
Yukina 4:4b3ae90ec778 215 bmp.ReadData(&temperature,&pressure);
Yukina 4:4b3ae90ec778 216 altitude = (pow((p0/pressure), (1.0f/5.257f))-1.0f)*(temperature+273.15f)/0.0065f;
Yukina 4:4b3ae90ec778 217 return altitude;
Yukina 4:4b3ae90ec778 218 }
Yukina 4:4b3ae90ec778 219
sashida_h 9:42b4d337d4cc 220 float _DMS2DEG(float raw_data){
sashida_h 9:42b4d337d4cc 221 int d=(int)(raw_data/100);
sashida_h 9:42b4d337d4cc 222 float m=(raw_data-(float)d*100);
sashida_h 9:42b4d337d4cc 223 return (float)d+m/60;
sashida_h 9:42b4d337d4cc 224 }
sashida_h 9:42b4d337d4cc 225
sashida_h 9:42b4d337d4cc 226
sashida_h 9:42b4d337d4cc 227 float _median(float data[], int num){
sashida_h 9:42b4d337d4cc 228 float *data_cpy, ans;
sashida_h 9:42b4d337d4cc 229 data_cpy = new float[num];
sashida_h 9:42b4d337d4cc 230 memcpy(data_cpy,data,sizeof(float)*num);
sashida_h 9:42b4d337d4cc 231
sashida_h 9:42b4d337d4cc 232 for(i=0; i<num; i++){
sashida_h 9:42b4d337d4cc 233 for(int j=0; j<num-i-1; j++){
sashida_h 9:42b4d337d4cc 234 if(data_cpy[j]>data_cpy[j+1]){
sashida_h 9:42b4d337d4cc 235 float buff = data_cpy[j+1];
sashida_h 9:42b4d337d4cc 236 data_cpy[j+1] = data_cpy[j];
sashida_h 9:42b4d337d4cc 237 data_cpy[j] = buff;
sashida_h 9:42b4d337d4cc 238 }
sashida_h 9:42b4d337d4cc 239 }
sashida_h 9:42b4d337d4cc 240 }
sashida_h 9:42b4d337d4cc 241
sashida_h 9:42b4d337d4cc 242 if(num%2!=0) ans = data_cpy[num/2];
sashida_h 9:42b4d337d4cc 243 else ans = (data_cpy[num/2-1]+data_cpy[num/2])/2.0;
sashida_h 9:42b4d337d4cc 244 delete[] data_cpy;
sashida_h 9:42b4d337d4cc 245 return ans;
sashida_h 9:42b4d337d4cc 246 }
sashida_h 9:42b4d337d4cc 247
Yukina 5:f6e956e8a060 248 void _SendGPS(){
sashida_h 7:9953d922499d 249 char gps_data[256];
sashida_h 7:9953d922499d 250 while(1){
sashida_h 7:9953d922499d 251 if(gps.readable()){
sashida_h 7:9953d922499d 252 gps_data[cnt_gps] = gps.getc();
sashida_h 7:9953d922499d 253 if(gps_data[cnt_gps] == '$' || cnt_gps ==256){
sashida_h 7:9953d922499d 254 cnt_gps = 0;
sashida_h 7:9953d922499d 255 memset(gps_data,'\0',256);
sashida_h 7:9953d922499d 256 }else if(gps_data[cnt_gps] == '\r'){
sashida_h 7:9953d922499d 257 float world_time, lon_east, lat_north;
sashida_h 7:9953d922499d 258 int rlock, sat_num;
sashida_h 7:9953d922499d 259 char lat,lon;
sashida_h 7:9953d922499d 260 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 261 if(rlock==1){
sashida_h 7:9953d922499d 262 lat_north = _DMS2DEG(lat_north);
sashida_h 7:9953d922499d 263 lon_east = _DMS2DEG(lon_east);
sashida_h 7:9953d922499d 264 //pc.printf("%s\r\n",gps_data);
sashida_h 7:9953d922499d 265 //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 266 int japan_time = int(world_time) - 9;
sashida_h 13:3e6411bfd581 267 pc.printf("Lat:%f,Lon:%f,MAX_ALT:%f\r\n",lat_north,lon_east,alt_max);
sashida_h 10:1a626929850e 268 for(i=0;i<2;i++){
sashida_h 10:1a626929850e 269 c[i]=pc.getc();
sashida_h 10:1a626929850e 270 }
sashida_h 10:1a626929850e 271 //pc.printf("%c",c[1]);
sashida_h 10:1a626929850e 272 if(c[1]!='O'){
sashida_h 10:1a626929850e 273 ES920_RST = 1;
sashida_h 10:1a626929850e 274 wait(0.1);
sashida_h 10:1a626929850e 275 ES920_RST = 0;
sashida_h 10:1a626929850e 276 wait(1.0);
sashida_h 10:1a626929850e 277 myled = 1;
sashida_h 10:1a626929850e 278 }else{
sashida_h 10:1a626929850e 279 myled = 0;
sashida_h 10:1a626929850e 280 }
sashida_h 10:1a626929850e 281
sashida_h 7:9953d922499d 282 break;
sashida_h 7:9953d922499d 283 }else{
sashida_h 7:9953d922499d 284 //pc.printf("%s\r\n",gps_data);
sashida_h 14:f932a8a297ff 285 //pc.printf("NoGPSSignal\r\n");
sashida_h 7:9953d922499d 286 break;
sashida_h 7:9953d922499d 287 }
Yukina 5:f6e956e8a060 288 }else{
sashida_h 13:3e6411bfd581 289 //pc.printf("No_Satellite_signal\r\n");
Yukina 5:f6e956e8a060 290 }
sashida_h 7:9953d922499d 291 }else{
sashida_h 7:9953d922499d 292 cnt_gps++;
Yukina 5:f6e956e8a060 293 }
Yukina 5:f6e956e8a060 294 }
Yukina 4:4b3ae90ec778 295 }
Yukina 4:4b3ae90ec778 296
sashida_h 7:9953d922499d 297 }