test

Dependencies:   mbed TB6612FNG HMC6352

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers us015.cpp Source File

us015.cpp

00001 //**************************************************
00002 //
00003 // us015.cpp
00004 // 概要:超音波距離センサ US-015
00005 //
00006 //**************************************************
00007 #include "mbed.h"
00008 #include "us015.h"
00009 
00010 
00011 
00012 //*********************************
00013 // コンストラクタ
00014 //*********************************
00015 US015::US015(PinName trg, PinName ech)
00016     :_trigerOut(trg),_interruptEcho(ech)
00017 {
00018     // タイマーの開始
00019     timer.start();
00020     // トリガー信号の定期出力を行う
00021     //tk.attach_us(this,&US015::TrigerOut,US015_PERIODIC_TRIGER);
00022     // エコーバック信号の立ち上がり・立ち下がり処置の割り込み
00023     _interruptEcho.fall(this,&US015::EchoFall);
00024     _interruptEcho.rise(this,&US015::EchoRise);
00025 }
00026 // End of Constructor
00027 
00028 
00029 //*********************************
00030 //  トリガー信号を出力する
00031 //*********************************
00032 void US015::TrigerOut()
00033 {
00034     // トリガー出力として10[us]のパルス信号を出す
00035     _trigerOut = US015_TRIGER_ON;
00036     wait_us(US015_TRIGER_PALUSE_WIDTH);    
00037     _trigerOut = US015_TRIGER_OFF;
00038 }
00039 // End of TrigerOut
00040 
00041 
00042 //*********************************
00043 // 距離情報の取得
00044 //*********************************
00045 float US015::GetDistance()
00046 {
00047     return distance;    
00048 }
00049 // End of GetDistance
00050 
00051 
00052 //*********************************
00053 // エコーの信号の立ち下がり
00054 //*********************************
00055 void US015::EchoFall()
00056 {   
00057     //エコー信号の立ち下がり時間を取得
00058     tmEnd=timer.read_us();
00059 
00060     // 反射面距離の計算(往復の距離を音速と経過時間から求め、その半分を片道の距離とする)
00061     // (エコー受信時間 - トリガー発信時間)* 音速0.340[mm/us]/2
00062     distance = ((tmEnd-tmBegin)*(0.340))/2;
00063 }
00064 // End of EchoFall
00065 
00066 
00067 //*********************************
00068 // エコーの信号の立ち上がり処理
00069 //*********************************
00070 void US015::EchoRise()
00071 {
00072     //エコー信号の立ち上がり時間を記録
00073     tmBegin=timer.read_us();
00074 }
00075 // End of EchoRise