![](/media/cache/group/icon1_qbDJ87a.png.50x50_q85.png)
izu2021mission
Dependencies: mbed ADS1015 PQ_LPS22HB SPS30 SDFileSystem
Diff: main.cpp
- Revision:
- 0:08e4b6ebbc85
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Mar 04 07:38:29 2021 +0000 @@ -0,0 +1,308 @@ +#include "mbed.h" +//#include <stdio.h> +#include <cmath> + +#include "SDFileSystem.h" +#include "Adafruit_ADS1015.h" +#include "PQ_LPS22HB.h" +#include "sps30.h" + +#define ADS1115_ADDR 0b1001000 +#define LOG_RATE 1 + +FILE *fp; + +Serial pc(USBTX,USBRX,115200); +I2C i2c(D4,D5); +SDFileSystem sd(D11, D12, D13, A3, "sd"); + +DigitalIn fligt_sig(D3); +DigitalIn sep_sig(D6); +DigitalOut relay(D0); + +Adafruit_ADS1115 ads1115(&i2c, ADS1115_ADDR); +LPS22HB lps33hw(i2c, LPS22HB::SA0_HIGH); +LPS22HB lps22hb(i2c, LPS22HB::SA0_LOW); +Sps30 sps(D4, D5, 100000); + +Timer mission_timer; +Timer flight_timer; +Timer sd_timer; +Ticker log_ticker; + +char file_name[64]; +char f_outside_lps; +char f_inside_lps; + +enum { + READY, + RISE, + FALL, + RECOVERY, +} phase; + +bool cut_off; +bool recovery; + +int mission_time; +int flight_time; + +float pitot_speed; +float density = 1.3; +float ground_press; +float outside_press; +float outside_temp; +float inside_press; +float inside_temp; + +void init(); +void pitot_tube(); +void lps33hw_mission(); +void lps22hb_mission(); +void record(); +void read(); + +int main() +{ + init(); + mission_timer.start(); + while(1) { + //pitot_tube(); + lps33hw_mission(); + lps22hb_mission(); + //read(); + switch(phase) { + case READY: //0 + if(fligt_sig) { + phase = RISE; + flight_timer.start(); + } + break; + case RISE: //1 + if(sep_sig) { + phase = FALL; + } + break; + case FALL: //2 + if(outside_press > ground_press) { + phase = RECOVERY; + fclose(fp); + flight_timer.stop(); + } + break; + case RECOVERY: //3 + if(!recovery){ + relay = 0; //センサーへの電源供給停止 + cut_off = true; + recovery = true; + } + break; + } + wait(0.5); + } +} + +void init() +{ + pc.printf("---PQ_Hybrid_Mission---\r\n"); + relay = 1; //センサーへ電源供給開始 + wait(1); + + lps33hw.begin(); + lps22hb.begin(); + ads1115.setGain(GAIN_TWOTHIRDS); + sps.StartFanClean(); + + sd.mount(); + mkdir("/sd", 0777); + char file_name_format[] = "/sd/IZU2021_MISSION_%d.dat"; + int file_number = 1; + while(1) { + sprintf(file_name, file_name_format, file_number); + fp = fopen(file_name, "r"); + if(fp != NULL) { + fclose(fp); + file_number++; + } else { + sprintf(file_name, "/sd/IZU2021_MISSION_%d.dat", file_number); + break; + } + } + fp = fopen(file_name, "w"); + sd_timer.start(); + if(fp) { + fprintf(fp, "mission_time[s]\t"); + fprintf(fp, "flight_time[s]\t"); + fprintf(fp, "phase\t"); + fprintf(fp, "cut_off\t"); + fprintf(fp, "pitot_speed[m/s]\t"); + fprintf(fp, "pitot_speed[km/h]\t"); + fprintf(fp, "mass_1p0\t"); + fprintf(fp, "mass_2p5\t"); + fprintf(fp, "mass_4p0\t"); + fprintf(fp, "mass_10p0\t"); + fprintf(fp, "num_0p5\t"); + fprintf(fp, "num_1p0\t"); + fprintf(fp, "num_2p5\t"); + fprintf(fp, "num_4p0\t"); + fprintf(fp, "num_10p0\t"); + fprintf(fp, "typ_pm_size\t"); + fprintf(fp, "outside_press[Pa]\t"); + fprintf(fp, "outside_temp[degC]\t"); + fprintf(fp, "inside_press[Pa]\t"); + fprintf(fp, "inside_temp[degC]\t"); + fprintf(fp, "\r\n"); + } + + /***************************************** + /地上の気圧を測定。リレーの解除に使う + ******************************************/ + float temp_press = 0; + float sum = 0; + int i = 0; + while(i < 10) { + lps33hw.read_press(&temp_press); + sum += temp_press; + i++; + wait(0.5); + } + ground_press = sum/10.0f; + + log_ticker.attach(record, 1.0f/LOG_RATE); + //キャッシュオーバーに注意 + //read_ticker.attach(read, 0.5); +} + +/********************************************* +/ MPXV5004DPの場合:(Differencial) = ((Vout / Vs) - 0.2) / 0.2 +/ MPX5010DPの場合:(Differencial) = ((Vout / Vs) - 0.04) / 0.09 +/ speed = root(2 * P / ρ) +/ pitot_paが負のときnanが返される +**********************************************/ +void pitot_tube(){ + float sum = 0; + float voltage = 0; + float pitot_pa = 0; + float pitot_volt = 0; + for(int i = 0; i < 10; i++){ + int16_t val = ads1115.readADC_SingleEnded(0); + voltage = val / 32768.0 * 6.144; + sum += voltage; + } + float v_ave = sum / 10.0f; + pitot_volt = voltage - v_ave + 0.2f; + pitot_pa = (pitot_volt / 5.0f - 0.04f) / 0.09f; + pitot_speed = sqrt(2 * pitot_pa * 1000 / density); +} + +/********************************************* +/ 水密外配置。リレーの条件に使用する。 +**********************************************/ +void lps33hw_mission() +{ + f_outside_lps = lps33hw.test(); + if(f_outside_lps) { + outside_press = 0; + outside_temp = 0; + lps33hw.read_press(&outside_press); + lps33hw.read_temp(&outside_temp); + } +} + +/********************************************* +/ 水密内配置。水密になっているかの確認用。 +**********************************************/ +void lps22hb_mission() +{ + f_inside_lps = lps22hb.test(); + if(f_inside_lps) { + inside_press = 0; + inside_temp = 0; + lps22hb.read_press(&inside_press); + lps22hb.read_temp(&inside_temp); + } +} + +void record() +{ + mission_time = mission_timer.read(); + flight_time = flight_timer.read(); + fprintf(fp, "%4d\t", mission_time); + fprintf(fp, "%4d\t", flight_time); + fprintf(fp, "%1d\t", phase); + fprintf(fp, "%1d\t", cut_off); + fprintf(fp, "%f\t", pitot_speed); + fprintf(fp, "%f\t", pitot_speed * 3600 / 1000.0f); + fprintf(fp, "%f\t", sps.mass_1p0_f); + fprintf(fp, "%f\t", sps.mass_2p5_f); + fprintf(fp, "%f\t", sps.mass_4p0_f); + fprintf(fp, "%f\t", sps.mass_10p0_f); + fprintf(fp, "%f\t", sps.num_0p5_f); + fprintf(fp, "%f\t", sps.num_1p0_f); + fprintf(fp, "%f\t", sps.num_2p5_f); + fprintf(fp, "%f\t", sps.num_4p0_f); + fprintf(fp, "%f\t", sps.num_10p0_f); + fprintf(fp, "%f\t", sps.typ_pm_size_f); + fprintf(fp, "%f\t", outside_press); + fprintf(fp, "%f\t", outside_temp); + fprintf(fp, "%f\t", inside_press); + fprintf(fp, "%f\t", inside_temp); + fprintf(fp, "\r\n"); + + if(sd_timer.read_ms() >= 60*1000) { //1分ごとにリセット、開き直す。 + sd_timer.reset(); + sd_timer.start(); + if(fp) { + fclose(fp); + fp = fopen(file_name, "a"); //a:追加書き込みモード + } + } +} + +/* +void read() +{ + pc.printf("mission_time:\t"); + pc.printf("flight_time:\t"); + pc.printf("phase:\t"); + pc.printf("f_sps:\t"); + pc.printf("SPEED[m/s]:\t"); + pc.printf("SPEED[km/h]:\t"); + pc.printf("MASS_1P0\t"); + pc.printf("MASS_2P5\t"); + pc.printf("MASS_4P0\t"); + pc.printf("MASS_10P0\t"); + pc.printf("NUM_0P5\t"); + pc.printf("NUM_1P0\t"); + pc.printf("NUM_2P5\t"); + pc.printf("NUM_4P0\t"); + pc.printf("NUM_10P0\t"); + pc.printf("PM_SIZE\t"); + pc.printf("OUT_PRESS\t"); + pc.printf("OUT_TEMP\t"); + pc.printf("IN_PRESS\t"); + pc.printf("IN_TEMP\t"); + pc.printf("\r\n"); + pc.printf("%4d\t", mission_time); + pc.printf("%d\t", (int)phase); + pc.printf("%c\t", (int)relay); + pc.printf("%f\t", pitot_speed); + pc.printf("%f\t", pitot_speed * 3600 / 1000.0f); + pc.printf("%f\t", sps.mass_1p0_f); + pc.printf("%f\t", sps.mass_2p5_f); + pc.printf("%f\t", sps.mass_4p0_f); + pc.printf("%f\t", sps.mass_10p0_f); + pc.printf("%f\t", sps.num_0p5_f); + pc.printf("%f\t", sps.num_1p0_f); + pc.printf("%f\t", sps.num_2p5_f); + pc.printf("%f\t", sps.num_4p0_f); + pc.printf("%f\t", sps.num_10p0_f); + pc.printf("%f\t", sps.typ_pm_size_f); + pc.printf("%.2f\t", ground_press); + pc.printf("%.2f\t", outside_press); + pc.printf("%.1f\t", outside_temp); + pc.printf("%.2f\t", inside_press); + pc.printf("%.1f\t", inside_temp); + pc.printf("\r\n"); +} +*/ \ No newline at end of file