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: Pulse
Dependents: Grove-UltrasonicRanger_Example PM2_Libary PM2_Libary PM2_Libary
RangeFinder.cpp@7:2865b7aec20c, 2022-02-10 (annotated)
- Committer:
- pmic
- Date:
- Thu Feb 10 12:08:27 2022 +0000
- Revision:
- 7:2865b7aec20c
- Parent:
- 5:600591c78153
Minor adjustments.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| NickRyder | 0:05c9036328ee | 1 | /* Copyright (c) 2012 Nick Ryder, University of Oxford |
| NickRyder | 0:05c9036328ee | 2 | * nick.ryder@physics.ox.ac.uk |
| NickRyder | 0:05c9036328ee | 3 | * |
| NickRyder | 0:05c9036328ee | 4 | * MIT License |
| NickRyder | 0:05c9036328ee | 5 | * |
| NickRyder | 0:05c9036328ee | 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
| NickRyder | 0:05c9036328ee | 7 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
| NickRyder | 0:05c9036328ee | 8 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
| NickRyder | 0:05c9036328ee | 9 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
| NickRyder | 0:05c9036328ee | 10 | * furnished to do so, subject to the following conditions: |
| NickRyder | 0:05c9036328ee | 11 | * |
| NickRyder | 0:05c9036328ee | 12 | * The above copyright notice and this permission notice shall be included in all copies or |
| NickRyder | 0:05c9036328ee | 13 | * substantial portions of the Software. |
| NickRyder | 0:05c9036328ee | 14 | * |
| NickRyder | 0:05c9036328ee | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
| NickRyder | 0:05c9036328ee | 16 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| NickRyder | 0:05c9036328ee | 17 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| NickRyder | 0:05c9036328ee | 18 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| NickRyder | 0:05c9036328ee | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| NickRyder | 0:05c9036328ee | 20 | */ |
| NickRyder | 0:05c9036328ee | 21 | |
| NickRyder | 0:05c9036328ee | 22 | |
| NickRyder | 0:05c9036328ee | 23 | #include "RangeFinder.h" |
| pmic | 3:cbb20cd7905d | 24 | |
| pmic | 3:cbb20cd7905d | 25 | |
| pmic | 3:cbb20cd7905d | 26 | using namespace std; |
| NickRyder | 0:05c9036328ee | 27 | |
| pmic | 2:89d7dd44ecfd | 28 | RangeFinder::RangeFinder(PinName pin, int pulsetime, float scale, float offset, int time): |
| pmic | 2:89d7dd44ecfd | 29 | pio(pin), scale(scale), offset(offset), pulsetime(pulsetime), timeout(time) { |
| NickRyder | 0:05c9036328ee | 30 | } |
| NickRyder | 0:05c9036328ee | 31 | |
| pmic | 5:600591c78153 | 32 | RangeFinder::RangeFinder(PinName pin, float scale, float offset, int time): |
| pmic | 5:600591c78153 | 33 | pio(pin), scale(scale), offset(offset), pulsetime(10), timeout(time) { |
| pmic | 5:600591c78153 | 34 | } |
| pmic | 5:600591c78153 | 35 | |
| NickRyder | 0:05c9036328ee | 36 | RangeFinder::~RangeFinder() { |
| NickRyder | 0:05c9036328ee | 37 | } |
| NickRyder | 0:05c9036328ee | 38 | |
| pmic | 5:600591c78153 | 39 | float RangeFinder::read_cm() { |
| NickRyder | 0:05c9036328ee | 40 | pio.write_us(1, pulsetime); |
| NickRyder | 0:05c9036328ee | 41 | float t = (float) pio.read_high_us(timeout); |
| NickRyder | 0:05c9036328ee | 42 | if (t == -1.0) { |
| NickRyder | 0:05c9036328ee | 43 | return -1.0; |
| NickRyder | 0:05c9036328ee | 44 | } |
| pmic | 5:600591c78153 | 45 | return 100.0f*( t / scale + offset ); |
| NickRyder | 0:05c9036328ee | 46 | } |