Advertisement
Sidsh

Hardcoded LSA

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