Advertisement
Sidsh

Stop logic LSA

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