Advertisement
Guest User

dist

a guest
Jan 22nd, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. #include "Distance.h"
  2. #include <Arduino.h>
  3. #include <avr/io.h>
  4. #include <math.h>
  5.  
  6. uint16_t ADC_mV_Faktor;
  7. int i=0;
  8. uint16_t dist_old;
  9. static uint16_t d[3];
  10.  
  11. /**
  12. * @brief Configures the ADC related to mode, clock speed, gain
  13. * resolution and refrence voltage
  14. *
  15. *
  16. * chose the reference voltage carfully related to the output
  17. * range of the sensor.
  18. */
  19. void configADC()
  20. {
  21. //MUX
  22. //ADCSR
  23. //REFS0 auf 1 setzen, damit wird Voltage Reference gesetzt
  24. //AVCC with external capacitor on AREF pin
  25. ADMUX |= (1<<REFS0);
  26. //ADCSRA-Register
  27. ADCSRA = 0;
  28. //ADEN auf 1 setzen (ADC aktivieren)
  29. //ADSC auf 1 setzen (ADC Conversion starten)
  30. //ADPS0 auf 1 setzen (Prescaler setzen) - Division Faktor = 128
  31. ADCSRA |= (1<<ADEN) | (1<<ADSC) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0);
  32. }
  33.  
  34. /**
  35. * @brief Starts a single conversion and receives a result return in mV
  36. */
  37. int16_t readADC(uint8_t channel)
  38. {
  39. ADMUX = channel;
  40. configADC();
  41.  
  42. while ((ADCSRA & (1<<ADIF)) == 0){
  43. }
  44. int16_t mV=0;
  45.  
  46. mV = ADC;
  47. mV = mV*(3300/1023);
  48. return mV;
  49.  
  50. }
  51.  
  52. bool checkDistance()
  53. {
  54. uint16_t valueSR=linearizeSR(readADC(1));
  55. uint16_t valueLR=linearizeLR(readADC(0));
  56. //uint16_t d [3];
  57. uint16_t dist;
  58. i=i%3;
  59. if ((valueSR < 200)&&(valueSR!=0)){
  60. d[i] = valueSR;
  61. }
  62. else {
  63. d[i] = valueLR;
  64. }
  65. i++;
  66. // if (i==3){
  67. dist=max(min(d[0],d[1]),min(max(d[0],d[1]),d[2]));
  68. if (dist<150){
  69. return false;
  70. }
  71. else{
  72. return true;
  73. }
  74.  
  75. }
  76. /**
  77. * @brief Maps the digital voltage information on a distance
  78. * in mm
  79. */
  80. uint16_t linearizeSR(uint16_t distmV)
  81. {
  82. //short range
  83. //4 bis 30 cm
  84. //Look-up Tabelle: mapping Voltage(mV), Distanz (mm)
  85. uint16_t lookup[17][2]={{2247,40},{2025,50},{1809,60},{1593,70},{1365,80},{1223,90},
  86. {1104,100},{1014,110},{921,120},{861,130},{804,140},{765,150},
  87. {615,180},{537,210},{462,240},{402,270},{327,300}
  88. };
  89. //der beste Wert muss gefunden werden
  90. int i=0;
  91. while(distmV < lookup[i][0]){
  92. i++;
  93. }
  94.  
  95. if (i==17){
  96. return 0;
  97. }
  98. if (i==0){
  99. return lookup[0][1];
  100. }
  101. else if ((lookup[i-1][0]-distmV)<=(distmV-lookup[i][0])){
  102. return lookup[i-1][1];
  103. }
  104. else{
  105. return lookup[i][1];
  106. }
  107. }
  108.  
  109. /**
  110. * @brief Maps the digital voltage information on a distance
  111. * in mm
  112. */
  113. uint16_t linearizeLR(uint16_t distmV)
  114. {
  115. //long range
  116. //20 bis 150 cm
  117. //Look-up Tabelle: mapping Voltage (mV), Distanz (mm)
  118. uint16_t lookup[13][2]={{2750,150},{2500,200},{2214,250},{2000,300},{1641,350},
  119. {1500,400},{1300,450},{1250,500},{1068,550},{1050,600},
  120. {882,650},{711,700},{682,800}
  121. };
  122. //der beste Wert muss gefunden werden
  123. int i=0;
  124. while(distmV < lookup[i][0]){
  125. i++;
  126. }
  127. if (i==13){
  128. return 0;
  129. }
  130. if (i==0){
  131. return lookup[0][1];
  132. }
  133. else if ((lookup[i-1][0]-distmV)<=(distmV-lookup[i][0])){
  134. return lookup[i-1][1];
  135. }
  136. else{
  137. return lookup[i][1];
  138. }
  139.  
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement