Library for the SRF05 Ultrasonic Rangefinder
Fork of SRF05 by
SRF05.cpp@1:f7420569d67d, 2018-03-08 (annotated)
- Committer:
- 15062200
- Date:
- Thu Mar 08 17:12:19 2018 +0000
- Revision:
- 1:f7420569d67d
- Parent:
- 0:e758665e072c
fixed srf05
Who changed what in which revision?
User | Revision | Line number | New 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 |
15062200 | 1:f7420569d67d | 30 | _echo.rise(callback(this,&SRF05::_rising)); |
15062200 | 1:f7420569d67d | 31 | _echo.fall(callback(this,&SRF05::_falling)); |
15062200 | 1:f7420569d67d | 32 | _ticker.attach(callback(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; |
15062200 | 1:f7420569d67d | 38 | //wait (0.000002); |
15062200 | 1:f7420569d67d | 39 | wait_us(40); |
simon | 0:e758665e072c | 40 | _trigger = 0; |
simon | 0:e758665e072c | 41 | } |
simon | 0:e758665e072c | 42 | |
simon | 0:e758665e072c | 43 | // Clear and start the timer at the begining of the echo pulse |
simon | 0:e758665e072c | 44 | void SRF05::_rising(void) { |
simon | 0:e758665e072c | 45 | _timer.reset(); |
simon | 0:e758665e072c | 46 | _timer.start(); |
simon | 0:e758665e072c | 47 | } |
simon | 0:e758665e072c | 48 | |
simon | 0:e758665e072c | 49 | // Stop and read the timer at the end of the pulse |
simon | 0:e758665e072c | 50 | void SRF05::_falling(void) { |
simon | 0:e758665e072c | 51 | _timer.stop(); |
simon | 0:e758665e072c | 52 | _dist = _timer.read_us()/58.0; |
15062200 | 1:f7420569d67d | 53 | _dist=99.0; |
simon | 0:e758665e072c | 54 | } |
simon | 0:e758665e072c | 55 | |
simon | 0:e758665e072c | 56 | float SRF05::read(void) { |
simon | 0:e758665e072c | 57 | // spin until there is a good value |
simon | 0:e758665e072c | 58 | return (_dist); |
simon | 0:e758665e072c | 59 | } |
simon | 0:e758665e072c | 60 | |
simon | 0:e758665e072c | 61 | SRF05::operator float() { |
simon | 0:e758665e072c | 62 | return read(); |
simon | 0:e758665e072c | 63 | } |