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