test
Dependencies: mbed ros_lib_kinetic nhk19mr2_can_info splitData SerialHalfDuplex_HM
hcsr04.cpp
00001 /* Copyright (c) 2013 Prabhu Desai 00002 * pdtechworld@gmail.com 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00005 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00006 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00007 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00008 * furnished to do so, subject to the following conditions: 00009 * 00010 * The above copyright notice and this permission notice shall be included in all copies or 00011 * substantial portions of the Software. 00012 * 00013 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00014 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00015 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00016 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00017 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00018 */ 00019 00020 00021 #include "hcsr04.h" 00022 00023 00024 HCSR04::HCSR04(PinName TrigPin,PinName EchoPin): 00025 trigger(TrigPin), echo(EchoPin) 00026 { 00027 pulsetime.stop(); 00028 pulsetime.reset(); 00029 echo.rise(this,&HCSR04::isr_rise); 00030 echo.fall(this,&HCSR04::isr_fall); 00031 trigger=0; 00032 } 00033 00034 HCSR04::~HCSR04() 00035 { 00036 } 00037 00038 void HCSR04::isr_rise(void) 00039 { 00040 pulsetime.start(); 00041 } 00042 void HCSR04::start(void) 00043 { 00044 trigger=1; 00045 wait_us(10); 00046 trigger=0; 00047 } 00048 00049 void HCSR04::isr_fall(void) 00050 { 00051 pulsetime.stop(); 00052 pulsedur = pulsetime.read_us(); 00053 distance= (pulsedur*343)/20000; 00054 pulsetime.reset(); 00055 } 00056 00057 void HCSR04::rise (void (*fptr)(void)) 00058 { 00059 echo.rise(fptr); 00060 } 00061 void HCSR04::fall (void (*fptr)(void)) 00062 { 00063 echo.fall(fptr); 00064 } 00065 00066 float HCSR04::get_dist_cm() 00067 { 00068 return distance; 00069 } 00070 float HCSR04::get_pulse_us() 00071 { 00072 return pulsedur; 00073 } 00074 00075 00076 00077 /******************************************************* 00078 Here is a sample code usage 00079 ********************************************************* 00080 #include "hcsr04.h" 00081 HCSR04 usensor(p25,p6); 00082 int main() 00083 { 00084 unsigned char count=0; 00085 while(1) { 00086 usensor.start(); 00087 wait_ms(500); 00088 dist=usensor.get_dist_cm(); 00089 lcd.cls(); 00090 lcd.locate(0,0); 00091 lcd.printf("cm:%ld",dist ); 00092 00093 count++; 00094 lcd.locate(0,1); 00095 lcd.printf("Distance =%d",count); 00096 00097 } 00098 */
Generated on Thu Jul 14 2022 13:15:26 by 1.7.2