|
1 |
| -/* |
2 |
| -SharpDistSensor.h |
3 |
| -Source: https://github.com/DrGFreeman/SharpDistSensor |
4 |
| -
|
5 |
| -MIT License |
6 |
| -
|
7 |
| -Copyright (c) 2017 Julien de la Bruere-Terreault <drgfreeman@tuta.io> |
8 |
| -
|
9 |
| -Permission is hereby granted, free of charge, to any person obtaining a copy |
10 |
| -of this software and associated documentation files (the "Software"), to deal |
11 |
| -in the Software without restriction, including without limitation the rights |
12 |
| -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13 |
| -copies of the Software, and to permit persons to whom the Software is |
14 |
| -furnished to do so, subject to the following conditions: |
15 |
| -
|
16 |
| -The above copyright notice and this permission notice shall be included in all |
17 |
| -copies or substantial portions of the Software. |
18 |
| -
|
19 |
| -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 |
| -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 |
| -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22 |
| -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 |
| -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 |
| -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
25 |
| -SOFTWARE. |
26 |
| -*/ |
27 |
| - |
28 |
| -/* |
29 |
| -SharpDistSensor; Sharp analog distance sensor library |
30 |
| -
|
31 |
| -This is a library for the Arduino IDE that helps interface with Sharp IR analog |
32 |
| -distance sensors. |
33 |
| -
|
34 |
| -The analog value from the sensor is converted to distance using either a fifth |
35 |
| -order polynomial fit function or a power fit function. |
36 |
| -
|
37 |
| -By default, this library is set to use polynomial coefficients calibrated for |
38 |
| -the Sharp GP2Y0A60SZLF Analog Distance Sensor 10-150cm 5V, over a range of |
39 |
| -50-1500 mm (analog values 30-875). The returned distance is in millimeters (mm) |
40 |
| -units. For different accuracy, range, sensor model or units, different |
41 |
| -coefficients may be required. |
42 |
| -
|
43 |
| -Use the setModel method to change the sensor model calibration. The following |
44 |
| -models are currently supported: |
45 |
| --GP2Y0A60SZLF_5V (GP2Y0A60SZLF Analog Distance Sensor 10-150cm, 5V) |
46 |
| --GP2Y0A710K0F_5V_DS (GP2Y0A710K0F Analog Distance Sensor 100-500cm, 5V) |
47 |
| -
|
48 |
| -Use the setPolyFitCoeffs method to define custom polynomial coefficients. |
49 |
| -
|
50 |
| -Use the setPowerFitCoeffs method to define custom power coefficients. |
51 |
| -
|
52 |
| -Use the setValMinMax method to define different analog values range. |
53 |
| -
|
54 |
| -The distance output is filtered using median filtering. MedianFilter class from |
55 |
| -the following library is used: https://github.com/daPhoosa/MedianFilter. |
56 |
| -*/ |
57 |
| - |
58 |
| -#ifndef SharpDistSensor_h |
59 |
| -#define SharpDistSensor_h |
60 |
| - |
61 |
| -#include <Arduino.h> |
62 |
| -#include <MedianFilter.h> |
63 |
| - |
64 |
| -class SharpDistSensor |
65 |
| -{ |
66 |
| -public: |
67 |
| - // List of pre-defined sensor models |
68 |
| - enum models |
69 |
| - { |
70 |
| - // Constant for GP2Y0A60SZLF 5V model |
71 |
| - GP2Y0A60SZLF_5V, |
72 |
| - // Constant for GP2Y0A710K0F 5V model |
73 |
| - GP2Y0A710K0F_5V_DS |
74 |
| - }; |
75 |
| - |
76 |
| - /** Constructor |
77 |
| - pin: Arduino analog pin the sensor is connected to |
78 |
| - size: Window size of the median filter (1 = no filtering) |
79 |
| - **/ |
80 |
| - SharpDistSensor(const byte pin, const byte size = 1); |
81 |
| - |
82 |
| - // Return the measured distance |
83 |
| - uint16_t getDist(); |
84 |
| - |
85 |
| - // Set the sensor model |
86 |
| - void setModel(const models model); |
87 |
| - |
88 |
| - /* Set the polynomial fit function coefficients and range |
89 |
| - nbCoeffs: Number of coefficients (1 min, 6 max) |
90 |
| - coeffs: Coefficients (x^0 to x^5) |
91 |
| - valMin: Minimal analog value for which to return a distance |
92 |
| - valMax: Maximal analog value for which to return a distance |
93 |
| - */ |
94 |
| - void setPolyFitCoeffs(const byte nbCoeffs, const float* coeffs, |
95 |
| - const uint16_t valMin, const uint16_t valMax); |
96 |
| - |
97 |
| - /* Set the power fit function coefficients and range |
98 |
| - C and P: Coefficients in Distance = C*x^P relation |
99 |
| - valMin: Minimal analog value for which to return a distance |
100 |
| - valMax: Maximal analog value for which to return a distance |
101 |
| - */ |
102 |
| - void setPowerFitCoeffs(const float C, const float P, |
103 |
| - const uint16_t valMin, const uint16_t valMax); |
104 |
| - |
105 |
| - // Set the analog value range for which to return a distance |
106 |
| - void setValMinMax(const uint16_t valMin, const uint16_t valMax); |
107 |
| - |
108 |
| -private: |
109 |
| - // Arduino analog pin the sensor is connected to |
110 |
| - byte _pin; |
111 |
| - |
112 |
| - // Window size of the median filter (1 = no filtering) |
113 |
| - byte _mfSize; |
114 |
| - |
115 |
| - // Minimal analog value for which to return a distance |
116 |
| - uint16_t _valMin; |
117 |
| - |
118 |
| - // Maximal analog value for which to return a distance |
119 |
| - uint16_t _valMax; |
120 |
| - |
121 |
| - // Polynomial function coefficients to convert analog signal to distance |
122 |
| - float _polyCoeffs[6]; |
123 |
| - |
124 |
| - // Power function coefficients to convert analog signal to distance |
125 |
| - float _powerCoeffC; |
126 |
| - float _powerCoeffP; |
127 |
| - |
128 |
| - // Possible types of fit functions |
129 |
| - enum fitTypes |
130 |
| - { |
131 |
| - FIT_POLY, |
132 |
| - FIT_POWER |
133 |
| - }; |
134 |
| - |
135 |
| - // Fit function type used |
136 |
| - fitTypes _fitType; |
137 |
| - |
138 |
| - // Median filter object instance |
139 |
| - MedianFilter medFilt; |
140 |
| -}; |
141 |
| - |
142 |
| -#endif |
| 1 | +/* |
| 2 | +SharpDistSensor.h |
| 3 | +Source: https://github.com/DrGFreeman/SharpDistSensor |
| 4 | +
|
| 5 | +MIT License |
| 6 | +
|
| 7 | +Copyright (c) 2018 Julien de la Bruere-Terreault <drgfreeman@tuta.io> |
| 8 | +
|
| 9 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +of this software and associated documentation files (the "Software"), to deal |
| 11 | +in the Software without restriction, including without limitation the rights |
| 12 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +copies of the Software, and to permit persons to whom the Software is |
| 14 | +furnished to do so, subject to the following conditions: |
| 15 | +
|
| 16 | +The above copyright notice and this permission notice shall be included in all |
| 17 | +copies or substantial portions of the Software. |
| 18 | +
|
| 19 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | +SOFTWARE. |
| 26 | +*/ |
| 27 | + |
| 28 | +/* |
| 29 | +SharpDistSensor; Sharp analog distance sensor library |
| 30 | +
|
| 31 | +This is a library for the Arduino IDE that helps interface with Sharp IR analog |
| 32 | +distance sensors. |
| 33 | +
|
| 34 | +The analog value from the sensor is converted to distance using either a fifth |
| 35 | +order polynomial fit function or a power fit function. |
| 36 | +
|
| 37 | +By default, this library is set to use polynomial coefficients calibrated for |
| 38 | +the Sharp GP2Y0A60SZLF Analog Distance Sensor 10-150cm 5V, over a range of |
| 39 | +50-1500 mm (analog values 30-875). The returned distance is in millimeters (mm) |
| 40 | +units. For different accuracy, range, sensor model or units, different |
| 41 | +coefficients may be required. |
| 42 | +
|
| 43 | +Use the setModel method to change the sensor model calibration. The following |
| 44 | +models are currently supported: |
| 45 | +-GP2Y0A60SZLF_5V (GP2Y0A60SZLF Analog Distance Sensor 10-150cm, 5V) |
| 46 | +-GP2Y0A710K0F_5V_DS (GP2Y0A710K0F Analog Distance Sensor 100-500cm, 5V) |
| 47 | +
|
| 48 | +Use the setPolyFitCoeffs method to define custom polynomial coefficients. |
| 49 | +
|
| 50 | +Use the setPowerFitCoeffs method to define custom power coefficients. |
| 51 | +
|
| 52 | +Use the setValMinMax method to define different analog values range. |
| 53 | +
|
| 54 | +The distance output is filtered using median filtering. MedianFilter class from |
| 55 | +the following library is used: https://github.com/daPhoosa/MedianFilter. |
| 56 | +*/ |
| 57 | + |
| 58 | +#ifndef SharpDistSensor_h |
| 59 | +#define SharpDistSensor_h |
| 60 | + |
| 61 | +#include <Arduino.h> |
| 62 | +#include <MedianFilter.h> |
| 63 | + |
| 64 | +class SharpDistSensor |
| 65 | +{ |
| 66 | +public: |
| 67 | + // List of pre-defined sensor models |
| 68 | + enum models |
| 69 | + { |
| 70 | + // Constant for GP2Y0A60SZLF 5V model |
| 71 | + GP2Y0A60SZLF_5V, |
| 72 | + // Constant for GP2Y0A710K0F 5V model |
| 73 | + GP2Y0A710K0F_5V_DS, |
| 74 | + // Constant for GP2Y0A51SK0F 5V model |
| 75 | + GP2Y0A51SK0F_5V_DS, |
| 76 | + }; |
| 77 | + |
| 78 | + /** Constructor |
| 79 | + pin: Arduino analog pin the sensor is connected to |
| 80 | + size: Window size of the median filter (1 = no filtering) |
| 81 | + **/ |
| 82 | + SharpDistSensor(const byte pin, const byte size = 1); |
| 83 | + |
| 84 | + // Return the measured distance |
| 85 | + uint16_t getDist(); |
| 86 | + |
| 87 | + // Set the sensor model |
| 88 | + void setModel(const models model); |
| 89 | + |
| 90 | + /* Set the polynomial fit function coefficients and range |
| 91 | + nbCoeffs: Number of coefficients (1 min, 6 max) |
| 92 | + coeffs: Coefficients (x^0 to x^5) |
| 93 | + valMin: Minimal analog value for which to return a distance |
| 94 | + valMax: Maximal analog value for which to return a distance |
| 95 | + */ |
| 96 | + void setPolyFitCoeffs(const byte nbCoeffs, const float* coeffs, |
| 97 | + const uint16_t valMin, const uint16_t valMax); |
| 98 | + |
| 99 | + /* Set the power fit function coefficients and range |
| 100 | + C and P: Coefficients in Distance = C*x^P relation |
| 101 | + valMin: Minimal analog value for which to return a distance |
| 102 | + valMax: Maximal analog value for which to return a distance |
| 103 | + */ |
| 104 | + void setPowerFitCoeffs(const float C, const float P, |
| 105 | + const uint16_t valMin, const uint16_t valMax); |
| 106 | + |
| 107 | + // Set the analog value range for which to return a distance |
| 108 | + void setValMinMax(const uint16_t valMin, const uint16_t valMax); |
| 109 | + |
| 110 | +private: |
| 111 | + // Arduino analog pin the sensor is connected to |
| 112 | + byte _pin; |
| 113 | + |
| 114 | + // Window size of the median filter (1 = no filtering) |
| 115 | + byte _mfSize; |
| 116 | + |
| 117 | + // Minimal analog value for which to return a distance |
| 118 | + uint16_t _valMin; |
| 119 | + |
| 120 | + // Maximal analog value for which to return a distance |
| 121 | + uint16_t _valMax; |
| 122 | + |
| 123 | + // Polynomial function coefficients to convert analog signal to distance |
| 124 | + float _polyCoeffs[6]; |
| 125 | + |
| 126 | + // Power function coefficients to convert analog signal to distance |
| 127 | + float _powerCoeffC; |
| 128 | + float _powerCoeffP; |
| 129 | + |
| 130 | + // Possible types of fit functions |
| 131 | + enum fitTypes |
| 132 | + { |
| 133 | + FIT_POLY, |
| 134 | + FIT_POWER |
| 135 | + }; |
| 136 | + |
| 137 | + // Fit function type used |
| 138 | + fitTypes _fitType; |
| 139 | + |
| 140 | + // Median filter object instance |
| 141 | + MedianFilter medFilt; |
| 142 | +}; |
| 143 | + |
| 144 | +#endif |
0 commit comments