Advertisement
Sidsh

Documented LSA

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