Advertisement
Sidsh

Dijkstra+LSA

Feb 8th, 2022
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VeriLog 14.07 KB | None | 0 0
  1. module LSA(
  2.     output [7:0]m1,  //motor1                                            PIN_D3
  3.     output [7:0]m2,  //motor2                                            PIN_C3
  4.     input [11:0]s1,  //12-bit output of ch. 5 (parallel)
  5.     input [11:0]s2,  //12-bit output of ch. 6 (parallel)
  6.     input [11:0]s3,  //12-bit output of ch. 7 (parallel)
  7.     input clk_50,    //50 MHz clock
  8.     input reset,
  9.      output Led1,    //Led used to indicate position of bot i.e. node or line
  10.      output Led2,
  11.      output Led3,
  12.      output HL1,        // to control uart
  13.      output [2:0] id
  14.     );
  15.  
  16. reg signed[7:0]error = 0;
  17. reg signed[7:0]difference = 0;
  18. reg signed[7:0]correction = 0;
  19. reg signed[7:0]cumulative_error = 0;
  20. reg signed[7:0]preverror = 0;
  21. reg [3:0]nodecount = -4'd1;     //No. of nodes bot has traversed, initially set to -1
  22. //reg [3:0]flag=0;
  23. reg flag = 0;  
  24.  
  25. reg [6:0]k, l, adj, near, parnode;
  26.  
  27. reg [11:0] dist [0:36];
  28. reg [6:0] parent [0:36];
  29. reg visited[0:36];
  30.  
  31. integer length=0;    // length of path
  32. reg [6:0] sp[0:36];     // Store path
  33. reg [1:0] p_dc[0:36];       //Stores The turns
  34.  
  35.  
  36. reg led1=0;
  37. reg led2=0;
  38. reg led3=0;
  39.  
  40. parameter V = 37;
  41.  
  42. reg [7:0] i,j;
  43.  
  44. reg [11:0] cost [0:36][0:36];
  45. reg [11:0] cost2 [0:36][0:36];
  46.  
  47. // Function to find out the shortest distance
  48. function automatic [6:0]minNode;
  49.     input [6:0]a;
  50.     reg [6:0]minValue = 999;
  51.    reg [6:0]minNode1 = 0;
  52.     begin
  53.         for (k = 0; k < V; k=k+1)
  54.         begin
  55.         if (dist[k] < minValue && visited[k] == 0)
  56.         begin
  57.             minValue = dist[k];
  58.             minNode1 = k;
  59.                 //$display("Enter min");
  60.         end
  61.         end
  62.     minNode = minNode1;
  63.      //$display("Function Called %6d", minNode1);
  64.     end
  65. endfunction
  66. //
  67.  
  68.  
  69. //Storing Values of the cost matrix
  70. initial begin
  71.     for(i=0;i<V;i=i+1)
  72. begin
  73.     for(j=0;j<V;j=j+1) begin
  74.        
  75.         if(i==j) begin
  76.                 cost2[i][j] = 0;
  77.                 //cost[i][j] = 0;
  78.           end
  79.         else begin
  80.                 cost2[i][j] = 999;
  81.                 //cost[i][j] = 999;
  82.           end
  83.     end
  84. end
  85. cost2[0][1] = 3;
  86. cost2[0][2] = 2;
  87. cost2[0][8] = 1;
  88.  
  89. cost2[1][0] = 4;
  90.  
  91. cost2[2][0] = 1;
  92. cost2[2][3] = 2;
  93. cost2[2][4] = 3;
  94.  
  95. cost2[3][2] = 1;
  96. cost2[3][6] = 3;
  97. cost2[3][17] = 2;
  98.  
  99. cost2[4][2] = 4;
  100. cost2[4][5] = 2;
  101. cost2[4][11] = 3;
  102.  
  103. cost2[5][4] = 1;
  104.  
  105. cost2[6][3] = 4;
  106. cost2[6][7] = 2;
  107. cost2[6][14] = 3;
  108.  
  109. cost2[7][6] = 1;
  110.  
  111. cost2[8][0] = 4;
  112. cost2[8][10] = 2;
  113. cost2[8][33] = 3;
  114.  
  115. cost2[9][10] = 3;
  116.  
  117. cost2[10][8] = 1;
  118. cost2[10][9] = 4;
  119. cost2[10][11] = 2;
  120.  
  121. cost2[11][4] = 4;
  122. cost2[11][10] = 1;
  123. cost2[11][12] = 2;
  124. cost2[11][18] = 3;
  125.  
  126. cost2[12][11] = 1;        //M
  127. cost2[12][13] = 4;
  128. cost2[12][14] = 2;
  129. cost2[12][23] = 3;
  130.  
  131. cost2[13][12] = 3;
  132.  
  133. cost2[14][6] = 4;        //O
  134. cost2[14][12] = 1;
  135. cost2[14][15] = 2;
  136.  
  137. cost2[15][14] = 1;
  138. cost2[15][16] = 4;
  139. cost2[15][17] = 2;
  140.  
  141. cost2[16][15] = 3;       //Q
  142.  
  143. cost2[17][15] = 1;
  144. cost2[17][3] = 4;
  145. cost2[17][26] = 3;
  146.  
  147. cost2[18][19] = 2;
  148. cost2[18][11] = 4;
  149. cost2[18][21] = 3;       //S
  150.  
  151. cost2[19][18] = 1;
  152.  
  153. cost2[20][21] = 2;       //U
  154.  
  155. cost2[21][20] = 1;
  156. cost2[21][22] = 2;
  157. cost2[21][28] = 3;
  158. cost2[21][18] = 4;
  159.  
  160. cost2[22][21] = 1;       //W
  161.  
  162. cost2[23][24] = 2;
  163. cost2[23][12] = 4;
  164. cost2[23][34] = 3;
  165.  
  166. cost2[24][22] = 1;
  167.  
  168. cost2[25][26] = 2;       //Z
  169.  
  170. cost2[26][25] = 1;
  171. cost2[26][17] = 4;
  172. cost2[26][34] = 3;       //AA
  173.  
  174. cost2[27][28] = 2;
  175.  
  176. cost2[28][27] = 1;
  177. cost2[28][21] = 4;
  178. cost2[28][29] = 2;
  179. cost2[28][31] = 3;
  180.  
  181. cost2[29][28] = 1;       //AD
  182.  
  183. cost2[30][31] = 2;
  184.  
  185. cost2[31][30] = 1;       //AF
  186. cost2[31][28] = 4;
  187. cost2[31][32] = 2;
  188. cost2[31][33] = 3;
  189.  
  190. cost2[32][31] = 1;       //AG
  191.  
  192. cost2[33][31] = 4;
  193. cost2[33][8] = 1;
  194. cost2[33][34] = 2;
  195.  
  196. cost2[34][35] = 2;
  197. cost2[34][23] = 4;
  198. cost2[34][33] = 1;
  199.  
  200. cost2[35][36] = 4;
  201. cost2[35][26] = 2;
  202. cost2[35][34] = 1;
  203.  
  204. cost[36][35] = 3;
  205. end
  206.  
  207. initial begin
  208. for(i=0;i<V;i=i+1)
  209. begin
  210.     for(j=0;j<V;j=j+1) begin
  211.        
  212.         if(i==j) begin
  213.                 //cost2[i][j] = 0;
  214.                 cost[i][j] = 0;
  215.           end
  216.         else begin
  217.                 //cost2[i][j] = 999;
  218.                 cost[i][j] = 999;
  219.           end
  220.     end
  221. end
  222. cost[0][1] = 100;               //A
  223. cost[0][2] = 2;
  224. cost[0][8] = 3;
  225.  
  226. cost[1][0] = 100;               //B
  227.  
  228. cost[2][0] = 2;                 //C
  229. cost[2][3] = 3;
  230. cost[2][4] = 2;
  231.  
  232. cost[3][2] = 3;                 //D
  233. cost[3][6] = 2;
  234. cost[3][17] = 4;
  235.  
  236. cost[4][2] = 2;         //E
  237. cost[4][5] = 100;
  238. cost[4][11] = 2;
  239.  
  240. cost[5][4] = 100;       //F
  241.  
  242. cost[6][3] = 2;         //G
  243. cost[6][7] = 100;
  244. cost[6][14] = 3;
  245.  
  246. cost[7][6] = 100;       //H
  247.  
  248. cost[8][0] = 3;         //I
  249. cost[8][10] = 2;
  250. cost[8][33] = 4;
  251.  
  252. cost[9][10] = 100;      //J
  253.  
  254. cost[10][8] = 2;        //K
  255. cost[10][9] = 100;
  256. cost[10][11] = 1;
  257.  
  258. cost[11][4] = 2;        //L
  259. cost[11][10] = 1;
  260. cost[11][12] = 3;
  261. cost[11][18] = 1;
  262.  
  263. cost[12][11] = 3;           //M
  264. cost[12][13] = 100;
  265. cost[12][14] = 1;
  266. cost[12][23] = 2;
  267.  
  268. cost[13][12] = 100;     //N
  269.  
  270. cost[14][6] = 3;            //O
  271. cost[14][12] = 1;
  272. cost[14][15] = 2;
  273.  
  274. cost[15][14] = 2;       //P
  275. cost[15][16] = 100;
  276. cost[15][17] = 1;
  277.  
  278. cost[16][15] = 100;         //Q
  279.  
  280. cost[17][15] = 1;       //R
  281. cost[17][3] = 4;
  282. cost[17][26] = 2;
  283.  
  284. cost[18][19] = 100;     //S
  285. cost[18][11] = 1;
  286. cost[18][21] = 1;      
  287.  
  288. cost[19][18] = 100;     //T
  289.  
  290. cost[20][21] = 100;         //U
  291.  
  292. cost[21][20] = 100;     //V
  293. cost[21][22] = 100;
  294. cost[21][28] = 1;
  295. cost[21][18] = 1;
  296.  
  297. cost[22][21] = 100;             //W
  298.  
  299. cost[23][24] = 100;     //X
  300. cost[23][12] = 2;
  301. cost[23][34] = 3;
  302.  
  303. cost[24][23] = 100;     //Y
  304.  
  305. cost[25][26] = 100;             //Z
  306.  
  307. cost[26][25] = 100;     //AA
  308. cost[26][17] = 2;
  309. cost[26][34] = 4;      
  310.  
  311. cost[27][28] = 100;     //AB
  312.  
  313. cost[28][27] = 100;     //AC
  314. cost[28][21] = 1;
  315. cost[28][29] = 100;
  316. cost[28][31] = 1;
  317.  
  318. cost[29][28] = 100;         //AD
  319.  
  320. cost[30][31] = 100;     //AE
  321.  
  322. cost[31][30] = 100;             //AF
  323. cost[31][28] = 1;
  324. cost[31][32] = 100;
  325. cost[31][33] = 1;
  326.  
  327. cost[32][31] = 100;             //AG
  328.  
  329. cost[33][31] = 1;       //AH
  330. cost[33][8] = 4;
  331. cost[33][34] = 3;
  332.  
  333. cost[34][35] = 2;       //AI
  334. cost[34][23] = 3;
  335. cost[34][33] = 3;
  336.  
  337. cost[35][36] = 100;     //AJ
  338. cost[35][26] = 4;
  339. cost[35][34] = 2;
  340.  
  341. cost[36][35] = 100;     //AK
  342.  
  343. end
  344.  
  345. reg [7:0]odc =50;     //optimum duty cycle
  346. reg [7:0]mo1 = 50;    // pwm to motor1, initially set to 50
  347. reg [7:0]mo2 = 50;    // pwm to motor2, initially set to 50
  348. reg [7:0]ml1 = 50;    // pwm to motor1 when it is on line
  349. reg [7:0]ml2 = 50;    // pwm to motor2 when it is on line
  350. reg [7:0]mn1 = 50;    // pwm to motor1 when it is on node
  351. reg [7:0]mn2 = 50;    // pwm to motor2 when it is on node
  352. reg [11:0]thr = 2000; //Condition to differentiate between white and black surface
  353. reg [7:0]r_COUNT = 0; //counter
  354. reg clk_1 = 0;        //1 MHz Clock
  355.  
  356. reg [2:0] id1 =0;        //to control uart
  357. reg hl;
  358.  
  359.  
  360. always @(posedge clk_50) begin   //Scaling down of 50MHz clock to 1 MHz
  361.  
  362.     if(reset == 0)
  363.      begin
  364.         r_COUNT <= 0;
  365.      end
  366.  
  367.    if (r_COUNT == 49 )begin             //Check if count reaches 49
  368.       r_COUNT <= 0;                     //Reset counter if it reaches 49
  369.    end else begin
  370.       r_COUNT <= r_COUNT + 1;           //increment counter by 1 till 49
  371.    end
  372.     clk_1 <= (r_COUNT < 25);
  373. end
  374.  
  375.  
  376. //dijkstra function
  377. function automatic dijkstra;
  378. input [6:0] start;
  379. input [6:0] enda;
  380. begin
  381.      $display("Enter dijkstra");
  382.     for (i = 0; i < V; i=i+1)
  383.     begin
  384.         dist[i] = 999;
  385.         parent[i] = i;
  386.           visited[i] = 0;
  387.           sp[i] = 0;
  388.     end
  389.     dist[start] = 0;
  390.         for (i = 0; i < V; i=i+1)
  391.         begin
  392.         near = minNode(1);
  393.         visited[near] = 1;
  394.  
  395.         for (adj = 0; adj < V; adj=adj+1)
  396.         begin
  397.             if (cost[near][adj] != 0 && dist[adj] > dist[near] + cost[near][adj])
  398.             begin
  399.                 dist[adj] = dist[near] + cost[near][adj];
  400.                 parent[adj] = near;
  401.             end
  402.         end
  403.         end
  404.       parnode = parent[enda];
  405.       sp[length] = enda;
  406.       for(i=0;i<V;i=i+1)
  407.            begin
  408.                 if(parnode != start) begin
  409.                //$display(" <- %6d || %6d",parnode,parent[parnode]);
  410.                 length = length + 1;
  411.                 sp[length] = parnode;
  412.                 parnode = parent[parnode];                   
  413.                     end
  414.             end
  415.         length= length + 1;
  416.         sp[length] = start;
  417.       //$display("%6d", length);
  418.         //direction
  419.         i=0;
  420.         for(j=35;j>0;j=j-1) begin
  421.             //$display("%6d", sp[j]);
  422.            
  423.             if(j<length && j>0) begin
  424.                 $display("%6d", sp[j]);
  425.                 if(((cost2[sp[j]][sp[j+1]]==1) && (cost2[sp[j]][sp[j-1]] == 2)) || ((cost2[sp[j]][sp[j+1]]==2) && (cost2[sp[j]][sp[j-1]] == 1)) || ((cost2[sp[j]][sp[j+1]]==4) && (cost2[sp[j]][sp[j-1]] == 3)) || ((cost2[sp[j]][sp[j+1]]==3) &&(cost2[sp[j]][sp[j-1]] == 4)))
  426.                     begin //$display("Straight");
  427.                         p_dc[i]=0;
  428.                         i=i+1;
  429.                     end
  430.                 if(((cost2[sp[j]][sp[j+1]]==1) && (cost2[sp[j]][sp[j-1]] == 3)) || ((cost2[sp[j]][sp[j+1]]==3) && (cost2[sp[j]][sp[j-1]] == 2)) || ((cost2[sp[j]][sp[j+1]]==2) && (cost2[sp[j]][sp[j-1]] == 4)) || ((cost2[sp[j]][sp[j+1]]==4) && (cost2[sp[j]][sp[j-1]] == 1)))
  431.                     begin //$display("Left");
  432.                         p_dc[i]=1;
  433.                         i=i+1;
  434.                     end
  435.                 if(((cost2[sp[j]][sp[j+1]]==1) && (cost2[sp[j]][sp[j-1]] == 4)) || ((cost2[sp[j]][sp[j+1]]==4) && (cost2[sp[j]][sp[j-1]] == 2)) || ((cost2[sp[j]][sp[j+1]]==2) && (cost2[sp[j]][sp[j-1]] == 3)) || ((cost2[sp[j]][sp[j+1]]==3) && (cost2[sp[j]][sp[j-1]] == 1)))
  436.                     begin //$display("Right");
  437.                         p_dc[i]=2;
  438.                         i=i+1;
  439.                     end
  440.                
  441.             end
  442.            
  443.            
  444.         end
  445.          
  446.     dijkstra = 1;
  447. end
  448. endfunction
  449. //
  450.  
  451. reg flag_dijkstra = 0;
  452. always @(posedge clk_1) begin    
  453.  
  454. //Call dijkstra
  455.     if(flag_dijkstra == 0) begin
  456.         flag_dijkstra = dijkstra(33, 7);
  457.         i=-1;
  458.         end
  459.        
  460. // Line Following
  461.     if(reset == 0)
  462.      begin
  463.         nodecount =-4'd1 ;         //Initially node count set to -1
  464.      end
  465.    
  466.      led1 <= (s1>thr);              //Led lights up when s1 has a greater value than 2000
  467.      led2 <= (s2>thr);              //Led lights up when s2 has a greater value than 2000
  468.      led3 <= (s3>thr);              //Led lights up when s3 has a greater value than 2000
  469.  
  470.     error = (s1>thr) - (s3>thr);    //Relative error between sensors s1 and s3: values range from -1 to 1  P
  471.  
  472.     cumulative_error <= cumulative_error + error;  //Adds up the error to give a cumulative error  I
  473.      
  474.     if (cumulative_error > 10)      //Condition to reset the value of cumulative error to 10 if it crosses 10  
  475.     begin
  476.         cumulative_error <= 10;
  477.     end
  478.  
  479.      if (cumulative_error < -10)    //Condition to reset the value of cumulative error to -10 if it crosses -10  
  480.     begin
  481.         cumulative_error <= -10;
  482.     end
  483.      
  484.      if (s1<thr && s2>thr && s3<thr) //Cumulative error resets to zero when the bot is on line i.e WBW
  485.      begin
  486.             cumulative_error <= 0;  
  487.             hl=0;
  488.      end
  489.      
  490.      difference <= error-preverror;   //forms the differential part D
  491.      correction <= ((10*error) + cumulative_error + (2*difference)); // kp = 10, ki = 1, kd =2
  492.      
  493.       preverror <= error;  // Stores value of current error to previous error so that it can be used in next loop cycle
  494.      
  495.      
  496.      ml1 <= odc - correction;  // PID tuning for motor 1
  497.      ml2 <= odc + correction;  // PID tuning for motor 2
  498.  
  499.      if (ml1>70)      //Resetting value of ml1 to 70 if it crosses 70
  500.      begin
  501.          ml1 <= 70;
  502.      end
  503.      if (ml2>70)      //Resetting value of ml2 to 70 if it crosses 70
  504.      begin
  505.          ml2 <= 70;
  506.      end
  507.      if (ml1<30)       //Resetting value of ml1 to 30 if it becomes less than 30
  508.      begin
  509.          ml1 <= 30;
  510.      end
  511.      if (ml2<30)       //Resetting value of ml2 to 30 if it becomes less than 30
  512.      begin
  513.          ml2 <= 30;
  514.      end  
  515.     /*if(s1>thr && s2>thr && s3>thr)
  516.     begin
  517.         flag = 1;
  518.         mo1 <= 0;
  519.         mo2 <= 63;
  520.     end*/
  521.     /*if(s1>thr && s2<thr && s3<thr && flag == 1)  //end turning
  522.     begin
  523.         flag <= 0;
  524.         mo1 <= odc - correction;
  525.       mo2 <= odc + correction;
  526.     end
  527.     /*if(s1<thr && s2>thr && s3<thr && flag == 1)
  528.     begin
  529.         flag <= 0;
  530.         mo1 <= odc - correction;
  531.       mo2 <= odc + correction;
  532.     end*/
  533.     if (s1 < thr && s2 > thr && s3 < thr) //ready for next node, flag resets to zero on line : WBW
  534.     begin
  535.         flag <= 0;
  536.     end
  537.      /*else begin
  538.         flag <= 1;
  539.     end*/
  540.  
  541.     // Only detect node once.
  542.     if (s1>thr && s2>thr && s3>thr && flag == 0)  //detect node
  543.     begin
  544.         /*if (nodecount == 7)        // Nodecount resets to zero if it crosses 7
  545.         begin
  546.             id1 = 4;
  547.             hl = 1;
  548.             nodecount <= 0;
  549.             flag <= 1;            //flag set to 1 when it detects a node
  550.         end else
  551.         begin
  552.             id1 = 4;
  553.             hl = 1;
  554.             nodecount <= nodecount + 1;
  555.          flag <= 1;
  556.         end */
  557.          
  558.           hl = 1;
  559.           id1 = 4;
  560.           flag <= 1;
  561.          
  562.           //nodecount = p_dc[i];
  563.           i=i+1;         
  564.     end
  565.        
  566.     /*if (s1>thr && s2>thr && s3>thr)
  567.         begin
  568.             if(flag == 3)
  569.             begin
  570.                 if (nodecount == 7)
  571.                 begin
  572.                     nodecount <= 0;
  573.                     flag <= 0;
  574.                     flag2 <= 1;
  575.                 end else
  576.                 begin
  577.                     nodecount <= nodecount + 1;
  578.                     flag <=0;
  579.                     flag2 <= 1;
  580.                 end
  581.             end else
  582.             begin
  583.                 flag <= flag + 1;
  584.             end
  585.         end else
  586.             begin
  587.                 flag <= 0;
  588.             end*/
  589.            
  590.     if(flag == 1)    //Detection of node and applying pwm accordingly
  591.     begin
  592.    
  593.     if(p_dc[i] == 0)
  594.     begin            
  595.         mn1 <= odc;
  596.       mn2 <= odc;
  597.     end
  598.     if(p_dc[i] == 1)
  599.     begin            
  600.         mn1 <= 5;
  601.       mn2 <= 60;
  602.     end
  603.     if(p_dc[i] == 2)
  604.     begin            
  605.         mn1 <= 60;
  606.       mn2 <= 5;
  607.     end
  608.     /*case (nodecount)
  609.     0: begin            
  610.         mn1 <= odc;
  611.       mn2 <= odc;
  612.         end
  613.     1: begin
  614.         mn1 <= 5;
  615.         mn2 <= 60;
  616.         end
  617.     2: begin
  618.         mn1 <= 60;
  619.       mn2 <= 5;
  620.         end
  621.     4: begin
  622.         mn1 <= 5;
  623.         mn2 <= 60;
  624.         end
  625.     5: begin
  626.         mn1 <= odc;
  627.       mn2 <= odc;
  628.         end
  629.     6: begin
  630.         mn1 <= odc;
  631.       mn2 <= odc;
  632.         end
  633.     7: begin
  634.         mn1 <= 5;
  635.         mn2 <= 60;
  636.         end
  637.     0: begin
  638.         mn1 <= odc;
  639.       mn2 <= odc;
  640.         end
  641.     endcase*/
  642.     /*
  643.         mn1 <= 5;
  644.         mn2 <= 60;*/
  645.     end
  646.    
  647.     if(flag == 0)    //assigning values of ml1 and ml2 (line condition) to mo1 and mo2 respectively
  648.     begin
  649.         mo1 <= ml1;
  650.         mo2 <= ml2;
  651.     end else
  652.     begin            //assigning values of mn1 and mn2 (node condition) to mo1 and mo2 respectively
  653.         mo1 <= mn1;
  654.         mo2 <= mn2;
  655.     end
  656. end
  657.        
  658. assign Led1 = led1;
  659. assign Led2 = led2;
  660. assign Led3 = led3;
  661.  
  662. assign id = id1;
  663. assign HL1 = hl;
  664. assign m1 = mo1;
  665. assign m2 = mo2;
  666.  
  667. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement