Library for the LidarLite, gets distance in cm using the pwm output

Dependents:   LidarLite_test

Fork of MaxbotixDriver by Daniel Casner

Files at this revision

API Documentation at this revision

Comitter:
DanielC
Date:
Wed Nov 21 20:47:05 2012 +0000
Child:
1:32f0a592b9e5
Commit message:
Initial commit of probably working but untested sonar driver library.

Changed in this revision

sonar.cpp Show annotated file Show diff for this revision Revisions of this file
sonar.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sonar.cpp	Wed Nov 21 20:47:05 2012 +0000
@@ -0,0 +1,28 @@
+#include "sonar.h"
+
+Sonar::Sonar(PinName input, Timer& t) :
+    interrupt(input),
+    time(t),
+    pulseStartTime(0),
+    range(0) {
+    interrupt.rise(this, &Sonar::pulseStart);
+    interrupt.fall(this, &Sonar::pulseStop);
+}
+
+int Sonar::read() {
+    return range;
+}
+
+Sonar::operator int() {
+    return read();
+}
+
+void Sonar::pulseStart() {
+    pulseStartTime = time.read_us();
+}
+
+void Sonar::pulseStop() {
+    int endTime = time.read_us();
+    if (endTime < pulseStartTime) return; // Escape if there's been a roll over
+    range = (endTime - pulseStartTime) / 58; // 58uS per CM
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sonar.h	Wed Nov 21 20:47:05 2012 +0000
@@ -0,0 +1,42 @@
+#include "mbed.h"
+/*** @file Driver for Maxbotix sonar modules
+ * Uses pulse width input to read range results.
+ * @author Daniel Casner <http://www.danielcasner.org>
+ *
+ * Known Issues:
+ * If a timer rollover occures during a sample, the driver will drop that sample.
+ */
+ 
+ /** Object representation of a sonar instance
+  */
+ class Sonar {
+ public:
+    /** Sets up the driver and starts taking measurements
+     * @param input The pin to read pulses from
+     * @param t A running timer instance used to measure pulse width.
+     * Note that the timer must have been started or results will be
+     * invalid.
+     */
+    Sonar(PinName input, Timer& t);
+    
+    /// Returns range in cm as int
+    int read();
+    
+    /// Returns the range in CM as an int
+    operator int();
+    
+ private:
+    /// Inturrupt at start of pulse
+    void pulseStart();
+    /// Interrupt at end of pulse
+    void pulseStop();
+    
+    /// Interrupt driver for the input pin
+    InterruptIn interrupt;
+    /// Reference to global timer instance
+    Timer& time;
+    /// Time of the start of the current pulse
+    int pulseStartTime;
+    /// The most recent sample
+    int range;
+ };
\ No newline at end of file