module LSA( output [7:0]m1, //motor1 PIN_D3 output [7:0]m2, //motor2 PIN_C3 input [11:0]s1, //12-bit output of ch. 5 (parallel) input [11:0]s2, //12-bit output of ch. 6 (parallel) input [11:0]s3, //12-bit output of ch. 7 (parallel) input clk_50, //50 MHz clock input reset, output Led1, //Led used to indicate position of bot i.e. node or line output Led2, output Led3, output HL1, // to control uart output [2:0] id ); reg signed[7:0]error = 0; reg signed[7:0]difference = 0; reg signed[7:0]correction = 0; reg signed[7:0]cumulative_error = 0; reg signed[7:0]preverror = 0; reg [3:0]nodecount = -4'd1; //No. of nodes bot has traversed, initially set to -1 //reg [3:0]flag=0; reg flag = 0; reg [6:0]k, l, adj, near, parnode; reg [11:0] dist [0:36]; reg [6:0] parent [0:36]; reg visited[0:36]; reg [6:0] length=0; // length of path reg [6:0] sp[0:36]; // Store path reg [1:0] p_dc[0:36]; //Stores The turns reg led1=0; reg led2=0; reg led3=0; parameter V = 37; reg [7:0] i,j; reg [11:0] cost [0:36][0:36]; reg [11:0] cost2 [0:36][0:36]; // Function to find out the shortest distance function automatic [6:0]minNode; input [6:0]a; reg [6:0]minValue = 999; reg [6:0]minNode1 = 0; begin for (k = 0; k < V; k=k+1) begin if (dist[k] < minValue && visited[k] == 0) begin minValue = dist[k]; minNode1 = k; //$display("Enter min"); end end minNode = minNode1; //$display("Function Called %6d", minNode1); end endfunction // //Storing Values of the cost matrix initial begin for(i=0;i dist[near] + cost[near][adj]) begin dist[adj] = dist[near] + cost[near][adj]; parent[adj] = near; end end end parnode = parent[enda]; sp[length] = enda; for(i=0;i0;j=j-1) begin //$display("%6d", sp[j]); if(j0) begin $display("%6d", sp[j]); 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))) begin $display("Straight"); p_dc[i]=0; $display("%6d",p_dc[i]); i=i+1; end 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))) begin $display("Left"); p_dc[i]=1; $display("%6d",p_dc[i]); i=i+1; end 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))) begin $display("Right"); p_dc[i]=2; $display("%6d",p_dc[i]); i=i+1; end end end dijkstra = 1; end endfunction // reg flag_dijkstra = 0; reg [6:0] count_dijkstra =-1; always @(posedge clk_1) begin //Call dijkstra if(flag_dijkstra == 0) begin flag_dijkstra = dijkstra(start1, 3); end // Line Following if(reset == 0) begin nodecount =-4'd1 ; //Initially node count set to -1 end led1 <= (s1>thr); //Led lights up when s1 has a greater value than 2000 led2 <= (s2>thr); //Led lights up when s2 has a greater value than 2000 led3 <= (s3>thr); //Led lights up when s3 has a greater value than 2000 error = (s1>thr) - (s3>thr); //Relative error between sensors s1 and s3: values range from -1 to 1 P cumulative_error <= cumulative_error + error; //Adds up the error to give a cumulative error I if (cumulative_error > 10) //Condition to reset the value of cumulative error to 10 if it crosses 10 begin cumulative_error <= 10; end if (cumulative_error < -10) //Condition to reset the value of cumulative error to -10 if it crosses -10 begin cumulative_error <= -10; end if (s1thr && s370) //Resetting value of ml1 to 70 if it crosses 70 begin ml1 <= 70; end if (ml2>70) //Resetting value of ml2 to 70 if it crosses 70 begin ml2 <= 70; end if (ml1<30) //Resetting value of ml1 to 30 if it becomes less than 30 begin ml1 <= 30; end if (ml2<30) //Resetting value of ml2 to 30 if it becomes less than 30 begin ml2 <= 30; end if (s1 < thr && s2 > thr && s3 < thr) //ready for next node, flag resets to zero on line : WBW begin flag <= 0; end /*else begin flag <= 1; end*/ // Only detect node once. if (s1>thr && s2>thr && s3>thr && flag == 0 && flag_dijkstra==1) //detect node begin hl = 1; id1 = 4; flag <= 1; //$display("%7d " ,i); //nodecount = p_dc[i]; count_dijkstra = count_dijkstra+1; $display("%6d, %6d, %6d",p_dc[0], count_dijkstra, p_dc[count_dijkstra]); if(count_dijkstra==(length-1)) begin start1 = end1; if(sp[0] == end1) begin end1=s6; end if(sp[0] == s6) begin end1=dzn1; end flag_dijkstra = 0; end if(p_dc[count_dijkstra] == 0) begin //$display("straight"); mn1 <= odc; mn2 <= odc; end if(p_dc[count_dijkstra] == 1) begin //$display("left"); mn1 <= 5; mn2 <= 60; end if(p_dc[count_dijkstra] == 2) begin //$display("right"); mn1 <= 60; mn2 <= 5; end end /*if(flag == 1) //Detection of node and applying pwm accordingly begin if(p_dc[i] == 0) begin $display("straight"); mn1 <= odc; mn2 <= odc; end if(p_dc[i] == 1) begin $display("left"); mn1 <= 5; mn2 <= 60; end if(p_dc[i] == 2) begin $display("right"); mn1 <= 60; mn2 <= 5; end end*/ if(flag == 0) //assigning values of ml1 and ml2 (line condition) to mo1 and mo2 respectively begin mo1 <= ml1; mo2 <= ml2; end else begin //assigning values of mn1 and mn2 (node condition) to mo1 and mo2 respectively mo1 <= mn1; mo2 <= mn2; end end assign Led1 = led1; assign Led2 = led2; assign Led3 = led3; assign id = id1; assign HL1 = hl; assign m1 = mo1; assign m2 = mo2; endmodule