Advertisement
Sidsh

Path Traversal SM

Feb 27th, 2022
199
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]m1b,
  4.     output [7:0]m2,  //motor2                                            PIN_C3
  5.      output [7:0]m2b,
  6.     input s1,  //12-bit output of ch. 5 (parallel)
  7.     input s2,  //12-bit output of ch. 6 (parallel)
  8.     input s3,  //12-bit output of ch. 7 (parallel)
  9.     input clk_1,    //50 MHz clock
  10.     input reset,
  11.      input [2:0]Clr,
  12.      output Led1,    //Led used to indicate position of bot i.e. node or line
  13.      output Led2,
  14.      output Led3,
  15.      output HL1,        // to control uart
  16.      output [2:0] id,
  17.      output EM,
  18.      output Clr_en
  19.     );
  20.  
  21. reg signed[7:0]error = 0;
  22. reg signed[7:0]difference = 0;
  23. reg signed[7:0]correction = 0;
  24. reg signed[7:0]cumulative_error = 0;
  25. reg signed[7:0]preverror = 0;
  26. reg [4:0]nodecount = -5'd1;     //No. of nodes bot has traversed, initially set to -1
  27. reg [4:0]i = -5'd1;
  28. reg [4:0]j = -5'd2;
  29. reg flag = 0;  
  30.  
  31. reg led1=0;
  32. reg led2=0;
  33. reg led3=0;
  34.  
  35. reg [7:0]odc =56;     //optimum duty cycle
  36. reg [7:0]mo1 = 50;    // pwm to motor1, initially set to 50
  37. reg [7:0]mo1r= 0;
  38. reg [7:0]mo2 = 50;    // pwm to motor2, initially set to 50
  39. reg [7:0]mo2r= 0;
  40. reg [7:0]ml1 = 50;    // pwm to motor1 when it is on line
  41. reg [7:0]ml1r=0;
  42. reg [7:0]ml2 = 50;    // pwm to motor2 when it is on line
  43. reg [7:0]ml2r=0;
  44. reg [7:0]mn1 = 50;    // pwm to motor1 when it is on node
  45. reg [7:0]mn1r= 0;
  46. reg [7:0]mn2 = 50;    // pwm to motor2 when it is on node
  47. reg [7:0]mn2r= 0;
  48.  
  49. reg [2:0] id1 =0;        //to control uart
  50. reg hl=0;
  51. reg n=0;
  52.  
  53. reg en=0;
  54. reg [1:0]flag1=0; //0: arena, 1: pick, 2:place
  55. reg [2:0]path_no = 0;  //no. of path
  56. reg [3:0]path_len[5:0]; //length of each path
  57.  
  58.  
  59. reg [1:0]path[36:0];
  60. initial begin
  61. path[0]<= 0;
  62. path[1]<= 0;
  63. path[2]<= 0;
  64. path[3]<= 0;
  65. path[4]<= 1;
  66. path[5]<= 0;
  67. path[6]<= 2;
  68. path[7]<= 0;
  69. end
  70.  
  71. reg clr_en = 1;  //disable clr detection
  72. reg [1:0]clrsN=0;
  73. reg [1:0]clrs[2:0];
  74.  
  75. reg [1:0]rcount=0;     //count no. of red color patches
  76. reg [1:0]gcount=0;     //count no. of green color patches
  77. reg [1:0]bcount=0;     //count no. of blue color patches
  78.  
  79. reg [1:0]clrPath[8:0][9:0];   //enter 9 S paths in sequence RGB
  80. reg [2:0]clrPathlen[8:0]; //enter 9 S paths length in sequence RGB
  81. initial
  82. begin
  83.     //configuration RRRGGGBNN
  84.     clrPath[0][0] <= 0;
  85.     clrPath[0][1] <= 1;
  86.     clrPath[0][2] <= 3;
  87.     clrPath[0][3] <= 2;
  88.     clrPath[0][4] <= 0;
  89.    
  90.     clrPath[1][0] <= 0;
  91.     clrPath[1][1] <= 0;
  92.     clrPath[1][2] <= 1;
  93.     clrPath[1][3] <= 3;
  94.     clrPath[1][4] <= 2;
  95.     clrPath[1][5] <= 0;
  96.     clrPath[1][6] <= 0;
  97.    
  98.     clrPath[2][0] <= 0;
  99.     clrPath[2][1] <= 0;
  100.     clrPath[2][2] <= 0
  101.     clrPath[2][3] <= 1;
  102.     clrPath[2][4] <= 3;
  103.     clrPath[2][5] <= 2;
  104.     clrPath[2][6] <= 0;
  105.     clrPath[2][7] <= 0;
  106.     clrPath[2][8] <= 0;
  107.    
  108.     clrPath[3][0] <= 0;
  109.     clrPath[3][1] <= 0;
  110.     clrPath[3][2] <= 0;
  111.     clrPath[3][3] <= 2;
  112.     clrPath[3][4] <= 3;
  113.     clrPath[3][5] <= 1;
  114.     clrPath[3][6] <= 0;
  115.     clrPath[3][7] <= 0;
  116.     clrPath[3][8] <= 0;
  117.    
  118.     clrPath[4][0] <= 0;
  119.     clrPath[4][1] <= 0;
  120.     clrPath[4][2] <= 2;
  121.     clrPath[4][3] <= 3;
  122.     clrPath[4][4] <= 1;
  123.     clrPath[4][5] <= 0;
  124.     clrPath[4][6] <= 0;
  125.    
  126.     clrPath[5][0] <= 0;
  127.     clrPath[5][1] <= 2;
  128.     clrPath[5][2] <= 3;
  129.     clrPath[5][3] <= 1;
  130.     clrPath[5][4] <= 0;
  131.    
  132.     clrPath[6][0] <= 2;
  133.     clrPath[6][1] <= 3;
  134.     clrPath[6][2] <= 1;
  135.    
  136.     clrPathlen[0] <= 4;
  137.     clrPathlen[1] <= 6;
  138.     clrPathlen[2] <= 8;
  139.     clrPathlen[3] <= 8;
  140.     clrPathlen[4] <= 6;
  141.     clrPathlen[5] <= 4;
  142.     clrPathlen[6] <= 2;
  143.     clrPathlen[7] <= 0;
  144.     clrPathlen[8] <= 0;
  145.  
  146. end
  147.  
  148. reg [1:0]path[3:0][10:0];
  149. reg [2:0]path_len[3:0];
  150. initial
  151. begin
  152.    
  153.     path[0][0] <= 0;
  154.     path[0][1] <= 0;
  155.     path[0][2] <= 0;
  156.     path[0][3] <= 0;
  157.     path[0][4] <= 2;
  158.     path[0][5] <= 0;
  159.     path[0][6] <= 1;
  160.     path[0][7] <= 0;
  161.     path[0][8] <= 1;
  162.     path[0][9] <= 0;
  163.     path[0][10] <= 0;
  164.    
  165.     path[1][0] <= 0;
  166.     path[1][1] <= 0;
  167.     path[1][2] <= 0;
  168.     path[1][3] <= 1;
  169.     path[1][4] <= 0;
  170.     path[1][5] <= 0;
  171.     path[1][6] <= 1;
  172.     path[1][7] <= 0;
  173.     path[1][8] <= 2;
  174.     path[1][9] <= 2;
  175.  
  176.     path[2][0] <= 0;
  177.     path[2][1] <= 1;
  178.     path[2][2] <= 1;
  179.     path[2][3] <= 0;
  180.     path[2][4] <= 1;
  181.     path[2][5] <= 0;
  182.    
  183.     path[2][6] <= 2;
  184.     path[3][0] <= 0;
  185.     path[3][1] <= 1;
  186.     path[3][2] <= 0;
  187.     path[3][3] <= 1;
  188.     path[3][4] <= 0;
  189.     path[3][5] <= 1;
  190.     path[3][6] <= 0;
  191.     path[3][7] <= 2;
  192.     path[3][8] <= 2;
  193.     path[3][9] <= 0;
  194.     path[3][10] <= 0;  
  195.    
  196.     path_len[0] <= 10;
  197.     path_len[1] <= 9;
  198.     path_len[2] <= 5;
  199.     path_len[3] <= 10;
  200.    
  201. end;
  202.  
  203. function automatic pwm;
  204.     input [1:0]n;
  205.     begin
  206.         case(n)
  207.         0:begin
  208.             mn1 = odc;
  209.             mn1r=0;
  210.             mn2 = odc;
  211.             mn2r=0;
  212.           end
  213.         1:begin
  214.             mn1 = 5;
  215.             mn1r=0;
  216.             mn2 = 56;
  217.             mn2r=0;
  218.           end
  219.         2:begin
  220.             mn1 = 58;
  221.             mn1r=0;
  222.             mn2 = 5;
  223.             mn2r=0;
  224.           end
  225.         3:begin
  226.             mn1=0;
  227.             mn1r=odc;
  228.             mn2=odc;
  229.             mn2r=0;
  230.           end
  231.         endcase
  232.         pwm = 1;
  233.      end
  234.     endfunction
  235.  
  236. always @(posedge clk_1) begin    
  237.    
  238.    
  239.     if(reset == 0)
  240.      begin
  241.         nodecount <=-4'd1 ;         //Initially node count set to -1
  242.      end
  243.    
  244.      led1 <= (s1);              //Led lights up when s1 has a greater value than 2000
  245.      led2 <= (s2);              //Led lights up when s2 has a greater value than 2000
  246.      led3 <= (s3);              //Led lights up when s3 has a greater value than 2000
  247.  
  248.     error <= (s1) - (s3);    //Relative error between sensors s1 and s3: values range from -1 to 1  P
  249.  
  250.     cumulative_error <= cumulative_error + error;  //Adds up the error to give a cumulative error  I
  251.      
  252.     if (cumulative_error > 10)      //Condition to reset the value of cumulative error to 10 if it crosses 10  
  253.     begin
  254.         cumulative_error <= 10;
  255.     end
  256.  
  257.      if (cumulative_error < -10)    //Condition to reset the value of cumulative error to -10 if it crosses -10  
  258.     begin
  259.         cumulative_error <= -10;
  260.     end
  261.      
  262.      if ((s1==0) && s2 && (s3==0)) //Cumulative error resets to zero when the bot is on line i.e WBW
  263.      begin
  264.             cumulative_error <= 0;  
  265.             hl <=0;
  266.             id1 <= 0;
  267.      end
  268.      
  269.      difference <= error-preverror;   //forms the differential part D
  270.      correction <= ((10*error) + cumulative_error + (2*difference)); // kp = 10, ki = 1, kd =2
  271.      
  272.       preverror <= error;  // Stores value of current error to previous error so that it can be used in next loop cycle
  273.      
  274.      
  275.      ml1 <= odc - correction;  // PID tuning for motor 1
  276.      ml2 <= odc + correction;  // PID tuning for motor 2
  277.  
  278.      if (ml1>70)      //Resetting value of ml1 to 70 if it crosses 70
  279.      begin
  280.          ml1 <= 70;
  281.      end
  282.      if (ml2>70)      //Resetting value of ml2 to 70 if it crosses 70
  283.      begin
  284.          ml2 <= 70;
  285.      end
  286.      if (ml1<30)       //Resetting value of ml1 to 30 if it becomes less than 30
  287.      begin
  288.          ml1 <= 30;
  289.      end
  290.      if (ml2<30)       //Resetting value of ml2 to 30 if it becomes less than 30
  291.      begin
  292.          ml2 <= 30;
  293.      end
  294.    
  295.     if ((s1==0) && s2 && (s3==0)) //ready for next node, flag resets to zero on line : WBW
  296.     begin
  297.         flag <= 0;
  298.           ml1r <= 0;
  299.           ml2r <= 0;
  300.     end
  301.     /*if ((s1==0) && (s2==0) && (s3==0)&& (flag ==0))
  302.     begin
  303.         flag <= 0;
  304.         n=pwm(3);
  305.         ml1 <=0;
  306.         ml1r<=odc;
  307.         ml2 <=0;
  308.         ml2r<=odc;
  309.     end*/
  310.    
  311.     if (s1 && s2 && s3 && flag == 0)  //detect node
  312.     begin
  313.             id1 <= 4;
  314.             hl <= 1;
  315.             nodecount <= nodecount + 1;
  316.          flag <= 1;
  317.             i <= i+2;
  318.             j <= j+2;
  319.     end
  320.    
  321.     if(flag == 1)    //Detection of node and applying pwm accordingly
  322.     begin
  323.     //n<=pwm(path[nodecount]);
  324.     if(flag1==0)
  325.     begin
  326.         n<=pwm(path[path_no][i:j]);
  327.         if(nodecount==path_len[path_no])
  328.         begin
  329.             path_no <= path_no + 1;
  330.             nodecount <= -5'd1;
  331.             if(clrsN > 0)
  332.             begin
  333.                 flag1 <= 1;
  334.             end
  335.         end
  336.     end else
  337.     if(flag1 == 1)
  338.     begin
  339.         if(clrs[clrsN] == 1)
  340.         begin
  341.         n<=pwm(clrPath[0+rcount][nodecount]);
  342.             if(nodecount==clrPathlen[0+rcount])
  343.             begin
  344.                 flag1 <= 2;
  345.                 nodecount <= -5'd1;
  346.                 rcount <= rcount + 1;
  347.             end
  348.         end
  349.     end else
  350.     if(
  351.        
  352.    
  353.     /*if(path_no == && nodecount == )      //enable clr module only at patches
  354.     begin
  355.         clr_en <= 0;
  356.     end else
  357.     begin
  358.         clr_en <= 1;
  359.     end*/
  360.    
  361.     if(clr_en == 1 && Clr != 0)
  362.     begin
  363.         clrsN <= clrsN +1;
  364.         clrs[clrsN] <= Clr;
  365.     end
  366.    
  367.     case (nodecount)
  368.     0: begin
  369.         n<=pwm(0);
  370.         end
  371.     1: begin            
  372.         n<=pwm(0);
  373.         end
  374.     2: begin
  375.         n<=pwm(0);
  376.         end
  377.     3: begin
  378.         n<=pwm(0);
  379.         end
  380.     4: begin
  381.         n<=pwm(1);
  382.         end
  383.     5: begin
  384.         n<=pwm(0);
  385.         end
  386.     6: begin
  387.         n<=pwm(2);
  388.         end
  389.     7: begin
  390.         n<=pwm(0);
  391.         end
  392.     8: begin
  393.         n<=pwm(2);
  394.         end
  395.     9: begin
  396.         n<=pwm(2);
  397.         end
  398.     10:begin
  399.         n<=pwm(0);
  400.         end
  401.     11:begin
  402.         n<=pwm(0);
  403.         end
  404.     12:begin
  405.         n<=pwm(0);
  406.         end
  407.     13:begin
  408.         n<=pwm(2);
  409.         end
  410.     14:begin
  411.         en <= 1;
  412.         n<=pwm(3);
  413.         id1 <= 5;
  414.         hl <= 1;
  415.         end
  416.     15:begin
  417.         n<=pwm(1);
  418.         end
  419.     16:begin
  420.         n<=pwm(0);
  421.         end
  422.     17:begin
  423.         n<=pwm(1);
  424.         end
  425.     18:begin
  426.         n<=pwm(0);
  427.         end
  428.     19:begin
  429.         n<=pwm(2);
  430.         end
  431.     20:begin
  432.         n<=pwm(1);
  433.         end
  434.     21:begin
  435.         en <= 0;
  436.         n<=pwm(3);
  437.         id1 <= 6;
  438.         hl <= 1;
  439.         end
  440.     endcase
  441.     end
  442.    
  443.     if(flag == 0)    //assigning values of ml1 and ml2 (line condition) to mo1 and mo2 respectively
  444.     begin
  445.         mo1 <= ml1;
  446.         mo1r<= ml1r;
  447.         mo2 <= ml2;
  448.         mo2r<= ml2r;
  449.     end else
  450.     begin            //assigning values of mn1 and mn2 (node condition) to mo1 and mo2 respectively
  451.         mo1 <= mn1;
  452.         mo1r<= mn1r;
  453.         mo2 <= mn2;
  454.         mo2r<= mn2r;
  455.     end
  456. end
  457.        
  458. assign Led1 = led1;
  459. assign Led2 = led2;
  460. assign Led3 = led3;
  461.  
  462. assign id = id1;
  463. assign HL1 = hl;
  464. assign m1 = mo1;
  465. assign m1b= mo1r;
  466. assign m2 = mo2;
  467. assign m2b= mo2r;
  468.  
  469. assign EM = en;
  470. assign Clr_en = flag;
  471. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement