distance sensor HCSR04

Dependents:   inverted_pendulum_system

Fork of HCSR04 by Prabhu Desai

Files at this revision

API Documentation at this revision

Comitter:
dudu941014
Date:
Fri Aug 25 21:01:39 2017 +0000
Parent:
7:71da0dbf4400
Commit message:
this is a new module for distance sensor;

Changed in this revision

hcsr04.cpp Show annotated file Show diff for this revision Revisions of this file
hcsr04.h Show annotated file Show diff for this revision Revisions of this file
diff -r 71da0dbf4400 -r a7680a852da3 hcsr04.cpp
--- a/hcsr04.cpp	Sat Mar 30 18:55:03 2013 +0000
+++ b/hcsr04.cpp	Fri Aug 25 21:01:39 2017 +0000
@@ -1,22 +1,15 @@
-/* Copyright (c) 2013 Prabhu Desai
- * pdtechworld@gmail.com
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
- * and associated documentation files (the "Software"), to deal in the Software without restriction,
- * including without limitation the rights to use, copy, modify, merge, publish, distribute,
- * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all copies or
- * substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
- * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
- * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
-
+//////////////////////////////////////////////////////////////////////////////////
+// Company: edinburgh of university
+// Engineer: ZEjun DU
+// 
+// Create Date: 2017/08/20 13:06:52
+// Design Name: Inverted Pendulum Balancer
+// Module Name: distance sensor
+// Tool Versions: “Keil 5” or “Mbed Complie Online”
+// Description: this part is basic function of position to get distance from distance sensor hcsr04.cpp
+//                           
+// 
+//////////////////////////////////////////////////////////////////////////////////
 
 #include "hcsr04.h"
 
@@ -39,19 +32,27 @@
 {
     pulsetime.start();
 }
+
+////////////////////////////////////////////////////////////
+//this funcition is to trigger the distance sensor
+//after triggering, the distance sensor will send a 40KHz signal
+////////////////////////////////////////////////////////////
 void HCSR04::start(void)
 {
     trigger=1;
-    wait_us(10);
+    wait_us(12);
     trigger=0;
 }
 
+////////////////////////////////////////////////////////////
+//this funcition is to calculate the distance according the high level time of the ENCH pin
+////////////////////////////////////////////////////////////
 void HCSR04::isr_fall(void)
 {
-    pulsetime.stop();
-    pulsedur = pulsetime.read_us();
-    distance= (pulsedur*343)/20000;
-    pulsetime.reset();
+    pulsetime.stop();//stop the pulsetime
+    pulsedur = pulsetime.read_us();//read the high level time
+    distance= (pulsedur*343)/20000;//calculate the distance according to the high level time
+    pulsetime.reset();//reset the variable
 }
 
 void HCSR04::rise (void (*fptr)(void))
diff -r 71da0dbf4400 -r a7680a852da3 hcsr04.h
--- a/hcsr04.h	Sat Mar 30 18:55:03 2013 +0000
+++ b/hcsr04.h	Fri Aug 25 21:01:39 2017 +0000
@@ -1,25 +1,15 @@
-/* Copyright (c) 2013 Prabhu Desai
- * pdtechworld@gmail.com
- *
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
- * and associated documentation files (the "Software"), to deal in the Software without restriction,
- * including without limitation the rights to use, copy, modify, merge, publish, distribute,
- * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all copies or
- * substantial portions of the Software.
- *
- * For more details on the sensor :
- * http://www.elecfreaks.com/store/hcsr04-ultrasonic-sensor-distance-measuring-module-p-91.html?zenid=pgm8pgnvaodbe36dibq5s1soi3
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
- * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
- * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
+//////////////////////////////////////////////////////////////////////////////////
+// Company: edinburgh of university
+// Engineer: ZEjun DU
+// 
+// Create Date: 2017/08/20 13:06:52
+// Design Name: Inverted Pendulum Balancer
+// Module Name: distance sensor
+// Tool Versions: “Keil 5” or “Mbed Complie Online”
+// Description: this part is basic function of position to get distance from distance sensor hcsr04.cpp
+//                           
+// 
+//////////////////////////////////////////////////////////////////////////////////
 
 #ifndef MBED_HCSR04_H
 #define MBED_HCSR04_H
@@ -63,6 +53,10 @@
     InterruptIn echo;
     unsigned int pulsedur;
     unsigned int distance;
+//    float pulsedur;
+//    float distance;
+    
+    
 };
 
 #endif
\ No newline at end of file