201708能代のメインマイコンの保存データ送信用プログラム

Dependencies:   MPU6050 mbed MS5607

Files at this revision

API Documentation at this revision

Comitter:
oichan
Date:
Tue Aug 15 11:51:32 2017 +0000
Parent:
2:1dbaef45ae46
Child:
4:918ea47cc35c
Commit message:
201708????????????????????????

Changed in this revision

MPU6050.lib Show annotated file Show diff for this revision Revisions of this file
SDFileSystem.lib Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MPU6050.lib	Tue Aug 15 11:51:32 2017 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/Sissors/code/MPU6050/#5c63e20c50f3
--- a/SDFileSystem.lib	Tue Jul 18 14:22:08 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/mbed_official/code/SDFileSystem/#8db0d3b02cec
--- a/main.cpp	Tue Jul 18 14:22:08 2017 +0000
+++ b/main.cpp	Tue Aug 15 11:51:32 2017 +0000
@@ -1,50 +1,66 @@
+
+/*  データ取得&送信するプログラム             *
+ *  各データの終わりの印として'$'を送信        *
+ *  なぜか割り込みタイマの周期が一定にならない   */
+
 #include "mbed.h"
-#include "string.h"
-#include "SDFileSystem.h"
+#include "MPU6050.h"
 
-#define BUFFNUM 50
-#define CHARNUM 16
+#define ACC 16384
+#define BUFFNUM 30 /*受信側のBUFFNUMと同値*/
+#define CHARANUM 30
+#define RATE 30.0   /*RATE[Hz]でデータ取得*/
 
-SDFileSystem sd(dp2, dp1, dp6, dp14, "sd");
-Serial mainCP(dp16, dp15);
+MPU6050 mpu(p9,p10);
+Serial  subCP(p28, p27);
+Ticker  log_timer;
+Ticker  send_timer;
+Timer   t;
 
-bool row = false;
-int8_t col=0, cnt=0;
-char data[2][BUFFNUM][CHARNUM] = {};
-FILE *fp;
+float Time[2][BUFFNUM] = {};
+float Data[2][BUFFNUM] = {};
+char  Moji[2][BUFFNUM][CHARANUM] = {};
+bool Row = false;
+int8_t Col = 0;
+int Send_cnt = 0;
+int Moji_cnt = 0;
 
 void _log();
+void _send();
 
-int main() {
-  printf("Hello World!\r\n");
-  mkdir("/sd/mydir", 0777);
-  fp = fopen("/sd/mydir/log.txt", "w");
-  if(fp == NULL) error("Could not open file for write\r\n");
-  fprintf(fp, "Hello fun SD Card World!\n");
-  fclose(fp);
-  mainCP.attach(&_log, Serial::RxIrq);
-  while(1);
+int main(){
+    t.start();
+    log_timer.attach(_log,1.0/RATE);
+    while(1);
 }
 
-void _log(){//受信割り込み用.\n区切りで保存する.
-  data[row][col][cnt] = mainCP.getc();
-  cnt++;
-  if(data[row][col][cnt]=='\n'){
-    col++;
-    cnt = 0;
-  }else if(cnt==CHARNUM){
-    memset(&data[row][col][0],'\0',CHARNUM);
-    cnt = 0;
-  }
+/*  データ取得   */
+void _log(){
+    //TODO: 高度データの取得
+    //TODO: 取得する加速度を合成加速度に変更
+    Data[Row][Col] = (float)mpu.getAcceleroRawZ()/ACC;
+    Time[Row][Col] = t.read();
+    Col++;
+    if(Col == BUFFNUM){
+        Col = 0;
+        Row = !Row;
+        send_timer.attach(_send,(BUFFNUM/(CHARANUM*RATE*RATE))*0.8);
+    }
+}
 
-  if(col==BUFFNUM){
-    col = 0;
-    row = !row;
-    fp = fopen("/sd/mydir/log.txt", "a");
-    for (int i = 0; i < BUFFNUM; i++){
-      fprintf(fp,&data[!row][i][0]);
+void _send(){
+    if(Moji_cnt==0){
+        sprintf(&Moji[Row][Send_cnt][0],"%f, %f",Time[!Row][Send_cnt],Data[!Row][Send_cnt]);
     }
-    fclose(fp);
-    memset(&data[row][0][0],'\0',BUFFNUM*CHARNUM);
-  }
+    subCP.putc(Moji[Row][Send_cnt][Moji_cnt]);
+    Moji_cnt++;
+    if(Moji_cnt==CHARANUM || Moji[Row][Send_cnt][Moji_cnt]=='\0'){
+        Moji_cnt = 0;
+        subCP.putc('$');
+        Send_cnt++;
+        if(Send_cnt==BUFFNUM){
+            Send_cnt = 0;
+            send_timer.detach();
+        }
+    }
 }
\ No newline at end of file