Skip to content

Commit 0c95472

Browse files
committed
Added support of power fit functions
1 parent 604ce88 commit 0c95472

7 files changed

Lines changed: 187 additions & 41 deletions

File tree

README.md

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,67 @@
11
# SharpDistSensor
22
A library for the Arduino IDE that helps interface with Sharp analog distance sensors.
33

4-
Version 1.0.0
4+
Version 1.1.0
55
[![Build Status](https://travis-ci.org/DrGFreeman/SharpDistSensor.svg?branch=master)](https://travis-ci.org/DrGFreeman/SharpDistSensor)
66
By Julien de la Bruère-Terreault (drgfreeman@tuta.io)
77

88
## 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.
1011

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.
1213

1314
For different accuracy, range, sensor model or units, different coefficients may be required.
1415

1516
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.
1617

1718
## Examples
18-
Three example sketches are provided with the library:
19+
Four example sketches are provided with the library:
1920
* _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.
2122

2223
* _SharpDistSensorByModel.ino_
2324
This example shows how to use the library by selecting a pre-defined sensor
2425
model calibration.
2526

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.
2832

2933
## Library Reference
3034
* `SharpDistSensor(const byte pin, const byte size = 1)`
3135
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).
3236

3337
* `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.
3539

3640
* `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.
3842

3943
* `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:
4145
_Distance = C0 + C1 * A + C2 * A^2 + ... + C5 * A^5_
4246
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.
4347

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+
4453
* `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.
4655

4756
## Pre-defined sensor models
4857
* `GP2Y0A60SZLF_5V` - GP2Y0A60SZLF Analog Distance Sensor 10-150cm, 5V
4958

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
5362

5463
## Version history
64+
* 1.1.0 (2017-04-05): Added support of power fit functions.
5565
* 1.0.0 (2017-03-29): Initial major release for Arduino Library Manager (no change rel. to 0.3.2)
5666
* 0.3.2 (2017-03-29): Modified models enum and related type in setModel method arguments.
5767
* 0.3.1 (2017-03-28): Improved README and fixed comments in SharpDistSensorByModel example.

SharpDistSensor.cpp

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,23 @@ uint16_t SharpDistSensor::getDist()
5353
// Constrain sensor values to remain within set min-max range
5454
sensVal = constrain(sensVal, _valMin, _valMax);
5555

56-
// Calculate distance from polynomial fit curve
5756
uint16_t dist = 0;
58-
dist += _coeffs[5] * pow(sensVal, 5);
59-
dist += _coeffs[4] * pow(sensVal, 4);
60-
dist += _coeffs[3] * pow(sensVal, 3);
61-
dist += _coeffs[2] * pow(sensVal, 2);
62-
dist += _coeffs[1] * sensVal;
63-
dist += _coeffs[0];
57+
58+
if (_fitType == FIT_POLY)
59+
{
60+
// Calculate distance from polynomial fit function
61+
dist += _polyCoeffs[5] * pow(sensVal, 5);
62+
dist += _polyCoeffs[4] * pow(sensVal, 4);
63+
dist += _polyCoeffs[3] * pow(sensVal, 3);
64+
dist += _polyCoeffs[2] * pow(sensVal, 2);
65+
dist += _polyCoeffs[1] * sensVal;
66+
dist += _polyCoeffs[0];
67+
}
68+
else if (_fitType == FIT_POWER)
69+
{
70+
// Calculate distance from power fit function
71+
dist = _powerCoeffC * pow(sensVal, _powerCoeffP);
72+
}
6473

6574
if (_mfSize > 1)
6675
{
@@ -86,29 +95,47 @@ void SharpDistSensor::setModel(const models model)
8695
}
8796
}
8897

89-
// Set the polynomial fit curve coefficients and range
98+
// Set the polynomial fit function coefficients and range
9099
void SharpDistSensor::setPolyFitCoeffs(const byte nbCoeffs,
91100
const float* coeffs, const uint16_t valMin, const uint16_t valMax)
92101
{
102+
// Set fit type to FIT_POLY
103+
_fitType = FIT_POLY;
104+
93105
// Set coefficients
94106
for (byte i = 0; i < 6; i++)
95107
{
96108
if (i < nbCoeffs)
97109
{
98110
// Coefficient is provided
99-
_coeffs[i] = coeffs[i];
111+
_polyCoeffs[i] = coeffs[i];
100112
}
101113
else
102114
{
103115
// Coefficient is not provided, set to zero
104-
_coeffs[i] = 0;
116+
_polyCoeffs[i] = 0;
105117
}
106118
}
107119

108120
// Set analog value range
109121
setValMinMax(valMin, valMax);
110122
}
111123

