Advertisement
Rusab

Untitled

Jun 25th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.69 KB | None | 0 0
  1. //my line follower code basic
  2.  
  3. #define sensorNum 8
  4. #define maxSpeed 240
  5. int rotationSpeed=200;
  6.  
  7.  
  8.  
  9. int blackLimit[sensorNum];
  10. int digitalReading[sensorNum];
  11. const int motorPin1=6,motorPin2=5; //right motor
  12. const int motorPin3=9,motorPin4=10; //left motor
  13.  
  14. const int fSonarTrig = 8;
  15. const int fSonarEcho = 7;
  16.  
  17. float error, prevError=0;
  18. float mappedValue, targetValue = 7; //changed ferom 4.5 to 9
  19.  
  20. float safety=0.35; //fixed
  21.  
  22. float kp=40; //496
  23. float kd=3; //180
  24. float kt;
  25.  
  26. int motorResponse;
  27. float correction;
  28.  
  29. int leftSpeed,rightSpeed;
  30. int digitalValue;
  31. int allBlack=0;
  32. int CRotateKey=0,ACRotateKey=0;
  33.  
  34. float lastAct;
  35. int time=3;
  36. int danceDelay = 350;
  37.  
  38. int prev, curr, diff;
  39. int leftSide,rightSide;
  40. int leftIR,rightIR;
  41. int allBlackcount=0;
  42. int stopThreshold = 14;
  43. int fDistance;
  44. int obsKey;
  45.  
  46. void setup()
  47. {
  48. //initialize for IR pins
  49. for(int i = 0; i < sensorNum; i++)
  50. {
  51. pinMode(A0 + i, INPUT);
  52. }
  53.  
  54. //initialize motor pins
  55. pinMode(motorPin1, OUTPUT);
  56. pinMode(motorPin2, OUTPUT);
  57. pinMode(motorPin3, OUTPUT);
  58. pinMode(motorPin4, OUTPUT);
  59. delay(1000);
  60. calibration();
  61.  
  62. Serial.begin(9600);
  63. }
  64.  
  65.  
  66. void loop() {
  67.  
  68. //sensorRead();
  69. sensorMapping();
  70. Serial.println(mappedValue);
  71.  
  72. fDistance = trigger(fSonarTrig, fSonarEcho);
  73.  
  74. if(fDistance < 10)
  75.  
  76. {
  77. obsKey = 1;
  78. for(int i = 0; i < 3; i++)
  79. {
  80.  
  81. fDistance = trigger(fSonarTrig, fSonarEcho);
  82. if(fDistance > 10)
  83. {
  84. obsKey = 0;
  85. }
  86. }
  87. }
  88.  
  89.  
  90. while(obsKey == 1)
  91. {
  92. brake();
  93. fDistance = trigger(fSonarTrig, fSonarEcho);
  94. if(fDistance > 10)
  95. {
  96. obsKey = 0;
  97. }
  98. }
  99.  
  100. if(allBlack == 1) //depend upon track for acute angle
  101. {
  102. //rotate
  103. allBlackcount++; // ive to check it for my bot
  104. }
  105. else
  106. {
  107. allBlackcount = 0;
  108. }
  109.  
  110.  
  111. if(allBlackcount > stopThreshold)
  112. {
  113. brake();
  114. }
  115.  
  116. if(leftSide == 1)
  117. {
  118. plannedACRotate();
  119. delay(500);
  120. }
  121.  
  122. //ei tukun ei sudhu
  123.  
  124.  
  125.  
  126. if(mappedValue != 100)
  127. {
  128. pid();
  129. motor(leftSpeed, rightSpeed);
  130. }
  131.  
  132. else
  133. {
  134. if(leftIR == 0 && rightIR == 1)
  135. {
  136. plannedCRotate();
  137. while(digitalReading[3] != 1)
  138. {
  139. sensorMapping();
  140. }
  141. rightIR = 0;
  142. pid();
  143. motor(leftSpeed, rightSpeed);
  144. }
  145.  
  146. else if(leftIR == 1 && rightIR == 0)
  147. {
  148. plannedACRotate();
  149. while(digitalReading[3] != 1)
  150. {
  151. sensorMapping();
  152. }
  153. leftIR = 0;
  154. pid();
  155. motor(leftSpeed, rightSpeed);
  156. }
  157.  
  158. else if (leftIR == 0 && rightIR == 0)
  159. {
  160. motor(maxSpeed, maxSpeed);
  161. }
  162. }
  163.  
  164.  
  165.  
  166. }
  167.  
  168.  
  169.  
  170. void sensorMapping()
  171. {
  172. int sum=0,count=0;
  173. for(int i = 0; i < sensorNum; i++)
  174. {
  175. if(analogRead(i) < blackLimit[i])
  176. {
  177.  
  178. sum += i*2;
  179. count++;
  180.  
  181. digitalReading[i] = 1;
  182. }
  183.  
  184. else
  185. {
  186. digitalReading[i] = 0;
  187. }
  188.  
  189. }
  190.  
  191. if(count != 0)
  192. {
  193. mappedValue = sum/count;
  194. }
  195. else
  196. mappedValue = 100;
  197.  
  198. if(digitalReading[0] || digitalReading[sensorNum - 1])
  199. {
  200. leftIR = digitalReading[0];
  201. rightIR = digitalReading[sensorNum-1];
  202. }
  203.  
  204. if(count == sensorNum)
  205. {
  206. allBlack = 1;
  207. }
  208. else
  209. allBlack = 0;
  210.  
  211.  
  212. //loop case
  213. if(digitalReading[3] || digitalReading [4] && digitalReading[7]
  214. { rightSide= 1;
  215. }
  216.  
  217. //turn full right or left depends upon track
  218. /* if(digitalReading[0] || digitaReading[1] || digitalReading[2])
  219. {
  220. leftSide = 1;
  221. }
  222. else
  223. leftSide = 0;
  224. if(digitalReading[5] || digitaReading[6] || digitalReading[7])
  225. {
  226. rightSide = 1;
  227. }
  228. else
  229. rightSide = 0;
  230.  
  231. */
  232.  
  233. }
  234.  
  235.  
  236.  
  237.  
  238. void pid()
  239. {
  240.  
  241. error=targetValue-mappedValue;
  242. correction=(kp*error)+(kd*(error-prevError));
  243. prevError=error;
  244. motorResponse=(int)correction;
  245.  
  246. if(motorResponse>maxSpeed) motorResponse=maxSpeed;
  247.  
  248. if(motorResponse<-maxSpeed) motorResponse=-maxSpeed;
  249.  
  250. if(motorResponse>0)
  251. {
  252. rightSpeed=maxSpeed;
  253. leftSpeed=maxSpeed-motorResponse;
  254. }
  255. else
  256. {
  257. rightSpeed=maxSpeed+ motorResponse;
  258. leftSpeed=maxSpeed;
  259. }
  260.  
  261. }
  262.  
  263.  
  264.  
  265.  
  266. void motor(int left, int right)
  267. {
  268.  
  269. if(right>0)
  270. {
  271. analogWrite(motorPin1,right);
  272. analogWrite(motorPin2,0);
  273. }
  274. else
  275. {
  276. analogWrite(motorPin1,0);
  277. analogWrite(motorPin2,-right);
  278. }
  279.  
  280. if(left>0)
  281. {
  282. analogWrite(motorPin3,left);
  283. analogWrite(motorPin4,0);
  284. }
  285. else
  286. {
  287. analogWrite(motorPin3,0);
  288. analogWrite(motorPin4,-left);
  289. }
  290.  
  291. }
  292.  
  293.  
  294.  
  295. void brake(void)
  296. {
  297. analogWrite(motorPin1, 0);
  298. analogWrite(motorPin2, 0);
  299. analogWrite(motorPin3, 0);
  300. analogWrite(motorPin4, 0);
  301. }
  302.  
  303. void plannedACRotate()
  304. {
  305. analogWrite(motorPin1,rotationSpeed);
  306. analogWrite(motorPin2, 0);
  307. analogWrite(motorPin3, 0);
  308. analogWrite(motorPin4,rotationSpeed);
  309.  
  310. }
  311.  
  312. void plannedCRotate()
  313. {
  314. analogWrite(motorPin1,0);
  315. analogWrite(motorPin2, rotationSpeed);
  316. analogWrite(motorPin3, rotationSpeed);
  317. analogWrite(motorPin4,0);
  318.  
  319. }
  320.  
  321.  
  322.  
  323. /*void dance(void)
  324. {
  325.  
  326. int loopCounter = (int) (danceDelay / diff);
  327.  
  328. for(int i = loopCounter; i > 0; i--)
  329. {
  330.  
  331. plannedCRotate();
  332.  
  333. sensorMapping();
  334.  
  335. if(mappedValue != 100)
  336. {
  337. pid();
  338. motor(leftSpeed, rightSpeed);
  339. return;
  340. }
  341.  
  342. }
  343.  
  344. for(int i = 2 * loopCounter; i > 0; i--)
  345. {
  346. plannedACRotate();
  347.  
  348. sensorMapping();
  349.  
  350. if(mappedValue != 100)
  351. {
  352. pid();
  353. motor(leftSpeed, rightSpeed);
  354. return;
  355. }
  356.  
  357. }
  358.  
  359. } */
  360.  
  361.  
  362. //auto calibration
  363. void calibration()
  364. {
  365. plannedCRotate();
  366. float upSum = 0,lowSum = 0;
  367. int sensorArray[sensorNum][2];
  368.  
  369. for(int i = 0; i < sensorNum; i++)
  370. {
  371. sensorArray[i][0] = analogRead(A0+i);
  372. sensorArray[i][1] = analogRead(A0+i);
  373. }
  374.  
  375.  
  376. int loopCounter = (int)(time * 1000 / 2.5);
  377. while(loopCounter)
  378. {
  379. for(int i = 0; i < sensorNum; i++)
  380. {
  381. if(analogRead(A0+i)<sensorArray[i][0]) sensorArray[i][0]=analogRead(A0+i);
  382. if(analogRead(A0+i)>sensorArray[i][1]) sensorArray[i][1]=analogRead(A0+i);
  383. }
  384. loopCounter--;
  385.  
  386. }
  387.  
  388. for(int i=0; i < sensorNum; i++)
  389. blackLimit[i] = (int)(sensorArray[i][0] + safety * (sensorArray[i][1] - sensorArray[i][0]));
  390. prev = millis();
  391. sensorMapping ();
  392. curr= millis();
  393. diff = curr - prev;
  394.  
  395. brake();
  396. delay(1000);
  397.  
  398. }
  399.  
  400.  
  401.  
  402. //sonar functions
  403. long mstocm(long microseconds)
  404. {
  405.  
  406. return (microseconds*346.3)/2/10000;
  407. }
  408.  
  409.  
  410. int trigger(int trigPin,int echopin)
  411. {
  412. digitalWrite(trigPin, LOW);
  413.  
  414. digitalWrite(trigPin, HIGH);
  415.  
  416. digitalWrite(trigPin, LOW);
  417. int distance = mstocm(pulseIn(echopin, HIGH));
  418. return distance;
  419. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement