NMF Atlantis
/
NMFAtlantis_Final
Code required to drive the microcontroller of the NMF Atlantis
RangeFinder.cpp@0:dbaef09f6d82, 2020-01-20 (annotated)
- Committer:
- nmf_atlantis
- Date:
- Mon Jan 20 15:47:07 2020 +0000
- Revision:
- 0:dbaef09f6d82
NMF Atlantis final version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nmf_atlantis | 0:dbaef09f6d82 | 1 | /* Copyright (c) 2012 Nick Ryder, University of Oxford |
nmf_atlantis | 0:dbaef09f6d82 | 2 | * nick.ryder@physics.ox.ac.uk |
nmf_atlantis | 0:dbaef09f6d82 | 3 | * |
nmf_atlantis | 0:dbaef09f6d82 | 4 | * MIT License |
nmf_atlantis | 0:dbaef09f6d82 | 5 | * |
nmf_atlantis | 0:dbaef09f6d82 | 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
nmf_atlantis | 0:dbaef09f6d82 | 7 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
nmf_atlantis | 0:dbaef09f6d82 | 8 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
nmf_atlantis | 0:dbaef09f6d82 | 9 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
nmf_atlantis | 0:dbaef09f6d82 | 10 | * furnished to do so, subject to the following conditions: |
nmf_atlantis | 0:dbaef09f6d82 | 11 | * |
nmf_atlantis | 0:dbaef09f6d82 | 12 | * The above copyright notice and this permission notice shall be included in all copies or |
nmf_atlantis | 0:dbaef09f6d82 | 13 | * substantial portions of the Software. |
nmf_atlantis | 0:dbaef09f6d82 | 14 | * |
nmf_atlantis | 0:dbaef09f6d82 | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
nmf_atlantis | 0:dbaef09f6d82 | 16 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
nmf_atlantis | 0:dbaef09f6d82 | 17 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
nmf_atlantis | 0:dbaef09f6d82 | 18 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
nmf_atlantis | 0:dbaef09f6d82 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
nmf_atlantis | 0:dbaef09f6d82 | 20 | */ |
nmf_atlantis | 0:dbaef09f6d82 | 21 | |
nmf_atlantis | 0:dbaef09f6d82 | 22 | |
nmf_atlantis | 0:dbaef09f6d82 | 23 | #include "RangeFinder.h" |
nmf_atlantis | 0:dbaef09f6d82 | 24 | |
nmf_atlantis | 0:dbaef09f6d82 | 25 | RangeFinder::RangeFinder(PinName pin, int pulsetime, float scale, int time): |
nmf_atlantis | 0:dbaef09f6d82 | 26 | pio(pin), scale(scale), pulsetime(pulsetime), timeout(time) { |
nmf_atlantis | 0:dbaef09f6d82 | 27 | } |
nmf_atlantis | 0:dbaef09f6d82 | 28 | |
nmf_atlantis | 0:dbaef09f6d82 | 29 | RangeFinder::~RangeFinder() { |
nmf_atlantis | 0:dbaef09f6d82 | 30 | } |
nmf_atlantis | 0:dbaef09f6d82 | 31 | |
nmf_atlantis | 0:dbaef09f6d82 | 32 | float RangeFinder::read_m() { |
nmf_atlantis | 0:dbaef09f6d82 | 33 | pio.write_us(1, pulsetime); |
nmf_atlantis | 0:dbaef09f6d82 | 34 | float t = (float) pio.read_high_us(timeout); |
nmf_atlantis | 0:dbaef09f6d82 | 35 | if (t == -1.0) { |
nmf_atlantis | 0:dbaef09f6d82 | 36 | return -1.0; |
nmf_atlantis | 0:dbaef09f6d82 | 37 | } |
nmf_atlantis | 0:dbaef09f6d82 | 38 | return t / scale; |
nmf_atlantis | 0:dbaef09f6d82 | 39 | } |
nmf_atlantis | 0:dbaef09f6d82 | 40 |