124+
// Set the power fit function coefficients and range
125+
void SharpDistSensor::setPowerFitCoeffs(const float C, const float P,
126+
const uint16_t valMin, const uint16_t valMax)
127+
{
128+
// Set fit type to FIT_POWER
129+
_fitType = FIT_POWER;
130+
131+
// Set power fit function coefficients
132+
_powerCoeffC = C;
133+
_powerCoeffP = P;
134+
135+
// Set analog value range
136+
setValMinMax(valMin, valMax);
137+
}
138+
112139
// Set the analog value range for which to return a distance
113140
void SharpDistSensor::setValMinMax(const uint16_t valMin, const uint16_t valMax)
114141
{

SharpDistSensor.h

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,22 @@ SharpDistSensor; Sharp analog distance sensor library
3131
This is a library for the Arduino IDE that helps interface with Sharp IR analog
3232
distance sensors.
3333
34-
The analog value from the sensor is converted to distance using a fifth order
35-
polynomial fit curve.
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.
3636
37-
The default polynomial coefficients in this lilbrary are calibrated for the
38-
Sharp GP2Y0A60SZLF Analog Distance Sensor 10-150cm, 5V over a range of 50-1500
39-
mm (analog values 30-875). Distance units are millimeters (mm). For different
40-
accuracy, range, sensor model or units, different coefficients may be required.
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.
4142
4243
Use the setModel method to change the sensor model calibration. The following
4344
models are currently supported:
4445
-GP2Y0A60SZLF_5V (GP2Y0A60SZLF Analog Distance Sensor 10-150cm, 5V)
4546
46-
Use the setPolyFitCoeffs method to define custom coefficients.
47+
Use the setPolyFitCoeffs method to define custom polynomial coefficients.
48+
49+
Use the setPowerFitCoeffs method to define custom power coefficients.
4750
4851
Use the setValMinMax method to define different analog values range.
4952
@@ -79,7 +82,7 @@ class SharpDistSensor
7982
// Set the sensor model
8083
void setModel(const models model);
8184

82-
/* Set the polynomial fit curve coefficients and range
85+
/* Set the polynomial fit function coefficients and range
8386
nbCoeffs: Number of coefficients (1 min, 6 max)
8487
coeffs: Coefficients (x^0 to x^5)
8588
valMin: Minimal analog value for which to return a distance
@@ -88,6 +91,14 @@ class SharpDistSensor
8891
void setPolyFitCoeffs(const byte nbCoeffs, const float* coeffs,
8992
const uint16_t valMin, const uint16_t valMax);
9093

94+
/* Set the power fit function coefficients and range
95+
C and P: Coefficients in Distance = C*x^P relation
96+
valMin: Minimal analog value for which to return a distance
97+
valMax: Maximal analog value for which to return a distance
98+
*/
99+
void setPowerFitCoeffs(const float C, const float P,
100+
const uint16_t valMin, const uint16_t valMax);
101+
91102
// Set the analog value range for which to return a distance
92103
void setValMinMax(const uint16_t valMin, const uint16_t valMax);
93104

@@ -104,8 +115,22 @@ class SharpDistSensor
104115
// Maximal analog value for which to return a distance
105116
uint16_t _valMax;
106117

107-
// Polynomial curve coefficients to convert analog signal to distance
108-
float _coeffs[6];
118+
// Polynomial function coefficients to convert analog signal to distance
119+
float _polyCoeffs[6];
120+
121+
// Power function coefficients to convert analog signal to distance
122+
float _powerCoeffC;
123+
float _powerCoeffP;
124+
125+
// Possible types of fit functions
126+
enum fitTypes
127+
{
128+
FIT_POLY,
129+
FIT_POWER
130+
};
131+
132+
// Fit function type used
133+
fitTypes _fitType;
109134

110135
// Median filter object instance
111136
MedianFilter medFilt;

examples/SharpDistSensorCustom/SharpDistSensorCustom.ino renamed to examples/SharpDistSensorCustomPoly/SharpDistSensorCustomPoly.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
SharpDistSensorCustom.ino
2+
SharpDistSensorCustomPoly.ino
33
Source: https://github.com/DrGFreeman/SharpDistSensor
44
55
MIT License
@@ -60,8 +60,8 @@ const byte nbCoefficients = 6; // Number of coefficients
6060
* These should represent a range of analog values within which the
6161
* polynomial fit curve is valid.
6262
*/
63-
const uint16_t minVal = 134; // ~800 mm
64-
const uint16_t maxVal = 875; // ~50mm
63+
const unsigned int minVal = 134; // ~800 mm
64+
const unsigned int maxVal = 875; // ~50mm
6565

6666
void setup() {
6767
Serial.begin(9600);
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
SharpDistSensorCustomPower.ino
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+
This example shows how to use the SharpDistSensor library to continuously
30+
read the sensor and display the analog value and the corrseponding distance
31+
using custom power fit curve and range.
32+
*/
33+
34+
#include <SharpDistSensor.h>
35+
36+
// Analog pin to which the sensor is connected
37+
const byte sensorPin = A0;
38+
39+
// Window size of the median filter (odd number, 1 = no filtering)
40+
const byte mediumFilterWindowSize = 5;
41+
42+
// Create an object instance of the SharpDistSensor class
43+
SharpDistSensor sensor(sensorPin, mediumFilterWindowSize);
44+
45+
/* Set the power fit curve coefficients and range
46+
* C and P: Coefficients in Distance = C*A^P relation
47+
* where A is the analog value read from the sensor.
48+
*/
49+
const float C = 90373.;
50+
const float P = -1.027;
51+
52+
/*
53+
* Minimum and maximum analog values for which to return a distance
54+
* These should represent a range of analog values within which the
55+
* power fit curve is valid.
56+
*/
57+
const unsigned int minVal = 134; // ~800 mm
58+
const unsigned int maxVal = 875; // ~50mm
59+
60+
void setup() {
61+
Serial.begin(9600);
62+
63+
// Set custom power fit curve coefficients and range
64+
sensor.setPowerFitCoeffs(C, P, minVal, maxVal);
65+
}
66+
67+
void loop() {
68+
/* Print raw analog value. This step is not required to read the sensor,
69+
it is included here to show the relation between the raw analog value
70+
and the returned distance. */
71+
Serial.print(analogRead(sensorPin));
72+
73+
Serial.print(", ");
74+
75+
// Get distance from sensor
76+
unsigned int distance = sensor.getDist();
77+
78+
// Print distance to Serial
79+
Serial.println(distance);
80+
81+
// Wait some time
82+
delay(50);
83+
}

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ SharpDistSensor KEYWORD1
22
getDist KEYWORD2
33
setModel KEYWORD2
44
setPolyFitCoeffs KEYWORD2
5+
setPowerFitCoeffs KEYWORD2
56
setValMinMax KEYWORD2
67
GP2Y0A60SZLF_5V LITERAL1
78
GP2Y0A60SZLF_3V LITERAL1

library.properties

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=SharpDistSensor
2-
version=1.0.0
3-
author=Julien de la Bruere-Terreault
4-
maintainer=Julien de la Bruere-Terreault <drgfreeman@tuta.io>
2+
version=1.1.0
3+
author=Julien de la Bruere-Terreault (drgfreeman@tuta.io)
4+
maintainer=Julien de la Bruere-Terreault (drgfreeman@tuta.io)
55
sentence=Sharp analog distance sensor library
6-
paragraph=This is a library for the Arduino IDE that helps interface with Sharp IR analog distance sensors.
6+
paragraph=This is a library for the Arduino IDE that helps interface with Sharp IR analog distance sensors. It supports polynomial and power fit functions as well as real-time median filtering (sliding window of ajustable size).
77
category=Sensors
88
url=https://github.com/DrGFreeman/SharpDistSensor
99
architectures=*

0 commit comments

Comments
 (0)