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

Dependents:   escapeFromYou Sample_LinetraceWithSonicDist

Committer:
natuga117
Date:
Sat Oct 08 22:15:39 2016 +0000
Revision:
7:4a171a80f085
Parent:
6:ba801e0b68df
Child:
8:8bb311d39a99
improved precision

Who changed what in which revision?

UserRevisionLine numberNew contents of line
natuga117 3:b22567a5f7d6 1 #include "mbed.h"
natuga117 3:b22567a5f7d6 2 #include "SonicDist.h"
natuga117 6:ba801e0b68df 3 SonicDist::SonicDist(PinName Trig, PinName Echo)
natuga117 6:ba801e0b68df 4 {
natuga117 3:b22567a5f7d6 5 trig=new DigitalOut(Trig);
natuga117 6:ba801e0b68df 6 echo=new InterruptIn(Echo);
natuga117 3:b22567a5f7d6 7 soundspeed=340;
natuga117 3:b22567a5f7d6 8 startflag=false;
natuga117 6:ba801e0b68df 9 dist=-1;//まだ未測定時かどうかの判定用にdistはありえない値(負の値を代入しておく)
natuga117 3:b22567a5f7d6 10 trig->write(1);
natuga117 3:b22567a5f7d6 11 echo->rise( this, &SonicDist::StartMeasure );
natuga117 3:b22567a5f7d6 12 echo->fall( this, &SonicDist::endMeasure );
natuga117 3:b22567a5f7d6 13 }
natuga117 6:ba801e0b68df 14 void SonicDist::StartMeasure(void)
natuga117 6:ba801e0b68df 15 {
natuga117 7:4a171a80f085 16 if(startflag) {
natuga117 7:4a171a80f085 17 echotime.reset();
natuga117 7:4a171a80f085 18 echotime.start();
natuga117 7:4a171a80f085 19 }
natuga117 3:b22567a5f7d6 20 }
natuga117 6:ba801e0b68df 21 void SonicDist::endMeasure(void)
natuga117 6:ba801e0b68df 22 {
natuga117 7:4a171a80f085 23 if(dist!=-1&& startflag) {
natuga117 3:b22567a5f7d6 24 echotime.stop();
natuga117 3:b22567a5f7d6 25 dist=echotime.read()*soundspeed*1000/2;//mmで出力
natuga117 3:b22567a5f7d6 26 startflag=false;
natuga117 6:ba801e0b68df 27 }
natuga117 3:b22567a5f7d6 28 }
natuga117 6:ba801e0b68df 29 void SonicDist::setTrig(void)
natuga117 6:ba801e0b68df 30 {
natuga117 7:4a171a80f085 31 if(startflag)dist=3000;
natuga117 6:ba801e0b68df 32 startflag=true;
natuga117 3:b22567a5f7d6 33 trig->write(0);
natuga117 6:ba801e0b68df 34 systime.attach(this,&SonicDist::releaseTrig, 0.000010);
natuga117 3:b22567a5f7d6 35 }
natuga117 6:ba801e0b68df 36 void SonicDist::releaseTrig(void)
natuga117 6:ba801e0b68df 37 {
natuga117 3:b22567a5f7d6 38 trig->write(1);
natuga117 3:b22567a5f7d6 39 }
natuga117 6:ba801e0b68df 40 double SonicDist::read(void)
natuga117 6:ba801e0b68df 41 {
natuga117 7:4a171a80f085 42 return SonicDist::read_mm()/10;
natuga117 3:b22567a5f7d6 43 }
natuga117 6:ba801e0b68df 44 double SonicDist::read_mm(void)
natuga117 6:ba801e0b68df 45 {
natuga117 7:4a171a80f085 46 if(dist<0) {
natuga117 7:4a171a80f085 47 SonicDist::takeDist();
natuga117 7:4a171a80f085 48 wait(0.03);
natuga117 7:4a171a80f085 49 }
natuga117 6:ba801e0b68df 50 return dist;
natuga117 3:b22567a5f7d6 51 }
natuga117 6:ba801e0b68df 52 double SonicDist::takeDist(void)
natuga117 6:ba801e0b68df 53 {
natuga117 7:4a171a80f085 54 if(dist==-1) {
natuga117 7:4a171a80f085 55 pulse.attach(this,&SonicDist::setTrig,0.05);
natuga117 7:4a171a80f085 56 dist=-2;
natuga117 7:4a171a80f085 57 }
natuga117 6:ba801e0b68df 58 return (double)dist/10;
natuga117 6:ba801e0b68df 59 }