PING Library

Files at this revision

API Documentation at this revision

Comitter:
mshoemaker
Date:
Thu Feb 11 03:47:41 2010 +0000
Commit message:

Changed in this revision

PING.cpp Show annotated file Show diff for this revision Revisions of this file
PING.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r ae8d48256ff1 PING.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PING.cpp	Thu Feb 11 03:47:41 2010 +0000
@@ -0,0 +1,59 @@
+#include "PING.h"
+#include "mbed.h"
+
+/*
+ * Constructor
+ */
+PING::PING(PinName trigger) : _trigger(trigger)
+{
+   // Attach interrupts
+   _ticker.attach(this, &PING::_startRange, 0.1);
+}
+
+void PING::_startRange()
+{
+   // send a trigger pulse, 20uS long
+   _trigger.output(); // set pin to output trigger pulse
+   _trigger = 0; // ensure pin starts low
+   wait_us(2); // wait two microseconds
+   _trigger = 1; // send trigger pulse by setting pin high
+   wait_us(5); // keep sending pulse for 5 microseconds
+   _trigger = 0; // set pin low to stop pulse
+
+   _trigger.input();
+
+   // pulseIn
+   while (!_trigger); // wait for high
+   _timer.reset();
+   _timer.start();
+   while (_trigger); // wait for low
+   _timer.stop();
+   _dist = _timer.read_us(); //provides echo time in microseconds
+}
+
+// returns distance in meters
+// The speed of sound is 340 m/s or 58 microseconds per meter.
+// The ping travels out and back, so to find the distance of the
+// object we take half of the distance travelled.
+float PING::read(void)
+{
+   // spin until there is a good value
+   return (_dist/29/2);
+}
+
+// returns distance in inches
+// According to Parallax's datasheet for the PING))), there are
+// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
+// second).  This gives the distance travelled by the ping, outbound
+// and return, so we divide by 2 to get the distance of the obstacle.
+// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
+float PING::read_in(void)
+{
+   // spin until there is a good value
+   return (_dist/74/2);
+}
+
+PING::operator float()
+{
+   return read();
+}
\ No newline at end of file
diff -r 000000000000 -r ae8d48256ff1 PING.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PING.h	Thu Feb 11 03:47:41 2010 +0000
@@ -0,0 +1,71 @@
+#ifndef MBED_PING_H
+#define MBED_PING_H
+#include "mbed.h"
+
+/* Class: PING
+ *  Abstraction for the PING Ultrasonic range finder
+ *
+ * Example:
+ * > // Print measured distance
+ * >
+ * > #include "mbed.h"
+ * > #include "PING.h"
+ * >
+ * > PING ping(p9);
+ * >
+ * > int main() {
+ * >     while(1) {
+ * >         printf("Measured : %.1f\n", ping.read());
+ * >         wait(0.2);
+ * >     }
+ * > }
+ */
+class PING
+{
+// Public functions
+public:
+   /* Constructor: PING
+    *  Create a PING object, connected to the specified pins
+    *
+    * Variables:
+    *  trigger - Output to trigger the PING
+    */
+   PING(PinName trigger);
+
+   /* Function: read
+    *  A non-blocking function that will return the last measurement
+    *
+    * Variables:
+    *  returns - floating point representation in cm.
+    */
+   float read(void);
+
+   /* Function: read_in
+    *  A non-blocking function that will return the last measurement
+    *
+    * Variables:
+    *  returns - floating point representation in in.
+    */
+   float read_in(void);
+
+   /* Function: operator float
+    *  A short hand way of using the read function
+    *
+    * Example:
+    * > float range = ping.read();
+    * > float range = ping;
+    * >
+    * > if(ping.read() > 0.25) { ... }
+    * > if(ping > 0.25) { ... }
+    */
+   operator float();
+
+private :
+   DigitalInOut _trigger;
+   Timer _timer;
+   Ticker _ticker;
+   void _startRange(void);
+   float _dist;
+};
+
+#endif
\ No newline at end of file