Ultrasonic Range Finder Sensors Library. May be used for SRF05 and all others sensors of the same kind (50ms period, 10us pulse)

Dependents:   FRC_2018 TestVMA 0hackton_08_06_18 lib_FRC_2019 ... more

VMA306.cpp

Committer:
haarkon
Date:
2018-05-22
Revision:
2:6bb02f1d4ca6
Parent:
0:bc016581f12b
Child:
3:4d9c742b860b

File content as of revision 2:6bb02f1d4ca6:

/* mbed VMA306 Ultrasonic Rangefiner Library
 */

#include "VMA306.h"

VMA306::VMA306(PinName trigger, PinName echo) 
    : _trigger(trigger), _echo(echo) {    
        
    // Attach interrupts
    _echo.rise(callback(this, &VMA306::_rising));
    _echo.fall(callback(this, &VMA306::_falling));
}
  
void VMA306::_startRange() {
    // send a trigger pulse, 20uS long
    _trigger = 1;
    wait (0.000002);
    _trigger = 0;
}

// Clear and start the timer at the begining of the echo pulse
void VMA306::_rising(void) {
    _timer.reset();
    _timer.start();
}

// Stop and read the timer at the end of the pulse
void VMA306::_falling(void) {
    _timer.stop();
    _dist = _timer.read_us()/58.0;
    _VMA306Flag = 1;
}

void VMA306::startPeriodicTrigger (float period) {
    _ticker.attach(callback(this, &VMA306::_startRange), period);
}  

int VMA306::isDataReady (void){
    return (_VMA306Flag);
}


float VMA306::read(void) {
    _VMA306Flag = 0;
    return (_dist);
}

VMA306::operator float() {
    _VMA306Flag = 0;
    return read();
}