From 4ea16a69cdc07f7ab496940a41b65f0039c29b2b Mon Sep 17 00:00:00 2001 From: Christian Gosch Date: Sun, 13 Sep 2020 15:47:00 -0700 Subject: [PATCH 1/5] Adding Sharp GP2Y0A21F_5V_DS --- SharpDistSensor.cpp | 5 +++++ SharpDistSensor.h | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/SharpDistSensor.cpp b/SharpDistSensor.cpp index 35d168d..096b5d1 100755 --- a/SharpDistSensor.cpp +++ b/SharpDistSensor.cpp @@ -112,6 +112,11 @@ void SharpDistSensor::setModel(const models model) setPowerFitCoeffs(4.03576E+4, -1.26093, 70, 500); break; } + case GP2Y0A21F_5V_DS: + { + setPowerFitCoeffs(11944.04288048986, -1.137769099177962, 0, 1023); + break; + } } } diff --git a/SharpDistSensor.h b/SharpDistSensor.h index 41f9922..efc5636 100755 --- a/SharpDistSensor.h +++ b/SharpDistSensor.h @@ -74,7 +74,8 @@ class SharpDistSensor // Constant for GP2Y0A51SK0F 5V model GP2Y0A51SK0F_5V_DS, // Constant for GP2Y0A41SK0F 5V model - GP2Y0A41SK0F_5V_DS + GP2Y0A41SK0F_5V_DS, + GP2Y0A21F_5V_DS }; /** Constructor From e2e546074fa45ee144aa81ce0a92c8cf0b102f72 Mon Sep 17 00:00:00 2001 From: Christian Gosch Date: Tue, 15 Sep 2020 22:10:14 -0700 Subject: [PATCH 2/5] Updating README. This is using cm for the added sensor model. --- README.md | 48 +++++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index d43318a..65ceaaa 100755 --- a/README.md +++ b/README.md @@ -1,15 +1,15 @@ # SharpDistSensor -A library for the Arduino IDE that helps interface with Sharp analog distance sensors. +A library for the Arduino IDE that helps interface with Sharp analog distance sensors. -Version 1.5.0 -[![Build Status](https://travis-ci.org/DrGFreeman/SharpDistSensor.svg?branch=master)](https://travis-ci.org/DrGFreeman/SharpDistSensor) +Version 1.5.0 +[![Build Status](https://travis-ci.org/DrGFreeman/SharpDistSensor.svg?branch=master)](https://travis-ci.org/DrGFreeman/SharpDistSensor) By Julien de la Bruère-Terreault (drgfreeman@tuta.io) ## Summary The analog value from the sensor is converted to distance using either a polynomial fit function up to fifth order or a power fit function. -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. +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. The library also allows the use of pre-defined calibrations for different sensor models as well as custom defined polynomial or power fit functions. Examples are provided showing how to use pre-defined calibrations or how to define custom fit functions. @@ -18,43 +18,43 @@ The distance output is filtered using real-time median filtering (sliding window ## Examples Five example sketches are provided with the library: -* _SharpDistSensorBasic.ino_ +* _SharpDistSensorBasic.ino_ This example shows how to use the library with the default calibration function for the Sharp GP2Y0A60SZLF 5V sensor. -* _SharpDistSensorByModel.ino_ +* _SharpDistSensorByModel.ino_ This example shows how to use the library by selecting a pre-defined sensor model calibration. -* _SharpDistSensorCustomPoly.ino_ +* _SharpDistSensorCustomPoly.ino_ This example shows how to use the library with a custom, user defined polynomial calibration function. -* _SharpDistSensorCustomPower.ino_ +* _SharpDistSensorCustomPower.ino_ This example shows how to use the library with a custom, user defined power calibration function. -* _SharpDistSensorArray.ino_ +* _SharpDistSensorArray.ino_ This example shows how to use the library with an array of multiple sensors. ## Library Reference -* `SharpDistSensor(const byte pin, const byte size = 1)` -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). +* `SharpDistSensor(const byte pin, const byte size = 1)` +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). -* `uint16_t getDist()` -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. +* `uint16_t getDist()` +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. * `void setModel(const byte model)` Sets the calibration based on pre-defined sensor model fit functions. Note that the model argument must be preceded by the SharpDistSensor namespace, i.e.: `setModel(SharpDistSensor::Model_Name)` -* `void setPolyFitCoeffs(const byte nbCoeffs, const float* coeffs, const uint16_t valMin, const uint16_t valMax)` -Sets the polynomial fit function coefficients _C0_ to _C5_ in the relation: -_Distance = C0 + C1 * A + C2 * A^2 + ... + C5 * A^5_ -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. +* `void setPolyFitCoeffs(const byte nbCoeffs, const float* coeffs, const uint16_t valMin, const uint16_t valMax)` +Sets the polynomial fit function coefficients _C0_ to _C5_ in the relation: +_Distance = C0 + C1 * A + C2 * A^2 + ... + C5 * A^5_ +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. -* `void setPowerFitCoeffs(const float C, const float P, const uint16_t valMin, const uint16_t valMax)` -Sets the power fit function coefficients _C_ and _P_ in the relation: -_Distance = C * A^P_ -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. +* `void setPowerFitCoeffs(const float C, const float P, const uint16_t valMin, const uint16_t valMax)` +Sets the power fit function coefficients _C_ and _P_ in the relation: +_Distance = C * A^P_ +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. -* `void setValMinMax(const uint16_t valMin, const uint16_t valMax)` +* `void setValMinMax(const uint16_t valMin, const uint16_t valMax)` 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. ## Pre-defined sensor models @@ -62,7 +62,7 @@ Sets the range of analog values for which the polynomial or power fit is valid ( * `GP2Y0A41SK0F_5V_DS`: GP2Y0A41SK0F Analog Distance Sensor 4-30cm, 5V (data sheet), polynomial fit. * `GP2Y0A51SK0F_5V_DS`: GP2Y0A51SK0F Analog Distance Sensor 2-15cm, 5V (data sheet), power fit. * `GP2Y0A710K0F_5V_DS`: GP2Y0A710K0F Analog Distance Sensor 100-500cm, 5V (data sheet), polynomial fit. - +* `GP2Y0A21F_5V_DS`: GP2Y0A21F Analog Distance Sensor 10-80cm, 5V (data sheet), power fit #### Polynomial / Power fit coefficients: Model | Units | C0/C | C1/P | C2 | C3 | C4 | C5 @@ -71,6 +71,7 @@ Model | Units | C0/C | C1/P | C2 | C3 | C4 | C5 **GP2Y0A41SK0F_5V_DS** | mm | 761.9 | -8.13336 | 4.18857E-2 | -1.11338E-4 | 1.46237E-7 | -7.49656E-11 **GP2Y0A51SK0F_5V_DS** | mm | 4.03576E+4 | -1.26093 | | | | **GP2Y0A710K0F_5V_DS** | mm | 178506 | -1607.72 | 5.5239 | -8.47601E-3 | 4.87819E-6 | +**GP2Y0A21F_5V_DS** | cm | 11944.04288048986 | -1.137769099177962 | | | | #### Analog values range: @@ -80,6 +81,7 @@ Model | valMin | valMax **GP2Y0A41SK0F_5V_DS** | 61 | 614 **GP2Y0A51SK0F_5V_DS** | 70 | 500 **GP2Y0A710K0F_5V_DS** | 284 | 507 +**GP2Y0A21F_5V_DS** | 0 | 1023 **Important Note:** The analog voltage returned by the sensor is largely dependent of the reflected object size and reflectivity. The distance returned by these pre-defined calibration functions can therefore vary significantly from the real distance depending on the object detected. Where accuracy is required by the application, it is recommended to perform calibration with the object to be detected and use custom calibration fit functions instead. From de8c99b28a306bc2bade2dbd9fe825b2f2ed0c04 Mon Sep 17 00:00:00 2001 From: Christian Gosch Date: Wed, 16 Sep 2020 22:17:55 -0700 Subject: [PATCH 3/5] GP2Y0A21F_5V_DS is in mm now. --- README.md | 2 +- SharpDistSensor.cpp | 2 +- keywords.txt | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 65ceaaa..2fddfc8 100755 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ Model | Units | C0/C | C1/P | C2 | C3 | C4 | C5 **GP2Y0A41SK0F_5V_DS** | mm | 761.9 | -8.13336 | 4.18857E-2 | -1.11338E-4 | 1.46237E-7 | -7.49656E-11 **GP2Y0A51SK0F_5V_DS** | mm | 4.03576E+4 | -1.26093 | | | | **GP2Y0A710K0F_5V_DS** | mm | 178506 | -1607.72 | 5.5239 | -8.47601E-3 | 4.87819E-6 | -**GP2Y0A21F_5V_DS** | cm | 11944.04288048986 | -1.137769099177962 | | | | +**GP2Y0A21F_5V_DS** | mm | 116483.399834958 | -1.132373887917551 | | | | #### Analog values range: diff --git a/SharpDistSensor.cpp b/SharpDistSensor.cpp index 096b5d1..c51252a 100755 --- a/SharpDistSensor.cpp +++ b/SharpDistSensor.cpp @@ -114,7 +114,7 @@ void SharpDistSensor::setModel(const models model) } case GP2Y0A21F_5V_DS: { - setPowerFitCoeffs(11944.04288048986, -1.137769099177962, 0, 1023); + setPowerFitCoeffs(116483.399834958, -1.132373887917551, 0, 1023); break; } } diff --git a/keywords.txt b/keywords.txt index a539129..5c9ea63 100755 --- a/keywords.txt +++ b/keywords.txt @@ -8,3 +8,4 @@ GP2Y0A60SZLF_5V LITERAL1 GP2Y0A41SK0F_5V_DS LITERAL1 GP2Y0A51SK0F_5V_DS LITERAL1 GP2Y0A710K0F_5V_DS LITERAL1 +GP2Y0A21F_5V_DS LITERAL1 From dfc46f76837712774f7b2d6d2c41677c08b2b879 Mon Sep 17 00:00:00 2001 From: DrGFreeman Date: Sat, 26 Sep 2020 11:37:07 -0400 Subject: [PATCH 4/5] Restore double tailing whitespaces Restore double trailing whitespaces (markdown line breaks) to maintain original formatting. --- README.md | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 2fddfc8..8766b45 100755 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # SharpDistSensor A library for the Arduino IDE that helps interface with Sharp analog distance sensors. -Version 1.5.0 -[![Build Status](https://travis-ci.org/DrGFreeman/SharpDistSensor.svg?branch=master)](https://travis-ci.org/DrGFreeman/SharpDistSensor) +Version 1.5.0 +[![Build Status](https://travis-ci.org/DrGFreeman/SharpDistSensor.svg?branch=master)](https://travis-ci.org/DrGFreeman/SharpDistSensor) By Julien de la Bruère-Terreault (drgfreeman@tuta.io) ## Summary @@ -18,43 +18,43 @@ The distance output is filtered using real-time median filtering (sliding window ## Examples Five example sketches are provided with the library: -* _SharpDistSensorBasic.ino_ +* _SharpDistSensorBasic.ino_ This example shows how to use the library with the default calibration function for the Sharp GP2Y0A60SZLF 5V sensor. -* _SharpDistSensorByModel.ino_ +* _SharpDistSensorByModel.ino_ This example shows how to use the library by selecting a pre-defined sensor model calibration. -* _SharpDistSensorCustomPoly.ino_ +* _SharpDistSensorCustomPoly.ino_ This example shows how to use the library with a custom, user defined polynomial calibration function. -* _SharpDistSensorCustomPower.ino_ +* _SharpDistSensorCustomPower.ino_ This example shows how to use the library with a custom, user defined power calibration function. -* _SharpDistSensorArray.ino_ +* _SharpDistSensorArray.ino_ This example shows how to use the library with an array of multiple sensors. ## Library Reference -* `SharpDistSensor(const byte pin, const byte size = 1)` +* `SharpDistSensor(const byte pin, const byte size = 1)` 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). -* `uint16_t getDist()` +* `uint16_t getDist()` 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. -* `void setModel(const byte model)` +* `void setModel(const byte model)` Sets the calibration based on pre-defined sensor model fit functions. Note that the model argument must be preceded by the SharpDistSensor namespace, i.e.: `setModel(SharpDistSensor::Model_Name)` * `void setPolyFitCoeffs(const byte nbCoeffs, const float* coeffs, const uint16_t valMin, const uint16_t valMax)` -Sets the polynomial fit function coefficients _C0_ to _C5_ in the relation: -_Distance = C0 + C1 * A + C2 * A^2 + ... + C5 * A^5_ +Sets the polynomial fit function coefficients _C0_ to _C5_ in the relation: +_Distance = C0 + C1 * A + C2 * A^2 + ... + C5 * A^5_ 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. -* `void setPowerFitCoeffs(const float C, const float P, const uint16_t valMin, const uint16_t valMax)` -Sets the power fit function coefficients _C_ and _P_ in the relation: -_Distance = C * A^P_ +* `void setPowerFitCoeffs(const float C, const float P, const uint16_t valMin, const uint16_t valMax)` +Sets the power fit function coefficients _C_ and _P_ in the relation: +_Distance = C * A^P_ 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. -* `void setValMinMax(const uint16_t valMin, const uint16_t valMax)` +* `void setValMinMax(const uint16_t valMin, const uint16_t valMax)` 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. ## Pre-defined sensor models From cc4d25fbcd209496dc6ee6378af4045a9e84e7a9 Mon Sep 17 00:00:00 2001 From: DrGFreeman Date: Sat, 26 Sep 2020 11:38:52 -0400 Subject: [PATCH 5/5] Change spaces to tab Tab separator is required by Arduino IDE. --- keywords.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/keywords.txt b/keywords.txt index 5c9ea63..69cc621 100755 --- a/keywords.txt +++ b/keywords.txt @@ -8,4 +8,4 @@ GP2Y0A60SZLF_5V LITERAL1 GP2Y0A41SK0F_5V_DS LITERAL1 GP2Y0A51SK0F_5V_DS LITERAL1 GP2Y0A710K0F_5V_DS LITERAL1 -GP2Y0A21F_5V_DS LITERAL1 +GP2Y0A21F_5V_DS LITERAL1