Advertisement
Sidsh

Path Traversal+Deposition SM

Feb 28th, 2022
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VeriLog 11.46 KB | None | 0 0
  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][11: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] <= 2;
  166.     path[1][1] <= 0;
  167.     path[1][2] <= 0;
  168.     path[1][3] <= 0;
  169.     path[1][4] <= 1;
  170.     path[1][5] <= 0;
  171.     path[1][6] <= 0;
  172.     path[1][7] <= 1;
  173.     path[1][8] <= 0;
  174.     path[1][9] <= 2;
  175.     path[1][10] <= 2;
  176.  
  177.     path[2][0] <= 1;
  178.     path[2][1] <= 0;
  179.     path[2][2] <= 1;
  180.     path[2][3] <= 1;
  181.     path[2][4] <= 0;
  182.     path[2][5] <= 1;
  183.     path[2][6] <= 0;
  184.     path[2][7] <= 2;
  185.    
  186.     path[3][0] <= 1;
  187.     path[3][1] <= 0;
  188.     path[3][2] <= 1;
  189.     path[3][3] <= 0;
  190.     path[3][4] <= 1;
  191.     path[3][5] <= 0;
  192.     path[3][6] <= 1;
  193.     path[3][7] <= 0;
  194.     path[3][8] <= 2;
  195.     path[3][9] <= 2;
  196.     path[3][10] <= 0;
  197.     path[3][11] <= 0;  
  198.    
  199.     path_len[0] <= 10;
  200.     path_len[1] <= 10;
  201.     path_len[2] <= 7;
  202.     path_len[3] <= 11;
  203.    
  204. end;
  205.  
  206. reg [1:0]DepPath[8:0][10:0];
  207. reg [2:0]DepPath_len[8:0];
  208. initial
  209. begin
  210.  
  211.     DepPath [0][0] <= 2;
  212.     DepPath [0][1] <= 0;
  213.     DepPath [0][2] <= 1;
  214.     DepPath [0][3] <= 1;
  215.     DepPath [0][4] <= 3;
  216.     DepPath [0][5] <= 2;
  217.     DepPath [0][6] <= 2;
  218.     DepPath [0][7] <= 0;
  219.     DepPath [0][8] <= 1;
  220.     //for DZP2
  221.     DepPath [1][0] <= 2;
  222.     DepPath [1][1] <= 1;
  223.     DepPath [1][2] <= 3;
  224.     DepPath [1][3] <= 2;
  225.     DepPath [1][4] <= 1;
  226.     //for DZM1
  227.     DepPath [2][0] <= 1;
  228.     DepPath [2][1] <= 1;
  229.     DepPath [2][2] <= 2;
  230.     DepPath [2][3] <= 3;
  231.     DepPath [2][4] <= 1;
  232.     DepPath [2][5] <= 2;
  233.     DepPath [2][6] <= 2;
  234.     //for DZM2
  235.     DepPath [3][0] <= 1;
  236.     DepPath [3][1] <= 1;
  237.     DepPath [3][2] <= 0;
  238.     DepPath [3][3] <= 2;
  239.     DepPath [3][4] <= 2;
  240.     DepPath [3][5] <= 3;
  241.     DepPath [3][6] <= 1;
  242.     DepPath [3][7] <= 0;
  243.     DepPath [3][8] <= 1; //This shall place the bot at start
  244.     //for DZM3
  245.     DepPath [4][0] <= 1;
  246.     DepPath [4][1] <= 0;
  247.     DepPath [4][2] <= 0;
  248.     DepPath [4][3] <= 0;
  249.     DepPath [4][4] <= 1;
  250.     DepPath [4][5] <= 1;
  251.     DepPath [4][6] <= 3;
  252.     DepPath [4][7] <= 1;
  253.     DepPath [4][8] <= 0;
  254.     DepPath [4][9] <= 0;
  255.     DepPath [4][10] <= 1; //This shall place the bot at start
  256.     //for DZV2
  257.     DepPath [5][0] <= 1;
  258.     DepPath [5][1] <= 2;
  259.     DepPath [5][2] <= 3;
  260.     DepPath [5][3] <= 1;
  261.     DepPath [5][4] <= 2;
  262.     //for DZV1
  263.     DepPath [6][0] <= 0;
  264.     DepPath [6][1] <= 1;
  265.     DepPath [6][2] <= 3;
  266.     DepPath [6][3] <= 2;
  267.     DepPath [6][4] <= 0;
  268.     //for DZN1
  269.     DepPath [7][0] <= 1;
  270.     DepPath [7][1] <= 0;
  271.     DepPath [7][2] <= 2;
  272.     DepPath [7][3] <= 1;
  273.     DepPath [7][4] <= 3;
  274.     DepPath [7][5] <= 2;
  275.     DepPath [7][6] <= 1;
  276.     DepPath [7][7] <= 0;
  277.     DepPath [7][8] <= 2;
  278.     //for DZN2
  279.     DepPath [8][0] <= 1;
  280.     DepPath [8][1] <= 0;
  281.     DepPath [8][2] <= 0;
  282.     DepPath [8][3] <= 2;
  283.     DepPath [8][4] <= 3;
  284.     DepPath [8][5] <= 1;
  285.     DepPath [8][6] <= 0;
  286.     DepPath [8][7] <= 0;
  287.     DepPath [8][8] <= 2;
  288.    
  289.     DepPath_len [0] <= 8;
  290.     DepPath_len [1] <= 4;
  291.     DepPath_len [2] <= 6;
  292.     DepPath_len [3] <= 8;
  293.     DepPath_len [4] <= 10;
  294.     DepPath_len [5] <= 4;
  295.     DepPath_len [6] <= 4;
  296.     DepPath_len [7] <= 8;
  297.     DepPath_len [8] <= 8;
  298.    
  299. end;
  300.    
  301.  
  302. function automatic pwm;
  303.     input [1:0]n;
  304.     begin
  305.         case(n)
  306.         0:begin
  307.             mn1 = odc;
  308.             mn1r=0;
  309.             mn2 = odc;
  310.             mn2r=0;
  311.           end
  312.         1:begin
  313.             mn1 = 5;
  314.             mn1r=0;
  315.             mn2 = 56;
  316.             mn2r=0;
  317.           end
  318.         2:begin
  319.             mn1 = 58;
  320.             mn1r=0;
  321.             mn2 = 5;
  322.             mn2r=0;
  323.           end
  324.         3:begin
  325.             mn1=0;
  326.             mn1r=odc;
  327.             mn2=odc;
  328.             mn2r=0;
  329.           end
  330.         endcase
  331.         pwm = 1;
  332.      end
  333.     endfunction
  334.  
  335. always @(posedge clk_1) begin    
  336.    
  337.    
  338.     if(reset == 0)
  339.      begin
  340.         nodecount <=-4'd1 ;         //Initially node count set to -1
  341.      end
  342.    
  343.      led1 <= (s1);              //Led lights up when s1 has a greater value than 2000
  344.      led2 <= (s2);              //Led lights up when s2 has a greater value than 2000
  345.      led3 <= (s3);              //Led lights up when s3 has a greater value than 2000
  346.  
  347.     error <= (s1) - (s3);    //Relative error between sensors s1 and s3: values range from -1 to 1  P
  348.  
  349.     cumulative_error <= cumulative_error + error;  //Adds up the error to give a cumulative error  I
  350.      
  351.     if (cumulative_error > 10)      //Condition to reset the value of cumulative error to 10 if it crosses 10  
  352.     begin
  353.         cumulative_error <= 10;
  354.     end
  355.  
  356.      if (cumulative_error < -10)    //Condition to reset the value of cumulative error to -10 if it crosses -10  
  357.     begin
  358.         cumulative_error <= -10;
  359.     end
  360.      
  361.      if ((s1==0) && s2 && (s3==0)) //Cumulative error resets to zero when the bot is on line i.e WBW
  362.      begin
  363.             cumulative_error <= 0;  
  364.             hl <=0;
  365.             id1 <= 0;
  366.      end
  367.      
  368.      difference <= error-preverror;   //forms the differential part D
  369.      correction <= ((10*error) + cumulative_error + (2*difference)); // kp = 10, ki = 1, kd =2
  370.      
  371.       preverror <= error;  // Stores value of current error to previous error so that it can be used in next loop cycle
  372.      
  373.      
  374.      ml1 <= odc - correction;  // PID tuning for motor 1
  375.      ml2 <= odc + correction;  // PID tuning for motor 2
  376.  
  377.      if (ml1>70)      //Resetting value of ml1 to 70 if it crosses 70
  378.      begin
  379.          ml1 <= 70;
  380.      end
  381.      if (ml2>70)      //Resetting value of ml2 to 70 if it crosses 70
  382.      begin
  383.          ml2 <= 70;
  384.      end
  385.      if (ml1<30)       //Resetting value of ml1 to 30 if it becomes less than 30
  386.      begin
  387.          ml1 <= 30;
  388.      end
  389.      if (ml2<30)       //Resetting value of ml2 to 30 if it becomes less than 30
  390.      begin
  391.          ml2 <= 30;
  392.      end
  393.    
  394.     if ((s1==0) && s2 && (s3==0)) //ready for next node, flag resets to zero on line : WBW
  395.     begin
  396.         flag <= 0;
  397.           ml1r <= 0;
  398.           ml2r <= 0;
  399.     end
  400.     /*if ((s1==0) && (s2==0) && (s3==0)&& (flag ==0))
  401.     begin
  402.         flag <= 0;
  403.         n=pwm(3);
  404.         ml1 <=0;
  405.         ml1r<=odc;
  406.         ml2 <=0;
  407.         ml2r<=odc;
  408.     end*/
  409.    
  410.     if (s1 && s2 && s3 && flag == 0)  //detect node
  411.     begin
  412.             id1 <= 4;
  413.             hl <= 1;
  414.             nodecount <= nodecount + 1;
  415.          flag <= 1;
  416.             i <= i+2;
  417.             j <= j+2;
  418.     end
  419.    
  420.     if(flag == 1)    //Detection of node and applying pwm accordingly
  421.     begin
  422.     //n<=pwm(path[nodecount]);
  423.     if(flag1==0)
  424.     begin
  425.         n<=pwm(path[path_no][i:j]);
  426.         if(nodecount==path_len[path_no])
  427.         begin
  428.             path_no <= path_no + 1;
  429.             nodecount <= -5'd1;
  430.             if(clrsN > 0)
  431.             begin
  432.                 flag1 <= 1;
  433.             end
  434.         end
  435.     end else
  436.     if(flag1 == 1)
  437.     begin
  438.         if(clrs[clrsN] == 1)
  439.         begin
  440.         n<=pwm(clrPath[0+rcount][nodecount]);
  441.             if(nodecount==clrPathlen[0+rcount])
  442.             begin
  443.                 flag1 <= 2;
  444.                 nodecount <= -5'd1;
  445.                 rcount <= rcount + 1;
  446.             end
  447.         end
  448.     end else
  449.     if(
  450.        
  451.    
  452.     /*if(path_no == && nodecount == )      //enable clr module only at patches
  453.     begin
  454.         clr_en <= 0;
  455.     end else
  456.     begin
  457.         clr_en <= 1;
  458.     end*/
  459.    
  460.     if(clr_en == 1 && Clr != 0)
  461.     begin
  462.         clrsN <= clrsN +1;
  463.         clrs[clrsN] <= Clr;
  464.     end
  465.    
  466.     case (nodecount)
  467.     0: begin
  468.         n<=pwm(0);
  469.         end
  470.     1: begin            
  471.         n<=pwm(0);
  472.         end
  473.     2: begin
  474.         n<=pwm(0);
  475.         end
  476.     3: begin
  477.         n<=pwm(0);
  478.         end
  479.     4: begin
  480.         n<=pwm(1);
  481.         end
  482.     5: begin
  483.         n<=pwm(0);
  484.         end
  485.     6: begin
  486.         n<=pwm(2);
  487.         end
  488.     7: begin
  489.         n<=pwm(0);
  490.         end
  491.     8: begin
  492.         n<=pwm(2);
  493.         end
  494.     9: begin
  495.         n<=pwm(2);
  496.         end
  497.     10:begin
  498.         n<=pwm(0);
  499.         end
  500.     11:begin
  501.         n<=pwm(0);
  502.         end
  503.     12:begin
  504.         n<=pwm(0);
  505.         end
  506.     13:begin
  507.         n<=pwm(2);
  508.         end
  509.     14:begin
  510.         en <= 1;
  511.         n<=pwm(3);
  512.         id1 <= 5;
  513.         hl <= 1;
  514.         end
  515.     15:begin
  516.         n<=pwm(1);
  517.         end
  518.     16:begin
  519.         n<=pwm(0);
  520.         end
  521.     17:begin
  522.         n<=pwm(1);
  523.         end
  524.     18:begin
  525.         n<=pwm(0);
  526.         end
  527.     19:begin
  528.         n<=pwm(2);
  529.         end
  530.     20:begin
  531.         n<=pwm(1);
  532.         end
  533.     21:begin
  534.         en <= 0;
  535.         n<=pwm(3);
  536.         id1 <= 6;
  537.         hl <= 1;
  538.         end
  539.     endcase
  540.     end
  541.    
  542.     if(flag == 0)    //assigning values of ml1 and ml2 (line condition) to mo1 and mo2 respectively
  543.     begin
  544.         mo1 <= ml1;
  545.         mo1r<= ml1r;
  546.         mo2 <= ml2;
  547.         mo2r<= ml2r;
  548.     end else
  549.     begin            //assigning values of mn1 and mn2 (node condition) to mo1 and mo2 respectively
  550.         mo1 <= mn1;
  551.         mo1r<= mn1r;
  552.         mo2 <= mn2;
  553.         mo2r<= mn2r;
  554.     end
  555. end
  556.        
  557. assign Led1 = led1;
  558. assign Led2 = led2;
  559. assign Led3 = led3;
  560.  
  561. assign id = id1;
  562. assign HL1 = hl;
  563. assign m1 = mo1;
  564. assign m1b= mo1r;
  565. assign m2 = mo2;
  566. assign m2b= mo2r;
  567.  
  568. assign EM = en;
  569. assign Clr_en = flag;
  570. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement