FET改良版
Dependencies: mbed
us015.cpp@26:05d9d3df4eba, 2019-12-15 (annotated)
- Committer:
- YUPPY
- Date:
- Sun Dec 15 03:27:59 2019 +0000
- Revision:
- 26:05d9d3df4eba
- Parent:
- 7:fae874e898b3
fet
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
YUPPY | 7:fae874e898b3 | 1 | //************************************************** |
YUPPY | 7:fae874e898b3 | 2 | // |
YUPPY | 7:fae874e898b3 | 3 | // us015.cpp |
YUPPY | 7:fae874e898b3 | 4 | // 概要:超音波距離センサ US-015 |
YUPPY | 7:fae874e898b3 | 5 | // |
YUPPY | 7:fae874e898b3 | 6 | //************************************************** |
YUPPY | 7:fae874e898b3 | 7 | #include "mbed.h" |
YUPPY | 7:fae874e898b3 | 8 | #include "us015.h" |
YUPPY | 7:fae874e898b3 | 9 | |
YUPPY | 7:fae874e898b3 | 10 | |
YUPPY | 7:fae874e898b3 | 11 | |
YUPPY | 7:fae874e898b3 | 12 | //********************************* |
YUPPY | 7:fae874e898b3 | 13 | // コンストラクタ |
YUPPY | 7:fae874e898b3 | 14 | //********************************* |
YUPPY | 7:fae874e898b3 | 15 | US015::US015(PinName trg, PinName ech) |
YUPPY | 7:fae874e898b3 | 16 | :_trigerOut(trg),_interruptEcho(ech) |
YUPPY | 7:fae874e898b3 | 17 | { |
YUPPY | 7:fae874e898b3 | 18 | // タイマーの開始 |
YUPPY | 7:fae874e898b3 | 19 | timer.start(); |
YUPPY | 7:fae874e898b3 | 20 | // トリガー信号の定期出力を行う |
YUPPY | 7:fae874e898b3 | 21 | //tk.attach_us(this,&US015::TrigerOut,US015_PERIODIC_TRIGER); |
YUPPY | 7:fae874e898b3 | 22 | // エコーバック信号の立ち上がり・立ち下がり処置の割り込み |
YUPPY | 7:fae874e898b3 | 23 | _interruptEcho.fall(this,&US015::EchoFall); |
YUPPY | 7:fae874e898b3 | 24 | _interruptEcho.rise(this,&US015::EchoRise); |
YUPPY | 7:fae874e898b3 | 25 | } |
YUPPY | 7:fae874e898b3 | 26 | // End of Constructor |
YUPPY | 7:fae874e898b3 | 27 | |
YUPPY | 7:fae874e898b3 | 28 | |
YUPPY | 7:fae874e898b3 | 29 | //********************************* |
YUPPY | 7:fae874e898b3 | 30 | // トリガー信号を出力する |
YUPPY | 7:fae874e898b3 | 31 | //********************************* |
YUPPY | 7:fae874e898b3 | 32 | void US015::TrigerOut() |
YUPPY | 7:fae874e898b3 | 33 | { |
YUPPY | 7:fae874e898b3 | 34 | // トリガー出力として10[us]のパルス信号を出す |
YUPPY | 7:fae874e898b3 | 35 | _trigerOut = US015_TRIGER_ON; |
YUPPY | 7:fae874e898b3 | 36 | wait_us(US015_TRIGER_PALUSE_WIDTH); |
YUPPY | 7:fae874e898b3 | 37 | _trigerOut = US015_TRIGER_OFF; |
YUPPY | 7:fae874e898b3 | 38 | } |
YUPPY | 7:fae874e898b3 | 39 | // End of TrigerOut |
YUPPY | 7:fae874e898b3 | 40 | |
YUPPY | 7:fae874e898b3 | 41 | |
YUPPY | 7:fae874e898b3 | 42 | //********************************* |
YUPPY | 7:fae874e898b3 | 43 | // 距離情報の取得 |
YUPPY | 7:fae874e898b3 | 44 | //********************************* |
YUPPY | 7:fae874e898b3 | 45 | float US015::GetDistance() |
YUPPY | 7:fae874e898b3 | 46 | { |
YUPPY | 7:fae874e898b3 | 47 | return distance; |
YUPPY | 7:fae874e898b3 | 48 | } |
YUPPY | 7:fae874e898b3 | 49 | // End of GetDistance |
YUPPY | 7:fae874e898b3 | 50 | |
YUPPY | 7:fae874e898b3 | 51 | |
YUPPY | 7:fae874e898b3 | 52 | //********************************* |
YUPPY | 7:fae874e898b3 | 53 | // エコーの信号の立ち下がり |
YUPPY | 7:fae874e898b3 | 54 | //********************************* |
YUPPY | 7:fae874e898b3 | 55 | void US015::EchoFall() |
YUPPY | 7:fae874e898b3 | 56 | { |
YUPPY | 7:fae874e898b3 | 57 | //エコー信号の立ち下がり時間を取得 |
YUPPY | 7:fae874e898b3 | 58 | tmEnd=timer.read_us(); |
YUPPY | 7:fae874e898b3 | 59 | |
YUPPY | 7:fae874e898b3 | 60 | // 反射面距離の計算(往復の距離を音速と経過時間から求め、その半分を片道の距離とする) |
YUPPY | 7:fae874e898b3 | 61 | // (エコー受信時間 - トリガー発信時間)* 音速0.340[mm/us]/2 |
YUPPY | 7:fae874e898b3 | 62 | distance = ((tmEnd-tmBegin)*(0.340))/2; |
YUPPY | 7:fae874e898b3 | 63 | } |
YUPPY | 7:fae874e898b3 | 64 | // End of EchoFall |
YUPPY | 7:fae874e898b3 | 65 | |
YUPPY | 7:fae874e898b3 | 66 | |
YUPPY | 7:fae874e898b3 | 67 | //********************************* |
YUPPY | 7:fae874e898b3 | 68 | // エコーの信号の立ち上がり処理 |
YUPPY | 7:fae874e898b3 | 69 | //********************************* |
YUPPY | 7:fae874e898b3 | 70 | void US015::EchoRise() |
YUPPY | 7:fae874e898b3 | 71 | { |
YUPPY | 7:fae874e898b3 | 72 | //エコー信号の立ち上がり時間を記録 |
YUPPY | 7:fae874e898b3 | 73 | tmBegin=timer.read_us(); |
YUPPY | 7:fae874e898b3 | 74 | } |
YUPPY | 7:fae874e898b3 | 75 | // End of EchoRise |