Advertisement
Sidsh

Stop logic LSA

Jan 31st, 2022
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     );
  13.  
  14. reg signed[7:0]error = 0;
  15. reg signed[7:0]difference = 0;
  16. reg signed[7:0]correction = 0;
  17. reg signed[7:0]cumulative_error = 0;
  18. reg signed[7:0]preverror = 0;
  19. reg [3:0]nodecount = -4'd1;     //No. of nodes bot has traversed, initially set to -1
  20. //reg [3:0]flag=0;
  21. reg flag = 0;  
  22. reg [1:0]loopcount = 1;
  23. reg led1=0;
  24. reg led2=0;
  25. reg led3=0;
  26.  
  27. reg [7:0]odc =50;     //optimum duty cycle
  28. reg [7:0]mo1 = 50;    // pwm to motor1, initially set to 50
  29. reg [7:0]mo2 = 50;    // pwm to motor2, initially set to 50
  30. reg [7:0]ml1 = 50;    // pwm to motor1 when it is on line
  31. reg [7:0]ml2 = 50;    // pwm to motor2 when it is on line
  32. reg [7:0]mn1 = 50;    // pwm to motor1 when it is on node
  33. reg [7:0]mn2 = 50;    // pwm to motor2 when it is on node
  34. reg [11:0]thr = 2000; //Condition to differentiate between white and black surface
  35. reg [7:0]r_COUNT = 0; //counter
  36. reg clk_1 = 0;        //1 MHz Clock
  37.  
  38. /*reg [9:0]s1v1=0;
  39. reg [9:0]s1v2=0;
  40. reg [9:0]s1v3=0;
  41. reg [9:0]s1v4=0;
  42. reg [9:0]s1v5=0;
  43. reg [9:0]s1v6=0;
  44.  
  45. reg [9:0]s2v1=0;
  46. reg [9:0]s2v2=0;
  47. reg [9:0]s2v3=0;
  48. reg [9:0]s2v4=0;
  49. reg [9:0]s2v5=0;
  50. reg [9:0]s2v6=0;
  51.  
  52. reg [9:0]s3v1=0;
  53. reg [9:0]s3v2=0;
  54. reg [9:0]s3v3=0;
  55. reg [9:0]s3v4=0;
  56. reg [9:0]s3v5=0;
  57. reg [9:0]s3v6=0;
  58.  
  59. reg [11:0]s1avg=0;
  60. reg [11:0]s2avg=0;
  61. reg [11:0]s3avg=0;*/
  62.  
  63. always @(posedge clk_50) begin   //Scaling down of 50MHz clock to 1 MHz
  64.  
  65.     if(reset == 0)
  66.      begin
  67.         r_COUNT <= 0;
  68.      end
  69.  
  70.    if (r_COUNT == 49 )begin             //Check if count reaches 49
  71.       r_COUNT <= 0;                     //Reset counter if it reaches 49
  72.    end else begin
  73.       r_COUNT <= r_COUNT + 1;           //increment counter by 1 till 49
  74.    end
  75.     clk_1 <= (r_COUNT < 25);
  76. end
  77.  
  78. always @(posedge clk_1) begin    
  79.    
  80.    
  81.     if(reset == 0)
  82.      begin
  83.         nodecount <=-4'd1 ;         //Initially node count set to -1
  84.      end
  85.    
  86.      led1 <= (s1>thr);              //Led lights up when s1 has a greater value than 2000
  87.      led2 <= (s2>thr);              //Led lights up when s2 has a greater value than 2000
  88.      led3 <= (s3>thr);              //Led lights up when s3 has a greater value than 2000
  89.  
  90.     error = (s1>thr) - (s3>thr);    //Relative error between sensors s1 and s3: values range from -1 to 1  P
  91.  
  92.     cumulative_error <= cumulative_error + error;  //Adds up the error to give a cumulative error  I
  93.      
  94.     if (cumulative_error > 10)      //Condition to reset the value of cumulative error to 10 if it crosses 10  
  95.     begin
  96.         cumulative_error <= 10;
  97.     end
  98.  
  99.      if (cumulative_error < -10)    //Condition to reset the value of cumulative error to -10 if it crosses -10  
  100.     begin
  101.         cumulative_error <= -10;
  102.     end
  103.      
  104.      if (s1<thr && s2>thr && s3<thr) //Cumulative error resets to zero when the bot is on line i.e WBW
  105.      begin
  106.             cumulative_error <= 0;  
  107.      end
  108.      
  109.      difference <= error-preverror;   //forms the differential part D
  110.      correction <= ((10*error) + cumulative_error + (2*difference)); // kp = 10, ki = 1, kd =2
  111.      
  112.       preverror <= error;  // Stores value of current error to previous error so that it can be used in next loop cycle
  113.      
  114.      
  115.      ml1 <= odc - correction;  // PID tuning for motor 1
  116.      ml2 <= odc + correction;  // PID tuning for motor 2
  117.  
  118.      if (ml1>70)      //Resetting value of ml1 to 70 if it crosses 70
  119.      begin
  120.          ml1 <= 70;
  121.      end
  122.      if (ml2>70)      //Resetting value of ml2 to 70 if it crosses 70
  123.      begin
  124.          ml2 <= 70;
  125.      end
  126.      if (ml1<30)       //Resetting value of ml1 to 30 if it becomes less than 30
  127.      begin
  128.          ml1 <= 30;
  129.      end
  130.      if (ml2<30)       //Resetting value of ml2 to 30 if it becomes less than 30
  131.      begin
  132.          ml2 <= 30;
  133.      end
  134.      
  135.      
  136.     /*  s1v1 <= s1v2;
  137.       s1v2 <= s1v3;
  138.       s1v3 <= s1v4;
  139.       s1v4 <= s1v5;
  140.       s1v5 <= s1v6;
  141.       s1v6 <= s1[11:2];
  142.       s1avg <= s1v1 + s1v2 + s1v3 + s1v4 + s1v5 + s1v6;
  143.      
  144.       s2v1 <= s2v2;
  145.       s2v2 <= s2v3;
  146.       s2v3 <= s2v4;
  147.       s2v4 <= s2v5;
  148.       s2v5 <= s2v6;
  149.       s2v6 <= s2[11:2];
  150.       s2avg <= s2v1 + s2v2 + s2v3 + s2v4 + s2v5 + s2v6;
  151.      
  152.       s3v1 <= s3v2;
  153.       s3v2 <= s3v3;
  154.       s3v3 <= s3v4;
  155.       s3v4 <= s3v5;
  156.       s3v5 <= s3v6;
  157.       s3v6 <= s3[11:2];
  158.       s3avg <= s3v1 + s3v2 + s3v3 + s3v4 + s3v5 + s3v6; */
  159.      
  160.     /*if(s1>thr && s2>thr && s3>thr)
  161.     begin
  162.         flag = 1;
  163.         mo1 <= 0;
  164.         mo2 <= 63;
  165.     end*/
  166.     /*if(s1>thr && s2<thr && s3<thr && flag == 1)  //end turning
  167.     begin
  168.         flag <= 0;
  169.         mo1 <= odc - correction;
  170.       mo2 <= odc + correction;
  171.     end
  172.     /*if(s1<thr && s2>thr && s3<thr && flag == 1)
  173.     begin
  174.         flag <= 0;
  175.         mo1 <= odc - correction;
  176.       mo2 <= odc + correction;
  177.     end*/
  178.     if (s1 < thr && s2 > thr && s3 < thr) //ready for next node, flag resets to zero on line : WBW
  179.     begin
  180.         flag <= 0;
  181.     end
  182.      /*else begin
  183.         flag <= 1;
  184.     end*/
  185.  
  186.     // Only detect node once.
  187.     if (s1>thr && s2>thr && s3>thr && flag == 0)  //detect node
  188.     begin
  189.         if (nodecount == 7)        // Nodecount resets to zero if it crosses 7
  190.         begin
  191.             nodecount <= 0;
  192.             flag <= 1;
  193.             loopcount = loopcount + 1;            //flag set to 1 when it detects a node
  194.         end else
  195.         begin
  196.             nodecount <= nodecount + 1;
  197.          flag <= 1;
  198.         end
  199.     end
  200.        
  201.     /*if (s1>thr && s2>thr && s3>thr)
  202.         begin
  203.             if(flag == 3)
  204.             begin
  205.                 if (nodecount == 7)
  206.                 begin
  207.                     nodecount <= 0;
  208.                     flag <= 0;
  209.                     flag2 <= 1;
  210.                 end else
  211.                 begin
  212.                     nodecount <= nodecount + 1;
  213.                     flag <=0;
  214.                     flag2 <= 1;
  215.                 end
  216.             end else
  217.             begin
  218.                 flag <= flag + 1;
  219.             end
  220.         end else
  221.             begin
  222.                 flag <= 0;
  223.             end*/
  224.            
  225.     if(flag == 1)    //Detection of node and applying pwm accordingly
  226.     begin
  227.     case (nodecount)
  228.     0: begin            
  229.         mn1 <= odc;
  230.       mn2 <= odc;
  231.         end
  232.     1: begin
  233.         mn1 <= 5;
  234.         mn2 <= 60;
  235.         end
  236.     2: begin
  237.         mn1 <= odc;
  238.       mn2 <= odc;
  239.         end
  240.     3: begin
  241.         mn1 <= 5;
  242.         mn2 <= 60;
  243.         end
  244.     4: begin
  245.         mn1 <= odc;
  246.       mn2 <= odc;
  247.         end
  248.     5: begin
  249.         mn1 <= odc;
  250.       mn2 <= odc;
  251.         end
  252.     6: begin
  253.         mn1 <= 5;
  254.         mn2 <= 60;
  255.         end
  256.     7: begin
  257.         if (loopcount == 2)
  258.         begin
  259.             mn1 <=0;
  260.         mn2 <= 0;
  261.         loopcount = 1;
  262.         end
  263.         else
  264.         begin
  265.             mn1 <= odc;
  266.             mn2 <= odc;
  267.         end
  268.         end
  269.     endcase
  270.     /*
  271.         mn1 <= 5;
  272.         mn2 <= 60;*/
  273.     end
  274.    
  275.     if(flag == 0)    //assigning values of ml1 and ml2 (line condition) to mo1 and mo2 respectively
  276.     begin
  277.         mo1 <= ml1;
  278.         mo2 <= ml2;
  279.     end else
  280.     begin            //assigning values of mn1 and mn2 (node condition) to mo1 and mo2 respectively
  281.         mo1 <= mn1;
  282.         mo2 <= mn2;
  283.     end
  284. end
  285.        
  286. assign Led1 = led1;
  287. assign Led2 = led2;
  288. assign Led3 = led3;
  289.        
  290.  
  291. assign m1 = mo1;
  292. assign m2 = mo2;
  293.  
  294. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement