Sharp IR GP2Y0A02YK0F
Dependents: SharpIR_test m3pi_object-avoider lab4_proximityGame_timer
SHARPIR.cpp@0:323d66022af5, 2010-08-02 (annotated)
- Committer:
- Tomas
- Date:
- Mon Aug 02 12:38:00 2010 +0000
- Revision:
- 0:323d66022af5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Tomas | 0:323d66022af5 | 1 | /* mbed SHARPIR distance sensor |
Tomas | 0:323d66022af5 | 2 | * Copyright (c) 2010 Tomas Johansen |
Tomas | 0:323d66022af5 | 3 | * |
Tomas | 0:323d66022af5 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
Tomas | 0:323d66022af5 | 5 | * of this software and associated documentation files (the "Software"), to deal |
Tomas | 0:323d66022af5 | 6 | * in the Software without restriction, including without limitation the rights |
Tomas | 0:323d66022af5 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
Tomas | 0:323d66022af5 | 8 | * copies of the Software, and to permit persons to whom the Software is |
Tomas | 0:323d66022af5 | 9 | * furnished to do so, subject to the following conditions: |
Tomas | 0:323d66022af5 | 10 | * |
Tomas | 0:323d66022af5 | 11 | * The above copyright notice and this permission notice shall be included in |
Tomas | 0:323d66022af5 | 12 | * all copies or substantial portions of the Software. |
Tomas | 0:323d66022af5 | 13 | * |
Tomas | 0:323d66022af5 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Tomas | 0:323d66022af5 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Tomas | 0:323d66022af5 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
Tomas | 0:323d66022af5 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
Tomas | 0:323d66022af5 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Tomas | 0:323d66022af5 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
Tomas | 0:323d66022af5 | 20 | * THE SOFTWARE. |
Tomas | 0:323d66022af5 | 21 | */ |
Tomas | 0:323d66022af5 | 22 | |
Tomas | 0:323d66022af5 | 23 | |
Tomas | 0:323d66022af5 | 24 | #include "mbed.h" |
Tomas | 0:323d66022af5 | 25 | #include "SHARPIR.h" |
Tomas | 0:323d66022af5 | 26 | |
Tomas | 0:323d66022af5 | 27 | |
Tomas | 0:323d66022af5 | 28 | /* This function is currently only working for the Sharp GP2Y0A02YK0F sensor |
Tomas | 0:323d66022af5 | 29 | * which measures from around 20cm to 150cm. To adapt this to other Sharp IR |
Tomas | 0:323d66022af5 | 30 | * sensors, you have to calculate the variables reg and exp. |
Tomas | 0:323d66022af5 | 31 | * |
Tomas | 0:323d66022af5 | 32 | * To quickly calculate reg and exp, use microsoft excel to plot the graph |
Tomas | 0:323d66022af5 | 33 | * provided in the datasheet. You should create a scatterplot (except that |
Tomas | 0:323d66022af5 | 34 | * the y-axis is [cm], and x-axis is [V]). |
Tomas | 0:323d66022af5 | 35 | * |
Tomas | 0:323d66022af5 | 36 | * When you get the values, right click the line in the graph and select |
Tomas | 0:323d66022af5 | 37 | * "Add Trendline". Select "power". Also, in the trendline options, check |
Tomas | 0:323d66022af5 | 38 | * that you want to see the function. It will then be printed in your |
Tomas | 0:323d66022af5 | 39 | * scatterplot. |
Tomas | 0:323d66022af5 | 40 | * |
Tomas | 0:323d66022af5 | 41 | * This is the function: Reg*x^exp. |
Tomas | 0:323d66022af5 | 42 | * |
Tomas | 0:323d66022af5 | 43 | * |
Tomas | 0:323d66022af5 | 44 | * Example: |
Tomas | 0:323d66022af5 | 45 | * |
Tomas | 0:323d66022af5 | 46 | * SHARPIR sensor(p20); |
Tomas | 0:323d66022af5 | 47 | * sensor.calibrate(57.373, 1.3166, 0.45, 2.5); |
Tomas | 0:323d66022af5 | 48 | * while(1){ |
Tomas | 0:323d66022af5 | 49 | * serial.printf("cm: %f ", sensor.cm()); |
Tomas | 0:323d66022af5 | 50 | * wait_ms(50); |
Tomas | 0:323d66022af5 | 51 | * } |
Tomas | 0:323d66022af5 | 52 | * |
Tomas | 0:323d66022af5 | 53 | * You can also use this method to manually plot values you've measured with |
Tomas | 0:323d66022af5 | 54 | * the "volt" function, which in the end should give a result similar to the |
Tomas | 0:323d66022af5 | 55 | * values provided below. |
Tomas | 0:323d66022af5 | 56 | * |
Tomas | 0:323d66022af5 | 57 | * Feel free to contact me with improvements for the source code, and |
Tomas | 0:323d66022af5 | 58 | * especially for values that would work for other sensors. |
Tomas | 0:323d66022af5 | 59 | */ |
Tomas | 0:323d66022af5 | 60 | |
Tomas | 0:323d66022af5 | 61 | |
Tomas | 0:323d66022af5 | 62 | SHARPIR::SHARPIR(PinName AnalogPort) |
Tomas | 0:323d66022af5 | 63 | : _analogin(AnalogPort) { |
Tomas | 0:323d66022af5 | 64 | higherrange=2.5; |
Tomas | 0:323d66022af5 | 65 | lowerrange=0.45; |
Tomas | 0:323d66022af5 | 66 | reg=57.373; //60.495 |
Tomas | 0:323d66022af5 | 67 | exp=-1.3166; //-1.1904 |
Tomas | 0:323d66022af5 | 68 | } |
Tomas | 0:323d66022af5 | 69 | |
Tomas | 0:323d66022af5 | 70 | void calibrate(double reg, float exp, double lowerrange, double higherrange) { //sets new reg and exp value |
Tomas | 0:323d66022af5 | 71 | } |
Tomas | 0:323d66022af5 | 72 | |
Tomas | 0:323d66022af5 | 73 | float SHARPIR::volt() { |
Tomas | 0:323d66022af5 | 74 | 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. |
Tomas | 0:323d66022af5 | 75 | } |
Tomas | 0:323d66022af5 | 76 | |
Tomas | 0:323d66022af5 | 77 | float SHARPIR::cm() { |
Tomas | 0:323d66022af5 | 78 | float v; |
Tomas | 0:323d66022af5 | 79 | v=volt(); |
Tomas | 0:323d66022af5 | 80 | if (v>higherrange) //sensor is out of higher range |
Tomas | 0:323d66022af5 | 81 | return(0); |
Tomas | 0:323d66022af5 | 82 | else if (v<lowerrange) |
Tomas | 0:323d66022af5 | 83 | return(-1.0); //sensor is out of lower range |
Tomas | 0:323d66022af5 | 84 | else |
Tomas | 0:323d66022af5 | 85 | return(reg*pow(v, exp)); |
Tomas | 0:323d66022af5 | 86 | } |
Tomas | 0:323d66022af5 | 87 | |
Tomas | 0:323d66022af5 | 88 | float SHARPIR::inch() { |
Tomas | 0:323d66022af5 | 89 | float v; |
Tomas | 0:323d66022af5 | 90 | v=volt(); |
Tomas | 0:323d66022af5 | 91 | if (v>higherrange) //sensor is out of higher range |
Tomas | 0:323d66022af5 | 92 | return(0); |
Tomas | 0:323d66022af5 | 93 | else if (v<lowerrange) |
Tomas | 0:323d66022af5 | 94 | return(-1.0); //sensor is out of range |
Tomas | 0:323d66022af5 | 95 | else |
Tomas | 0:323d66022af5 | 96 | return(0.393700787*reg*pow(v, exp)); |
Tomas | 0:323d66022af5 | 97 | } |