SEEED STUDIO SEN136B5B test (Using PING library in local)

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
mio
Date:
Mon Sep 24 15:52:56 2012 +0000
Commit message:
Import PING code to local.

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
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PING.cpp	Mon Sep 24 15:52:56 2012 +0000
@@ -0,0 +1,91 @@
+#include "PING.h"
+#include "mbed.h"
+
+/*
+ * Constructor
+ */
+PING::PING(PinName trigger) : _trigger(trigger)
+{
+   // Attach interrupts
+   _ticker.attach(this, &PING::_startRange, 0.1);
+   _invalidctr = 0 ;
+}
+
+void PING::_startRange()
+{
+   int invalid ;
+   invalid = 0 ;
+
+   // 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
+   _timer.reset();
+   _timer.start();
+   while (!_trigger) {
+    if (_timer.read_us() > 10000) { // invalid thre
+        invalid = 1;
+        break ;
+    }
+   } // wait for high
+   if (invalid == 0) 
+   {
+    _timer.stop();
+    _timer.reset();
+    _timer.start();
+    while (_trigger) {
+     if (_timer.read_us() > 10000) 
+     { // invalid thre
+            invalid = 1 ;
+            break ;
+     }
+    } // wait for low
+    _timer.stop();
+   }
+   if (invalid == 0) {
+        _dist = _timer.read_us(); //provides echo time in microseconds
+        _invalidctr = 0 ;
+   } else {
+        _invalidctr++ ;
+        if (_invalidctr > 5) { // Invalid (none) thre
+            _dist = 0.0 ;
+            _invalidctr = 0 ;
+        }
+   }
+}
+
+// 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
+
+// 2012.9.25 : Add check echo timeout detection
+
+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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PING.h	Mon Sep 24 15:52:56 2012 +0000
@@ -0,0 +1,72 @@
+#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;
+   int _invalidctr;
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Sep 24 15:52:56 2012 +0000
@@ -0,0 +1,14 @@
+#include "mbed.h"
+#include "PING.h"
+
+PING ping(p9);
+Serial pc(USBTX,USBRX) ;
+int main() 
+{
+    pc.baud(115200) ;
+    pc.printf("start!\r\n") ;
+    while(1) {
+        pc.printf("Measured : %.1f\r\n", ping.read());
+        wait(0.2);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Sep 24 15:52:56 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/cd19af002ccc
\ No newline at end of file