ZSharpIR.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. ZSharpIR
  3. Arduino library for retrieving distance (in mm) from the analog GP2Y0A21Y and GP2Y0A02YK,...
  4. Original comment from Dr. Marcal Casas-Cartagena :
  5. inspired from :
  6. - https://github.com/qub1750ul/Arduino_SharpIR.git
  7. - https://github.com/jeroendoggen/Arduino-GP2Y0A21YK-library.git
  8. - https://github.com/guillaume-rico/SharpIR.git
  9. - https://github.com/nikv96/MDP-Arduino.git
  10. - https://github.com/jeroendoggen/Arduino-GP2Y0A21YK-library.git
  11. */
  12. #include "Arduino.h"
  13. #include "WMath.h"
  14. #include "ZSharpIR.h"
  15. // Initialisation function
  16. // + irPin : is obviously the pin where the IR sensor is attached
  17. // + sensorModel is a int to differentiate the two sensor models this library currently supports:
  18. // 1080 is the int for the GP2Y0A21Y and
  19. // 20150 is the int for GP2Y0A02YK and
  20. // 100500 is the long for GP2Y0A710K0F
  21. // The numbers reflect the distance range they are designed for (in cm)
  22. ZSharpIR::ZSharpIR(int irPin, const uint32_t sensorModel) {
  23. _irPin=irPin;
  24. _model=sensorModel;
  25. // Define pin as Input
  26. pinMode (_irPin, INPUT);
  27. _Adcres=10;
  28. _refVoltage=5000;
  29. }
  30. // Sort an array
  31. void ZSharpIR::sort(int a[], int size) {
  32. for(int i=0; i<(size-1); i++) {
  33. bool flag = true;
  34. for(int o=0; o<(size-(i+1)); o++) {
  35. if(a[o] > a[o+1]) {
  36. int t = a[o];
  37. a[o] = a[o+1];
  38. a[o+1] = t;
  39. flag = false;
  40. }
  41. }
  42. if (flag) break;
  43. }
  44. }
  45. // Read distance and compute it
  46. int ZSharpIR::distance() {
  47. int ir_val[NB_SAMPLE];
  48. int distanceMM;
  49. float current;
  50. for (int i=0; i<NB_SAMPLE; i++){
  51. // Read analog value
  52. ir_val[i] = analogRead(_irPin);
  53. }
  54. // Sort it
  55. sort(ir_val,NB_SAMPLE);
  56. if (_model==1080)//GP2Y0A21YK0F
  57. {
  58. // Different expressions required as the Photon has 12 bit ADCs vs 10 bit for Arduinos
  59. distanceMM =(int)( 277.28 * pow(map(ir_val[NB_SAMPLE / 2], 0, (1<<_Adcres)-1, 0, _refVoltage)/1000.0, -1.2045));
  60. }
  61. else if (_model==GP2D12_24)//GP2D12_24
  62. {
  63. // Different expressions required as the Photon has 12 bit ADCs vs 10 bit for Arduinos
  64. distanceMM =(int)( 24.65251 /(map(ir_val[NB_SAMPLE / 2], 0, (1<<_Adcres)-1, 0, _refVoltage)/1000.0-0.1065759));
  65. }
  66. else if (_model==20150)//GP2Y0A02YK0F
  67. {
  68. // Previous formula used by Dr. Marcal Casas-Cartagena
  69. // puntualDistance=61.573*pow(voltFromRaw/1000, -1.1068);
  70. // Different expressions required as the Photon has 12 bit ADCs vs 10 bit for Arduinos
  71. distanceMM =(int)( 603.74 * pow(map(ir_val[NB_SAMPLE / 2], 0, (1<<_Adcres)-1, 0, _refVoltage)/1000.0, -1.16));
  72. } else if (_model==430)//GP2Y0A41SK0F
  73. {
  74. // Different expressions required as the Photon has 12 bit ADCs vs 10 bit for Arduinos
  75. distanceMM =(int)( 120.8 * pow(map(ir_val[NB_SAMPLE / 2], 0, (1<<_Adcres)-1, 0, _refVoltage)/1000.0, -1.058));
  76. } else if (_model==100500)//GP2Y0A710K0F
  77. {
  78. current = map(ir_val[NB_SAMPLE / 2], 0, (1<<_Adcres)-1, 0, _refVoltage);
  79. // use the inverse number of distance like in the datasheet (1/L)
  80. // y = mx + b = 137500*x + 1125
  81. // x = (y - 1125) / 137500
  82. // Different expressions required as the Photon has 12 bit ADCs vs 10 bit for Arduinos
  83. if (current < 1400 || current > 3300) {
  84. //false data
  85. distanceMM = 0;
  86. } else {
  87. distanceMM =(int)( 10.0 / (((current - 1125.0) / 1000.0) / 137.5));
  88. }
  89. }
  90. return distanceMM;
  91. }
  92. /// <summary>
  93. /// setARefVoltage:set the ADC reference voltage: (default value: 5000mV, set to 3300mV, typically 3.3 on Arduino boards)
  94. /// </summary>
  95. void ZSharpIR::setARefVoltage(int refV)
  96. {
  97. _refVoltage=refV;
  98. }
  99. /// <summary>
  100. /// SetAnalogReadResolution:set the ADC resolution : (default value: 10, set to 12, typically 10 on Arduino boards)
  101. /// </summary>
  102. void ZSharpIR::SetAnalogReadResolution(int res)
  103. {
  104. _Adcres=res;
  105. analogReadResolution( res);
  106. }