Committer:
xiaxia686
Date:
Wed Mar 14 17:05:15 2012 +0000
Revision:
0:1e4910aeb884
RF + SRF05 libray

Who changed what in which revision?

UserRevisionLine numberNew contents of line
xiaxia686 0:1e4910aeb884 1 /* mbed SRF05 Ultrasonic Rangefiner Library
xiaxia686 0:1e4910aeb884 2 * Copyright (c) 2007-2010, cstyles, sford
xiaxia686 0:1e4910aeb884 3 *
xiaxia686 0:1e4910aeb884 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
xiaxia686 0:1e4910aeb884 5 * of this software and associated documentation files (the "Software"), to deal
xiaxia686 0:1e4910aeb884 6 * in the Software without restriction, including without limitation the rights
xiaxia686 0:1e4910aeb884 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
xiaxia686 0:1e4910aeb884 8 * copies of the Software, and to permit persons to whom the Software is
xiaxia686 0:1e4910aeb884 9 * furnished to do so, subject to the following conditions:
xiaxia686 0:1e4910aeb884 10 *
xiaxia686 0:1e4910aeb884 11 * The above copyright notice and this permission notice shall be included in
xiaxia686 0:1e4910aeb884 12 * all copies or substantial portions of the Software.
xiaxia686 0:1e4910aeb884 13 *
xiaxia686 0:1e4910aeb884 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
xiaxia686 0:1e4910aeb884 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
xiaxia686 0:1e4910aeb884 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
xiaxia686 0:1e4910aeb884 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
xiaxia686 0:1e4910aeb884 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
xiaxia686 0:1e4910aeb884 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
xiaxia686 0:1e4910aeb884 20 * THE SOFTWARE.
xiaxia686 0:1e4910aeb884 21 */
xiaxia686 0:1e4910aeb884 22
xiaxia686 0:1e4910aeb884 23 #include "RFSRF05.h"
xiaxia686 0:1e4910aeb884 24 #include "mbed.h"
xiaxia686 0:1e4910aeb884 25 //#include "RF12B.h"
xiaxia686 0:1e4910aeb884 26
xiaxia686 0:1e4910aeb884 27 RFSRF05::RFSRF05(PinName trigger,
xiaxia686 0:1e4910aeb884 28 PinName echo0,
xiaxia686 0:1e4910aeb884 29 PinName echo1,
xiaxia686 0:1e4910aeb884 30 PinName echo2,
xiaxia686 0:1e4910aeb884 31 PinName echo3,
xiaxia686 0:1e4910aeb884 32 PinName echo4,
xiaxia686 0:1e4910aeb884 33 PinName echo5,
xiaxia686 0:1e4910aeb884 34 PinName SDI,
xiaxia686 0:1e4910aeb884 35 PinName SDO,
xiaxia686 0:1e4910aeb884 36 PinName SCK,
xiaxia686 0:1e4910aeb884 37 PinName NCS,
xiaxia686 0:1e4910aeb884 38 PinName NIRQ)
xiaxia686 0:1e4910aeb884 39 : _trigger(trigger),
xiaxia686 0:1e4910aeb884 40 _echo0(echo0),
xiaxia686 0:1e4910aeb884 41 _echo1(echo1),
xiaxia686 0:1e4910aeb884 42 _echo2(echo2),
xiaxia686 0:1e4910aeb884 43 _echo3(echo3),
xiaxia686 0:1e4910aeb884 44 _echo4(echo4),
xiaxia686 0:1e4910aeb884 45 _echo5(echo5),
xiaxia686 0:1e4910aeb884 46 _rf(SDI,SDO,SCK,NCS,NIRQ) {
xiaxia686 0:1e4910aeb884 47 // initialises codes
xiaxia686 0:1e4910aeb884 48 _code[0] = CODE0;
xiaxia686 0:1e4910aeb884 49 _code[1] = CODE1;
xiaxia686 0:1e4910aeb884 50 _code[2] = CODE2;
xiaxia686 0:1e4910aeb884 51
xiaxia686 0:1e4910aeb884 52 // innitialises beacon counter
xiaxia686 0:1e4910aeb884 53 _beacon_counter = 0;
xiaxia686 0:1e4910aeb884 54 // Attach interrupts
xiaxia686 0:1e4910aeb884 55 _echo0.rise(this, &RFSRF05::_rising);
xiaxia686 0:1e4910aeb884 56 _echo0.fall(this, &RFSRF05::_falling);
xiaxia686 0:1e4910aeb884 57 _echo1.fall(this, &RFSRF05::_falling);
xiaxia686 0:1e4910aeb884 58 _echo2.fall(this, &RFSRF05::_falling);
xiaxia686 0:1e4910aeb884 59 _echo3.fall(this, &RFSRF05::_falling);
xiaxia686 0:1e4910aeb884 60 _echo4.fall(this, &RFSRF05::_falling);
xiaxia686 0:1e4910aeb884 61 _echo5.fall(this, &RFSRF05::_falling);
xiaxia686 0:1e4910aeb884 62 //Interrupts every 50ms
xiaxia686 0:1e4910aeb884 63 _ticker.attach(this, &RFSRF05::_startRange, 0.05);
xiaxia686 0:1e4910aeb884 64 }
xiaxia686 0:1e4910aeb884 65
xiaxia686 0:1e4910aeb884 66 void RFSRF05::_startRange() {
xiaxia686 0:1e4910aeb884 67 //Calculates dist
xiaxia686 0:1e4910aeb884 68 _dist[_beacon_counter] = _timer.read_us()/29.0;
xiaxia686 0:1e4910aeb884 69
xiaxia686 0:1e4910aeb884 70 // increments counter
xiaxia686 0:1e4910aeb884 71 _beacon_counter = (_beacon_counter + 1) % 3;
xiaxia686 0:1e4910aeb884 72
xiaxia686 0:1e4910aeb884 73 // writes code to RF port
xiaxia686 0:1e4910aeb884 74 _rf.write(_code[_beacon_counter]);
xiaxia686 0:1e4910aeb884 75
xiaxia686 0:1e4910aeb884 76 // send a trigger pulse, 10uS long
xiaxia686 0:1e4910aeb884 77 _trigger = 1;
xiaxia686 0:1e4910aeb884 78 wait_us (10);
xiaxia686 0:1e4910aeb884 79 _trigger = 0;
xiaxia686 0:1e4910aeb884 80 }
xiaxia686 0:1e4910aeb884 81
xiaxia686 0:1e4910aeb884 82 // Clear and start the timer at the begining of the echo pulse
xiaxia686 0:1e4910aeb884 83 void RFSRF05::_rising(void) {
xiaxia686 0:1e4910aeb884 84 _timer.reset();
xiaxia686 0:1e4910aeb884 85 _timer.start();
xiaxia686 0:1e4910aeb884 86 }
xiaxia686 0:1e4910aeb884 87
xiaxia686 0:1e4910aeb884 88 // Stop and read the timer at the end of the pulse
xiaxia686 0:1e4910aeb884 89 void RFSRF05::_falling(void) {
xiaxia686 0:1e4910aeb884 90 _timer.stop();
xiaxia686 0:1e4910aeb884 91 }
xiaxia686 0:1e4910aeb884 92
xiaxia686 0:1e4910aeb884 93 float RFSRF05::read0() {
xiaxia686 0:1e4910aeb884 94 // returns distance
xiaxia686 0:1e4910aeb884 95 return (_dist[0]);
xiaxia686 0:1e4910aeb884 96 }
xiaxia686 0:1e4910aeb884 97
xiaxia686 0:1e4910aeb884 98 float RFSRF05::read1() {
xiaxia686 0:1e4910aeb884 99 // returns distance
xiaxia686 0:1e4910aeb884 100 return (_dist[1]);
xiaxia686 0:1e4910aeb884 101 }
xiaxia686 0:1e4910aeb884 102
xiaxia686 0:1e4910aeb884 103 float RFSRF05::read2() {
xiaxia686 0:1e4910aeb884 104 // returns distance
xiaxia686 0:1e4910aeb884 105 return (_dist[2]);
xiaxia686 0:1e4910aeb884 106 }
xiaxia686 0:1e4910aeb884 107
xiaxia686 0:1e4910aeb884 108 //SRF05::operator float() {
xiaxia686 0:1e4910aeb884 109 // return read();
xiaxia686 0:1e4910aeb884 110 //}