SRF02_Edurobot

Fork of SRF02 by Roberto D'Amico

Committer:
BjornVB
Date:
Tue Feb 18 09:21:39 2014 +0000
Revision:
2:e5a05e0969fa
Parent:
1:6b978ea41787
SRF02_Ra

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bobboteck 0:8ac34d529a3b 1 /* mbed SRF02 Ultrasonic Ranger Sensor Library
bobboteck 0:8ac34d529a3b 2 * Created by bobboteck at 16/11/2011
bobboteck 0:8ac34d529a3b 3 *
bobboteck 0:8ac34d529a3b 4 * Based on: Chris Styles [http://mbed.org/users/chris/] Library for SRF08
bobboteck 0:8ac34d529a3b 5 * "http://mbed.org/users/chris/programs/SRF08/603nk/docs/"
bobboteck 0:8ac34d529a3b 6 *
bobboteck 0:8ac34d529a3b 7 * Permission is hereby granted, free of charge, to any person obtaining a copy
bobboteck 0:8ac34d529a3b 8 * of this software and associated documentation files (the "Software"), to deal
bobboteck 0:8ac34d529a3b 9 * in the Software without restriction, including without limitation the rights
bobboteck 0:8ac34d529a3b 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
bobboteck 0:8ac34d529a3b 11 * copies of the Software, and to permit persons to whom the Software is
bobboteck 0:8ac34d529a3b 12 * furnished to do so, subject to the following conditions:
bobboteck 0:8ac34d529a3b 13 *
bobboteck 0:8ac34d529a3b 14 * The above copyright notice and this permission notice shall be included in
bobboteck 0:8ac34d529a3b 15 * all copies or substantial portions of the Software.
bobboteck 0:8ac34d529a3b 16 *
bobboteck 0:8ac34d529a3b 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
bobboteck 0:8ac34d529a3b 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
bobboteck 0:8ac34d529a3b 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
bobboteck 0:8ac34d529a3b 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
bobboteck 0:8ac34d529a3b 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
bobboteck 0:8ac34d529a3b 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
bobboteck 0:8ac34d529a3b 23 * THE SOFTWARE.
bobboteck 0:8ac34d529a3b 24 */
bobboteck 0:8ac34d529a3b 25
bobboteck 0:8ac34d529a3b 26 #ifndef SRF02_H
bobboteck 0:8ac34d529a3b 27 #define SRF02_H
bobboteck 0:8ac34d529a3b 28
bobboteck 0:8ac34d529a3b 29 #include "mbed.h"
bobboteck 0:8ac34d529a3b 30
bobboteck 1:6b978ea41787 31 /** Library for the SRF02 Ultrasonic Ranger Sensor, using the I2C bus for the
bobboteck 0:8ac34d529a3b 32 * comunication. Remeber that sensor can function in serial mode if MODE pin
bobboteck 0:8ac34d529a3b 33 * is connected to the ground, but this libary not support this mode.
bobboteck 0:8ac34d529a3b 34 *
bobboteck 0:8ac34d529a3b 35 * Example:
bobboteck 0:8ac34d529a3b 36 * @code
bobboteck 0:8ac34d529a3b 37 * // Continuously measuring range
bobboteck 0:8ac34d529a3b 38 * #include "mbed.h"
bobboteck 0:8ac34d529a3b 39 * #include "SRF02.h"
bobboteck 0:8ac34d529a3b 40 * // Create instance of class SRF02 for device at address 0xE0 and take mesure in cm
BjornVB 2:e5a05e0969fa 41 * SRF02 srf02(p28, p27, 0xE0, 0x51);
bobboteck 0:8ac34d529a3b 42 * DigitalOut led1(LED1);
bobboteck 0:8ac34d529a3b 43 *
bobboteck 0:8ac34d529a3b 44 * int main()
bobboteck 0:8ac34d529a3b 45 * {
bobboteck 0:8ac34d529a3b 46 * led1=1;
bobboteck 0:8ac34d529a3b 47 * while(1)
bobboteck 0:8ac34d529a3b 48 * {
bobboteck 0:8ac34d529a3b 49 * printf("Measured range : %.2f cm\n\r",srf02.read());
bobboteck 0:8ac34d529a3b 50 * wait(0.5);
bobboteck 0:8ac34d529a3b 51 * led1=!led1;
bobboteck 0:8ac34d529a3b 52 * }
bobboteck 0:8ac34d529a3b 53 * }
bobboteck 0:8ac34d529a3b 54 * @endcode
bobboteck 0:8ac34d529a3b 55 */
bobboteck 0:8ac34d529a3b 56 class SRF02
bobboteck 0:8ac34d529a3b 57 {
bobboteck 0:8ac34d529a3b 58 public:
BjornVB 2:e5a05e0969fa 59 //enum _typem{INCHES=0x050,CENTIMETERS=0x51,MICROSECONDS=0x52};
bobboteck 1:6b978ea41787 60 /** Creates an instance of class. Setting the pin used for I2C, the address of device and the measure range type.
bobboteck 0:8ac34d529a3b 61 *
bobboteck 0:8ac34d529a3b 62 * @param sda A pin used for SDA I2C signal.
bobboteck 0:8ac34d529a3b 63 * @param scl A pin used for SCL I2C signal.
bobboteck 0:8ac34d529a3b 64 * @param addr The address of I2C SRF02 device.
BjornVB 2:e5a05e0969fa 65 * @param measure_type The of mesure response (0x50-inches,0x51-centimeters,0x52-micro-seconds).
bobboteck 0:8ac34d529a3b 66 */
bobboteck 0:8ac34d529a3b 67 SRF02(PinName sda, PinName scl, int addr, char type);
bobboteck 0:8ac34d529a3b 68
bobboteck 0:8ac34d529a3b 69 /* Destroyer of class instance. */
bobboteck 0:8ac34d529a3b 70 ~SRF02();
bobboteck 0:8ac34d529a3b 71
bobboteck 1:6b978ea41787 72 /** Start and return the range measure.
bobboteck 0:8ac34d529a3b 73 *
bobboteck 0:8ac34d529a3b 74 * @returns The value of measure.
bobboteck 0:8ac34d529a3b 75 */
bobboteck 0:8ac34d529a3b 76 float read();
bobboteck 0:8ac34d529a3b 77
bobboteck 0:8ac34d529a3b 78 private:
bobboteck 0:8ac34d529a3b 79 I2C _i2c; // I2C object to comunicate with SRF02
bobboteck 0:8ac34d529a3b 80 int _addr; // Address of sensor
bobboteck 1:6b978ea41787 81 char _typem; // Type of mesure
bobboteck 0:8ac34d529a3b 82 };
bobboteck 0:8ac34d529a3b 83
bobboteck 0:8ac34d529a3b 84 #endif