Library for the SRF05 Ultrasonic Rangefinder

Dependencies:   RangeFinder

Fork of SRF05 by Simon Ford

Committer:
simon
Date:
Fri Nov 19 22:34:56 2010 +0000
Revision:
0:e758665e072c
Child:
1:f7420569d67d
Revision in new library format (extracted from old cookbook), with doxygen comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 0:e758665e072c 1 /* mbed SRF05 Ultrasonic Rangefiner Library
simon 0:e758665e072c 2 * Copyright (c) 2007-2010, cstyles, sford
simon 0:e758665e072c 3 *
simon 0:e758665e072c 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
simon 0:e758665e072c 5 * of this software and associated documentation files (the "Software"), to deal
simon 0:e758665e072c 6 * in the Software without restriction, including without limitation the rights
simon 0:e758665e072c 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
simon 0:e758665e072c 8 * copies of the Software, and to permit persons to whom the Software is
simon 0:e758665e072c 9 * furnished to do so, subject to the following conditions:
simon 0:e758665e072c 10 *
simon 0:e758665e072c 11 * The above copyright notice and this permission notice shall be included in
simon 0:e758665e072c 12 * all copies or substantial portions of the Software.
simon 0:e758665e072c 13 *
simon 0:e758665e072c 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
simon 0:e758665e072c 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
simon 0:e758665e072c 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
simon 0:e758665e072c 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
simon 0:e758665e072c 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
simon 0:e758665e072c 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
simon 0:e758665e072c 20 * THE SOFTWARE.
simon 0:e758665e072c 21 */
simon 0:e758665e072c 22
simon 0:e758665e072c 23 #include "SRF05.h"
simon 0:e758665e072c 24 #include "mbed.h"
simon 0:e758665e072c 25
simon 0:e758665e072c 26 SRF05::SRF05(PinName trigger, PinName echo)
simon 0:e758665e072c 27 : _trigger(trigger), _echo(echo) {
simon 0:e758665e072c 28
simon 0:e758665e072c 29 // Attach interrupts
simon 0:e758665e072c 30 _echo.rise(this, &SRF05::_rising);
simon 0:e758665e072c 31 _echo.fall(this, &SRF05::_falling);
simon 0:e758665e072c 32 _ticker.attach(this, &SRF05::_startRange, 0.1);
simon 0:e758665e072c 33 }
simon 0:e758665e072c 34
simon 0:e758665e072c 35 void SRF05::_startRange() {
simon 0:e758665e072c 36 // send a trigger pulse, 20uS long
simon 0:e758665e072c 37 _trigger = 1;
simon 0:e758665e072c 38 wait (0.000002);
simon 0:e758665e072c 39 _trigger = 0;
simon 0:e758665e072c 40 }
simon 0:e758665e072c 41
simon 0:e758665e072c 42 // Clear and start the timer at the begining of the echo pulse
simon 0:e758665e072c 43 void SRF05::_rising(void) {
simon 0:e758665e072c 44 _timer.reset();
simon 0:e758665e072c 45 _timer.start();
simon 0:e758665e072c 46 }
simon 0:e758665e072c 47
simon 0:e758665e072c 48 // Stop and read the timer at the end of the pulse
simon 0:e758665e072c 49 void SRF05::_falling(void) {
simon 0:e758665e072c 50 _timer.stop();
simon 0:e758665e072c 51 _dist = _timer.read_us()/58.0;
simon 0:e758665e072c 52 }
simon 0:e758665e072c 53
simon 0:e758665e072c 54 float SRF05::read(void) {
simon 0:e758665e072c 55 // spin until there is a good value
simon 0:e758665e072c 56 return (_dist);
simon 0:e758665e072c 57 }
simon 0:e758665e072c 58
simon 0:e758665e072c 59 SRF05::operator float() {
simon 0:e758665e072c 60 return read();
simon 0:e758665e072c 61 }