mbed device driver for HC-SR04 ultrasonic range finder

Dependents:   TJPS UltraTest NavigationTest NavigationTest_ ... more

Fork of HCSR04 by Robert Abad

Committer:
srsmitherman
Date:
Thu Dec 12 17:22:48 2013 +0000
Revision:
2:3ebde19131af
Parent:
1:68ad9acbec81
Improved missed echo detection.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rabad1 0:5461d44a187c 1 /* File: HCSR04.h
rabad1 0:5461d44a187c 2 * Author: Robert Abad Copyright (c) 2013
rabad1 0:5461d44a187c 3 *
rabad1 0:5461d44a187c 4 * Desc: driver for HCSR04 Ultrasonic Range Finder. See header file,
rabad1 0:5461d44a187c 5 * HCSR04.h, for more details.
rabad1 0:5461d44a187c 6 */
rabad1 0:5461d44a187c 7
rabad1 0:5461d44a187c 8 #include "mbed.h"
rabad1 0:5461d44a187c 9 #include "HCSR04.h"
srsmitherman 2:3ebde19131af 10 #include "stdio.h"
srsmitherman 2:3ebde19131af 11
rabad1 0:5461d44a187c 12
rabad1 0:5461d44a187c 13 #define SPEED_OF_SOUND (343.2f) // meters/sec
rabad1 0:5461d44a187c 14
rabad1 0:5461d44a187c 15 // HCSR04 TRIGGER pin
rabad1 0:5461d44a187c 16 #define SIGNAL_HIGH (1)
rabad1 0:5461d44a187c 17 #define SIGNAL_LOW (0)
rabad1 0:5461d44a187c 18
rabad1 0:5461d44a187c 19 #define TRIGGER_TIME (10) // microseconds
rabad1 0:5461d44a187c 20 // Name: HCSR04
rabad1 0:5461d44a187c 21 // Desc: HCSR04 constructor
rabad1 0:5461d44a187c 22 // Inputs: PinName - pin used for trigger signal
rabad1 0:5461d44a187c 23 // PinName - pin used for echo signal
rabad1 0:5461d44a187c 24 // Outputs: none
rabad1 0:5461d44a187c 25 //
rabad1 0:5461d44a187c 26 HCSR04::HCSR04( PinName pinTrigger, PinName pinEcho ) :
rabad1 0:5461d44a187c 27 trigger(pinTrigger),
rabad1 0:5461d44a187c 28 echo(pinEcho),
rabad1 0:5461d44a187c 29 measTimeStart_us(0),
rabad1 0:5461d44a187c 30 measTimeStop_us(0)
rabad1 0:5461d44a187c 31 {
srsmitherman 2:3ebde19131af 32
rabad1 0:5461d44a187c 33 echo.rise( this, &HCSR04::ISR_echoRising );
rabad1 0:5461d44a187c 34 echo.fall( this, &HCSR04::ISR_echoFalling );
rabad1 0:5461d44a187c 35 echoTimer.start();
srsmitherman 2:3ebde19131af 36
rabad1 0:5461d44a187c 37 }
rabad1 0:5461d44a187c 38
rabad1 0:5461d44a187c 39 // Name: startMeas
rabad1 0:5461d44a187c 40 // Desc: initiates range measurement driving the trigger signal HIGH then sets
rabad1 0:5461d44a187c 41 // a timer to keep trigger HIGH for the duration of TRIGGER_TIME
rabad1 0:5461d44a187c 42 // Inputs: none
rabad1 0:5461d44a187c 43 // Outputs: none
rabad1 0:5461d44a187c 44 //
rabad1 0:5461d44a187c 45 void HCSR04::startMeas(void)
rabad1 0:5461d44a187c 46 {
srsmitherman 2:3ebde19131af 47 //printf("Start US\n\n");
rabad1 0:5461d44a187c 48 echoTimer.reset();
srsmitherman 2:3ebde19131af 49 trigger = SIGNAL_HIGH;
rabad1 0:5461d44a187c 50 triggerTicker.attach_us(this, &HCSR04::triggerTicker_cb, TRIGGER_TIME);
srsmitherman 2:3ebde19131af 51
rabad1 0:5461d44a187c 52 }
rabad1 0:5461d44a187c 53
rabad1 0:5461d44a187c 54 // Name: getMeas
rabad1 0:5461d44a187c 55 // Desc: returns range measurement in meters
rabad1 0:5461d44a187c 56 // Inputs: float & - reference to range measurement
rabad1 0:5461d44a187c 57 // Outputs: etHCSR04 - RANGE_MEAS_VALID or RANGE_MEAS_INVALID
rabad1 0:5461d44a187c 58 //
rabad1 0:5461d44a187c 59 etHCSR04_RANGE_STATUS HCSR04::getMeas(float &rRangeMeters)
srsmitherman 2:3ebde19131af 60 {
srsmitherman 2:3ebde19131af 61
srsmitherman 2:3ebde19131af 62 if ( status == RANGE_MEAS_VALID)
srsmitherman 2:3ebde19131af 63 {
srsmitherman 2:3ebde19131af 64 dTime_us = measTimeStop_us - measTimeStart_us;
srsmitherman 2:3ebde19131af 65 measTimeStart_us = 0;
srsmitherman 2:3ebde19131af 66 measTimeStop_us = 0;
srsmitherman 2:3ebde19131af 67
srsmitherman 2:3ebde19131af 68 rRangeMeters = (float)dTime_us * SPEED_OF_SOUND / 2000000.0 * MTRS_TO_INCH;
srsmitherman 2:3ebde19131af 69 status = RANGE_MEAS_INVALID;
srsmitherman 2:3ebde19131af 70 return RANGE_MEAS_VALID;
srsmitherman 2:3ebde19131af 71 }else{
srsmitherman 2:3ebde19131af 72
rabad1 0:5461d44a187c 73 return RANGE_MEAS_INVALID;
srsmitherman 2:3ebde19131af 74
rabad1 0:5461d44a187c 75 }
rabad1 0:5461d44a187c 76 }
rabad1 0:5461d44a187c 77
rabad1 0:5461d44a187c 78 // Name: triggerTicker_cb
rabad1 0:5461d44a187c 79 // Desc: Timer ticker callback function used to drive trigger signal LOW
rabad1 0:5461d44a187c 80 // Inputs: none
rabad1 0:5461d44a187c 81 // Outputs: none
rabad1 0:5461d44a187c 82 //
rabad1 0:5461d44a187c 83 void HCSR04::triggerTicker_cb(void)
srsmitherman 2:3ebde19131af 84 {
rabad1 0:5461d44a187c 85 trigger = SIGNAL_LOW;
srsmitherman 1:68ad9acbec81 86 triggerTicker.detach();
rabad1 0:5461d44a187c 87 }
rabad1 0:5461d44a187c 88
rabad1 0:5461d44a187c 89
rabad1 0:5461d44a187c 90 // Name: ISR_echoRising
rabad1 0:5461d44a187c 91 // Desc: ISR for rising edge of HCSR04 ECHO signal
rabad1 0:5461d44a187c 92 // Inputs: none
rabad1 0:5461d44a187c 93 // Outputs: none
rabad1 0:5461d44a187c 94 //
rabad1 0:5461d44a187c 95 void HCSR04::ISR_echoRising(void)
rabad1 0:5461d44a187c 96 {
rabad1 0:5461d44a187c 97 measTimeStart_us = echoTimer.read_us();
rabad1 0:5461d44a187c 98 }
rabad1 0:5461d44a187c 99
rabad1 0:5461d44a187c 100 // Name: ISR_echoFalling
rabad1 0:5461d44a187c 101 // Desc: ISR for falling edge of HCSR04 ECHO signal
rabad1 0:5461d44a187c 102 // Inputs: none
rabad1 0:5461d44a187c 103 // Outputs: none
rabad1 0:5461d44a187c 104 //
rabad1 0:5461d44a187c 105 void HCSR04::ISR_echoFalling(void)
rabad1 0:5461d44a187c 106 {
rabad1 0:5461d44a187c 107 measTimeStop_us = echoTimer.read_us();
srsmitherman 2:3ebde19131af 108 status = RANGE_MEAS_VALID;
rabad1 0:5461d44a187c 109 }
rabad1 0:5461d44a187c 110