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