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

Dependencies:   mbed Madgwick MPU6050 Kalman BMP180

Committer:
sashida_h
Date:
Sun Mar 10 08:17:44 2019 +0000
Revision:
11:c90db1500720
Parent:
10:1a626929850e
Child:
12:6a3c0e9075eb
2019_0310

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