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

Dependents:   escapeFromYou Sample_LinetraceWithSonicDist

Committer:
natuga117
Date:
Sat Jul 09 15:10:17 2016 +0000
Revision:
3:b22567a5f7d6
Child:
4:84f4fa630726
renamed

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 3:b22567a5f7d6 3 SonicDist::SonicDist(PinName Trig, PinName Echo){
natuga117 3:b22567a5f7d6 4 trig=new DigitalOut(Trig);
natuga117 3:b22567a5f7d6 5 echopin=Echo;
natuga117 3:b22567a5f7d6 6 // echo=new InterruptIn(Echo);
natuga117 3:b22567a5f7d6 7 soundspeed=340;
natuga117 3:b22567a5f7d6 8 startflag=false;
natuga117 3:b22567a5f7d6 9 trig->write(1);
natuga117 3:b22567a5f7d6 10 setmeasure=true;
natuga117 3:b22567a5f7d6 11 }
natuga117 3:b22567a5f7d6 12 void SonicDist::init(void){
natuga117 3:b22567a5f7d6 13 DigitalIn Ec(echopin);
natuga117 3:b22567a5f7d6 14 echotime.stop();
natuga117 3:b22567a5f7d6 15 echotime.reset();
natuga117 3:b22567a5f7d6 16 trig->write(1);
natuga117 3:b22567a5f7d6 17 wait(0.000010);
natuga117 3:b22567a5f7d6 18 trig->write(0);
natuga117 3:b22567a5f7d6 19 wait(0.000014);
natuga117 3:b22567a5f7d6 20 trig->write(1);
natuga117 3:b22567a5f7d6 21 while(Ec==0);
natuga117 3:b22567a5f7d6 22 SonicDist::StartMeasure();
natuga117 3:b22567a5f7d6 23 while(Ec==1||(double)echotime.read()<0.007);
natuga117 3:b22567a5f7d6 24 SonicDist::endMeasure();
natuga117 3:b22567a5f7d6 25 // Ec.dispose();
natuga117 3:b22567a5f7d6 26 echo=new InterruptIn(echopin);
natuga117 3:b22567a5f7d6 27 echo->rise( this, &SonicDist::StartMeasure );
natuga117 3:b22567a5f7d6 28 echo->fall( this, &SonicDist::endMeasure );
natuga117 3:b22567a5f7d6 29 SonicDist::setTrig();
natuga117 3:b22567a5f7d6 30 }
natuga117 3:b22567a5f7d6 31 void SonicDist::StartMeasure(void){
natuga117 3:b22567a5f7d6 32 echotime.reset();
natuga117 3:b22567a5f7d6 33 echotime.start();
natuga117 3:b22567a5f7d6 34 startflag=true;
natuga117 3:b22567a5f7d6 35 }
natuga117 3:b22567a5f7d6 36 void SonicDist::endMeasure(void){
natuga117 3:b22567a5f7d6 37 SonicDist::CalcDist();
natuga117 3:b22567a5f7d6 38 systime.detach();
natuga117 3:b22567a5f7d6 39 SonicDist::setTrig();
natuga117 3:b22567a5f7d6 40 }
natuga117 3:b22567a5f7d6 41 void SonicDist::CalcDist(void){
natuga117 3:b22567a5f7d6 42 if(startflag){
natuga117 3:b22567a5f7d6 43 echotime.stop();
natuga117 3:b22567a5f7d6 44 dist=echotime.read()*soundspeed*1000/2;//mmで出力
natuga117 3:b22567a5f7d6 45 startflag=false;
natuga117 3:b22567a5f7d6 46 }
natuga117 3:b22567a5f7d6 47 }
natuga117 3:b22567a5f7d6 48 void SonicDist::setTrig(void){
natuga117 3:b22567a5f7d6 49 trig->write(0);
natuga117 3:b22567a5f7d6 50 systime.attach(this,&SonicDist::releaseTrig, 0.000014);
natuga117 3:b22567a5f7d6 51 }
natuga117 3:b22567a5f7d6 52 void SonicDist::releaseTrig(void){
natuga117 3:b22567a5f7d6 53 trig->write(1);
natuga117 3:b22567a5f7d6 54 systime.attach(this,&SonicDist::timeout, 0.007);
natuga117 3:b22567a5f7d6 55 }
natuga117 3:b22567a5f7d6 56 void SonicDist::timeout(void){
natuga117 3:b22567a5f7d6 57 SonicDist::CalcDist();
natuga117 3:b22567a5f7d6 58 SonicDist::setTrig();
natuga117 3:b22567a5f7d6 59 if(startflag){printf("a");}
natuga117 3:b22567a5f7d6 60 }
natuga117 3:b22567a5f7d6 61 double SonicDist::getDist(void){
natuga117 3:b22567a5f7d6 62 if(setmeasure){
natuga117 3:b22567a5f7d6 63 SonicDist::init();
natuga117 3:b22567a5f7d6 64 setmeasure=false;
natuga117 3:b22567a5f7d6 65 }
natuga117 3:b22567a5f7d6 66 return (double)dist/10;
natuga117 3:b22567a5f7d6 67 }
natuga117 3:b22567a5f7d6 68 double SonicDist::getDist_mm(void){
natuga117 3:b22567a5f7d6 69 if(setmeasure){
natuga117 3:b22567a5f7d6 70 SonicDist::init();
natuga117 3:b22567a5f7d6 71 setmeasure=false;
natuga117 3:b22567a5f7d6 72 }
natuga117 3:b22567a5f7d6 73 return dist;
natuga117 3:b22567a5f7d6 74 }