From 98c61aad5023a06ad09bf8a28cb58aea9cd8e7c9 Mon Sep 17 00:00:00 2001 From: Felix Hauser <6338374+FelixHauser@users.noreply.github.com> Date: Tue, 20 Aug 2019 22:02:51 +0200 Subject: [PATCH] Added support for more sensors. --- README.md | 28 +++++++++++-------- SharpAverageIR/SharpAverageIR.cpp | 19 +++++++++---- SharpAverageIR/SharpAverageIR.h | 8 ++---- .../examples/SharpSensorCm/SharpSensorCm.ino | 2 ++ 4 files changed, 35 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 148fc5d..d200fdf 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ See the readme.md file of the master branch (v1.0) for a little bit of backgroun - The distance is given as a float instead of an integer. - Much better way to sort out out-of-tolerance values. - Gives better accuracy. +- GP2Y0A02Y and GP2Y0A710K0F sensors are also supported. # How it works @@ -28,27 +29,30 @@ The distance is calculated from a formula extracted from the graphs on the senso Since this library takes some measurements before giving a final result, it is not as responsive as direct measurements. -This library has the formulas to work with the GP2Y0A21Y and the GP2Y0A02YK sensors but expanding it for -other sensors is easy enough. +This library has the formulas to work with the GP2Y0A21Y, GP2Y0A02YK, GP2Y0A02Y and GP2Y0A710K0F sensors +but expanding it for other sensors with similar behaviour should be easy enough. # Setting it up -I won't be explaining how to copy the library into your Arduino IDE, it's well documented... +I won't be explaining how to copy the library into your Arduino IDE, it's well documented. But you +have to do it. #### 1. Import Library: - `` + #### 2. Initialize an object: - `SharpAverageIR sharp(irPin, avg, tolerance, model);` //I called the object sharp in this example + `SharpAverageIR sharp(irPin, avg, tolerance, model); //I called the object sharp in this example` - **irPin** is the Analog Pin in which the sensor is attached. - **avg** is an Int representing the number of measurements it will take. - **tolerance** is the requested tolerance among values. It is an Int between 1 and 100. Experiment with it. - **model** it is an Int which represent the sensor in use. - use **0 or 1080** on the constructor for the GP2Y0A21Y (from 10 to 80cm) - use **1 or 20150** on the constructor for the GP2Y0A02Y (from 20 to 150cm) +- **irPin** is the Analog Pin in which the sensor is attached. +- **avg** is an Int representing the number of measurements it will take. +- **tolerance** is the requested tolerance among values. It is an Int between 1 and 100. Experiment with it. +- **model** it is an Int which represent the sensor in use. +- use **0 or 1080** on the constructor for the GP2Y0A21Y (from 10 to 80cm) +- use **1 or 20150** on the constructor for the GP2Y0A02Y (from 20 to 150cm) +- use **2 or 0430** on the constructor for the GP2Y0A41SK0F (from 04 to 40cm) +- use **3 or 10550** on the constructor for the GP2Y0A710K0F (from 100 to 550cm) #### 3. Call the public method: - `float dis=sharp.distance();` //this method returns a float with the distance. + `float dis=sharp.distance(); //this method returns a float with the distance.` Please see the example provided on this Library. diff --git a/SharpAverageIR/SharpAverageIR.cpp b/SharpAverageIR/SharpAverageIR.cpp index 0841be8..b18c0d6 100644 --- a/SharpAverageIR/SharpAverageIR.cpp +++ b/SharpAverageIR/SharpAverageIR.cpp @@ -64,6 +64,20 @@ switch (caseVar) { case 20150: //GP2Y0A02Y (from 20 to 150cm) //left for legacy reasons puntualDistance=(61.573*pow(floatRaw, -1.1068))*NONDECIMALMULTIPLIER; break; + case 2: //GP2Y0A02Y (from 04 to 80cm) + puntualDistance=(12.577*pow(floatRaw, -1.0512))*NONDECIMALMULTIPLIER; + break; + case 0430: //GP2Y0A02Y (from 04 to 80cm) //left for legacy reasons + puntualDistance=(12.577*pow(floatRaw, -1.0512))*NONDECIMALMULTIPLIER; + break; + case 3: //GP2Y0A710K0F (from 100 to 550cm) + puntualDistance=(1078.4*pow(floatRaw, -2.6965))*NONDECIMALMULTIPLIER; + break; + case 100550: //GP2Y0A710K0F (from 100 to 550cm) //left for legacy reasons + puntualDistance=(1078.4*pow(floatRaw, -2.6965))*NONDECIMALMULTIPLIER; + break; + + default: //nothing to show break; @@ -73,8 +87,6 @@ switch (caseVar) { - - float SharpAverageIR::distance() { //first, an array is populated and the average calculated @@ -90,10 +102,8 @@ float SharpAverageIR::distance() { summ=summ/_avg; - //we sort out the values too far from the average - unsigned long newAverage; int averageCounter=0; @@ -107,7 +117,6 @@ float SharpAverageIR::distance() { } - // we return the new average return (newAverage*1.0/(averageCounter*NONDECIMALMULTIPLIER)); diff --git a/SharpAverageIR/SharpAverageIR.h b/SharpAverageIR/SharpAverageIR.h index e618040..31f7c4d 100644 --- a/SharpAverageIR/SharpAverageIR.h +++ b/SharpAverageIR/SharpAverageIR.h @@ -45,13 +45,9 @@ Since this library takes some measurements before giving a final result, it is not as responsive as direct measurements. - This library has the formulas to work with the GP2Y0A21Y and the GP2Y0A02YK sensors but exanding it for + This library has the formulas to work with the GP2Y0A21Y, GP2Y0A02YK and the GP2Y0A41SK0F sensors but exanding it for other sensors is easy enough. - Model refenceces: - use 0 or 1080 on the constructor for the GP2Y0A21Y (from 10 to 80cm) - use 1 or 20150 on the constructor for the GP2Y0A02Y (from 20 to 150cm) - */ /* **************************** HOW TO USE ******************* @@ -66,6 +62,8 @@ When you initialize the library object on your sketch you have to pass all the a *** sensorModel is a int to differentiate the two sensor models this library currently supports: use 0 or 1080 on the constructor for the GP2Y0A21Y (from 10 to 80cm) use 1 or 20150 on the constructor for the GP2Y0A02Y (from 20 to 150cm) + use 2 or 0430 on the constructor for the GP2Y0A41SK0F (from 04 to 40cm) + use 3 or 10550 on the constructor for the GP2Y0A710K0F (from 100 to 550cm) */ diff --git a/SharpAverageIR/examples/SharpSensorCm/SharpSensorCm.ino b/SharpAverageIR/examples/SharpSensorCm/SharpSensorCm.ino index e5eac27..5d2a728 100644 --- a/SharpAverageIR/examples/SharpSensorCm/SharpSensorCm.ino +++ b/SharpAverageIR/examples/SharpSensorCm/SharpSensorCm.ino @@ -30,6 +30,8 @@ When you initialize the library object on your sketch you have to pass all the a *** sensorModel is a int to differentiate the two sensor models this library currently supports: use 0 or 1080 on the constructor for the GP2Y0A21Y (from 10 to 80cm) use 1 or 20150 on the constructor for the GP2Y0A02Y (from 20 to 150cm) + use 2 or 0430 on the constructor for the GP2Y0A41SK0F (from 04 to 40cm) + use 3 or 10550 on the constructor for the GP2Y0A710K0F (from 100 to 550cm) 3- Call the public method: