This program is for Cansat made with BreadBoard which have pressure sensor LPS331. This program is based on http://mbed.org/users/nyamfg/code/LPS331_HelloWorld/

Dependencies:   mbed

Committer:
ohtsuka
Date:
Wed Sep 10 04:34:42 2014 +0000
Revision:
10:40521f3084ba
Parent:
9:3285ed711197
Child:
11:3342540c22d8
??????????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nyamfg 0:1a3c5ad01539 1 #include "mbed.h"
ohtsuka 7:8407141d1f70 2 #include <math.h>
nyamfg 0:1a3c5ad01539 3 #include "LPS331_I2C.h"
nyamfg 0:1a3c5ad01539 4
ohtsuka 7:8407141d1f70 5 //気圧計算用
ohtsuka 7:8407141d1f70 6 #define P0 1013.25f //海面気圧(hPa)
ohtsuka 7:8407141d1f70 7 #define TEMP 20 //気温(度)
ohtsuka 7:8407141d1f70 8
ohtsuka 4:4c01d79dd741 9 DigitalOut myled(LED1);
ohtsuka 6:496f4e967298 10 DigitalOut JPlow(p5); // p6ピンでLowを検出させるために使用。隣同士(p5,p6)のピンをショートさせるとログ出力停止
ohtsuka 3:cb51cc4a041d 11 DigitalIn JP(p6);
ohtsuka 3:cb51cc4a041d 12 LocalFileSystem local("local");
ohtsuka 4:4c01d79dd741 13
ohtsuka 8:505d06bba7b1 14 Serial telem(p13,p14);
ohtsuka 8:505d06bba7b1 15
ohtsuka 3:cb51cc4a041d 16 Timer t;
nyamfg 0:1a3c5ad01539 17
ohtsuka 10:40521f3084ba 18 //地面との相対高度算出用
ohtsuka 10:40521f3084ba 19 #define GROUND_AVE_NUM 10
ohtsuka 10:40521f3084ba 20 float ground_height=0;
ohtsuka 9:3285ed711197 21
ohtsuka 4:4c01d79dd741 22 LPS331_I2C lps331(p9, p10, LPS331_I2C_SA0_HIGH); // 気圧センサ
ohtsuka 4:4c01d79dd741 23
nyamfg 0:1a3c5ad01539 24 int main() {
ohtsuka 10:40521f3084ba 25 int i;
ohtsuka 9:3285ed711197 26 float temp, pres;
ohtsuka 6:496f4e967298 27 JPlow = 0;
ohtsuka 4:4c01d79dd741 28 JP.mode(PullUp);
ohtsuka 4:4c01d79dd741 29
nyamfg 0:1a3c5ad01539 30 lps331.setResolution(LPS331_I2C_PRESSURE_AVG_512, LPS331_I2C_TEMP_AVG_128);
ohtsuka 4:4c01d79dd741 31 lps331.setDataRate(LPS331_I2C_DATARATE_7HZ); // 7Hz(1秒間に7回更新)
ohtsuka 4:4c01d79dd741 32 lps331.setActive(true); // 動作開始
ohtsuka 3:cb51cc4a041d 33
ohtsuka 4:4c01d79dd741 34 FILE *fp = fopen("/local/data.txt", "a"); // 「追記」でファイルを開く
ohtsuka 4:4c01d79dd741 35 fprintf(fp,"BBsat start. **********\r\n"); // fprintf()は、ファイルに文字を出力
ohtsuka 3:cb51cc4a041d 36 fclose(fp);
nyamfg 0:1a3c5ad01539 37
ohtsuka 8:505d06bba7b1 38 telem.printf("BBsat start. **********\r\n"); // テレメトリ用出力
ohtsuka 8:505d06bba7b1 39
ohtsuka 3:cb51cc4a041d 40 t.start();
ohtsuka 9:3285ed711197 41
ohtsuka 10:40521f3084ba 42 // 地面の高度を求める
ohtsuka 10:40521f3084ba 43 for(i=0;i<GROUND_AVE_NUM;i++) {
ohtsuka 10:40521f3084ba 44 pres = lps331.getPressure(); // 気圧
ohtsuka 10:40521f3084ba 45 ground_height += lps331.getAltitude(P0, TEMP, pres);
ohtsuka 10:40521f3084ba 46 wait(1/7);
ohtsuka 10:40521f3084ba 47 }
ohtsuka 10:40521f3084ba 48 ground_height = ground_height / GROUND_AVE_NUM;
nyamfg 2:70ce034cfcfc 49
ohtsuka 4:4c01d79dd741 50 while(1) {
ohtsuka 4:4c01d79dd741 51 int ms=0; // リセットからの経過時間(単位:ミリ秒(MilliSecond)
ohtsuka 7:8407141d1f70 52 float height; // 高度
ohtsuka 9:3285ed711197 53 pres=lps331.getPressure(); // 気圧
ohtsuka 9:3285ed711197 54 temp=lps331.getTemperature(); // 気温
ohtsuka 3:cb51cc4a041d 55
ohtsuka 8:505d06bba7b1 56 // 高度算出
ohtsuka 9:3285ed711197 57 height = lps331.getAltitude(P0, TEMP, pres);
ohtsuka 7:8407141d1f70 58
ohtsuka 4:4c01d79dd741 59 if(JP==0) { // JPが0(GND)だったら、
ohtsuka 4:4c01d79dd741 60 break; //whileのループを終了
ohtsuka 3:cb51cc4a041d 61 }
ohtsuka 3:cb51cc4a041d 62
ohtsuka 4:4c01d79dd741 63 ms = t.read_ms(); //現在時刻を取得
ohtsuka 3:cb51cc4a041d 64
ohtsuka 4:4c01d79dd741 65 FILE *fp = fopen("/local/data.txt", "a");
ohtsuka 10:40521f3084ba 66 fprintf(fp, "%10d,%6.2f,%4.1f %5.1f \r\n", ms, pres, temp, height); // 時刻,気圧,気温,高度を出力
ohtsuka 10:40521f3084ba 67 printf( "%10d,%6.2f,%4.1f,Ground:%5.1f Height:%5.1f Diff%5.1f \r\n", ms, pres, temp, ground_height, height, height-ground_height);
ohtsuka 3:cb51cc4a041d 68 fclose(fp);
ohtsuka 4:4c01d79dd741 69
ohtsuka 10:40521f3084ba 70 telem.printf("%5.1f\n", height-ground_height); // 高度を出力
ohtsuka 4:4c01d79dd741 71 wait(0.05); // LED点滅
ohtsuka 4:4c01d79dd741 72 myled = 1;
ohtsuka 3:cb51cc4a041d 73 wait(0.05);
ohtsuka 4:4c01d79dd741 74 myled = 0;
nyamfg 0:1a3c5ad01539 75 }
nyamfg 0:1a3c5ad01539 76 }