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:
1:4f394d75bc7f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/VMA306.h	Mon May 21 12:37:44 2018 +0000
@@ -0,0 +1,100 @@
+/**
+ * @author Hugues Angelis
+ *
+ * @section LICENSE
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * @section DESCRIPTION
+ *
+ * Vellerman VMA306 Ultrasonic Range Finder Library
+ *
+ * @Note :
+ *  The purpose of this library is to adapt SRF05 library to allow full control
+ *  of the sensor : 
+ *      - allow cycle control (time between 2 pulses)
+ *      - delayed trigerring (start periodical mesurement with a delay) 
+ *  because in case of multiple US sensor use, pulse from a sensor can be
+ *  detected by another like its own echo and thus interfere with mesurment.
+ *
+ *  Cycle time must be greater or equal to 50ms.
+ *  After 30ms the sensor times out so 30ms output pulse means no detection
+ *
+ *  You may divide the echo pulse width by 58µs/cm to calculate the almost
+ *  exact distance to the obstacle.
+ *
+ *  To allow non blocking read you may use the global variable VMA306Flag witch
+ *  is set to one each time a new mesure is ready 
+ */
+
+#ifndef _VMA306_H
+#define _VMA306_H
+
+#include "mbed.h"
+
+class VMA306 {
+public:
+
+    /** Create a VMA306 object, connected to the specified pins
+     *
+     * @param trigger DigitalOut to the VMA306 trigger
+     * @param echo InterruptIn to measure the return pulse
+     */
+    VMA306(PinName trigger, PinName echo);
+    
+    /** Start periodic Trigger
+     *
+     *  @param period float time in second between two trig pulses
+     *
+     *  This function allows user to start each instance of Ultra Sonic range
+     *  finder with a delay from the others (delay managment must be done by
+     *  users).
+     *
+     *  The first pulse will be send 1 period after.
+     */
+    void startPeriodicTrigger (float period = 0.15);
+    
+    /** Gives indication of a new mesurement
+     *
+     *  @return 1 if new data is ready, else 0
+     */
+    int isDataReady ();
+    
+    /** A non-blocking function that will return the last measurement
+     *
+     * @returns floating point representation of distance in cm
+     */
+    float read();
+
+    /** A short hand way of using the read function */
+    operator float();
+        
+private :
+    DigitalOut _trigger;
+    InterruptIn _echo;
+    Timer _timer;
+    Ticker _ticker;
+    void _rising (void);
+    void _falling (void);
+    void _startRange (void);
+    float _dist;
+    int _VMA306Flag;
+};
+
+#endif