test

Dependencies:   mbed ros_lib_kinetic nhk19mr2_can_info splitData SerialHalfDuplex_HM

Committer:
shimizuta
Date:
Mon Mar 11 10:38:07 2019 +0000
Revision:
50:36741e8ab197
a

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shimizuta 50:36741e8ab197 1 /* Copyright (c) 2013 Prabhu Desai
shimizuta 50:36741e8ab197 2 * pdtechworld@gmail.com
shimizuta 50:36741e8ab197 3 *
shimizuta 50:36741e8ab197 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
shimizuta 50:36741e8ab197 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
shimizuta 50:36741e8ab197 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
shimizuta 50:36741e8ab197 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
shimizuta 50:36741e8ab197 8 * furnished to do so, subject to the following conditions:
shimizuta 50:36741e8ab197 9 *
shimizuta 50:36741e8ab197 10 * The above copyright notice and this permission notice shall be included in all copies or
shimizuta 50:36741e8ab197 11 * substantial portions of the Software.
shimizuta 50:36741e8ab197 12 *
shimizuta 50:36741e8ab197 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
shimizuta 50:36741e8ab197 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
shimizuta 50:36741e8ab197 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
shimizuta 50:36741e8ab197 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
shimizuta 50:36741e8ab197 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
shimizuta 50:36741e8ab197 18 */
shimizuta 50:36741e8ab197 19
shimizuta 50:36741e8ab197 20
shimizuta 50:36741e8ab197 21 #include "hcsr04.h"
shimizuta 50:36741e8ab197 22
shimizuta 50:36741e8ab197 23
shimizuta 50:36741e8ab197 24 HCSR04::HCSR04(PinName TrigPin,PinName EchoPin):
shimizuta 50:36741e8ab197 25 trigger(TrigPin), echo(EchoPin)
shimizuta 50:36741e8ab197 26 {
shimizuta 50:36741e8ab197 27 pulsetime.stop();
shimizuta 50:36741e8ab197 28 pulsetime.reset();
shimizuta 50:36741e8ab197 29 echo.rise(this,&HCSR04::isr_rise);
shimizuta 50:36741e8ab197 30 echo.fall(this,&HCSR04::isr_fall);
shimizuta 50:36741e8ab197 31 trigger=0;
shimizuta 50:36741e8ab197 32 }
shimizuta 50:36741e8ab197 33
shimizuta 50:36741e8ab197 34 HCSR04::~HCSR04()
shimizuta 50:36741e8ab197 35 {
shimizuta 50:36741e8ab197 36 }
shimizuta 50:36741e8ab197 37
shimizuta 50:36741e8ab197 38 void HCSR04::isr_rise(void)
shimizuta 50:36741e8ab197 39 {
shimizuta 50:36741e8ab197 40 pulsetime.start();
shimizuta 50:36741e8ab197 41 }
shimizuta 50:36741e8ab197 42 void HCSR04::start(void)
shimizuta 50:36741e8ab197 43 {
shimizuta 50:36741e8ab197 44 trigger=1;
shimizuta 50:36741e8ab197 45 wait_us(10);
shimizuta 50:36741e8ab197 46 trigger=0;
shimizuta 50:36741e8ab197 47 }
shimizuta 50:36741e8ab197 48
shimizuta 50:36741e8ab197 49 void HCSR04::isr_fall(void)
shimizuta 50:36741e8ab197 50 {
shimizuta 50:36741e8ab197 51 pulsetime.stop();
shimizuta 50:36741e8ab197 52 pulsedur = pulsetime.read_us();
shimizuta 50:36741e8ab197 53 distance= (pulsedur*343)/20000;
shimizuta 50:36741e8ab197 54 pulsetime.reset();
shimizuta 50:36741e8ab197 55 }
shimizuta 50:36741e8ab197 56
shimizuta 50:36741e8ab197 57 void HCSR04::rise (void (*fptr)(void))
shimizuta 50:36741e8ab197 58 {
shimizuta 50:36741e8ab197 59 echo.rise(fptr);
shimizuta 50:36741e8ab197 60 }
shimizuta 50:36741e8ab197 61 void HCSR04::fall (void (*fptr)(void))
shimizuta 50:36741e8ab197 62 {
shimizuta 50:36741e8ab197 63 echo.fall(fptr);
shimizuta 50:36741e8ab197 64 }
shimizuta 50:36741e8ab197 65
shimizuta 50:36741e8ab197 66 float HCSR04::get_dist_cm()
shimizuta 50:36741e8ab197 67 {
shimizuta 50:36741e8ab197 68 return distance;
shimizuta 50:36741e8ab197 69 }
shimizuta 50:36741e8ab197 70 float HCSR04::get_pulse_us()
shimizuta 50:36741e8ab197 71 {
shimizuta 50:36741e8ab197 72 return pulsedur;
shimizuta 50:36741e8ab197 73 }
shimizuta 50:36741e8ab197 74
shimizuta 50:36741e8ab197 75
shimizuta 50:36741e8ab197 76
shimizuta 50:36741e8ab197 77 /*******************************************************
shimizuta 50:36741e8ab197 78 Here is a sample code usage
shimizuta 50:36741e8ab197 79 *********************************************************
shimizuta 50:36741e8ab197 80 #include "hcsr04.h"
shimizuta 50:36741e8ab197 81 HCSR04 usensor(p25,p6);
shimizuta 50:36741e8ab197 82 int main()
shimizuta 50:36741e8ab197 83 {
shimizuta 50:36741e8ab197 84 unsigned char count=0;
shimizuta 50:36741e8ab197 85 while(1) {
shimizuta 50:36741e8ab197 86 usensor.start();
shimizuta 50:36741e8ab197 87 wait_ms(500);
shimizuta 50:36741e8ab197 88 dist=usensor.get_dist_cm();
shimizuta 50:36741e8ab197 89 lcd.cls();
shimizuta 50:36741e8ab197 90 lcd.locate(0,0);
shimizuta 50:36741e8ab197 91 lcd.printf("cm:%ld",dist );
shimizuta 50:36741e8ab197 92
shimizuta 50:36741e8ab197 93 count++;
shimizuta 50:36741e8ab197 94 lcd.locate(0,1);
shimizuta 50:36741e8ab197 95 lcd.printf("Distance =%d",count);
shimizuta 50:36741e8ab197 96
shimizuta 50:36741e8ab197 97 }
shimizuta 50:36741e8ab197 98 */