![](/media/cache/group/gazou_0224.jpg.50x50_q85.jpg)
LowpassFilter(仮)?を使って加速度の値をフィルタリングして角度をもとめてます。ローカルファイルシステムを使って値をグラフ化できるようにしてます。 ローパスフィルタについてアドバイスがあればお願いします。
Dependencies: LIS3DH_Hello mbed
Fork of LocalFileSystem_HelloWorld by
Diff: main.cpp
- Revision:
- 2:69e9bf864751
- Parent:
- 1:6bf0bf2bbb31
- Child:
- 3:edc2c71d87b1
--- a/main.cpp Fri Mar 27 20:14:46 2015 +0000 +++ b/main.cpp Mon Mar 30 15:33:24 2015 +0000 @@ -1,24 +1,42 @@ -/* mbed Example Program - * Copyright (c) 2006-2014 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ #include "mbed.h" - -LocalFileSystem local("local"); // Create the local filesystem under the name "local" - +#include "LIS3DH.h"//自作ライブラリ +#define pi 3.141592 +#define filter 0.8//俺的なローパスフィルタの係数一番いい感じなやつ(2015/3/31) +LIS3DH I2c(p9,p10); //sda,scl +Serial pc(USBTX,USBRX); +LocalFileSystem local("local"); +DigitalOut myled(LED4); +//角度を算出する関数(ローパスフィルタを使用) +float angle (float nowx,float nowy) { + static float x = 0,y = 0; + float theta,deg,getx,gety; + getx = filter*x + (1-filter)*nowx; + gety = filter*y + (1-filter)*nowy; + theta = atan(getx/gety); + deg = theta * 180 / pi; + x = nowx; + y = nowy; + return deg; +} int main() { - FILE *fp = fopen("/local/out.txt", "w"); // Open "out.txt" on the local file system for writing - fprintf(fp, "Hello World!"); - fclose(fp); + FILE *fp; + //float val; //操作量 + float data[3];//加速度の値を格納 + if ( NULL == (fp = fopen( "/local/test.csv", "w" )) ) + error( "" ); + + myled = 1; + + for ( int i = 0; i < 10000; i++ ) { + int check = I2c.start();//LIS3DHの動作チェック + if (check == 1) { + I2c.read_data(data);//LIS3DHより加速度を入手 + data[3] = angle(data[0],data[1]); + fprintf( fp, "%f\n", data[3] ); + //wait( 0.1 ); + } + } + + fclose( fp ); + myled = 0; } \ No newline at end of file