割り込みを利用して超音波測距センサーをバックグラウンドで計測し続けられます。
Dependents: escapeFromYou Sample_LinetraceWithSonicDist
SonicDist.cpp@8:8bb311d39a99, 2017-03-02 (annotated)
- Committer:
- natuga117
- Date:
- Thu Mar 02 18:01:45 2017 +0000
- Revision:
- 8:8bb311d39a99
- Parent:
- 7:4a171a80f085
improved precision
Who changed what in which revision?
User | Revision | Line number | New 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 | 8:8bb311d39a99 | 9 | NoiseCheckFlag=false; |
natuga117 | 6:ba801e0b68df | 10 | dist=-1;//まだ未測定時かどうかの判定用にdistはありえない値(負の値を代入しておく) |
natuga117 | 3:b22567a5f7d6 | 11 | trig->write(1); |
natuga117 | 3:b22567a5f7d6 | 12 | echo->rise( this, &SonicDist::StartMeasure ); |
natuga117 | 3:b22567a5f7d6 | 13 | echo->fall( this, &SonicDist::endMeasure ); |
natuga117 | 3:b22567a5f7d6 | 14 | } |
natuga117 | 6:ba801e0b68df | 15 | void SonicDist::StartMeasure(void) |
natuga117 | 6:ba801e0b68df | 16 | { |
natuga117 | 8:8bb311d39a99 | 17 | if(startflag && NoiseCheckFlag==false) { |
natuga117 | 7:4a171a80f085 | 18 | echotime.reset(); |
natuga117 | 8:8bb311d39a99 | 19 | }else{ |
natuga117 | 8:8bb311d39a99 | 20 | Postponement.detach(); |
natuga117 | 7:4a171a80f085 | 21 | } |
natuga117 | 3:b22567a5f7d6 | 22 | } |
natuga117 | 6:ba801e0b68df | 23 | void SonicDist::endMeasure(void) |
natuga117 | 6:ba801e0b68df | 24 | { |
natuga117 | 7:4a171a80f085 | 25 | if(dist!=-1&& startflag) { |
natuga117 | 8:8bb311d39a99 | 26 | distbuf=echotime.read()*soundspeed*1000/2;//mmで出力 |
natuga117 | 8:8bb311d39a99 | 27 | if(distbuf<10){distbuf=dist;}//立ち上がりノイズと判定 |
natuga117 | 3:b22567a5f7d6 | 28 | startflag=false; |
natuga117 | 8:8bb311d39a99 | 29 | NoiseCheckFlag=true; |
natuga117 | 8:8bb311d39a99 | 30 | Postponement.attach(this,&SonicDist::FallNoiseCheck,0.04); |
natuga117 | 6:ba801e0b68df | 31 | } |
natuga117 | 3:b22567a5f7d6 | 32 | } |
natuga117 | 8:8bb311d39a99 | 33 | |
natuga117 | 8:8bb311d39a99 | 34 | void SonicDist::FallNoiseCheck(void)//ここに到達すると立下りのノイズではないと判定される |
natuga117 | 8:8bb311d39a99 | 35 | { |
natuga117 | 8:8bb311d39a99 | 36 | dist=distbuf; |
natuga117 | 8:8bb311d39a99 | 37 | NoiseCheckFlag=false; |
natuga117 | 8:8bb311d39a99 | 38 | } |
natuga117 | 8:8bb311d39a99 | 39 | void SonicDist::Init(void) |
natuga117 | 8:8bb311d39a99 | 40 | { |
natuga117 | 8:8bb311d39a99 | 41 | echotime.start(); |
natuga117 | 8:8bb311d39a99 | 42 | pulse.attach(this,&SonicDist::setTrig,0.05); |
natuga117 | 8:8bb311d39a99 | 43 | dist=-2; |
natuga117 | 8:8bb311d39a99 | 44 | } |
natuga117 | 6:ba801e0b68df | 45 | void SonicDist::setTrig(void) |
natuga117 | 6:ba801e0b68df | 46 | { |
natuga117 | 6:ba801e0b68df | 47 | startflag=true; |
natuga117 | 8:8bb311d39a99 | 48 | trig->write(!trig->read()); |
natuga117 | 3:b22567a5f7d6 | 49 | } |
natuga117 | 6:ba801e0b68df | 50 | double SonicDist::read(void) |
natuga117 | 6:ba801e0b68df | 51 | { |
natuga117 | 7:4a171a80f085 | 52 | return SonicDist::read_mm()/10; |
natuga117 | 3:b22567a5f7d6 | 53 | } |
natuga117 | 6:ba801e0b68df | 54 | double SonicDist::read_mm(void) |
natuga117 | 6:ba801e0b68df | 55 | { |
natuga117 | 7:4a171a80f085 | 56 | if(dist<0) { |
natuga117 | 7:4a171a80f085 | 57 | SonicDist::takeDist(); |
natuga117 | 7:4a171a80f085 | 58 | wait(0.03); |
natuga117 | 7:4a171a80f085 | 59 | } |
natuga117 | 6:ba801e0b68df | 60 | return dist; |
natuga117 | 3:b22567a5f7d6 | 61 | } |
natuga117 | 6:ba801e0b68df | 62 | double SonicDist::takeDist(void) |
natuga117 | 6:ba801e0b68df | 63 | { |
natuga117 | 7:4a171a80f085 | 64 | if(dist==-1) { |
natuga117 | 8:8bb311d39a99 | 65 | SonicDist::Init(); |
natuga117 | 7:4a171a80f085 | 66 | } |
natuga117 | 6:ba801e0b68df | 67 | return (double)dist/10; |
natuga117 | 6:ba801e0b68df | 68 | } |