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:
Tue May 22 17:04:02 2018 +0000
Revision:
1:4f394d75bc7f
Parent:
0:bc016581f12b
Child:
3:4d9c742b860b
cosmetic changes

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 1:4f394d75bc7f 29 * The purpose of this library is to adapt SRF05 library to allow full control of the sensor :
haarkon 0:bc016581f12b 30 * - allow cycle control (time between 2 pulses)
haarkon 0:bc016581f12b 31 * - delayed trigerring (start periodical mesurement with a delay)
haarkon 1:4f394d75bc7f 32 * 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 mesurement.
haarkon 0:bc016581f12b 33 *
haarkon 0:bc016581f12b 34 * Cycle time must be greater or equal to 50ms.
haarkon 0:bc016581f12b 35 * After 30ms the sensor times out so 30ms output pulse means no detection
haarkon 0:bc016581f12b 36 *
haarkon 1:4f394d75bc7f 37 * You may divide the echo pulse width by 58µs/cm to calculate the almost exact distance to the obstacle in centimeters.
haarkon 0:bc016581f12b 38 *
haarkon 1:4f394d75bc7f 39 * To allow non blocking read you may use the global variable VMA306Flag witch is set to one each time a new mesure is ready. This variable must be cleared by user
haarkon 0:bc016581f12b 40 */
haarkon 0:bc016581f12b 41
haarkon 0:bc016581f12b 42 #ifndef _VMA306_H
haarkon 0:bc016581f12b 43 #define _VMA306_H
haarkon 0:bc016581f12b 44
haarkon 1:4f394d75bc7f 45 /**
haarkon 1:4f394d75bc7f 46 * Includes : Mbed Library
haarkon 1:4f394d75bc7f 47 */
haarkon 0:bc016581f12b 48 #include "mbed.h"
haarkon 0:bc016581f12b 49
haarkon 1:4f394d75bc7f 50 /**
haarkon 1:4f394d75bc7f 51 * VMA306 Ultrasonic Range Finder
haarkon 1:4f394d75bc7f 52 * similar to SRF05 : https://www.robot-electronics.co.uk/htm/srf05tech.htm
haarkon 1:4f394d75bc7f 53 * Poor documentation can be found here (FR) : https://www.gotronic.fr/art-capteur-a-ultrasons-vma306-26096.htm
haarkon 1:4f394d75bc7f 54 */
haarkon 0:bc016581f12b 55 class VMA306 {
haarkon 0:bc016581f12b 56 public:
haarkon 0:bc016581f12b 57
haarkon 1:4f394d75bc7f 58 /** Constructor : Create a VMA306 object, connected to the specified pins
haarkon 0:bc016581f12b 59 *
haarkon 1:4f394d75bc7f 60 * @param trigger : DigitalOut to the VMA306 trigger
haarkon 1:4f394d75bc7f 61 * @param echo : InterruptIn to measure the return pulse
haarkon 0:bc016581f12b 62 */
haarkon 0:bc016581f12b 63 VMA306(PinName trigger, PinName echo);
haarkon 0:bc016581f12b 64
haarkon 0:bc016581f12b 65 /** Start periodic Trigger
haarkon 0:bc016581f12b 66 *
haarkon 1:4f394d75bc7f 67 * @param period : float time in second between two trig pulses
haarkon 0:bc016581f12b 68 *
haarkon 1:4f394d75bc7f 69 * @note 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).
haarkon 1:4f394d75bc7f 70 * @note The first pulse will be send 1 period after.
haarkon 0:bc016581f12b 71 */
haarkon 0:bc016581f12b 72 void startPeriodicTrigger (float period = 0.15);
haarkon 0:bc016581f12b 73
haarkon 0:bc016581f12b 74 /** Gives indication of a new mesurement
haarkon 0:bc016581f12b 75 *
haarkon 0:bc016581f12b 76 * @return 1 if new data is ready, else 0
haarkon 0:bc016581f12b 77 */
haarkon 0:bc016581f12b 78 int isDataReady ();
haarkon 0:bc016581f12b 79
haarkon 0:bc016581f12b 80 /** A non-blocking function that will return the last measurement
haarkon 0:bc016581f12b 81 *
haarkon 0:bc016581f12b 82 * @returns floating point representation of distance in cm
haarkon 0:bc016581f12b 83 */
haarkon 0:bc016581f12b 84 float read();
haarkon 0:bc016581f12b 85
haarkon 0:bc016581f12b 86 /** A short hand way of using the read function */
haarkon 0:bc016581f12b 87 operator float();
haarkon 0:bc016581f12b 88
haarkon 0:bc016581f12b 89 private :
haarkon 0:bc016581f12b 90 DigitalOut _trigger;
haarkon 0:bc016581f12b 91 InterruptIn _echo;
haarkon 0:bc016581f12b 92 Timer _timer;
haarkon 0:bc016581f12b 93 Ticker _ticker;
haarkon 0:bc016581f12b 94 void _rising (void);
haarkon 0:bc016581f12b 95 void _falling (void);
haarkon 0:bc016581f12b 96 void _startRange (void);
haarkon 0:bc016581f12b 97 float _dist;
haarkon 0:bc016581f12b 98 int _VMA306Flag;
haarkon 0:bc016581f12b 99 };
haarkon 0:bc016581f12b 100
haarkon 0:bc016581f12b 101 #endif