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.
Dependents: QuadCopter mbed-IBooth-ETH
Revision 0:7e65f5077f5a, committed 2012-11-21
- Comitter:
- DanielC
- Date:
- Wed Nov 21 20:47:05 2012 +0000
- 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 |
diff -r 000000000000 -r 7e65f5077f5a sonar.cpp
--- /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
diff -r 000000000000 -r 7e65f5077f5a sonar.h
--- /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