Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. #include <QTRSensors.h>
  2. #include <Servo.h>
  3.  
  4. const int pingPin = 1;
  5. const int echoPin = 0;
  6. int powerPinL= 12; //Servo (wheel)
  7. int powerPinR= 13; //Servo (wheel)
  8. bool obstacle = false;
  9. const uint8_t SensorCount = 8;
  10. uint16_t sensorValues[SensorCount];
  11.  
  12. const float speedsLeft[]= {92,93};
  13. const float speedsRight[]= {91,90};
  14.  
  15. Servo servL;
  16. Servo servR;
  17. QTRSensors qtr;
  18.  
  19.  
  20. void setup(){
  21. Serial.begin(9600);
  22. calibrate();
  23. servL.attach(powerPinL);
  24. servR.attach(powerPinR);
  25. pinMode(pingPin, OUTPUT);
  26. pinMode(echoPin, INPUT);
  27. }
  28.  
  29. float pos= -1;
  30.  
  31. int lastAction= -1;
  32. void loop(){
  33. uint16_t position = qtr.readLineBlack(sensorValues);
  34. pos= evalPos(sensorValues, pos);
  35. Serial.print("pos " + String(pos) + " ");
  36.  
  37.  
  38. //obstacle = detectObstacle();
  39.  
  40. if(obstacle == true)
  41. {
  42. Serial.println("Turning");
  43. avoidObstacle();
  44. return;
  45. }
  46.  
  47. if(pos > -1 and pos < 4.5){
  48. //Serial.println("left");
  49. doTurn("left",int(2*(4.5-pos)+0.5),3);
  50. lastAction= 0;
  51. return;
  52. }
  53. if(pos > -1 and pos > 4.5){
  54. //Serial.println("right");
  55. doTurn("right",int(2*(pos-4.5)+0.5),3);
  56. lastAction= 1;
  57. return;
  58. }
  59.  
  60. //Serial.println("straight");
  61. if(lastAction == 0){doTurn("right",int(2*(pos-4.5)+0.5),4); delay(50);}
  62. if(lastAction == 1){doTurn("left",int(2*(4.5-pos)+0.5),4); delay(50); }
  63. doStraight(8);
  64. lastAction = -1;
  65.  
  66.  
  67. delay(20);
  68. }
  69.  
  70.  
  71.  
  72.  
  73. void avoidObstacle(){
  74.  
  75. int val = 10;
  76. int baseVal = 4;
  77.  
  78. //turn left
  79. servR.write(30);
  80. servL.write(92+baseVal);
  81. delay(750);
  82.  
  83. //go straight
  84. servR.write(91-val);
  85. servL.write(92+val);
  86. delay(500);
  87.  
  88. //turn right
  89. servR.write(91-baseVal);
  90. servL.write(180);
  91. delay(1000);
  92.  
  93. //go straight
  94. servR.write(91-val);
  95. servL.write(92+val);
  96. delay(800);
  97.  
  98. //turn right
  99. servR.write(91-baseVal);
  100. servL.write(180);
  101. delay(1000);
  102.  
  103. //go straight
  104. servR.write(91-val);
  105. servL.write(92+val);
  106. delay(750);
  107.  
  108. //turn left
  109. servR.write(30);
  110. servL.write(92+baseVal);
  111. delay(750);
  112.  
  113. //go straight
  114. servR.write(91-val);
  115. servL.write(92+val);
  116. delay(100);
  117.  
  118. }
  119.  
  120. void doStraight(int val){
  121. servR.write(91-val);
  122. servL.write(92+val);
  123. }
  124.  
  125. //10 degrees
  126. void doTurn(String dir, int val, int baseVal){
  127. if(dir == "right"){
  128. servR.write(91-val-baseVal);
  129. servL.write(92+baseVal);
  130. }
  131.  
  132. if(dir == "left"){
  133. servR.write(91-baseVal);
  134. servL.write(92+val+baseVal);
  135. }
  136. }
  137.  
  138.  
  139. bool detectObstacle(){
  140. unsigned long threshold= 1500;
  141.  
  142. digitalWrite(pingPin, LOW);
  143. delayMicroseconds(2);
  144. digitalWrite(pingPin, HIGH);
  145. delayMicroseconds(5);
  146. digitalWrite(pingPin, LOW);
  147.  
  148. unsigned long duration = pulseIn(echoPin, HIGH);
  149. Serial.print(duration);
  150. Serial.println(" ms");
  151.  
  152. if(duration < threshold && duration > 0){ return true; }
  153. return false;
  154. }
  155.  
  156.  
  157.  
  158. float evalPos(int sensorValues[], float lastPos){
  159. int lastActiveSensor= -1;
  160. float numActive= 0;
  161. float activeSum= 0;
  162. int threshold= 400;
  163.  
  164. for (uint8_t i = 0; i < SensorCount; i++){
  165. Serial.print(sensorValues[i]);
  166. Serial.print('\t');
  167.  
  168.  
  169. if(sensorValues[i] > threshold){
  170. if(lastActiveSensor == -1 or lastActiveSensor == i-1){
  171. numActive++;
  172. lastActiveSensor= i;
  173. activeSum+= i+1;
  174.  
  175. threshold= sensorValues[i];
  176. }
  177. else{//Serial.println("Failed consec. check.");
  178. return -1;
  179. }
  180. }
  181. }
  182.  
  183. // if(numActive <= 0){ if(lastPos > 0){return lastPos;} else{return 4.5;} }
  184. return float(lastActiveSensor);
  185. float avgPos= -1;
  186. // if(numActive > 0){ avgPos= activeSum / numActive; }
  187. // else{
  188. // if (lastPos < 2){
  189. // avgPos= 0;
  190. // }
  191. // if (lastPos > 6)
  192. // avgPos= 8;
  193. // }
  194. if(avgPos > 3 and avgPos < 5){ avgPos=4.5; }
  195. Serial.println(String(avgPos) + " " + String(activeSum) + " " + String(numActive));
  196. //delay(50);
  197.  
  198. return avgPos;
  199. }
  200.  
  201.  
  202. void calibrate(){
  203. Serial.println("beginnning");
  204. qtr.setTypeRC();
  205. qtr.setSensorPins((const uint8_t[]){3,4, 5, 6, 7, 8, 9,10}, SensorCount);
  206. qtr.setEmitterPin(3);
  207. delay(500);
  208.  
  209. for (uint16_t i = 0; i < 160; i++){
  210. if(i%40 == 0){Serial.println(i);}
  211. qtr.calibrate();
  212. }
  213.  
  214. Serial.println("finish");
  215. delay(500);
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement