Programma rilevamento ostacoli

Dependents:   Motor2FULL_copy

Fork of HCSR04 by Prabhu Desai

Committer:
Ghilardi
Date:
Thu Mar 16 13:50:35 2017 +0000
Revision:
8:276d618e4e14
Parent:
6:4be4fcd0c171
programma rilevamento ostacoli

Who changed what in which revision?

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