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