A program that demonstrates the development of library, using as an example an ultrasonic distance sensor HC-SR04.

Dependencies:   mbed HCSR04 AutomationElements

The purpose of this program is to encourage students to develope their own classes. Instructions how to follow the development of this program and class HCSR04 for ultrasonic distance measurement are given here.

Files at this revision

API Documentation at this revision

Comitter:
tbjazic
Date:
Mon Dec 07 10:44:07 2015 +0000
Parent:
3:3297ea6e3ae1
Child:
5:889566fb3a85
Child:
6:bca0839e8295
Commit message:
PT1 filter added

Changed in this revision

AutomationElements.lib Show annotated file Show diff for this revision Revisions of this file
HCSR04.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AutomationElements.lib	Mon Dec 07 10:44:07 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/teams/TVZ-Mechatronics-Team/code/AutomationElements/#b9e11da0f2eb
--- a/HCSR04.lib	Mon Dec 07 09:38:35 2015 +0000
+++ b/HCSR04.lib	Mon Dec 07 10:44:07 2015 +0000
@@ -1,1 +1,1 @@
-https://developer.mbed.org/teams/TVZ-Mechatronics-Team/code/HCSR04/#aae70f15357f
+https://developer.mbed.org/teams/TVZ-Mechatronics-Team/code/HCSR04/#a667b621f625
--- a/main.cpp	Mon Dec 07 09:38:35 2015 +0000
+++ b/main.cpp	Mon Dec 07 10:44:07 2015 +0000
@@ -1,15 +1,32 @@
 #include "mbed.h"
 #include "HCSR04.h"
+#include "AutomationElements.h"
 
 Serial pc(USBTX, USBRX);
+HCSR04 sensor(p5, p7);
+float sampleTime = 0.1;
+PT1 filter(1, 2, sampleTime);
+Ticker ticker;
+float distance;
+float filteredDistance; 
+DigitalOut led1(LED1);
+
+void calc() {
+    led1 = 1;
+    distance = sensor.getDistance_mm();
+    filter.in(distance);
+    filteredDistance = filter.out();
+    pc.printf("%7.1f mm  %7.1f mm  \r", distance, filteredDistance);
+    led1 = 0;
+}
 
 int main() {
-    HCSR04 sensor(p5, p7);
     sensor.setRanges(10, 110);
-    pc.printf("Min. range = %g cm\n\rMax. range = %g cm\n\r",
-     sensor.getMinRange(), sensor.getMaxRange());
+    pc.printf("Minimum sensor range = %g cm\n\rMaximum sensor range = %g cm\n\r", sensor.getMinRange(), sensor.getMaxRange());
+    pc.printf("Sensor:       Filtered:\n\r");
+    ticker.attach(&calc, sampleTime);
+    
     while(1) {
-        pc.printf("Distance: %5.1f mm\r", sensor.getDistance_mm());
         wait_ms(500);
     }
 }
\ No newline at end of file