나중에 급하게 PID 알고리즘을 적용해 짜본 코드... 시간이 충분치 않아서 그냥 원래 있던 코드를 수정해서 하기로 했기에 버려진 코드지만, 교수님께 참고용으로 Publish를 했다.

Dependencies:   mbed Adafruit_GFX

Committer:
21400688
Date:
Sat Jun 15 20:39:39 2019 +0000
Revision:
0:c4c874d702f9
first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
21400688 0:c4c874d702f9 1 #include "Ultrasonic.h"
21400688 0:c4c874d702f9 2
21400688 0:c4c874d702f9 3 Ultrasonic::Ultrasonic(PinName trigPin, PinName echoPin, float tick, bool repeat)
21400688 0:c4c874d702f9 4 : _trig(trigPin), _echo(echoPin), _toVal(tick), _repeat(repeat)
21400688 0:c4c874d702f9 5 {
21400688 0:c4c874d702f9 6 _done = 0;
21400688 0:c4c874d702f9 7 _timer.reset();
21400688 0:c4c874d702f9 8 _echo.rise(this, &Ultrasonic::_startT);
21400688 0:c4c874d702f9 9 _echo.fall(this, &Ultrasonic::_endT);
21400688 0:c4c874d702f9 10 if (_repeat) _ticker.attach(this, &Ultrasonic::_ticker_cb, _toVal);
21400688 0:c4c874d702f9 11 }
21400688 0:c4c874d702f9 12
21400688 0:c4c874d702f9 13 Ultrasonic::~Ultrasonic() {}
21400688 0:c4c874d702f9 14
21400688 0:c4c874d702f9 15 void Ultrasonic::_startT(void)
21400688 0:c4c874d702f9 16 {
21400688 0:c4c874d702f9 17 _timer.start();
21400688 0:c4c874d702f9 18 }
21400688 0:c4c874d702f9 19 void Ultrasonic::_endT(void)
21400688 0:c4c874d702f9 20 {
21400688 0:c4c874d702f9 21 _timer.stop();
21400688 0:c4c874d702f9 22 _pulseDuration = _timer.read_us();
21400688 0:c4c874d702f9 23 _distance = _pulseDuration / 58;
21400688 0:c4c874d702f9 24 _timer.reset();
21400688 0:c4c874d702f9 25 _done = 1;
21400688 0:c4c874d702f9 26 }
21400688 0:c4c874d702f9 27
21400688 0:c4c874d702f9 28
21400688 0:c4c874d702f9 29 void Ultrasonic::trig()
21400688 0:c4c874d702f9 30 {
21400688 0:c4c874d702f9 31 _echo.enable_irq();
21400688 0:c4c874d702f9 32 wait_us(2);
21400688 0:c4c874d702f9 33 _trig = 1;
21400688 0:c4c874d702f9 34 wait_us(10);
21400688 0:c4c874d702f9 35 _trig = 0;
21400688 0:c4c874d702f9 36 }
21400688 0:c4c874d702f9 37 void Ultrasonic::_ticker_cb(void)
21400688 0:c4c874d702f9 38 {
21400688 0:c4c874d702f9 39 this->trig();
21400688 0:c4c874d702f9 40 }
21400688 0:c4c874d702f9 41
21400688 0:c4c874d702f9 42 int Ultrasonic::getDistance(void)
21400688 0:c4c874d702f9 43 {
21400688 0:c4c874d702f9 44 return _distance;
21400688 0:c4c874d702f9 45 }
21400688 0:c4c874d702f9 46 int Ultrasonic::getPulseDuration(void)
21400688 0:c4c874d702f9 47 {
21400688 0:c4c874d702f9 48 return _pulseDuration;
21400688 0:c4c874d702f9 49 }
21400688 0:c4c874d702f9 50
21400688 0:c4c874d702f9 51 void Ultrasonic::pauseMeasure(void)
21400688 0:c4c874d702f9 52 {
21400688 0:c4c874d702f9 53 _repeat = false;
21400688 0:c4c874d702f9 54 _ticker.detach();
21400688 0:c4c874d702f9 55 }
21400688 0:c4c874d702f9 56 void Ultrasonic::setMode(bool mode)
21400688 0:c4c874d702f9 57 {
21400688 0:c4c874d702f9 58 _repeat = mode;
21400688 0:c4c874d702f9 59 if (_repeat) _ticker.attach(this, &Ultrasonic::_ticker_cb, _toVal);
21400688 0:c4c874d702f9 60 else _ticker.detach();
21400688 0:c4c874d702f9 61 }
21400688 0:c4c874d702f9 62
21400688 0:c4c874d702f9 63 int Ultrasonic::getStatus()
21400688 0:c4c874d702f9 64 {
21400688 0:c4c874d702f9 65 return _done;
21400688 0:c4c874d702f9 66 }
21400688 0:c4c874d702f9 67 void Ultrasonic::clearStatus(void)
21400688 0:c4c874d702f9 68 {
21400688 0:c4c874d702f9 69 _done = 0;
21400688 0:c4c874d702f9 70 }