Ultrasonic Range Finder Sensors Library. May be used for SRF05 and all others sensors of the same kind (50ms period, 10us pulse)

Dependents:   FRC_2018 TestVMA 0hackton_08_06_18 lib_FRC_2019 ... more

Revision:
0:bc016581f12b
Child:
2:6bb02f1d4ca6
diff -r 000000000000 -r bc016581f12b VMA306.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/VMA306.cpp	Mon May 21 12:37:44 2018 +0000
@@ -0,0 +1,51 @@
+/* mbed VMA306 Ultrasonic Rangefiner Library
+ */
+
+#include "VMA306.h"
+
+VMA306::VMA306(PinName trigger, PinName echo) 
+    : _trigger(trigger), _echo(echo) {    
+        
+    // Attach interrupts
+    _echo.rise(this, &VMA306::_rising);
+    _echo.fall(this, &VMA306::_falling);
+}
+  
+void VMA306::_startRange() {
+    // send a trigger pulse, 20uS long
+    _trigger = 1;
+    wait (0.000002);
+    _trigger = 0;
+}
+
+// Clear and start the timer at the begining of the echo pulse
+void VMA306::_rising(void) {
+    _timer.reset();
+    _timer.start();
+}
+
+// Stop and read the timer at the end of the pulse
+void VMA306::_falling(void) {
+    _timer.stop();
+    _dist = _timer.read_us()/58.0;
+    _VMA306Flag = 1;
+}
+
+void VMA306::startPeriodicTrigger (float period) {
+    _ticker.attach(this, &VMA306::_startRange, period);
+}  
+
+int VMA306::isDataReady (void){
+    return (_VMA306Flag);
+}
+
+
+float VMA306::read(void) {
+    _VMA306Flag = 0;
+    return (_dist);
+}
+
+VMA306::operator float() {
+    _VMA306Flag = 0;
+    return read();
+}