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.
SHARPIR.cpp
00001 /* mbed SHARPIR distance sensor 00002 * Copyright (c) 2010 Tomas Johansen 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy 00005 * of this software and associated documentation files (the "Software"), to deal 00006 * in the Software without restriction, including without limitation the rights 00007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 * copies of the Software, and to permit persons to whom the Software is 00009 * furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included in 00012 * all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 * 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 00020 * THE SOFTWARE. 00021 */ 00022 00023 00024 #include "mbed.h" 00025 #include "SHARPIR.h" 00026 00027 00028 /* This function is currently only working for the Sharp GP2Y0A02YK0F sensor 00029 * which measures from around 20cm to 150cm. To adapt this to other Sharp IR 00030 * sensors, you have to calculate the variables reg and exp. 00031 * 00032 * To quickly calculate reg and exp, use microsoft excel to plot the graph 00033 * provided in the datasheet. You should create a scatterplot (except that 00034 * the y-axis is [cm], and x-axis is [V]). 00035 * 00036 * When you get the values, right click the line in the graph and select 00037 * "Add Trendline". Select "power". Also, in the trendline options, check 00038 * that you want to see the function. It will then be printed in your 00039 * scatterplot. 00040 * 00041 * This is the function: Reg*x^exp. 00042 * 00043 * 00044 * Example: 00045 * 00046 * SHARPIR sensor(p20); 00047 * sensor.calibrate(57.373, 1.3166, 0.45, 2.5); 00048 * while(1){ 00049 * serial.printf("cm: %f ", sensor.cm()); 00050 * wait_ms(50); 00051 * } 00052 * 00053 * You can also use this method to manually plot values you've measured with 00054 * the "volt" function, which in the end should give a result similar to the 00055 * values provided below. 00056 * 00057 * Feel free to contact me with improvements for the source code, and 00058 * especially for values that would work for other sensors. 00059 */ 00060 00061 00062 SHARPIR::SHARPIR(PinName AnalogPort) 00063 : _analogin(AnalogPort) { 00064 higherrange=2.5; 00065 lowerrange=0.45; 00066 reg=57.373; //60.495 00067 exp=-1.3166; //-1.1904 00068 } 00069 00070 void calibrate(double reg, float exp, double lowerrange, double higherrange) { //sets new reg and exp value 00071 } 00072 00073 float SHARPIR::volt() { 00074 return(_analogin.read()*3.3); //analogin function returns a percentage which describes how much of 3.3v it's reading, therefor multiply it by 3,3 to get the correct analogin voltage. 00075 } 00076 00077 float SHARPIR::cm() { 00078 float v; 00079 v=volt(); 00080 if (v>higherrange) //sensor is out of higher range 00081 return(reg*pow(v, exp));//0 00082 else if (v<lowerrange) 00083 return(-1.0); //sensor is out of lower range 00084 else 00085 return(reg*pow(v, exp)); 00086 } 00087 00088 float SHARPIR::inch() { 00089 float v; 00090 v=volt(); 00091 if (v>higherrange) //sensor is out of higher range 00092 return(0); 00093 else if (v<lowerrange) 00094 return(-1.0); //sensor is out of range 00095 else 00096 return(0.393700787*reg*pow(v, exp)); 00097 }
Generated on Sun Jul 17 2022 19:48:27 by
1.7.2