Ryota Kotani / Mbed 2 deprecated ROS_R2_YONRIN

Dependencies:   mbed URF MD_MDDS30_oit_ ros_lib_melodic

URF.cpp

Committer:
akihiron
Date:
2022-05-03
Revision:
0:450a7208c682

File content as of revision 0:450a7208c682:

#include "URF.h"

#include "mbed.h"

URF::URF(PinName URF_PIN)
    : _event(URF_PIN)
    , _cmd(URF_PIN)
    , _timer()
{
    _event.rise(callback(this, &URF::_start));
    _event.fall(callback(this, &URF::_stop));
    _METER_PER_SECOND = 334.0;    // m/s
    
    _Busy = false;
    
    _cmd.output();
    _cmd.write(0);
    
    _timer.stop();
    _timer.reset();
}

void URF::_start(void)
{
//    _Valid = false;  // start the timere, and invalidate the current time.
    _Busy = true;
    _timer.reset();
    _timer.start();
}

void URF::_stop(void)
{
//    _Valid = true;  // When it stops, update the time
    _Busy = false;
    _timer.stop();
    _Time = _timer.read_us();
}

void URF::send(void)
{
    if(_Busy){
        _timer.stop();
        _Time = _timer.read_us();
        return ;
    }
    _cmd.output();
    _cmd.write(1);
    wait_us(10);
    _cmd.write(0);
    _cmd.input();
}

void URF::Set_MpS(float MpS)
{
    _METER_PER_SECOND = MpS;
}

int URF::read(int scale)
// -1 means not valid.
{
//    if(_Valid && ~_Busy)
        return int(((_Time*(_METER_PER_SECOND / float(scale)))/2.0f) + 0.5f);
//    else
//        return -1;
}

int URF::read(void)
// -1 means not valid.
{
//    if(_Valid && ~_Busy)
        return int(((_Time*(_METER_PER_SECOND / 1000.0f))/2.0f) + 0.5f);
//    else
//        return -1;
}