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

Committer:
haarkon
Date:
Mon May 21 12:37:44 2018 +0000
Revision:
0:bc016581f12b
Child:
1:4f394d75bc7f
Untested First version of VMA306 (SRF05 and others) ultrasonic range finder Sensors.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
haarkon 0:bc016581f12b 1 /**
haarkon 0:bc016581f12b 2 * @author Hugues Angelis
haarkon 0:bc016581f12b 3 *
haarkon 0:bc016581f12b 4 * @section LICENSE
haarkon 0:bc016581f12b 5 *
haarkon 0:bc016581f12b 6 * Permission is hereby granted, free of charge, to any person obtaining a copy
haarkon 0:bc016581f12b 7 * of this software and associated documentation files (the "Software"), to deal
haarkon 0:bc016581f12b 8 * in the Software without restriction, including without limitation the rights
haarkon 0:bc016581f12b 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
haarkon 0:bc016581f12b 10 * copies of the Software, and to permit persons to whom the Software is
haarkon 0:bc016581f12b 11 * furnished to do so, subject to the following conditions:
haarkon 0:bc016581f12b 12 *
haarkon 0:bc016581f12b 13 * The above copyright notice and this permission notice shall be included in
haarkon 0:bc016581f12b 14 * all copies or substantial portions of the Software.
haarkon 0:bc016581f12b 15 *
haarkon 0:bc016581f12b 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
haarkon 0:bc016581f12b 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
haarkon 0:bc016581f12b 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
haarkon 0:bc016581f12b 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
haarkon 0:bc016581f12b 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
haarkon 0:bc016581f12b 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
haarkon 0:bc016581f12b 22 * THE SOFTWARE.
haarkon 0:bc016581f12b 23 *
haarkon 0:bc016581f12b 24 * @section DESCRIPTION
haarkon 0:bc016581f12b 25 *
haarkon 0:bc016581f12b 26 * Vellerman VMA306 Ultrasonic Range Finder Library
haarkon 0:bc016581f12b 27 *
haarkon 0:bc016581f12b 28 * @Note :
haarkon 0:bc016581f12b 29 * The purpose of this library is to adapt SRF05 library to allow full control
haarkon 0:bc016581f12b 30 * of the sensor :
haarkon 0:bc016581f12b 31 * - allow cycle control (time between 2 pulses)
haarkon 0:bc016581f12b 32 * - delayed trigerring (start periodical mesurement with a delay)
haarkon 0:bc016581f12b 33 * because in case of multiple US sensor use, pulse from a sensor can be
haarkon 0:bc016581f12b 34 * detected by another like its own echo and thus interfere with mesurment.
haarkon 0:bc016581f12b 35 *
haarkon 0:bc016581f12b 36 * Cycle time must be greater or equal to 50ms.
haarkon 0:bc016581f12b 37 * After 30ms the sensor times out so 30ms output pulse means no detection
haarkon 0:bc016581f12b 38 *
haarkon 0:bc016581f12b 39 * You may divide the echo pulse width by 58µs/cm to calculate the almost
haarkon 0:bc016581f12b 40 * exact distance to the obstacle.
haarkon 0:bc016581f12b 41 *
haarkon 0:bc016581f12b 42 * To allow non blocking read you may use the global variable VMA306Flag witch
haarkon 0:bc016581f12b 43 * is set to one each time a new mesure is ready
haarkon 0:bc016581f12b 44 */
haarkon 0:bc016581f12b 45
haarkon 0:bc016581f12b 46 #ifndef _VMA306_H
haarkon 0:bc016581f12b 47 #define _VMA306_H
haarkon 0:bc016581f12b 48
haarkon 0:bc016581f12b 49 #include "mbed.h"
haarkon 0:bc016581f12b 50
haarkon 0:bc016581f12b 51 class VMA306 {
haarkon 0:bc016581f12b 52 public:
haarkon 0:bc016581f12b 53
haarkon 0:bc016581f12b 54 /** Create a VMA306 object, connected to the specified pins
haarkon 0:bc016581f12b 55 *
haarkon 0:bc016581f12b 56 * @param trigger DigitalOut to the VMA306 trigger
haarkon 0:bc016581f12b 57 * @param echo InterruptIn to measure the return pulse
haarkon 0:bc016581f12b 58 */
haarkon 0:bc016581f12b 59 VMA306(PinName trigger, PinName echo);
haarkon 0:bc016581f12b 60
haarkon 0:bc016581f12b 61 /** Start periodic Trigger
haarkon 0:bc016581f12b 62 *
haarkon 0:bc016581f12b 63 * @param period float time in second between two trig pulses
haarkon 0:bc016581f12b 64 *
haarkon 0:bc016581f12b 65 * This function allows user to start each instance of Ultra Sonic range
haarkon 0:bc016581f12b 66 * finder with a delay from the others (delay managment must be done by
haarkon 0:bc016581f12b 67 * users).
haarkon 0:bc016581f12b 68 *
haarkon 0:bc016581f12b 69 * The first pulse will be send 1 period after.
haarkon 0:bc016581f12b 70 */
haarkon 0:bc016581f12b 71 void startPeriodicTrigger (float period = 0.15);
haarkon 0:bc016581f12b 72
haarkon 0:bc016581f12b 73 /** Gives indication of a new mesurement
haarkon 0:bc016581f12b 74 *
haarkon 0:bc016581f12b 75 * @return 1 if new data is ready, else 0
haarkon 0:bc016581f12b 76 */
haarkon 0:bc016581f12b 77 int isDataReady ();
haarkon 0:bc016581f12b 78
haarkon 0:bc016581f12b 79 /** A non-blocking function that will return the last measurement
haarkon 0:bc016581f12b 80 *
haarkon 0:bc016581f12b 81 * @returns floating point representation of distance in cm
haarkon 0:bc016581f12b 82 */
haarkon 0:bc016581f12b 83 float read();
haarkon 0:bc016581f12b 84
haarkon 0:bc016581f12b 85 /** A short hand way of using the read function */
haarkon 0:bc016581f12b 86 operator float();
haarkon 0:bc016581f12b 87
haarkon 0:bc016581f12b 88 private :
haarkon 0:bc016581f12b 89 DigitalOut _trigger;
haarkon 0:bc016581f12b 90 InterruptIn _echo;
haarkon 0:bc016581f12b 91 Timer _timer;
haarkon 0:bc016581f12b 92 Ticker _ticker;
haarkon 0:bc016581f12b 93 void _rising (void);
haarkon 0:bc016581f12b 94 void _falling (void);
haarkon 0:bc016581f12b 95 void _startRange (void);
haarkon 0:bc016581f12b 96 float _dist;
haarkon 0:bc016581f12b 97 int _VMA306Flag;
haarkon 0:bc016581f12b 98 };
haarkon 0:bc016581f12b 99
haarkon 0:bc016581f12b 100 #endif