Advertisement
Guest User

Maze solving code with PID Control by irtesam

a guest
Oct 19th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.98 KB | None | 0 0
  1. #define PinL1 4
  2. #define PinL2 5
  3. #define PinR1 6
  4. #define PinR2 7
  5. #define ENA 3
  6. #define ENB 11
  7. #define ENASpeed 110
  8. #define ENBSpeed 110
  9. #define Sensor1 0
  10. #define Sensor2 1
  11. #define Sensor3 2
  12. #define Sensor4 3
  13. #define Sensor5 4
  14.  
  15.  
  16. //stuffs needed for maze solving
  17.  
  18. int sensorWeight[5]={-7,-4,0,4,7};
  19. int sensorData[5]={0,0,0,0,0};
  20. int pathLog[8]={0,0,0,0,0,0,0,0};
  21. int q=0;
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28. void pathLogCorrection()
  29. {
  30.  
  31. if(pathLog[q]==4)
  32. {
  33.  
  34. pathLog[q]=0;
  35. q--;
  36.  
  37. }
  38. else
  39. {
  40. q++;
  41. }
  42.  
  43. }
  44.  
  45. void goForward()
  46. {
  47.  
  48. analogWrite(ENA,ENASpeed);
  49. analogWrite(ENB,ENBSpeed);
  50.  
  51. digitalWrite(PinR1,HIGH);
  52. digitalWrite(PinR2,LOW);
  53. digitalWrite(PinL1,HIGH);
  54. digitalWrite(PinL2,LOW);
  55.  
  56. }
  57. void goRight()
  58. {
  59.  
  60. analogWrite(ENA,ENASpeed);
  61. analogWrite(ENB,ENBSpeed);
  62.  
  63. digitalWrite(PinR1,HIGH);
  64. digitalWrite(PinR2,LOW);
  65. digitalWrite(PinL1,HIGH);
  66. digitalWrite(PinL2,HIGH);
  67.  
  68. delay(800);
  69.  
  70. }
  71. void goLeft()
  72. {
  73.  
  74.  
  75. analogWrite(ENA,ENASpeed);
  76. analogWrite(ENB,ENBSpeed);
  77.  
  78. digitalWrite(PinR1,HIGH);
  79. digitalWrite(PinR2,HIGH);
  80. digitalWrite(PinL1,HIGH);
  81. digitalWrite(PinL2,LOW);
  82.  
  83. delay(800);
  84.  
  85. }
  86.  
  87. void takeUturn()
  88. {
  89.  
  90.  
  91. analogWrite(ENA,ENASpeed);
  92. analogWrite(ENB,ENBSpeed);
  93.  
  94. digitalWrite(PinR1,HIGH);
  95. digitalWrite(PinR2,LOW);
  96. digitalWrite(PinL1,LOW);
  97. digitalWrite(PinL2,HIGH);
  98.  
  99. delay(1500);
  100. }
  101.  
  102. void stopMotor()
  103. {
  104. analogWrite(ENA,0);
  105. analogWrite(ENB,0);
  106.  
  107. digitalWrite(PinR1,LOW);
  108. digitalWrite(PinR2,LOW);
  109. digitalWrite(PinL1,LOW);
  110. digitalWrite(PinL2,LOW);
  111.  
  112.  
  113. }
  114. void goExtraInch()
  115. {
  116.  
  117. goForward();
  118. delay(500);
  119.  
  120. }
  121.  
  122. void runSolvedPath()
  123. {
  124.  
  125.  
  126. //Emon eita tui likhbi :D //sathe Delay er bepar tao dekhis //ar PID er sathe jeno Maze er code clash na kore tao dekhis
  127.  
  128.  
  129. }
  130.  
  131.  
  132.  
  133.  
  134.  
  135. //stuffs needed for PID
  136.  
  137.  
  138. const int Kp=7,Ki=0,Kd=3;
  139. int error=0;
  140. int prev_error=0;
  141. int correction=0;
  142. int mod_ENASpeed;
  143. int mod_ENBSpeed;
  144.  
  145. int p=0;
  146. int i=0;
  147. int d=0;
  148.  
  149. //calculating error to go in the PID
  150.  
  151.  
  152. void errorCalc()
  153. {
  154. error=0;
  155.  
  156. for(int j=0;j<5;j++)
  157. {
  158. error=error+(sensorWeight[j]*sensorData[j]);
  159. }
  160.  
  161. }
  162.  
  163.  
  164. void PID()
  165. {
  166.  
  167. p=error;
  168. i=error+prev_error;
  169. d=error-prev_error;
  170.  
  171. prev_error=error;
  172.  
  173. correction = (Kp*p )+ (Ki*i )+ (Kd*d) ;
  174.  
  175. mod_ENASpeed=ENASpeed-correction;
  176. mod_ENBSpeed=ENBSpeed+correction;
  177.  
  178.  
  179.  
  180.  
  181. Serial.print("Correction:");
  182. Serial.println(correction);
  183.  
  184.  
  185.  
  186.  
  187. }
  188. void applyCorrection()
  189.  
  190. {
  191.  
  192. analogWrite(ENA,mod_ENASpeed);
  193. analogWrite(ENB,mod_ENBSpeed);
  194.  
  195. Serial.print("mod_ENASpeed:");
  196. Serial.println(mod_ENASpeed);
  197.  
  198. Serial.print("mod_ENBSpeed:");
  199. Serial.println(mod_ENBSpeed);
  200.  
  201.  
  202.  
  203. digitalWrite(PinR1,HIGH);
  204. digitalWrite(PinR2,LOW);
  205. digitalWrite(PinL1,HIGH);
  206. digitalWrite(PinL2,LOW);
  207.  
  208. }
  209.  
  210.  
  211.  
  212. void setup() {
  213. pinMode(Sensor1,INPUT);
  214. pinMode(Sensor2,INPUT);
  215. pinMode(Sensor3,INPUT);
  216. pinMode(Sensor4,INPUT);
  217. pinMode(Sensor5,INPUT) ;
  218.  
  219. pinMode(PinR1,OUTPUT);
  220. pinMode(PinR2,OUTPUT);
  221. pinMode(PinL1,OUTPUT);
  222. pinMode(PinL2,OUTPUT);
  223.  
  224.  
  225. pinMode(ENA,OUTPUT);
  226. pinMode(ENB,OUTPUT);
  227. Serial.begin(9600);
  228. }
  229.  
  230. void printSensor()
  231. {
  232.  
  233. for(int j=0;j<5;j++)
  234. {
  235.  
  236. Serial.print(sensorData[j]);
  237. Serial.print(" ");
  238.  
  239.  
  240. }
  241.  
  242.  
  243. Serial.println("");
  244.  
  245.  
  246. }
  247.  
  248. void loop() {
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255. readSensor();
  256. printSensor();
  257.  
  258.  
  259. runMotor();
  260. errorCalc();
  261. PID();
  262. applyCorrection();
  263. delay(300);
  264.  
  265.  
  266. }
  267.  
  268.  
  269. void readSensor()
  270. {
  271.  
  272.  
  273. for(int i=0;i<5;i++)
  274. {
  275.  
  276.  
  277. if((analogRead(i)>350))
  278.  
  279. {
  280. sensorData[i] = 0;
  281. }
  282. else
  283. {
  284.  
  285. sensorData[i] = 1;
  286.  
  287. }
  288.  
  289. }
  290.  
  291. }
  292.  
  293.  
  294.  
  295. void runMotor()
  296.  
  297. {
  298.  
  299. if(sensorData[0]==0 && sensorData[1]==0 && sensorData[2]==1 && sensorData[3]==0 && sensorData[4]==0) //could be 8 or dead end
  300.  
  301. {
  302. goExtraInch();
  303. readSensor();
  304. if(sensorData[0]==0 && sensorData[1]==0 && sensorData[2]==1 && sensorData[3]==0 && sensorData[4]==0) //path 8
  305. {
  306.  
  307. goForward();
  308. delay(300);//robot will go straight
  309. pathLog[q]=2;
  310. pathLogCorrection();
  311.  
  312. }
  313. }
  314. else
  315. {
  316.  
  317. takeUturn(); //dead end
  318. q--;
  319.  
  320. }
  321. }
  322. if(sensorData[0]==1 && sensorData[1]==1 && sensorData[2]==1 && sensorData[3]==0 && sensorData[4]==0) //could be 1 or 5
  323.  
  324. {
  325. goLeft(); //as left is out priority ,either way robot has go left
  326. pathLog[q]=1;
  327. pathLogCorrection();
  328.  
  329. }
  330. if(sensorData[0]==0 && sensorData[1]==0 && sensorData[2]==1 && sensorData[3]==1 && sensorData[4]==1) //could be 2 or 6
  331.  
  332. {
  333.  
  334. goExtraInch();
  335.  
  336. readSensor();
  337.  
  338. if(sensorData[0]==0 && sensorData[1]==0 && sensorData[2]==1 && sensorData[3]==0 && sensorData[4]==0) // path 6
  339.  
  340. {
  341.  
  342. goForward(); //robot will go forward
  343. pathLog[q]=2;
  344. pathLogCorrection();
  345.  
  346.  
  347. }
  348. else // path 2
  349. {
  350.  
  351. goRight(); //robot will go right
  352. pathLog[q]=3;
  353. pathLogCorrection();
  354.  
  355. }
  356.  
  357. }
  358.  
  359. if(sensorData[0]==1 && sensorData[1]==1 && sensorData[2]==1 && sensorData[3]==1 && sensorData[4]==1) //could be path 3 or 4 or 7
  360.  
  361. {
  362.  
  363.  
  364. goExtraInch();
  365. readSensor();
  366. if(sensorData[0]==1 && sensorData[1]==1 && sensorData[2]==1 && sensorData[3]==1 && sensorData[4]==1) // path 7
  367.  
  368. {
  369.  
  370. stopMotor();
  371. delay(5000); //will run along the solved path after 10 seconds of running to the end of the maze
  372. runSolvedPath();
  373.  
  374. }
  375.  
  376.  
  377. else //could be path 3 or 4
  378.  
  379. {
  380. goLeft() //but either way ,our robot has to go left
  381. pathLog[q]=1;
  382. pathLogCorrection();
  383. }
  384. }
  385. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement