altb_pmic / RangeFinder

Dependencies:   Pulse

Dependents:   Grove-UltrasonicRanger_Example PM2_Libary PM2_Libary PM2_Libary

Committer:
altb2
Date:
Fri Jul 12 15:19:53 2019 +0000
Revision:
4:fe63b514d4ef
Parent:
2:89d7dd44ecfd
Extended to 4 Sensors which are read sequentially

Who changed what in which revision?

UserRevisionLine numberNew 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"
NickRyder 0:05c9036328ee 24
altb2 4:fe63b514d4ef 25 // Init with 1 Sensor:
altb2 4:fe63b514d4ef 26 RangeFinder::RangeFinder(PinName pin0, int pulsetime, float scale, float offset, int time):
altb2 4:fe63b514d4ef 27 pio0(pin0), pio1(NC), pio2(NC), pio3(NC), scale(scale), offset(offset), pulsetime(pulsetime), timeout(time) {
altb2 4:fe63b514d4ef 28 n_sensors = 1;
altb2 4:fe63b514d4ef 29 this->old_val[0] = 0.0;
altb2 4:fe63b514d4ef 30 current_sensor = 0;
altb2 4:fe63b514d4ef 31 }
altb2 4:fe63b514d4ef 32
altb2 4:fe63b514d4ef 33 // Init with 2 Sensors:
altb2 4:fe63b514d4ef 34 RangeFinder::RangeFinder(PinName pin0, PinName pin1,int pulsetime, float scale, float offset, int time):
altb2 4:fe63b514d4ef 35 pio0(pin0), pio1(pin1), pio2(NC), pio3(NC), scale(scale), offset(offset), pulsetime(pulsetime), timeout(time) {
altb2 4:fe63b514d4ef 36 n_sensors = 2;
altb2 4:fe63b514d4ef 37 for(uint8_t k=0;k<n_sensors;k++)
altb2 4:fe63b514d4ef 38 this->old_val[k] = 0.0;
altb2 4:fe63b514d4ef 39 current_sensor = 0;
altb2 4:fe63b514d4ef 40
altb2 4:fe63b514d4ef 41 }
altb2 4:fe63b514d4ef 42 // Init with 3 Sensors:
altb2 4:fe63b514d4ef 43 RangeFinder::RangeFinder(PinName pin0,PinName pin1, PinName pin2, int pulsetime, float scale, float offset, int time):
altb2 4:fe63b514d4ef 44 pio0(pin0), pio1(pin1), pio2(pin2), pio3(NC), scale(scale), offset(offset), pulsetime(pulsetime), timeout(time) {
altb2 4:fe63b514d4ef 45 n_sensors = 3;
altb2 4:fe63b514d4ef 46 for(uint8_t k=0;k<n_sensors;k++)
altb2 4:fe63b514d4ef 47 this->old_val[k] = 0.0;
altb2 4:fe63b514d4ef 48 current_sensor = 0;
altb2 4:fe63b514d4ef 49 }
altb2 4:fe63b514d4ef 50 // Init with 4 Sensors:
altb2 4:fe63b514d4ef 51 RangeFinder::RangeFinder(PinName pin0, PinName pin1, PinName pin2,PinName pin3, int pulsetime, float scale, float offset, int time):
altb2 4:fe63b514d4ef 52 pio0(pin0), pio1(pin1), pio2(pin2), pio3(pin3), scale(scale), offset(offset), pulsetime(pulsetime), timeout(time) {
altb2 4:fe63b514d4ef 53 n_sensors = 4;
altb2 4:fe63b514d4ef 54 for(uint8_t k=0;k<n_sensors;k++)
altb2 4:fe63b514d4ef 55 this->old_val[k] = 0.0;
altb2 4:fe63b514d4ef 56 current_sensor = 0;
NickRyder 0:05c9036328ee 57 }
NickRyder 0:05c9036328ee 58
NickRyder 0:05c9036328ee 59 RangeFinder::~RangeFinder() {
NickRyder 0:05c9036328ee 60 }
altb2 4:fe63b514d4ef 61 void RangeFinder::read_and_filter(uint8_t i) {
altb2 4:fe63b514d4ef 62 val[i] = this->read_m(i);
altb2 4:fe63b514d4ef 63 if(val[i] < 0)
altb2 4:fe63b514d4ef 64 val[i] = old_val[i];
altb2 4:fe63b514d4ef 65 else{
altb2 4:fe63b514d4ef 66 old_val[i] = val[i];
altb2 4:fe63b514d4ef 67 }
altb2 4:fe63b514d4ef 68 }
altb2 4:fe63b514d4ef 69 // -----------------------------------------------------------------------------
altb2 4:fe63b514d4ef 70 float RangeFinder::read_m(uint8_t i) {
altb2 4:fe63b514d4ef 71 float t;
altb2 4:fe63b514d4ef 72 switch(i){
altb2 4:fe63b514d4ef 73 case 0:
altb2 4:fe63b514d4ef 74 pio0.write_us(1, pulsetime);
altb2 4:fe63b514d4ef 75 t = (float) pio0.read_high_us(timeout);
altb2 4:fe63b514d4ef 76 break;
altb2 4:fe63b514d4ef 77 case 1:
altb2 4:fe63b514d4ef 78 pio1.write_us(1, pulsetime);
altb2 4:fe63b514d4ef 79 t = (float) pio1.read_high_us(timeout);
altb2 4:fe63b514d4ef 80 break;
altb2 4:fe63b514d4ef 81 case 2:
altb2 4:fe63b514d4ef 82 pio2.write_us(1, pulsetime);
altb2 4:fe63b514d4ef 83 t = (float) pio2.read_high_us(timeout);
altb2 4:fe63b514d4ef 84 break;
altb2 4:fe63b514d4ef 85 case 3:
altb2 4:fe63b514d4ef 86 pio3.write_us(1, pulsetime);
altb2 4:fe63b514d4ef 87 t = (float) pio3.read_high_us(timeout);
altb2 4:fe63b514d4ef 88 break;
altb2 4:fe63b514d4ef 89 }
NickRyder 0:05c9036328ee 90 if (t == -1.0) {
NickRyder 0:05c9036328ee 91 return -1.0;
NickRyder 0:05c9036328ee 92 }
pmic 2:89d7dd44ecfd 93 return t / scale + offset;
NickRyder 0:05c9036328ee 94 }