Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 0:7163b97ee5f1, committed 2016-01-13
- Comitter:
- igbt6
- Date:
- Wed Jan 13 01:09:55 2016 +0000
- Commit message:
- Library finished
Changed in this revision
| US-015.cpp | Show annotated file Show diff for this revision Revisions of this file |
| US-015.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/US-015.cpp Wed Jan 13 01:09:55 2016 +0000
@@ -0,0 +1,82 @@
+#include "US-015.h"
+
+
+
+US015::US015(PinName triggerPin, PinName echoPin, uint32_t timeout):trigger(triggerPin),echoIn(echoPin),timeoutValue(timeout)
+{
+ echoIn.rise(this, &US015::startMeasureCallback);
+ echoIn.fall(this, &US015::stopMeasureCallback);
+ timerCtrl = new TimeController();
+ measurementRunning=false;
+ measuremntFinishedCallback= NULL;
+}
+
+int US015::convertMaxDistanceToMaxTimeout(int maxDistHandledBySensorInCm){
+ return (maxDistHandledBySensorInCm) / (float)((SPEED_OF_SOUND)/10000)/2;
+}
+
+void US015::setFinishCallback(void (*finishCallback)(int resultValue))
+{
+ measuremntFinishedCallback= finishCallback;
+}
+
+bool US015::doMeasurement(void)
+{
+ if(isMeasurementRuning())
+ return false;
+ timerCtrl->startTime=0;
+ trigger.write(1); //triggerPin
+ wait_us(30);
+ trigger.write(0);
+ measurementRunning=true;
+ return true;
+}
+
+bool US015::isMeasurementRuning()
+{
+ return measurementRunning;
+}
+
+int US015::convertTimeToDistanceValue_mm(int timeUs)
+{
+ return ((timeUs) * (float)((SPEED_OF_SOUND)/1000))/2; //(0.343mm/us)
+}
+
+int US015::convertTimeToDistanceValue_cm(int timeUs)
+{
+ return ((timeUs) *(float)((SPEED_OF_SOUND)/10000))/2; //(0.034cm/us)
+}
+
+int US015::getTimePassedValue(void)
+{
+ return timerCtrl->timer.read_us()-timerCtrl->startTime;
+}
+
+int US015::getTimeoutValue(void)
+{
+ return timeoutValue;
+}
+
+void US015::resetMeasuremnt(void)
+{
+ timerCtrl->timer.reset();
+ measurementRunning=false;
+}
+
+//private methods
+void US015::startMeasureCallback(void)
+{
+ timerCtrl->timer.start();
+ timerCtrl->startTime= timerCtrl->timer.read_us();
+}
+
+void US015::stopMeasureCallback(void)
+{
+ timerCtrl->difTime= timerCtrl->timer.read_us()-timerCtrl->startTime;
+ resetMeasuremnt();
+ if(measuremntFinishedCallback!=NULL)
+ {
+ (*measuremntFinishedCallback)(convertTimeToDistanceValue_mm(timerCtrl->difTime)); //for default
+ }
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/US-015.h Wed Jan 13 01:09:55 2016 +0000
@@ -0,0 +1,100 @@
+/*
+ @file US-015.h
+
+ @brief library for US-015 (or HC-SR04) Ultrasonic Module Distance Measuring Sensor
+
+ @Author lukasz uszko(luszko@op.pl)
+
+ Tested on most mbed platforms
+
+ Copyright (c) 2016 lukasz uszko
+ Released under the MIT License (see http://mbed.org/license/mit)
+
+ Documentation regarding the US-015 sensor can be found here:
+ http://www.micropik.com/PDF/HCSR04.pdf
+*/
+//DEMO - HOW TO USE:
+/*
+---------------------------------------- DEMO 1:----------------------------------------
+#include <mbed.h>
+#include "US-015.h"
+
+
+#define TRIGGER_PIN PC_1
+#define ECHO_PIN PC_0
+Serial debugPort(SERIAL_TX, SERIAL_RX); //Default 9600 bauds, 8-bit data, no parity
+
+void measurementFinished(int resultVal){
+ debugPort.printf("Distance: %d",resultVal);
+}
+int main()
+{
+
+ US015 us015(TRIGGER_PIN,ECHO_PIN,US015::convertMaxDistanceToMaxTimeout(MAX_DISTANCE_FOR_US015_SENSOR_CM) );
+ us015.setFinishCallback(measurementFinished);
+ bool timeoutFlag=true;
+ while (1) {
+ wait_ms(1000);
+ us015.doMeasurement();
+ while(us015.getTimePassedValue()<us015.getTimeoutValue())
+ {
+ if(us015.isMeasurementRuning())
+ {
+ timeoutFlag=true;
+ }
+ else
+ {
+ timeoutFlag =false;
+ break;
+ }
+ }
+ if(timeoutFlag){
+ debugPort.printf("Timeout Happened");
+ us015.resetMeasuremnt();
+ }
+
+ }
+}
+*/
+
+
+
+#include <mbed.h>
+
+#define DEFAULT_TIMEOUT 0xFFFFFFFF
+#define MAX_DISTANCE_FOR_US015_SENSOR_CM 400 //400cm
+#define SPEED_OF_SOUND 343.2f
+class US015{
+
+public:
+
+ struct TimeController {
+ Timer timer;
+ int startTime;
+ int difTime;
+ };
+
+ US015(PinName triggerPin, PinName echoPin,uint32_t timeout=DEFAULT_TIMEOUT);
+ static int convertMaxDistanceToMaxTimeout(int maxDistHandledBySensorInCm);
+ bool doMeasurement(void);
+ int convertTimeToDistanceValue_mm(int timeUs);
+ int convertTimeToDistanceValue_cm(int timeUs);
+ void setFinishCallback(void (*finishCallback)(int resultValue));
+ bool isMeasurementRuning(void );
+ int getTimePassedValue(void);
+ int getTimeoutValue(void);
+ void resetMeasuremnt(void);
+
+private:
+ void startMeasureCallback(void);
+ void stopMeasureCallback(void);
+ void (*measuremntFinishedCallback)(int result);
+
+private:
+ bool measurementRunning;
+ DigitalOut trigger;
+ InterruptIn echoIn;
+ uint32_t timeoutValue;
+ struct TimeController* timerCtrl;
+
+};
\ No newline at end of file