stephen smitherman / HCSR04

Dependents:   TJPS UltraTest NavigationTest NavigationTest_ ... more

Fork of HCSR04 by Robert Abad

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HCSR04.cpp Source File

HCSR04.cpp

00001 /* File: HCSR04.h
00002  * Author: Robert Abad  Copyright (c) 2013
00003  *
00004  * Desc: driver for HCSR04 Ultrasonic Range Finder.  See header file,
00005  *       HCSR04.h, for more details.
00006  */
00007 
00008 #include "mbed.h"
00009 #include "HCSR04.h"
00010 #include "stdio.h"
00011 
00012 
00013 #define SPEED_OF_SOUND      (343.2f)    // meters/sec
00014 
00015 // HCSR04 TRIGGER pin
00016 #define SIGNAL_HIGH     (1)
00017 #define SIGNAL_LOW      (0)
00018 
00019 #define TRIGGER_TIME    (10)            // microseconds
00020 // Name: HCSR04
00021 // Desc: HCSR04 constructor
00022 // Inputs: PinName - pin used for trigger signal
00023 //         PinName - pin used for echo signal
00024 // Outputs: none
00025 //
00026 HCSR04::HCSR04( PinName pinTrigger, PinName pinEcho ) : 
00027 trigger(pinTrigger),
00028 echo(pinEcho),
00029 measTimeStart_us(0),
00030 measTimeStop_us(0)
00031 {
00032      
00033     echo.rise( this, &HCSR04::ISR_echoRising );
00034     echo.fall( this, &HCSR04::ISR_echoFalling );
00035     echoTimer.start();
00036     
00037 }
00038 
00039 // Name: startMeas
00040 // Desc: initiates range measurement driving the trigger signal HIGH then sets
00041 //       a timer to keep trigger HIGH for the duration of TRIGGER_TIME
00042 // Inputs: none
00043 // Outputs: none
00044 //
00045 void HCSR04::startMeas(void)
00046 {
00047     //printf("Start US\n\n");
00048     echoTimer.reset();
00049     trigger = SIGNAL_HIGH;
00050     triggerTicker.attach_us(this, &HCSR04::triggerTicker_cb, TRIGGER_TIME);
00051     
00052 }
00053  
00054 // Name: getMeas
00055 // Desc: returns range measurement in meters
00056 // Inputs: float & - reference to range measurement
00057 // Outputs: etHCSR04 - RANGE_MEAS_VALID or RANGE_MEAS_INVALID
00058 //
00059 etHCSR04_RANGE_STATUS HCSR04::getMeas(float &rRangeMeters)
00060 { 
00061     
00062     if ( status ==  RANGE_MEAS_VALID)
00063     {   
00064         dTime_us = measTimeStop_us - measTimeStart_us;
00065         measTimeStart_us = 0;
00066         measTimeStop_us = 0;
00067     
00068         rRangeMeters = (float)dTime_us * SPEED_OF_SOUND / 2000000.0 * MTRS_TO_INCH;
00069         status = RANGE_MEAS_INVALID;
00070         return RANGE_MEAS_VALID;
00071     }else{  
00072         
00073         return RANGE_MEAS_INVALID;
00074         
00075     }
00076 }
00077 
00078 // Name: triggerTicker_cb
00079 // Desc: Timer ticker callback function used to drive trigger signal LOW
00080 // Inputs: none
00081 // Outputs: none
00082 //
00083 void HCSR04::triggerTicker_cb(void)
00084 { 
00085     trigger = SIGNAL_LOW;
00086     triggerTicker.detach();
00087 }
00088 
00089 
00090 // Name: ISR_echoRising
00091 // Desc: ISR for rising edge of HCSR04 ECHO signal
00092 // Inputs: none
00093 // Outputs: none
00094 //
00095 void HCSR04::ISR_echoRising(void)
00096 {
00097     measTimeStart_us = echoTimer.read_us();
00098 }
00099 
00100 // Name: ISR_echoFalling
00101 // Desc: ISR for falling edge of HCSR04 ECHO signal
00102 // Inputs: none
00103 // Outputs: none
00104 //
00105 void HCSR04::ISR_echoFalling(void)
00106 {
00107     measTimeStop_us = echoTimer.read_us();
00108     status = RANGE_MEAS_VALID;
00109 }
00110