Toyoshi Yoshimoto / us015

Files at this revision

API Documentation at this revision

Comitter:
jm6wud
Date:
Thu Oct 22 13:14:43 2020 +0000
Parent:
1:891c2c84286d
Child:
3:0a8266796866
Commit message:
jm6wud

Changed in this revision

us015.cpp Show annotated file Show diff for this revision Revisions of this file
us015.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/us015.cpp	Thu Oct 22 13:14:43 2020 +0000
@@ -0,0 +1,73 @@
+//**************************************************
+//
+// us015.cpp
+// 概要:超音波距離センサ US-015
+//
+//**************************************************
+#include "mbed.h"
+#include "us015.h"
+
+//*********************************
+// コンストラクタ
+//*********************************
+US015::US015(PinName trg, PinName ech)
+    :_trigerOut(trg),_interruptEcho(ech)
+{
+    // タイマーの開始
+    timer.start();
+    // トリガー信号の定期出力を行う
+    //tk.attach_us(this,&US015::TrigerOut,US015_PERIODIC_TRIGER);
+    // エコーバック信号の立ち上がり・立ち下がり処置の割り込み
+    _interruptEcho.fall(this,&US015::EchoFall);
+    _interruptEcho.rise(this,&US015::EchoRise);
+}
+// End of Constructor
+ 
+ 
+//*********************************
+//  トリガー信号を出力する
+//*********************************
+void US015::TrigerOut()
+{
+    // トリガー出力として10[us]のパルス信号を出す
+    _trigerOut = US015_TRIGER_ON;
+    wait_us(US015_TRIGER_PALUSE_WIDTH);    
+    _trigerOut = US015_TRIGER_OFF;
+}
+// End of TrigerOut
+ 
+ 
+//*********************************
+// 距離情報の取得
+//*********************************
+float US015::GetDistance()
+{
+    return distance;    
+}
+// End of GetDistance
+ 
+ 
+//*********************************
+// エコーの信号の立ち下がり
+//*********************************
+void US015::EchoFall()
+{   
+    //エコー信号の立ち下がり時間を取得
+    tmEnd=timer.read_us();
+ 
+    // 反射面距離の計算(往復の距離を音速と経過時間から求め、その半分を片道の距離とする)
+    // (エコー受信時間 - トリガー発信時間)* 音速0.340[mm/us]/2
+    distance = ((tmEnd-tmBegin)*(0.340))/2;
+}
+// End of EchoFall
+ 
+ 
+//*********************************
+// エコーの信号の立ち上がり処理
+//*********************************
+void US015::EchoRise()
+{
+    //エコー信号の立ち上がり時間を記録
+    tmBegin=timer.read_us();
+}
+// End of EchoRise
--- a/us015.h	Thu Oct 22 12:35:26 2020 +0000
+++ b/us015.h	Thu Oct 22 13:14:43 2020 +0000
@@ -3,17 +3,15 @@
 #define __US015_H
  
 #include "mbed.h"
- 
- 
+
 //====================================
 // define定義
 //====================================
 #define US015_TRIGER_ON             1    // トリガーON
 #define US015_TRIGER_OFF            0    // トリガーOFF
 #define US015_TRIGER_PALUSE_WIDTH   10   // トリガーの幅
-#define US015_PERIODIC_TRIGER 100000    // 音響測距トリガー
-#define US015_SOUND_OF_SPEED  0.340           // 音速[mm/us]
- 
+#define US015_PERIODIC_TRIGER 100000     // 音響測距トリガー
+#define US015_SOUND_OF_SPEED  0.340      // 音速[mm/us]
  
 //====================================
 // 音響測距センサ(US015)の制御クラス
@@ -37,5 +35,4 @@
         void EchoFall();                        // エコーの立ち下がり処理
         void EchoRise();                        // エコーの立ち上がり処理
 };
- 
 #endif