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

Dependencies:   mbed Madgwick MPU6050 Kalman BMP180

Committer:
sashida_h
Date:
Mon Mar 11 09:30:01 2019 +0000
Revision:
13:3e6411bfd581
Parent:
12:6a3c0e9075eb
Child:
14:f932a8a297ff
add CNT_LAUNCH; add 'g'_jumpGPSmode; 2019_0311

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;
Yukina 5:f6e956e8a060 55 /*センサの初期化等*/
sashida_h 7:9953d922499d 56 pc.baud(38400);
Yukina 5:f6e956e8a060 57 mpu.setAcceleroRange(3);
mikawataru 0:0ff20d8e9090 58 bmp.Initialize(64,BMP180_OSS_ULTRA_LOW_POWER);
Yukina 5:f6e956e8a060 59
Yukina 5:f6e956e8a060 60 /*初期位置の設定*/
Yukina 5:f6e956e8a060 61 mpu.getAccelero(acc);
Yukina 5:f6e956e8a060 62 mpu.getGyro(gyro);
Yukina 5:f6e956e8a060 63
Yukina 5:f6e956e8a060 64 Phase = STANDBY;
sashida_h 7:9953d922499d 65 for(cnt_data=0;cnt_data<NUM_CNT_MEDIAN;cnt_data++){
sashida_h 7:9953d922499d 66 alt_buff[cnt_data] = _getAlt();
sashida_h 7:9953d922499d 67 }
sashida_h 7:9953d922499d 68 alt_gnd = _median(alt_buff,NUM_CNT_MEDIAN);
sashida_h 7:9953d922499d 69 wait(2.0);
sashida_h 12:6a3c0e9075eb 70 pc.printf("0,0,0,0,0\r\n");
sashida_h 8:15a1b22df82f 71 wait(1.0);
sashida_h 9:42b4d337d4cc 72 timer_data.start();
sashida_h 11:c90db1500720 73 servo1_signal = 1;
sashida_h 11:c90db1500720 74 servo2_signal = 1;
sashida_h 13:3e6411bfd581 75 servo1.pulsewidth(0.0015);//close
sashida_h 13:3e6411bfd581 76 servo2.pulsewidth(0.0006);//close
Yukina 5:f6e956e8a060 77 while(1){
Yukina 5:f6e956e8a060 78 switch(Phase){
Yukina 5:f6e956e8a060 79 case STANDBY:
sashida_h 10:1a626929850e 80 /*サーボ入力待ち*/
sashida_h 10:1a626929850e 81 //servo();
sashida_h 10:1a626929850e 82 c[0]=pc.getc();
sashida_h 11:c90db1500720 83 if(c[0] == 'o'){
sashida_h 11:c90db1500720 84 myled = 1;
sashida_h 13:3e6411bfd581 85 servo1.pulsewidth(0.0006);//open
sashida_h 13:3e6411bfd581 86 servo2.pulsewidth(0.0015);//open
sashida_h 11:c90db1500720 87 mpu.getAccelero(acc);
sashida_h 12:6a3c0e9075eb 88 //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 12:6a3c0e9075eb 89 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),timer_data.read());
sashida_h 11:c90db1500720 90 }
sashida_h 11:c90db1500720 91 if(c[0] == 'l'){
sashida_h 11:c90db1500720 92 myled = 0;
sashida_h 13:3e6411bfd581 93 servo1.pulsewidth(0.0015);
sashida_h 13:3e6411bfd581 94 servo2.pulsewidth(0.0006);
sashida_h 11:c90db1500720 95 mpu.getAccelero(acc);
sashida_h 12:6a3c0e9075eb 96 //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 12:6a3c0e9075eb 97 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),timer_data.read());
sashida_h 11:c90db1500720 98 }
sashida_h 10:1a626929850e 99 if(c[0] == 'f'){
sashida_h 10:1a626929850e 100 //pc.printf("flight_mode\r\n");
sashida_h 10:1a626929850e 101 Phase = LAUNCH;
sashida_h 11:c90db1500720 102 servo1_signal = 0;
sashida_h 11:c90db1500720 103 servo2_signal = 0;
sashida_h 10:1a626929850e 104 }
sashida_h 13:3e6411bfd581 105 if(c[0] == 'g'){
sashida_h 13:3e6411bfd581 106 pc.printf("9,0,0,0,0\r\n");
sashida_h 13:3e6411bfd581 107 wait(0.5);
sashida_h 13:3e6411bfd581 108 tic_gps.attach(&_SendGPS, 1.0/RATE_GPS);
sashida_h 13:3e6411bfd581 109 Phase = RECOVERY;
sashida_h 13:3e6411bfd581 110 }
Yukina 5:f6e956e8a060 111 break;
sashida_h 8:15a1b22df82f 112
Yukina 5:f6e956e8a060 113 case LAUNCH:
Yukina 5:f6e956e8a060 114 for(cnt_data=0;cnt_data<NUM_CNT_MEDIAN;cnt_data++){
Yukina 5:f6e956e8a060 115 mpu.getAccelero(acc);
Yukina 5:f6e956e8a060 116 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 117 }
Yukina 5:f6e956e8a060 118 acc_abs = _median(acc_buff,NUM_CNT_MEDIAN);
sashida_h 8:15a1b22df82f 119 if(timer_data.read() - t > TIME_SEND){
sashida_h 12:6a3c0e9075eb 120 //pc.printf("LAUNCH,acc:%f,time:%3f,cnt:%d\r\n",acc_abs,timer_data.read(),cnt_judge);
sashida_h 12:6a3c0e9075eb 121 pc.printf("1,%f,%f,0,%3f\r\n",acc_abs,acc_abs,timer_data.read());
sashida_h 13:3e6411bfd581 122 cnt_judge = 0;
sashida_h 8:15a1b22df82f 123 t = timer_data.read();
sashida_h 7:9953d922499d 124 }
Yukina 5:f6e956e8a060 125 /*加速度判定*/
Yukina 5:f6e956e8a060 126 if(acc_abs>ACC_JUDGE_LAUNCH){
Yukina 5:f6e956e8a060 127 cnt_judge++;
Yukina 4:4b3ae90ec778 128 }
sashida_h 13:3e6411bfd581 129 if(cnt_judge==CNT_LAUNCH){
sashida_h 11:c90db1500720 130 servo1_signal = 1;
sashida_h 11:c90db1500720 131 servo1_signal = 1;
Yukina 5:f6e956e8a060 132 cnt_judge=0;
Yukina 5:f6e956e8a060 133 timer_open.start();
sashida_h 7:9953d922499d 134 Phase = RISE;
Yukina 5:f6e956e8a060 135 }
Yukina 5:f6e956e8a060 136 break;
sashida_h 8:15a1b22df82f 137
Yukina 5:f6e956e8a060 138 case RISE:
Yukina 5:f6e956e8a060 139 while(timer_open.read() < TIME_BURNING){
sashida_h 12:6a3c0e9075eb 140 for(cnt_data=0;cnt_data<NUM_CNT_MEDIAN;cnt_data++){
sashida_h 12:6a3c0e9075eb 141 alt_buff[cnt_data] = _getAlt();
sashida_h 12:6a3c0e9075eb 142 }
sashida_h 12:6a3c0e9075eb 143 alt_md = _median(alt_buff,NUM_CNT_MEDIAN);
sashida_h 12:6a3c0e9075eb 144 alt_md = alt_md - alt_gnd;
sashida_h 12:6a3c0e9075eb 145 for(cnt_data=0;cnt_data<NUM_CNT_MEDIAN;cnt_data++){
sashida_h 12:6a3c0e9075eb 146 mpu.getAccelero(acc);
sashida_h 12:6a3c0e9075eb 147 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 148 }
sashida_h 12:6a3c0e9075eb 149 acc_abs = _median(acc_buff,NUM_CNT_MEDIAN);
sashida_h 12:6a3c0e9075eb 150 //pc.printf("RISE,time from launch:%f\r\n",timer_open.read());
sashida_h 12:6a3c0e9075eb 151 pc.printf("3,%f,%f,%f,%3f\r\n",acc_abs,acc_abs,alt_md,timer_open.read());
sashida_h 12:6a3c0e9075eb 152 wait(0.9);
sashida_h 7:9953d922499d 153 i=0;
sashida_h 8:15a1b22df82f 154 timer_data.reset();
Yukina 5:f6e956e8a060 155 }
sashida_h 8:15a1b22df82f 156 Phase = OPEN;
sashida_h 8:15a1b22df82f 157 t = 0.0;
Yukina 5:f6e956e8a060 158 break;
Yukina 5:f6e956e8a060 159 case FIRE:
Yukina 5:f6e956e8a060 160 break;
Yukina 5:f6e956e8a060 161 case OPEN:
Yukina 5:f6e956e8a060 162 for(cnt_data=0;cnt_data<NUM_CNT_MEDIAN;cnt_data++){
Yukina 5:f6e956e8a060 163 alt_buff[cnt_data] = _getAlt();
Yukina 5:f6e956e8a060 164 }
Yukina 5:f6e956e8a060 165 alt_md = _median(alt_buff,NUM_CNT_MEDIAN);
sashida_h 7:9953d922499d 166 alt_md = alt_md - alt_gnd;
sashida_h 8:15a1b22df82f 167 if(timer_open.read() - t > TIME_SEND){
sashida_h 12:6a3c0e9075eb 168 //pc.printf("OPEN,alt:%f,time:%3f,cnt:%d\r\n",alt_md,timer_open.read(),cnt_judge);
sashida_h 12:6a3c0e9075eb 169 pc.printf("15,%f,%f,%f,%3f\r\n",acc_abs,acc_abs,alt_md,timer_open.read());
sashida_h 8:15a1b22df82f 170 t = timer_open.read();
sashida_h 7:9953d922499d 171 }
Yukina 5:f6e956e8a060 172 if(alt_md > alt_max){
Yukina 5:f6e956e8a060 173 alt_max = alt_md;
Yukina 5:f6e956e8a060 174 cnt_judge = 0;
Yukina 5:f6e956e8a060 175 }
Yukina 5:f6e956e8a060 176 else if((alt_max-alt_md) > ALT_JUDGE_OPEN){
Yukina 5:f6e956e8a060 177 cnt_judge++;
Yukina 5:f6e956e8a060 178 }
Yukina 4:4b3ae90ec778 179
sashida_h 7:9953d922499d 180 //if((timer_open.read()-time_judge) - TIME_JUDGE_CNT > 0) cnt_judge=0;
sashida_h 8:15a1b22df82f 181 if(cnt_judge == CNT_JUDGE || timer_open.read() > TIME_OPEN){
Yukina 5:f6e956e8a060 182 Phase = RECOVERY;
sashida_h 13:3e6411bfd581 183 servo1_signal = 1;
sashida_h 13:3e6411bfd581 184 servo2_signal = 1;
sashida_h 13:3e6411bfd581 185 wait(0.1);
sashida_h 13:3e6411bfd581 186 servo1.pulsewidth(0.0006);//open
sashida_h 13:3e6411bfd581 187 servo2.pulsewidth(0.0015);//open
sashida_h 12:6a3c0e9075eb 188 pc.printf("9,0,0,0,0\r\n");
sashida_h 12:6a3c0e9075eb 189 wait(0.5);
sashida_h 9:42b4d337d4cc 190 tic_gps.attach(&_SendGPS, 1.0/RATE_GPS);
Yukina 5:f6e956e8a060 191 }
Yukina 5:f6e956e8a060 192 break;
Yukina 5:f6e956e8a060 193 case RECOVERY:
Yukina 5:f6e956e8a060 194 break;
Yukina 5:f6e956e8a060 195 case SEA:
Yukina 5:f6e956e8a060 196 break;
Yukina 4:4b3ae90ec778 197 }
Yukina 4:4b3ae90ec778 198 }
Yukina 4:4b3ae90ec778 199 }
Yukina 4:4b3ae90ec778 200
Yukina 4:4b3ae90ec778 201 float _getAlt(){
Yukina 4:4b3ae90ec778 202 float altitude,pressure,temperature;
Yukina 4:4b3ae90ec778 203 bmp.ReadData(&temperature,&pressure);
Yukina 4:4b3ae90ec778 204 altitude = (pow((p0/pressure), (1.0f/5.257f))-1.0f)*(temperature+273.15f)/0.0065f;
Yukina 4:4b3ae90ec778 205 return altitude;
Yukina 4:4b3ae90ec778 206 }
Yukina 4:4b3ae90ec778 207
sashida_h 9:42b4d337d4cc 208 float _DMS2DEG(float raw_data){
sashida_h 9:42b4d337d4cc 209 int d=(int)(raw_data/100);
sashida_h 9:42b4d337d4cc 210 float m=(raw_data-(float)d*100);
sashida_h 9:42b4d337d4cc 211 return (float)d+m/60;
sashida_h 9:42b4d337d4cc 212 }
sashida_h 9:42b4d337d4cc 213
sashida_h 9:42b4d337d4cc 214
sashida_h 9:42b4d337d4cc 215 float _median(float data[], int num){
sashida_h 9:42b4d337d4cc 216 float *data_cpy, ans;
sashida_h 9:42b4d337d4cc 217 data_cpy = new float[num];
sashida_h 9:42b4d337d4cc 218 memcpy(data_cpy,data,sizeof(float)*num);
sashida_h 9:42b4d337d4cc 219
sashida_h 9:42b4d337d4cc 220 for(i=0; i<num; i++){
sashida_h 9:42b4d337d4cc 221 for(int j=0; j<num-i-1; j++){
sashida_h 9:42b4d337d4cc 222 if(data_cpy[j]>data_cpy[j+1]){
sashida_h 9:42b4d337d4cc 223 float buff = data_cpy[j+1];
sashida_h 9:42b4d337d4cc 224 data_cpy[j+1] = data_cpy[j];
sashida_h 9:42b4d337d4cc 225 data_cpy[j] = buff;
sashida_h 9:42b4d337d4cc 226 }
sashida_h 9:42b4d337d4cc 227 }
sashida_h 9:42b4d337d4cc 228 }
sashida_h 9:42b4d337d4cc 229
sashida_h 9:42b4d337d4cc 230 if(num%2!=0) ans = data_cpy[num/2];
sashida_h 9:42b4d337d4cc 231 else ans = (data_cpy[num/2-1]+data_cpy[num/2])/2.0;
sashida_h 9:42b4d337d4cc 232 delete[] data_cpy;
sashida_h 9:42b4d337d4cc 233 return ans;
sashida_h 9:42b4d337d4cc 234 }
sashida_h 9:42b4d337d4cc 235
Yukina 5:f6e956e8a060 236 void _SendGPS(){
sashida_h 7:9953d922499d 237 char gps_data[256];
sashida_h 7:9953d922499d 238 while(1){
sashida_h 7:9953d922499d 239 if(gps.readable()){
sashida_h 7:9953d922499d 240 gps_data[cnt_gps] = gps.getc();
sashida_h 7:9953d922499d 241 if(gps_data[cnt_gps] == '$' || cnt_gps ==256){
sashida_h 7:9953d922499d 242 cnt_gps = 0;
sashida_h 7:9953d922499d 243 memset(gps_data,'\0',256);
sashida_h 7:9953d922499d 244 }else if(gps_data[cnt_gps] == '\r'){
sashida_h 7:9953d922499d 245 float world_time, lon_east, lat_north;
sashida_h 7:9953d922499d 246 int rlock, sat_num;
sashida_h 7:9953d922499d 247 char lat,lon;
sashida_h 7:9953d922499d 248 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 249 if(rlock==1){
sashida_h 7:9953d922499d 250 lat_north = _DMS2DEG(lat_north);
sashida_h 7:9953d922499d 251 lon_east = _DMS2DEG(lon_east);
sashida_h 7:9953d922499d 252 //pc.printf("%s\r\n",gps_data);
sashida_h 7:9953d922499d 253 //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 254 int japan_time = int(world_time) - 9;
sashida_h 13:3e6411bfd581 255 pc.printf("Lat:%f,Lon:%f,MAX_ALT:%f\r\n",lat_north,lon_east,alt_max);
sashida_h 10:1a626929850e 256 for(i=0;i<2;i++){
sashida_h 10:1a626929850e 257 c[i]=pc.getc();
sashida_h 10:1a626929850e 258 }
sashida_h 10:1a626929850e 259 //pc.printf("%c",c[1]);
sashida_h 10:1a626929850e 260 if(c[1]!='O'){
sashida_h 10:1a626929850e 261 ES920_RST = 1;
sashida_h 10:1a626929850e 262 wait(0.1);
sashida_h 10:1a626929850e 263 ES920_RST = 0;
sashida_h 10:1a626929850e 264 wait(1.0);
sashida_h 10:1a626929850e 265 myled = 1;
sashida_h 10:1a626929850e 266 }else{
sashida_h 10:1a626929850e 267 myled = 0;
sashida_h 10:1a626929850e 268 }
sashida_h 10:1a626929850e 269
sashida_h 7:9953d922499d 270 break;
sashida_h 7:9953d922499d 271 }else{
sashida_h 7:9953d922499d 272 //pc.printf("%s\r\n",gps_data);
sashida_h 7:9953d922499d 273 pc.printf("NoGPSSignal\r\n");
sashida_h 7:9953d922499d 274 break;
sashida_h 7:9953d922499d 275 }
Yukina 5:f6e956e8a060 276 }else{
sashida_h 13:3e6411bfd581 277 //pc.printf("No_Satellite_signal\r\n");
Yukina 5:f6e956e8a060 278 }
sashida_h 7:9953d922499d 279 }else{
sashida_h 7:9953d922499d 280 cnt_gps++;
Yukina 5:f6e956e8a060 281 }
Yukina 5:f6e956e8a060 282 }
Yukina 4:4b3ae90ec778 283 }
Yukina 4:4b3ae90ec778 284
sashida_h 7:9953d922499d 285 }