Sharp GP2 familly distance sensor library
Dependents: FRC_2018 0hackton_08_06_18 0hackton_08_06_18_publish Kenya_2019 ... more
GP2A.h@4:4f443a6a6843, 2018-05-31 (annotated)
- Committer:
- haarkon
- Date:
- Thu May 31 17:25:25 2018 +0000
- Revision:
- 4:4f443a6a6843
- Parent:
- 3:dcd04d9d0eb7
- Child:
- 5:49fb7016797c
Tested GP2A library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
haarkon | 0:17de10d278c2 | 1 | /** |
haarkon | 0:17de10d278c2 | 2 | * @author Hugues Angelis |
haarkon | 0:17de10d278c2 | 3 | * |
haarkon | 0:17de10d278c2 | 4 | * @section LICENSE |
haarkon | 0:17de10d278c2 | 5 | * |
haarkon | 0:17de10d278c2 | 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
haarkon | 0:17de10d278c2 | 7 | * of this software and associated documentation files (the "Software"), to deal |
haarkon | 0:17de10d278c2 | 8 | * in the Software without restriction, including without limitation the rights |
haarkon | 0:17de10d278c2 | 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
haarkon | 0:17de10d278c2 | 10 | * copies of the Software, and to permit persons to whom the Software is |
haarkon | 0:17de10d278c2 | 11 | * furnished to do so, subject to the following conditions: |
haarkon | 0:17de10d278c2 | 12 | * |
haarkon | 0:17de10d278c2 | 13 | * The above copyright notice and this permission notice shall be included in |
haarkon | 0:17de10d278c2 | 14 | * all copies or substantial portions of the Software. |
haarkon | 0:17de10d278c2 | 15 | * |
haarkon | 0:17de10d278c2 | 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
haarkon | 0:17de10d278c2 | 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
haarkon | 0:17de10d278c2 | 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
haarkon | 0:17de10d278c2 | 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
haarkon | 0:17de10d278c2 | 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
haarkon | 0:17de10d278c2 | 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
haarkon | 0:17de10d278c2 | 22 | * THE SOFTWARE. |
haarkon | 0:17de10d278c2 | 23 | * |
haarkon | 0:17de10d278c2 | 24 | * @section DESCRIPTION |
haarkon | 0:17de10d278c2 | 25 | * |
haarkon | 0:17de10d278c2 | 26 | * SHARP GP2 Analog sensor familly library |
haarkon | 0:17de10d278c2 | 27 | * |
haarkon | 0:17de10d278c2 | 28 | * Any GP2 analog sensor may be added to this library using the constructor : |
haarkon | 0:17de10d278c2 | 29 | * minDistance : is the minimum distance for linear approximation (cm) |
haarkon | 0:17de10d278c2 | 30 | * maxDistance : is the maximum distance for linear approximation (cm) |
haarkon | 0:17de10d278c2 | 31 | * slope : is the slope of the linear part of the graph V = f(1/d) |
haarkon | 0:17de10d278c2 | 32 | * take special care that we use the 1/d instead of d for x |
haarkon | 0:17de10d278c2 | 33 | * axis. |
haarkon | 1:956ee3b2eaa0 | 34 | * The slope parameter must be computed by user unless the GP2 reference is |
haarkon | 1:956ee3b2eaa0 | 35 | * listed in the list below : |
haarkon | 4:4f443a6a6843 | 36 | * - GP2Y0A02YK0F (Min = 30cm, Max = 150cm, Slope = 55) |
haarkon | 4:4f443a6a6843 | 37 | * - GP2Y0A21YK0F (Min = 7cm, Max = 80cm, Slope = 10) |
haarkon | 1:956ee3b2eaa0 | 38 | * |
haarkon | 1:956ee3b2eaa0 | 39 | * You may add others sensors if you wish it |
haarkon | 0:17de10d278c2 | 40 | * |
haarkon | 0:17de10d278c2 | 41 | * One must be aware that under the minimum distance the sensor will output |
haarkon | 0:17de10d278c2 | 42 | * a false value of distance leading to strong errors. |
haarkon | 1:956ee3b2eaa0 | 43 | * @endsection |
haarkon | 0:17de10d278c2 | 44 | */ |
haarkon | 0:17de10d278c2 | 45 | |
haarkon | 0:17de10d278c2 | 46 | #ifndef GP2A_H |
haarkon | 0:17de10d278c2 | 47 | #define GP2A_H |
haarkon | 0:17de10d278c2 | 48 | |
haarkon | 0:17de10d278c2 | 49 | /** |
haarkon | 0:17de10d278c2 | 50 | * Slope definitions for common GP2A sensors |
haarkon | 0:17de10d278c2 | 51 | */ |
haarkon | 0:17de10d278c2 | 52 | |
haarkon | 4:4f443a6a6843 | 53 | #define GP2Y0A02YK0F 55.0 |
haarkon | 4:4f443a6a6843 | 54 | #define GP2Y0A21YK0F 10 |
haarkon | 0:17de10d278c2 | 55 | |
haarkon | 0:17de10d278c2 | 56 | /** |
haarkon | 3:dcd04d9d0eb7 | 57 | * Includes : Mbed library |
haarkon | 0:17de10d278c2 | 58 | */ |
haarkon | 0:17de10d278c2 | 59 | #include "mbed.h" |
haarkon | 0:17de10d278c2 | 60 | |
haarkon | 0:17de10d278c2 | 61 | /** |
haarkon | 3:dcd04d9d0eb7 | 62 | * GP2A Sensor : See sharp website for more informations |
haarkon | 3:dcd04d9d0eb7 | 63 | * http://www.sharp-world.com/products/device/lineup/selection/opto/haca/diagram.html |
haarkon | 0:17de10d278c2 | 64 | */ |
haarkon | 0:17de10d278c2 | 65 | class GP2A { |
haarkon | 0:17de10d278c2 | 66 | |
haarkon | 0:17de10d278c2 | 67 | public : |
haarkon | 0:17de10d278c2 | 68 | |
haarkon | 0:17de10d278c2 | 69 | /** |
haarkon | 3:dcd04d9d0eb7 | 70 | * Constructor of a Sharp GP2 Familly object. |
haarkon | 0:17de10d278c2 | 71 | * |
haarkon | 4:4f443a6a6843 | 72 | * @param vmes (PinName) : the Mbed pin used to connect with GP2A sensor |
haarkon | 4:4f443a6a6843 | 73 | * @param dMin (double) : the GP2A sensor min distance to mesure |
haarkon | 4:4f443a6a6843 | 74 | * @param dMax (double) : the GP2A sensor max distance to mesure |
haarkon | 4:4f443a6a6843 | 75 | * @param slope (double) : the slope of the linear part of the graph V = f(1/d) |
haarkon | 4:4f443a6a6843 | 76 | * @note you better use slope value that I've put above |
haarkon | 0:17de10d278c2 | 77 | */ |
haarkon | 0:17de10d278c2 | 78 | GP2A(PinName vmes, float dMin, float dMax, float slope); |
haarkon | 0:17de10d278c2 | 79 | |
haarkon | 0:17de10d278c2 | 80 | /** |
haarkon | 3:dcd04d9d0eb7 | 81 | * Return the distance to target mesured by sensor in cm |
haarkon | 0:17de10d278c2 | 82 | * |
haarkon | 3:dcd04d9d0eb7 | 83 | * @return Distance in cm |
haarkon | 4:4f443a6a6843 | 84 | * @note Distance is (very) approximative, to have a better mesurment I recommand an averaging of at least 5 measures |
haarkon | 4:4f443a6a6843 | 85 | * @note you may get a new mesurment every 50ms |
haarkon | 0:17de10d278c2 | 86 | */ |
haarkon | 2:5e591a5b8edd | 87 | double getDistance (void); |
haarkon | 0:17de10d278c2 | 88 | |
haarkon | 0:17de10d278c2 | 89 | /** |
haarkon | 3:dcd04d9d0eb7 | 90 | * Return the current voltage on GP2A output |
haarkon | 0:17de10d278c2 | 91 | * |
haarkon | 3:dcd04d9d0eb7 | 92 | * @return Voltage between 0 and 3.3V |
haarkon | 0:17de10d278c2 | 93 | */ |
haarkon | 2:5e591a5b8edd | 94 | double getVoltage (void); |
haarkon | 2:5e591a5b8edd | 95 | |
haarkon | 2:5e591a5b8edd | 96 | /** |
haarkon | 2:5e591a5b8edd | 97 | * A short hand of getDistance |
haarkon | 2:5e591a5b8edd | 98 | */ |
haarkon | 2:5e591a5b8edd | 99 | operator double(); |
haarkon | 2:5e591a5b8edd | 100 | |
haarkon | 2:5e591a5b8edd | 101 | |
haarkon | 0:17de10d278c2 | 102 | |
haarkon | 0:17de10d278c2 | 103 | private : |
haarkon | 0:17de10d278c2 | 104 | float m_dMin, m_dMax, m_slope; |
haarkon | 0:17de10d278c2 | 105 | |
haarkon | 0:17de10d278c2 | 106 | protected : |
haarkon | 0:17de10d278c2 | 107 | |
haarkon | 0:17de10d278c2 | 108 | AnalogIn _sensor ; |
haarkon | 0:17de10d278c2 | 109 | |
haarkon | 0:17de10d278c2 | 110 | }; |
haarkon | 0:17de10d278c2 | 111 | #endif //GP2A_H |