Minsu Kim / HC_SR04_Ultrasonic_Library

Dependents:   Group2 c8_Final_course1

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ultrasonic.cpp Source File

ultrasonic.cpp

00001  #include "ultrasonic.h"
00002     
00003 // generate a 10 us pulse to start HCSR04 ultrasonic sensor and
00004 // enable IRQ for echo pin.
00005 Ultrasonic::Ultrasonic(PinName trigPin, PinName echoPin, float timeout, bool repeat):\
00006                 _trig(trigPin), _echo(echoPin), _toVal(timeout), _repeat(repeat)
00007 {
00008     _timer.reset();
00009     _echo.rise(this, &Ultrasonic::_startT);
00010     _echo.fall(this, &Ultrasonic::_endT);
00011     _echo.disable_irq();
00012     _done = 0;
00013     _cnt = 0;
00014 }
00015 
00016 Ultrasonic::~Ultrasonic()
00017 {
00018 }
00019 
00020 void Ultrasonic::trig(void)
00021 {
00022     _done = 0;
00023     _echo.enable_irq();
00024     _trig = 1;        // trigger = 1
00025     wait_us(10);    // for 10 sec
00026     _trig = 0;        // turn off the trigger
00027 }
00028 
00029 int Ultrasonic::getDistance(void)
00030 {
00031     if(_distance >= 0)          // if the _distance is equal or more than 0, resume that detection is completed.
00032         return _distance;       // return _distance.
00033     else                        
00034         return -1;              // if not, return the '-1'.
00035 }
00036 
00037 // return the echo pulse duration in us and return -1 in case of failure.
00038 int Ultrasonic::getPulseDuration(void)
00039 {
00040     if(_pulseDuration >= 0)     // if the _pulseDuration is equal or more than 0, resume that detection is completed.
00041         return _pulseDuration;  // return _pulseDuration.
00042     else
00043         return -1;              // if not, return the '-1'.
00044 }
00045 
00046 int Ultrasonic::getStatus(void)
00047 {
00048     return _done;               // Measurement status
00049 }
00050 
00051 void Ultrasonic::clearStatus(void)
00052 {
00053     _done = 0;
00054     _timer.reset();
00055     _echo.disable_irq();
00056     _cnt = 0;
00057     _distance = 0;                  
00058 }
00059 
00060 void Ultrasonic::pauseMeasure(void)
00061 {
00062     _timeout.detach();             // make timeout detach to pause the measurement
00063 }
00064 
00065 
00066 void Ultrasonic::setMode(bool mode)
00067 {
00068     _repeat = mode;             // input the value to _repeat according to the value of mode
00069                                 // _repeat determine the way of measurement
00070 }
00071 
00072 void Ultrasonic::setTime(float time)
00073 {
00074     _toVal = time;              // input the value to _toVal accoring to the value of time
00075                                 // _toVal make the interrupt of _timer which is a timer object 
00076 }
00077 
00078 void Ultrasonic::_startT(void)
00079 {
00080     _timer.start();             // read timer microseconds, this is starting time of the measurement
00081 }
00082 
00083 void Ultrasonic::_endT(void)
00084 {
00085     _timer.stop();                          // when the pulse is falling, timer pulsetime is stopped.
00086     _pulseDuration = _timer.read_us();      // and the value of timer pulsetime enter the variable 'pulsedur'
00087     _distance = (_pulseDuration*343)/20000; // 340m/s * sec / 2(rounded) / 10000 (/u*100)
00088     _timer.reset();                         // After knowing the distance, time pulsetime is reseted.
00089     if(_repeat == true)
00090     {
00091         _timeout.attach(this, &Ultrasonic::_timeout_cb, _toVal);
00092         printf("%d trial: %dcm\r\n", (_cnt+1), _distance);
00093     }
00094     else
00095     {
00096         _echo.disable_irq();
00097         _done = 1;
00098         //printf("%dcm\r\n", _distance);
00099     }
00100 }
00101 
00102 void Ultrasonic::_timeout_cb(void)
00103 {
00104 
00105     if(_cnt >= 9)
00106     {
00107         _echo.disable_irq();
00108         _done = 1;
00109         _cnt = 0;
00110     }
00111     else
00112     {
00113          trig();
00114          _cnt++;
00115     }
00116 }
00117