SharpDistSensor.h 4.7 KB

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