Class to handle triggering and reading the distance measured by a range finder.
Dependents: SeeedUltrasoundRangeFinder Ultrasonic_sensor_circle_ui riversensor Labo_TRSE_Drone ... more
RangeFinder.cpp
00001 /* Copyright (c) 2012 Nick Ryder, University of Oxford 00002 * nick.ryder@physics.ox.ac.uk 00003 * 00004 * MIT License 00005 * 00006 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00007 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00008 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00009 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00010 * furnished to do so, subject to the following conditions: 00011 * 00012 * The above copyright notice and this permission notice shall be included in all copies or 00013 * substantial portions of the Software. 00014 * 00015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00016 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00017 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00018 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00020 */ 00021 00022 00023 #include "RangeFinder.h" 00024 00025 RangeFinder::RangeFinder(PinName pin, int pulsetime, float scale, int time): 00026 pio(pin), scale(scale), pulsetime(pulsetime), timeout(time) { 00027 } 00028 00029 RangeFinder::~RangeFinder() { 00030 } 00031 00032 float RangeFinder::read_m() { 00033 pio.write_us(1, pulsetime); 00034 float t = (float) pio.read_high_us(timeout); 00035 if (t == -1.0) { 00036 return -1.0; 00037 } 00038 return t / scale; 00039 }
Generated on Tue Jul 12 2022 12:27:23 by 1.7.2