加速度とジャイロを100HzでSDに保存するプログラム

Dependencies:   MPU6050 SDFileSystem mbed

Fork of SD_100Hz by kohei oikawa

Committer:
oichan
Date:
Sat Mar 10 02:39:47 2018 +0000
Revision:
3:1410f9acca63
Parent:
2:ac793e1667ec
201803??VASE?100Hz????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:bdbd3d6fc5d5 1 #include "mbed.h"
mbed_official 0:bdbd3d6fc5d5 2 #include "SDFileSystem.h"
oichan 2:ac793e1667ec 3 #include "MPU6050.h"
oichan 2:ac793e1667ec 4
oichan 2:ac793e1667ec 5 #define RATE 100 //サンプリングレート[Hz]
oichan 2:ac793e1667ec 6 #define SAVE_NUM 100 //一度に保存するデータ数 SAVE_NUM/RATE [s]ごとに保存する
oichan 2:ac793e1667ec 7
oichan 3:1410f9acca63 8 SDFileSystem sd(p5, p6, p7, p8, "sd");
oichan 2:ac793e1667ec 9 MPU6050 mpu(p9,p10);
oichan 2:ac793e1667ec 10 Timer jikan;
oichan 2:ac793e1667ec 11 Ticker get_t;
oichan 2:ac793e1667ec 12 Serial pc(USBTX,USBRX);
oichan 2:ac793e1667ec 13 FILE *bp;
oichan 2:ac793e1667ec 14
oichan 2:ac793e1667ec 15 void _getData();
oichan 2:ac793e1667ec 16
oichan 3:1410f9acca63 17 float Data[2][SAVE_NUM][7]={0};
oichan 2:ac793e1667ec 18 int Data_cnt = 0;
oichan 2:ac793e1667ec 19 bool row;
oichan 2:ac793e1667ec 20
oichan 2:ac793e1667ec 21 int main(){
oichan 2:ac793e1667ec 22 pc.printf("Hello!\r\n");
oichan 3:1410f9acca63 23 mpu.setAcceleroRange(3);
oichan 3:1410f9acca63 24 mpu.setGyroRange(3);
mbed_official 0:bdbd3d6fc5d5 25 mkdir("/sd/mydir", 0777);
oichan 2:ac793e1667ec 26 bp = fopen("/sd/mydir/data.bin","ab"); //ファイルは作成は時間がかかるため先にやる
oichan 2:ac793e1667ec 27 fclose(bp);
oichan 2:ac793e1667ec 28 jikan.start();
oichan 2:ac793e1667ec 29 get_t.attach(&_getData,1.0/RATE);
oichan 2:ac793e1667ec 30 while(1){
oichan 2:ac793e1667ec 31 if(Data_cnt==SAVE_NUM){
oichan 2:ac793e1667ec 32 Data_cnt=0;
oichan 2:ac793e1667ec 33 row = !row;
oichan 2:ac793e1667ec 34 bp = fopen("/sd/mydir/data.bin","ab");
oichan 3:1410f9acca63 35 fwrite(&Data[!row][0][0],sizeof(float),7*SAVE_NUM,bp);
oichan 2:ac793e1667ec 36 fclose(bp);
oichan 2:ac793e1667ec 37 }
oichan 3:1410f9acca63 38 if(jikan.read()==30.0*60.0)jikan.reset();
mbed_official 0:bdbd3d6fc5d5 39 }
oichan 2:ac793e1667ec 40 return 0;
mbed_official 0:bdbd3d6fc5d5 41 }
oichan 2:ac793e1667ec 42
oichan 2:ac793e1667ec 43 void _getData(){
oichan 2:ac793e1667ec 44 Data[row][Data_cnt][0] = jikan.read();
oichan 2:ac793e1667ec 45 mpu.getAccelero(&Data[row][Data_cnt][1]);
oichan 3:1410f9acca63 46 mpu.getGyro(&Data[row][Data_cnt][4]);
oichan 2:ac793e1667ec 47 Data_cnt++;
oichan 2:ac793e1667ec 48 }