Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed SDFileSystem_
Diff: main.cpp
- Revision:
- 27:e7bc38c6399c
- Parent:
- 26:7c2030d5878f
- Child:
- 28:ea423204d626
--- a/main.cpp Fri Sep 03 10:22:59 2021 +0000 +++ b/main.cpp Mon Sep 06 07:23:15 2021 +0000 @@ -1,6 +1,7 @@ #include "mbed.h" #include "SDFileSystem.h" #include <time.h> +#include <errno.h> /* This basic example just shows how to read the ADC internal channels raw values. @@ -34,6 +35,9 @@ //#define TIMEINTERVAL //キャリブレーション 仮値 +//#define OFFSET_X 220 +//#define OFFSET_Y 220 +//#define OFFSET_Z -40 #define OFFSET_X 375 #define OFFSET_Y 340 #define OFFSET_Z -200 @@ -64,8 +68,8 @@ unsigned long timecount = 0; int oldcount = 0; //uint8_t maxacc = 0; -float max_g=0; -float now_g=0; +long max_g=0; +long now_g=0; int update=0; char buffer1[512] = {}; char buffer2[512] = {}; @@ -74,13 +78,9 @@ char filename1[32]= {}; char filename2[32]= {}; int ret = 0,init=1; -//int timer_flag = 0; /* void timer(){ - timer_flag = 1; - timecount++; - oldcount++; } */ @@ -106,16 +106,22 @@ //az+= tz; //as+= scr; //cnt++; - pc.printf("new x:%2.2f y:%2.2f,z:%2.2f scaler = %2.2f \r\n",(float)tx*0.005,(float)ty*0.005,(float)tz*0.005,(float)scr*0.005); + pc.printf("time(%d)new x:%2.2f y:%2.2f,z:%2.2f scaler = %2.2f \r\n",time(NULL),(float)tx*0.005,(float)ty*0.005,(float)tz*0.005,(float)scr*0.005); //pc.printf("new x:%d y:%d,z:%d scaler = %d \r\n",tx,ty,tz,scr); if ( max_g < scr) { max_g = scr; update=1; - //pc.printf("x:%d y:%d,z:%d scaler = %d max g=%2.2fG \r\n",tx,ty,tz,scr, (float)scr/11.0f); - //pc.printf("new x:%d y:%d,z:%d scaler = %d max g=%2.2fG \r\n",tx,ty,tz,scr, (float)max_g/11.0f); + pc.printf("%max g=%d \r\n",max_g); //Save EEPROM - writeEEPROMByte(0, max_g); + //writeEEPROMByte(0, max_g); + for(int i = 0; i < 4; i++){ + pc.printf("writerom 0x%x\r\n",(max_g >> 8*i) & 0xFF); + writeEEPROMByte(i, (max_g >> 8*i) & 0xFF); + } + //for(int i = 0; i < 4; i++){ + //pc.printf("readEEPROM:0x%x\r\n",readEEPROMByte(i) << 8*i); + //} } sprintf(stracc1,"%d,%2.2f,%2.2f,%2.2f,%2.2f\r\n",timecount,(float)tx*0.005,(float)ty*0.005,(float)tz*0.005,(float)scr*0.005); @@ -124,15 +130,13 @@ //pc.printf("buffer over. write file2\r\n"); ret = fprintf(fp1,"%s",buffer1); - if(ret == 0){ - pc.printf("fp1 write failed. Reboot!!!\r\n"); + if(ret <= 0){ + pc.printf("fp1 write failed:%d. errno:%d. Reboot!!!\r\n",ret,errno); NVIC_SystemReset(); } - memset(buffer1,0,sizeof(buffer1)); sprintf(buffer1,"%s",stracc1); - } else { sprintf(buffer1,"%s%s",buffer1,stracc1); } @@ -147,8 +151,8 @@ if(strlen(buffer2)+ strlen(stracc2)> 512) { ret = fprintf(fp2,"%s",buffer2); - if(ret == 0){ - pc.printf("fp2 write failed. Reboot!!!\r\n"); + if(ret <= 0){ + pc.printf("fp2 write failed:%d. errno:%d. Reboot!!!\r\n",ret,errno); NVIC_SystemReset(); } @@ -163,48 +167,51 @@ if(time(NULL) > 86400){ //Create New File set_time(NULL); + //pc.printf("time over:%d\r\n",time(NULL)); timecount = 0; fclose(fp1); + fp1 = NULL; fclose(fp2); + fp2 = NULL; lognum1++; lognum2++; sprintf(filename1,"/sd/new_%d",lognum1); sprintf(filename2,"/sd/old_%d",lognum2); fp1 = fopen(filename1,"a"); if(!fp1){ - pc.printf("fp1 create file failed\r\n"); + pc.printf("fp1 create file failed:%d\r\n",errno); NVIC_SystemReset(); } fp2 = fopen(filename2,"a"); if(!fp2){ - pc.printf("fp2 create file failed\r\n"); + pc.printf("fp2 create file failed:%d\r\n",errno); NVIC_SystemReset(); } } - - - //timer_flag = 0; } int main() { - //h3dacc.init(H3LIS331DL_ODR_50Hz, H3LIS331DL_NORMAL,H3LIS331DL_FULLSCALE_8);//これで初期化している FULLSCALE_8=400G - //h3dacc.setHPFMode(H3LIS331DL_HPM_NORMAL_MODE_RES);//High Pass Filter ON - //h3dacc.setHPFCutOFF(H3LIS331DL_HPFCF_1); - sprintf(filename1,"/sd/new_%d",lognum1); sprintf(filename2,"/sd/old_%d",lognum2); fp1 = fopen(filename1,"a"); if(!fp1) { - pc.printf("fp1(%s) open failed\r\n",filename1); + pc.printf("fp1(%s) open failed errno:%d\r\n",filename1,errno); //NVIC_SystemReset(); } fp2 = fopen(filename2,"a"); if(!fp2) { - pc.printf("fp2(%s) open failed\r\n",filename2); + pc.printf("fp2(%s) open failed errno:%d\r\n",filename2,errno); //NVIC_SystemReset(); } + + //Read Max G + for(int i = 0; i < 4; i++){ + //pc.printf("readEEPROM:0x%x\r\n",readEEPROMByte(i)); + max_g |= readEEPROMByte(i) << 8*i; + } + pc.printf("read max_g from eeprom:%d\r\n",max_g); while(init) { init = initLIS331(); @@ -215,12 +222,10 @@ //interrupt.attach_us(&timer,20000);//20ms while(1) { - //if(timer_flag){ - sub(); - timecount++; - oldcount++; - //} - wait_ms(10); + sub(); + timecount++; + oldcount++; + wait_ms(20); } }