|
1 | 1 | # SharpDistSensor |
2 | 2 | A library for the Arduino IDE that helps interface with Sharp analog distance sensors. |
3 | 3 |
|
4 | | -Version 1.0.0 |
| 4 | +Version 1.1.0 |
5 | 5 | [](https://travis-ci.org/DrGFreeman/SharpDistSensor) |
6 | 6 | By Julien de la Bruère-Terreault (drgfreeman@tuta.io) |
7 | 7 |
|
8 | 8 | ## Summary |
9 | | -The analog value from the sensor is converted to distance using a polynomial fit curve up to fifth order. |
| 9 | +The analog value from the sensor is converted to distance using either a |
| 10 | +polynomial fit function up to fifth order or a power fit function. |
10 | 11 |
|
11 | | -The default polynomial coefficients in this library are calibrated for the Sharp GP2Y0A60SZLF Analog Distance Sensor 10-150cm 5V, over a range of 50-1500 mm (analog values 30-875). The returned distance is in millimeters (mm) units. |
| 12 | +By default, this library is set to use polynomial coefficients calibrated for the Sharp GP2Y0A60SZLF Analog Distance Sensor 10-150cm 5V, over a range of 50-1500 mm (analog values 30-875). The returned distance is in millimeters (mm) units. |
12 | 13 |
|
13 | 14 | For different accuracy, range, sensor model or units, different coefficients may be required. |
14 | 15 |
|
15 | 16 | The distance output is filtered using real-time median filtering (sliding window of ajustable size). The MedianFilter class from the following library is used: https://github.com/daPhoosa/MedianFilter. |
16 | 17 |
|
17 | 18 | ## Examples |
18 | | -Three example sketches are provided with the library: |
| 19 | +Four example sketches are provided with the library: |
19 | 20 | * _SharpDistSensorBasic.ino_ |
20 | | -This example shows how to use the library with the default calibration curve for the Sharp GP2Y0A60SZLF 5V sensor. |
| 21 | +This example shows how to use the library with the default calibration function for the Sharp GP2Y0A60SZLF 5V sensor. |
21 | 22 |
|
22 | 23 | * _SharpDistSensorByModel.ino_ |
23 | 24 | This example shows how to use the library by selecting a pre-defined sensor |
24 | 25 | model calibration. |
25 | 26 |
|
26 | | -* _SharpDistSensorCustom.ino_ |
27 | | -This example shows how to use the library with a custom, user defined calibration curve. |
| 27 | +* _SharpDistSensorCustomPoly.ino_ |
| 28 | +This example shows how to use the library with a custom, user defined polynomial calibration function. |
| 29 | + |
| 30 | +* _SharpDistSensorCustomPower.ino_ |
| 31 | +This example shows how to use the library with a custom, user defined power calibration function. |
28 | 32 |
|
29 | 33 | ## Library Reference |
30 | 34 | * `SharpDistSensor(const byte pin, const byte size = 1)` |
31 | 35 | Constructor: `pin` is the analog pin to which the sensor is connected, `size` is the size of the median filter window and should be an odd positive integer (default = 1 = no filtering). |
32 | 36 |
|
33 | 37 | * `uint16_t getDist()` |
34 | | -Returns the measured distance. Distance units are in millimeters (mm) if using the library default settings. If using custom calibration, units depend on the calibration used. |
| 38 | +Returns the measured distance. Distance units are in millimeters (mm) if using the library default settings or pre-defined sensor models. If using custom calibration, units depend on the calibration used. |
35 | 39 |
|
36 | 40 | * `void setModel(const byte model)` |
37 | | -Sets the calibration based on pre-defined sensor model polynomial fit curves. |
| 41 | +Sets the calibration based on pre-defined sensor model fit functions. |
38 | 42 |
|
39 | 43 | * `void setPolyFitCoeffs(const byte nbCoeffs, const float* coeffs, const uint16_t valMin, const uint16_t valMax)` |
40 | | -Sets the polynomial fit curve coefficients _C0_ to _C5_ in the relation: |
| 44 | +Sets the polynomial fit function coefficients _C0_ to _C5_ in the relation: |
41 | 45 | _Distance = C0 + C1 * A + C2 * A^2 + ... + C5 * A^5_ |
42 | 46 | where _A_ is the analog value read from the sensor. At least one coefficient must be provided and up to six maximum (5th order polynomial). `nbCoeffs` is the number of coefficients passed and `coeffs` a vector containing the coefficients (C0 to C5). `valMin` and `valMax` define the range of analog values for which the polynomial fit is valid. Analog values outside this range will be set to the respective min or max values. |
43 | 47 |
|
| 48 | +* `void setPowerFitCoeffs(const float C, const float P, const uint16_t valMin, const uint16_t valMax)` |
| 49 | +Sets the power fit function coefficients _C_ and _P_ in the relation: |
| 50 | +_Distance = C * A^P_ |
| 51 | +where _A_ is the analog value read from the sensor. `valMin` and `valMax` define the range of analog values for which the power fit is valid. Analog values outside this range will be set to the respective min or max values. |
| 52 | + |
44 | 53 | * `void setValMinMax(const uint16_t valMin, const uint16_t valMax)` |
45 | | -Sets the range of analog values for which the polynomial fit is valid (`valMin` and `valMax`). Analog values outside this range will be set to the respective min or max values. |
| 54 | +Sets the range of analog values for which the polynomial or power fit is valid (`valMin` and `valMax`). Analog values outside this range will be set to the respective min or max values. |
46 | 55 |
|
47 | 56 | ## Pre-defined sensor models |
48 | 57 | * `GP2Y0A60SZLF_5V` - GP2Y0A60SZLF Analog Distance Sensor 10-150cm, 5V |
49 | 58 |
|
50 | | -Model | C0 | C1 | C2 | C3 | C4 | C5 | valMin | valMax |
51 | | -------|----|----|----|----|----|----|--------|-------- |
52 | | -**GP2Y0A60SZLF_5V** | 1734 | -9.005 | 2.032E-2 | -2.251E-5 | 1.167E-8 | -2.037E-12 | 30 | 875 |
| 59 | +Model | Units | C0 | C1 | C2 | C3 | C4 | C5 | valMin | valMax |
| 60 | +------|-------|----|----|----|----|----|----|--------|-------- |
| 61 | +**GP2Y0A60SZLF_5V** | mm | 1734 | -9.005 | 2.032E-2 | -2.251E-5 | 1.167E-8 | -2.037E-12 | 30 | 875 |
53 | 62 |
|
54 | 63 | ## Version history |
| 64 | +* 1.1.0 (2017-04-05): Added support of power fit functions. |
55 | 65 | * 1.0.0 (2017-03-29): Initial major release for Arduino Library Manager (no change rel. to 0.3.2) |
56 | 66 | * 0.3.2 (2017-03-29): Modified models enum and related type in setModel method arguments. |
57 | 67 | * 0.3.1 (2017-03-28): Improved README and fixed comments in SharpDistSensorByModel example. |
|
0 commit comments