最大200Gの加速度と気圧から計算した高度をOpenLogで記録するロガー

Dependencies:   mbed ADXL375_i2c LPS25H_lib

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "ADXL375_i2c.h"
00003 #include "LPS25H_lib.h"
00004 
00005 #define TIME_PRINT 0.1
00006 #define ALT_SEPARATE 6.0
00007 
00008 /*******************************************************************************
00009 コンストラクタ
00010 *******************************************************************************/
00011 Serial pc(USBTX, USBRX, 115200);
00012 Serial sd(p13, p14, 115200);
00013 
00014 I2C i2c_bus(p9, p10);
00015 
00016 LPS25H_lib LPS25H(LPS25H_lib::AD0_HIGH, i2c_bus);
00017 ADXL375_i2c ADXL375(i2c_bus, ADXL375_i2c::ALT_ADDRESS_HIGH);
00018 
00019 Timer timer_rec;
00020 Timer timer_button;
00021 Ticker tick_print;
00022 
00023 InterruptIn pinReset(p17);
00024 InterruptIn pinRec(p16);
00025 
00026 DigitalOut LED_REC(LED1);
00027 DigitalOut LED_PT(LED2);
00028 
00029 /*******************************************************************************
00030 関数のプロトタイプ宣言
00031 *******************************************************************************/
00032 void setup();
00033 
00034 void readSensor();
00035 void resetPT();
00036 
00037 void printData();
00038 void readPC();
00039 
00040 void recData();
00041 void startRec();
00042 void stopRec();
00043 void buttonPush();
00044 void buttonRelease();
00045 
00046 /*******************************************************************************
00047 変数の宣言
00048 *******************************************************************************/
00049 int time_rec;
00050 
00051 float acc[3];
00052 float pres, temp, alt;
00053 float pres_0, temp_0;
00054 float alt_old, speed;
00055 int separate_c;
00056 
00057 bool save = false;
00058 bool separate = false;
00059 bool button_push = false;
00060 
00061 /*******************************************************************************
00062 メイン関数
00063 *******************************************************************************/
00064 int main() {
00065     setup();
00066     
00067     pc.attach(&readPC, Serial::RxIrq);
00068     tick_print.attach(&printData, TIME_PRINT);
00069     
00070     pinReset.mode(PullUp);
00071     pinReset.fall(&resetPT);
00072     
00073     pinRec.mode(PullUp);
00074     pinRec.fall(&buttonPush);
00075     pinRec.rise(&buttonRelease);
00076     
00077     while(1) {
00078         readSensor();
00079         recData();
00080         wait(0.003f);
00081     }
00082 }
00083 
00084 /*******************************************************************************
00085 センサー読み取り
00086 *******************************************************************************/
00087 void readSensor(){
00088     alt_old = alt;
00089     pres = LPS25H.getPres();
00090     temp = LPS25H.getTemp();
00091     alt = LPS25H.getAlt(pres_0, temp_0);
00092     speed = (alt - alt_old) / 0.1;
00093     
00094     if(save && alt <= ALT_SEPARATE && speed < 0.0f && !separate){
00095         separate_c ++;
00096         if(separate_c > 10){
00097             separate_c = 0;
00098             separate = true;
00099         }
00100     }
00101     
00102     ADXL375.getOutput(acc);
00103 }
00104 
00105 /*******************************************************************************
00106 高度リセット
00107 *******************************************************************************/
00108 void resetPT(){
00109     pres_0 = pres;
00110     temp_0 = temp;
00111     
00112     LED_PT = 1;
00113     wait(0.05f);
00114     LED_PT = 0;
00115     wait(0.05f);
00116     LED_PT = 1;
00117     wait(0.05f);
00118     LED_PT = 0;
00119     wait(0.05f);
00120     LED_PT = 1;
00121     wait(0.05f);
00122     LED_PT = 0;
00123 }
00124 
00125 /*******************************************************************************
00126 データをPCで表示
00127 *******************************************************************************/
00128 void printData(){
00129     pc.printf("%.4f[hPa]\t%.2f[degC]\t%.2f[m]  \t", pres, temp, alt);
00130     pc.printf("%.2f, %.2f, %.2f\t", acc[0], acc[1], acc[2]);
00131     pc.printf("%d\r\n", separate);
00132 }
00133 
00134 /*******************************************************************************
00135 PCからのコマンド読み取り
00136 *******************************************************************************/
00137 void readPC(){
00138     char c = pc.getc();
00139     switch(c){
00140         case '1':
00141         startRec();
00142         break;
00143         
00144         case '0':
00145         stopRec();
00146         break;
00147         
00148         case 'R':
00149         resetPT();
00150         break;
00151     }
00152 }
00153 
00154 /*******************************************************************************
00155 データを記録
00156 *******************************************************************************/
00157 void recData(){
00158     if(save){
00159         time_rec = timer_rec.read_us();
00160         sd.printf("%d,", time_rec);
00161         sd.printf("%f,%f,%f,", pres, temp, alt);
00162         sd.printf("%.2f,%.2f,%.2f,", acc[0], acc[1], acc[2]);
00163         sd.printf("%d\r\n", separate);
00164     }
00165 }
00166 
00167 /*******************************************************************************
00168 記録開始
00169 *******************************************************************************/
00170 void startRec(){
00171     if(!save){
00172         tick_print.detach();
00173         
00174         timer_rec.reset();
00175         timer_rec.start();
00176         save = true;
00177         LED_REC = 1;
00178     }
00179 }
00180 
00181 /*******************************************************************************
00182 記録停止
00183 *******************************************************************************/
00184 void stopRec(){
00185     if(save){
00186         save = false;
00187         LED_REC = 0;
00188         timer_rec.stop();
00189         sd.printf("\r\n\n");
00190         
00191         tick_print.attach(&printData, TIME_PRINT);
00192     }
00193 }
00194 
00195 /*******************************************************************************
00196 ボタンが押されたとき
00197 *******************************************************************************/
00198 void buttonPush(){
00199     if(!button_push){
00200         timer_button.start();
00201         button_push = true;
00202     }
00203 }
00204 
00205 /*******************************************************************************
00206 ボタンが離されたとき
00207 *******************************************************************************/
00208 void buttonRelease(){
00209     int time_button;
00210     
00211     if(button_push){
00212         button_push = false;
00213         time_button = timer_button.read();
00214         timer_button.stop();
00215         timer_button.reset();
00216         if(time_button >= 3){
00217             if(!save){
00218                 startRec();
00219             }
00220             else{
00221                 stopRec();
00222             }
00223         }
00224     }
00225 }
00226 
00227 /*******************************************************************************
00228 セットアップ(最初の1回実行)
00229 *******************************************************************************/
00230 void setup(){
00231     wait(0.5f);
00232     
00233     LPS25H.begin(25);
00234     LPS25H.setFIFO(16);
00235     if(LPS25H.whoAmI() == 0){
00236         pc.printf("LPS25H  : OK\r\n");
00237         readSensor();
00238         resetPT();
00239     }
00240     else{
00241         pc.printf("LPS25H  : NG.....\r\n");
00242     }
00243     
00244     ADXL375.setDataRate(ADXL375_3200HZ);
00245     if(ADXL375.whoAmI() == 1){
00246         pc.printf("ADXL375 : OK\r\n");
00247     }
00248     else{
00249         pc.printf("ADXL375 : NG.....\r\n");
00250     }
00251     ADXL375.offset(-0.3f, -0.6f, 0.3f);
00252     wait(0.5f);
00253 }