Library for US-015 (or HC-SR04) Ultrasonic Module Distance Measuring Sensors

Committer:
igbt6
Date:
Wed Jan 13 01:09:55 2016 +0000
Revision:
0:7163b97ee5f1
Library finished

Who changed what in which revision?

UserRevisionLine numberNew contents of line
igbt6 0:7163b97ee5f1 1 /*
igbt6 0:7163b97ee5f1 2 @file US-015.h
igbt6 0:7163b97ee5f1 3
igbt6 0:7163b97ee5f1 4 @brief library for US-015 (or HC-SR04) Ultrasonic Module Distance Measuring Sensor
igbt6 0:7163b97ee5f1 5
igbt6 0:7163b97ee5f1 6 @Author lukasz uszko(luszko@op.pl)
igbt6 0:7163b97ee5f1 7
igbt6 0:7163b97ee5f1 8 Tested on most mbed platforms
igbt6 0:7163b97ee5f1 9
igbt6 0:7163b97ee5f1 10 Copyright (c) 2016 lukasz uszko
igbt6 0:7163b97ee5f1 11 Released under the MIT License (see http://mbed.org/license/mit)
igbt6 0:7163b97ee5f1 12
igbt6 0:7163b97ee5f1 13 Documentation regarding the US-015 sensor can be found here:
igbt6 0:7163b97ee5f1 14 http://www.micropik.com/PDF/HCSR04.pdf
igbt6 0:7163b97ee5f1 15 */
igbt6 0:7163b97ee5f1 16 //DEMO - HOW TO USE:
igbt6 0:7163b97ee5f1 17 /*
igbt6 0:7163b97ee5f1 18 ---------------------------------------- DEMO 1:----------------------------------------
igbt6 0:7163b97ee5f1 19 #include <mbed.h>
igbt6 0:7163b97ee5f1 20 #include "US-015.h"
igbt6 0:7163b97ee5f1 21
igbt6 0:7163b97ee5f1 22
igbt6 0:7163b97ee5f1 23 #define TRIGGER_PIN PC_1
igbt6 0:7163b97ee5f1 24 #define ECHO_PIN PC_0
igbt6 0:7163b97ee5f1 25 Serial debugPort(SERIAL_TX, SERIAL_RX); //Default 9600 bauds, 8-bit data, no parity
igbt6 0:7163b97ee5f1 26
igbt6 0:7163b97ee5f1 27 void measurementFinished(int resultVal){
igbt6 0:7163b97ee5f1 28 debugPort.printf("Distance: %d",resultVal);
igbt6 0:7163b97ee5f1 29 }
igbt6 0:7163b97ee5f1 30 int main()
igbt6 0:7163b97ee5f1 31 {
igbt6 0:7163b97ee5f1 32
igbt6 0:7163b97ee5f1 33 US015 us015(TRIGGER_PIN,ECHO_PIN,US015::convertMaxDistanceToMaxTimeout(MAX_DISTANCE_FOR_US015_SENSOR_CM) );
igbt6 0:7163b97ee5f1 34 us015.setFinishCallback(measurementFinished);
igbt6 0:7163b97ee5f1 35 bool timeoutFlag=true;
igbt6 0:7163b97ee5f1 36 while (1) {
igbt6 0:7163b97ee5f1 37 wait_ms(1000);
igbt6 0:7163b97ee5f1 38 us015.doMeasurement();
igbt6 0:7163b97ee5f1 39 while(us015.getTimePassedValue()<us015.getTimeoutValue())
igbt6 0:7163b97ee5f1 40 {
igbt6 0:7163b97ee5f1 41 if(us015.isMeasurementRuning())
igbt6 0:7163b97ee5f1 42 {
igbt6 0:7163b97ee5f1 43 timeoutFlag=true;
igbt6 0:7163b97ee5f1 44 }
igbt6 0:7163b97ee5f1 45 else
igbt6 0:7163b97ee5f1 46 {
igbt6 0:7163b97ee5f1 47 timeoutFlag =false;
igbt6 0:7163b97ee5f1 48 break;
igbt6 0:7163b97ee5f1 49 }
igbt6 0:7163b97ee5f1 50 }
igbt6 0:7163b97ee5f1 51 if(timeoutFlag){
igbt6 0:7163b97ee5f1 52 debugPort.printf("Timeout Happened");
igbt6 0:7163b97ee5f1 53 us015.resetMeasuremnt();
igbt6 0:7163b97ee5f1 54 }
igbt6 0:7163b97ee5f1 55
igbt6 0:7163b97ee5f1 56 }
igbt6 0:7163b97ee5f1 57 }
igbt6 0:7163b97ee5f1 58 */
igbt6 0:7163b97ee5f1 59
igbt6 0:7163b97ee5f1 60
igbt6 0:7163b97ee5f1 61
igbt6 0:7163b97ee5f1 62 #include <mbed.h>
igbt6 0:7163b97ee5f1 63
igbt6 0:7163b97ee5f1 64 #define DEFAULT_TIMEOUT 0xFFFFFFFF
igbt6 0:7163b97ee5f1 65 #define MAX_DISTANCE_FOR_US015_SENSOR_CM 400 //400cm
igbt6 0:7163b97ee5f1 66 #define SPEED_OF_SOUND 343.2f
igbt6 0:7163b97ee5f1 67 class US015{
igbt6 0:7163b97ee5f1 68
igbt6 0:7163b97ee5f1 69 public:
igbt6 0:7163b97ee5f1 70
igbt6 0:7163b97ee5f1 71 struct TimeController {
igbt6 0:7163b97ee5f1 72 Timer timer;
igbt6 0:7163b97ee5f1 73 int startTime;
igbt6 0:7163b97ee5f1 74 int difTime;
igbt6 0:7163b97ee5f1 75 };
igbt6 0:7163b97ee5f1 76
igbt6 0:7163b97ee5f1 77 US015(PinName triggerPin, PinName echoPin,uint32_t timeout=DEFAULT_TIMEOUT);
igbt6 0:7163b97ee5f1 78 static int convertMaxDistanceToMaxTimeout(int maxDistHandledBySensorInCm);
igbt6 0:7163b97ee5f1 79 bool doMeasurement(void);
igbt6 0:7163b97ee5f1 80 int convertTimeToDistanceValue_mm(int timeUs);
igbt6 0:7163b97ee5f1 81 int convertTimeToDistanceValue_cm(int timeUs);
igbt6 0:7163b97ee5f1 82 void setFinishCallback(void (*finishCallback)(int resultValue));
igbt6 0:7163b97ee5f1 83 bool isMeasurementRuning(void );
igbt6 0:7163b97ee5f1 84 int getTimePassedValue(void);
igbt6 0:7163b97ee5f1 85 int getTimeoutValue(void);
igbt6 0:7163b97ee5f1 86 void resetMeasuremnt(void);
igbt6 0:7163b97ee5f1 87
igbt6 0:7163b97ee5f1 88 private:
igbt6 0:7163b97ee5f1 89 void startMeasureCallback(void);
igbt6 0:7163b97ee5f1 90 void stopMeasureCallback(void);
igbt6 0:7163b97ee5f1 91 void (*measuremntFinishedCallback)(int result);
igbt6 0:7163b97ee5f1 92
igbt6 0:7163b97ee5f1 93 private:
igbt6 0:7163b97ee5f1 94 bool measurementRunning;
igbt6 0:7163b97ee5f1 95 DigitalOut trigger;
igbt6 0:7163b97ee5f1 96 InterruptIn echoIn;
igbt6 0:7163b97ee5f1 97 uint32_t timeoutValue;
igbt6 0:7163b97ee5f1 98 struct TimeController* timerCtrl;
igbt6 0:7163b97ee5f1 99
igbt6 0:7163b97ee5f1 100 };