![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
This is a very simple guide, reviewing the steps required to get Blinky working on an Mbed OS platform.
Dependencies: mbed Adafruit_GFX
ultrasonic.cpp@95:250afd53b710, 2019-06-08 (annotated)
- Committer:
- ParkChunMyong
- Date:
- Sat Jun 08 12:00:38 2019 +0000
- Revision:
- 95:250afd53b710
first init
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ParkChunMyong | 95:250afd53b710 | 1 | #include "ultrasonic.h" |
ParkChunMyong | 95:250afd53b710 | 2 | |
ParkChunMyong | 95:250afd53b710 | 3 | // generate a 10 us pulse to start HCSR04 ulrasonic sensor and |
ParkChunMyong | 95:250afd53b710 | 4 | // enable IRQ for echo pin. |
ParkChunMyong | 95:250afd53b710 | 5 | Ultrasonic::Ultrasonic(PinName trigPin, PinName echoPin, float timeout, bool mode, float time) : |
ParkChunMyong | 95:250afd53b710 | 6 | _trig(trigPin), _echo(echoPin), _toVal(timeout), _pc(PA_2, PA_3), _repeat(mode), _repeatD(time){ |
ParkChunMyong | 95:250afd53b710 | 7 | _timer.reset(); |
ParkChunMyong | 95:250afd53b710 | 8 | _echo.rise(this, &Ultrasonic::_startT); |
ParkChunMyong | 95:250afd53b710 | 9 | _echo.fall(this, &Ultrasonic::_endT); |
ParkChunMyong | 95:250afd53b710 | 10 | _echo.mode(PullUp); |
ParkChunMyong | 95:250afd53b710 | 11 | _echo.disable_irq(); |
ParkChunMyong | 95:250afd53b710 | 12 | _pc.baud(115200); |
ParkChunMyong | 95:250afd53b710 | 13 | } |
ParkChunMyong | 95:250afd53b710 | 14 | |
ParkChunMyong | 95:250afd53b710 | 15 | |
ParkChunMyong | 95:250afd53b710 | 16 | Ultrasonic::~Ultrasonic(){} |
ParkChunMyong | 95:250afd53b710 | 17 | |
ParkChunMyong | 95:250afd53b710 | 18 | int Ultrasonic::getDistance(void) { |
ParkChunMyong | 95:250afd53b710 | 19 | calculateDistance(); |
ParkChunMyong | 95:250afd53b710 | 20 | return _distance; |
ParkChunMyong | 95:250afd53b710 | 21 | } |
ParkChunMyong | 95:250afd53b710 | 22 | |
ParkChunMyong | 95:250afd53b710 | 23 | void Ultrasonic::calculateDistance(void) { |
ParkChunMyong | 95:250afd53b710 | 24 | clearStatus(); |
ParkChunMyong | 95:250afd53b710 | 25 | |
ParkChunMyong | 95:250afd53b710 | 26 | trig(); |
ParkChunMyong | 95:250afd53b710 | 27 | getPulseDuration(); |
ParkChunMyong | 95:250afd53b710 | 28 | |
ParkChunMyong | 95:250afd53b710 | 29 | if (_pulseDuration >= 0) _distance = _pulseDuration / 29 / 2; |
ParkChunMyong | 95:250afd53b710 | 30 | else _distance = -1; |
ParkChunMyong | 95:250afd53b710 | 31 | |
ParkChunMyong | 95:250afd53b710 | 32 | } |
ParkChunMyong | 95:250afd53b710 | 33 | |
ParkChunMyong | 95:250afd53b710 | 34 | void Ultrasonic::trig(void) { |
ParkChunMyong | 95:250afd53b710 | 35 | _echo.enable_irq(); |
ParkChunMyong | 95:250afd53b710 | 36 | _trig = 1; |
ParkChunMyong | 95:250afd53b710 | 37 | wait_us(10); |
ParkChunMyong | 95:250afd53b710 | 38 | _trig = 0; |
ParkChunMyong | 95:250afd53b710 | 39 | } |
ParkChunMyong | 95:250afd53b710 | 40 | |
ParkChunMyong | 95:250afd53b710 | 41 | int Ultrasonic::getPulseDuration(void) { |
ParkChunMyong | 95:250afd53b710 | 42 | |
ParkChunMyong | 95:250afd53b710 | 43 | while (!_break) { |
ParkChunMyong | 95:250afd53b710 | 44 | _pc.printf("\r"); |
ParkChunMyong | 95:250afd53b710 | 45 | } // wait signal |
ParkChunMyong | 95:250afd53b710 | 46 | // we need to change |
ParkChunMyong | 95:250afd53b710 | 47 | |
ParkChunMyong | 95:250afd53b710 | 48 | _break = 0; |
ParkChunMyong | 95:250afd53b710 | 49 | _echo.disable_irq(); |
ParkChunMyong | 95:250afd53b710 | 50 | |
ParkChunMyong | 95:250afd53b710 | 51 | if (_end >= 0) _pulseDuration = _end - _start; |
ParkChunMyong | 95:250afd53b710 | 52 | else _pulseDuration = -1; |
ParkChunMyong | 95:250afd53b710 | 53 | |
ParkChunMyong | 95:250afd53b710 | 54 | return _pulseDuration; |
ParkChunMyong | 95:250afd53b710 | 55 | } |
ParkChunMyong | 95:250afd53b710 | 56 | |
ParkChunMyong | 95:250afd53b710 | 57 | int Ultrasonic::getStatus(void) { |
ParkChunMyong | 95:250afd53b710 | 58 | return _done; |
ParkChunMyong | 95:250afd53b710 | 59 | } |
ParkChunMyong | 95:250afd53b710 | 60 | |
ParkChunMyong | 95:250afd53b710 | 61 | void Ultrasonic::clearStatus(void) { |
ParkChunMyong | 95:250afd53b710 | 62 | _done = 0; |
ParkChunMyong | 95:250afd53b710 | 63 | _pulseDuration = 0; |
ParkChunMyong | 95:250afd53b710 | 64 | _distance = 0; |
ParkChunMyong | 95:250afd53b710 | 65 | _start = -1; |
ParkChunMyong | 95:250afd53b710 | 66 | _end = -1; |
ParkChunMyong | 95:250afd53b710 | 67 | _trig = 0; |
ParkChunMyong | 95:250afd53b710 | 68 | } |
ParkChunMyong | 95:250afd53b710 | 69 | |
ParkChunMyong | 95:250afd53b710 | 70 | void Ultrasonic::pauseMeasure(void) { |
ParkChunMyong | 95:250afd53b710 | 71 | _done = 1; |
ParkChunMyong | 95:250afd53b710 | 72 | } |
ParkChunMyong | 95:250afd53b710 | 73 | |
ParkChunMyong | 95:250afd53b710 | 74 | void Ultrasonic::_startT(void) { |
ParkChunMyong | 95:250afd53b710 | 75 | _timer.start(); |
ParkChunMyong | 95:250afd53b710 | 76 | _start = _timer.read_us(); |
ParkChunMyong | 95:250afd53b710 | 77 | _timeout.attach(callback(this, &Ultrasonic::_timeout_cb), _toVal); |
ParkChunMyong | 95:250afd53b710 | 78 | } |
ParkChunMyong | 95:250afd53b710 | 79 | |
ParkChunMyong | 95:250afd53b710 | 80 | void Ultrasonic::_endT(void) { |
ParkChunMyong | 95:250afd53b710 | 81 | _timeout.detach(); |
ParkChunMyong | 95:250afd53b710 | 82 | _end = _timer.read_us(); |
ParkChunMyong | 95:250afd53b710 | 83 | _timer.stop(); |
ParkChunMyong | 95:250afd53b710 | 84 | _timer.reset(); |
ParkChunMyong | 95:250afd53b710 | 85 | _break = 1; |
ParkChunMyong | 95:250afd53b710 | 86 | } |
ParkChunMyong | 95:250afd53b710 | 87 | |
ParkChunMyong | 95:250afd53b710 | 88 | void Ultrasonic::_timeout_cb(void) { |
ParkChunMyong | 95:250afd53b710 | 89 | _timer.stop(); |
ParkChunMyong | 95:250afd53b710 | 90 | _timer.reset(); |
ParkChunMyong | 95:250afd53b710 | 91 | _break = 1; |
ParkChunMyong | 95:250afd53b710 | 92 | } |
ParkChunMyong | 95:250afd53b710 | 93 | |
ParkChunMyong | 95:250afd53b710 | 94 | void Ultrasonic::setMode(bool mode){ |
ParkChunMyong | 95:250afd53b710 | 95 | _repeat = mode; |
ParkChunMyong | 95:250afd53b710 | 96 | } |
ParkChunMyong | 95:250afd53b710 | 97 | |
ParkChunMyong | 95:250afd53b710 | 98 | void Ultrasonic::printDistance(float interval){ |
ParkChunMyong | 95:250afd53b710 | 99 | |
ParkChunMyong | 95:250afd53b710 | 100 | int num = (int)(1/interval); |
ParkChunMyong | 95:250afd53b710 | 101 | |
ParkChunMyong | 95:250afd53b710 | 102 | if(_repeat == true) |
ParkChunMyong | 95:250afd53b710 | 103 | { |
ParkChunMyong | 95:250afd53b710 | 104 | for(int i=0; i<num; i++) |
ParkChunMyong | 95:250afd53b710 | 105 | { |
ParkChunMyong | 95:250afd53b710 | 106 | _pc.printf("distance: %d\r\n", getDistance()); |
ParkChunMyong | 95:250afd53b710 | 107 | wait(interval/2); |
ParkChunMyong | 95:250afd53b710 | 108 | } |
ParkChunMyong | 95:250afd53b710 | 109 | } |
ParkChunMyong | 95:250afd53b710 | 110 | |
ParkChunMyong | 95:250afd53b710 | 111 | else |
ParkChunMyong | 95:250afd53b710 | 112 | _pc.printf("distance: %d\r\n", getDistance()); |
ParkChunMyong | 95:250afd53b710 | 113 | |
ParkChunMyong | 95:250afd53b710 | 114 | num = 0; |
ParkChunMyong | 95:250afd53b710 | 115 | } |