割り込みを利用して超音波測距センサーをバックグラウンドで計測し続けられます。

Dependents:   escapeFromYou Sample_LinetraceWithSonicDist

Files at this revision

API Documentation at this revision

Comitter:
natuga117
Date:
Thu Mar 02 18:01:45 2017 +0000
Parent:
7:4a171a80f085
Commit message:
improved precision

Changed in this revision

SonicDist.cpp Show annotated file Show diff for this revision Revisions of this file
SonicDist.h Show annotated file Show diff for this revision Revisions of this file
diff -r 4a171a80f085 -r 8bb311d39a99 SonicDist.cpp
--- a/SonicDist.cpp	Sat Oct 08 22:15:39 2016 +0000
+++ b/SonicDist.cpp	Thu Mar 02 18:01:45 2017 +0000
@@ -6,6 +6,7 @@
     echo=new InterruptIn(Echo);
     soundspeed=340;
     startflag=false;
+    NoiseCheckFlag=false;
     dist=-1;//まだ未測定時かどうかの判定用にdistはありえない値(負の値を代入しておく)
     trig->write(1);
     echo->rise( this, &SonicDist::StartMeasure );
@@ -13,29 +14,38 @@
 }
 void SonicDist::StartMeasure(void)
 {
-    if(startflag) {
+    if(startflag && NoiseCheckFlag==false) {
         echotime.reset();
-        echotime.start();
+    }else{
+        Postponement.detach();
     }
 }
 void SonicDist::endMeasure(void)
 {
     if(dist!=-1&& startflag) {
-        echotime.stop();
-        dist=echotime.read()*soundspeed*1000/2;//mmで出力
+        distbuf=echotime.read()*soundspeed*1000/2;//mmで出力
+        if(distbuf<10){distbuf=dist;}//立ち上がりノイズと判定
         startflag=false;
+        NoiseCheckFlag=true;
+        Postponement.attach(this,&SonicDist::FallNoiseCheck,0.04);
     }
 }
+
+void SonicDist::FallNoiseCheck(void)//ここに到達すると立下りのノイズではないと判定される
+{
+    dist=distbuf;
+    NoiseCheckFlag=false;
+}
+void SonicDist::Init(void)
+{
+    echotime.start();
+    pulse.attach(this,&SonicDist::setTrig,0.05);
+    dist=-2;
+}
 void SonicDist::setTrig(void)
 {
-    if(startflag)dist=3000;
     startflag=true;
-    trig->write(0);
-    systime.attach(this,&SonicDist::releaseTrig, 0.000010);
-}
-void SonicDist::releaseTrig(void)
-{
-    trig->write(1);
+    trig->write(!trig->read());
 }
 double SonicDist::read(void)
 {
@@ -52,8 +62,7 @@
 double SonicDist::takeDist(void)
 {
     if(dist==-1) {
-        pulse.attach(this,&SonicDist::setTrig,0.05);
-        dist=-2;
+        SonicDist::Init();
     }
     return (double)dist/10;
 }
\ No newline at end of file
diff -r 4a171a80f085 -r 8bb311d39a99 SonicDist.h
--- a/SonicDist.h	Sat Oct 08 22:15:39 2016 +0000
+++ b/SonicDist.h	Thu Mar 02 18:01:45 2017 +0000
@@ -11,18 +11,22 @@
         double read(void);
         double read_mm(void);
         double takeDist(void);
+        void Init(void);
     private:
         void releaseTrig(void);
         void setTrig(void);
         void endMeasure(void);
+        void FallNoiseCheck(void);//立ち下がりノイズの判定
         void StartMeasure(void);
         double soundspeed;//音速
         bool startflag;
-        Timeout systime;
         Ticker pulse;
+        Timeout Postponement;//ノイズ判定を行うための割り込み
         DigitalOut* trig;
         InterruptIn* echo;
         Timer echotime;
+        int distbuf;//仮格納距離(mm)
+        bool NoiseCheckFlag;//ノイズチェックが終了しているかの判定用
         int dist;//距離(mm)
 };
 #endif