Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed URF MD_MDDS30_oit_ ros_lib_melodic
URF.cpp@0:450a7208c682, 2022-05-03 (annotated)
- Committer:
- akihiron
- Date:
- Tue May 03 08:02:58 2022 +0000
- Revision:
- 0:450a7208c682
05/03
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| akihiron | 0:450a7208c682 | 1 | #include "URF.h" |
| akihiron | 0:450a7208c682 | 2 | |
| akihiron | 0:450a7208c682 | 3 | #include "mbed.h" |
| akihiron | 0:450a7208c682 | 4 | |
| akihiron | 0:450a7208c682 | 5 | URF::URF(PinName URF_PIN) |
| akihiron | 0:450a7208c682 | 6 | : _event(URF_PIN) |
| akihiron | 0:450a7208c682 | 7 | , _cmd(URF_PIN) |
| akihiron | 0:450a7208c682 | 8 | , _timer() |
| akihiron | 0:450a7208c682 | 9 | { |
| akihiron | 0:450a7208c682 | 10 | _event.rise(callback(this, &URF::_start)); |
| akihiron | 0:450a7208c682 | 11 | _event.fall(callback(this, &URF::_stop)); |
| akihiron | 0:450a7208c682 | 12 | _METER_PER_SECOND = 334.0; // m/s |
| akihiron | 0:450a7208c682 | 13 | |
| akihiron | 0:450a7208c682 | 14 | _Busy = false; |
| akihiron | 0:450a7208c682 | 15 | |
| akihiron | 0:450a7208c682 | 16 | _cmd.output(); |
| akihiron | 0:450a7208c682 | 17 | _cmd.write(0); |
| akihiron | 0:450a7208c682 | 18 | |
| akihiron | 0:450a7208c682 | 19 | _timer.stop(); |
| akihiron | 0:450a7208c682 | 20 | _timer.reset(); |
| akihiron | 0:450a7208c682 | 21 | } |
| akihiron | 0:450a7208c682 | 22 | |
| akihiron | 0:450a7208c682 | 23 | void URF::_start(void) |
| akihiron | 0:450a7208c682 | 24 | { |
| akihiron | 0:450a7208c682 | 25 | // _Valid = false; // start the timere, and invalidate the current time. |
| akihiron | 0:450a7208c682 | 26 | _Busy = true; |
| akihiron | 0:450a7208c682 | 27 | _timer.reset(); |
| akihiron | 0:450a7208c682 | 28 | _timer.start(); |
| akihiron | 0:450a7208c682 | 29 | } |
| akihiron | 0:450a7208c682 | 30 | |
| akihiron | 0:450a7208c682 | 31 | void URF::_stop(void) |
| akihiron | 0:450a7208c682 | 32 | { |
| akihiron | 0:450a7208c682 | 33 | // _Valid = true; // When it stops, update the time |
| akihiron | 0:450a7208c682 | 34 | _Busy = false; |
| akihiron | 0:450a7208c682 | 35 | _timer.stop(); |
| akihiron | 0:450a7208c682 | 36 | _Time = _timer.read_us(); |
| akihiron | 0:450a7208c682 | 37 | } |
| akihiron | 0:450a7208c682 | 38 | |
| akihiron | 0:450a7208c682 | 39 | void URF::send(void) |
| akihiron | 0:450a7208c682 | 40 | { |
| akihiron | 0:450a7208c682 | 41 | if(_Busy){ |
| akihiron | 0:450a7208c682 | 42 | _timer.stop(); |
| akihiron | 0:450a7208c682 | 43 | _Time = _timer.read_us(); |
| akihiron | 0:450a7208c682 | 44 | return ; |
| akihiron | 0:450a7208c682 | 45 | } |
| akihiron | 0:450a7208c682 | 46 | _cmd.output(); |
| akihiron | 0:450a7208c682 | 47 | _cmd.write(1); |
| akihiron | 0:450a7208c682 | 48 | wait_us(10); |
| akihiron | 0:450a7208c682 | 49 | _cmd.write(0); |
| akihiron | 0:450a7208c682 | 50 | _cmd.input(); |
| akihiron | 0:450a7208c682 | 51 | } |
| akihiron | 0:450a7208c682 | 52 | |
| akihiron | 0:450a7208c682 | 53 | void URF::Set_MpS(float MpS) |
| akihiron | 0:450a7208c682 | 54 | { |
| akihiron | 0:450a7208c682 | 55 | _METER_PER_SECOND = MpS; |
| akihiron | 0:450a7208c682 | 56 | } |
| akihiron | 0:450a7208c682 | 57 | |
| akihiron | 0:450a7208c682 | 58 | int URF::read(int scale) |
| akihiron | 0:450a7208c682 | 59 | // -1 means not valid. |
| akihiron | 0:450a7208c682 | 60 | { |
| akihiron | 0:450a7208c682 | 61 | // if(_Valid && ~_Busy) |
| akihiron | 0:450a7208c682 | 62 | return int(((_Time*(_METER_PER_SECOND / float(scale)))/2.0f) + 0.5f); |
| akihiron | 0:450a7208c682 | 63 | // else |
| akihiron | 0:450a7208c682 | 64 | // return -1; |
| akihiron | 0:450a7208c682 | 65 | } |
| akihiron | 0:450a7208c682 | 66 | |
| akihiron | 0:450a7208c682 | 67 | int URF::read(void) |
| akihiron | 0:450a7208c682 | 68 | // -1 means not valid. |
| akihiron | 0:450a7208c682 | 69 | { |
| akihiron | 0:450a7208c682 | 70 | // if(_Valid && ~_Busy) |
| akihiron | 0:450a7208c682 | 71 | return int(((_Time*(_METER_PER_SECOND / 1000.0f))/2.0f) + 0.5f); |
| akihiron | 0:450a7208c682 | 72 | // else |
| akihiron | 0:450a7208c682 | 73 | // return -1; |
| akihiron | 0:450a7208c682 | 74 | } |