Advertisement
Sidsh

SM Path+Dep 1.2

Mar 1st, 2022
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VeriLog 13.57 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 flag = 0;  
  28.  
  29. reg led1=0;
  30. reg led2=0;
  31. reg led3=0;
  32.  
  33. reg [7:0]odc =58;     //optimum duty cycle
  34. reg [7:0]mo1 = 50;    // pwm to motor1, initially set to 50
  35. reg [7:0]mo1r= 0;
  36. reg [7:0]mo2 = 50;    // pwm to motor2, initially set to 50
  37. reg [7:0]mo2r= 0;
  38. reg [7:0]ml1 = 50;    // pwm to motor1 when it is on line
  39. reg [7:0]ml1r=0;
  40. reg [7:0]ml2 = 50;    // pwm to motor2 when it is on line
  41. reg [7:0]ml2r=0;
  42. reg [7:0]mn1 = 50;    // pwm to motor1 when it is on node
  43. reg [7:0]mn1r= 0;
  44. reg [7:0]mn2 = 50;    // pwm to motor2 when it is on node
  45. reg [7:0]mn2r= 0;
  46.  
  47. reg [2:0] id1 =0;        //to control uart
  48. reg hl=0;
  49. reg n=0;
  50.  
  51. reg en=0;
  52. reg [1:0]state = 0; //0: arena, 1: pick, 2:place
  53. reg [1:0]path_no = 0;  //no. of path
  54. reg [1:0]path[3:0][11:0];
  55. reg [3:0]path_len[3:0]; //length of each path
  56. reg [1:0]clrPath[8:0][9:0];   //enter 9 S paths in sequence RGB
  57. reg [2:0]clrPathlen[8:0]; //enter 9 S paths length in sequence RGB
  58. reg [1:0]destPath[8:0][12:0];
  59. reg [3:0]destPathlen[8:0];
  60. reg [1:0]DepPath[8:0][10:0];
  61. reg [2:0]DepPath_len[8:0];
  62.  
  63.  
  64. reg clr_en = 1;  //disable clr detection
  65. reg [1:0]clrsN=0;
  66. reg [2:0]clrs[0:2];
  67. reg [4:0]dest[2:0];
  68. reg [1:0]rcount=0;     //count no. of red color patches
  69. reg [1:0]gcount=0;     //count no. of green color patches
  70. reg [1:0]bcount=0;     //count no. of blue color patches
  71.  
  72. reg flag2=0;
  73.  
  74. reg done = 0;
  75.     //Arena
  76.  
  77. function automatic pwm;
  78.     input [1:0]n;
  79.     begin
  80.         case(n)
  81.         0:begin
  82.             mn1 = 56;
  83.             mn1r=0;
  84.             mn2 = 56;
  85.             mn2r=0;
  86.           end
  87.         1:begin
  88.             mn1 = 0;
  89.             mn1r=38;
  90.             mn2 = 54;
  91.             mn2r=0;
  92.           end
  93.         2:begin
  94.             mn1 = 54;
  95.             mn1r=0;
  96.             mn2 = 0;
  97.             mn2r=38;
  98.           end
  99.         3:begin
  100.             mn1=0;
  101.             mn1r=odc;
  102.             mn2=odc;
  103.             mn2r=0;
  104.             en = en+1;
  105.           end
  106.         endcase
  107.         pwm = 1;
  108.      end
  109.     endfunction
  110.  
  111. always @(posedge clk_1) begin  
  112. if(done == 0)
  113. begin
  114.     state <=0;
  115.    
  116.     path[0][0] <= 0;
  117.    path[0][1] <= 0;
  118.    path[0][2] <= 0;
  119.    path[0][3] <= 0;
  120.    path[0][4] <= 2;
  121.    path[0][5] <= 0;
  122.    path[0][6] <= 1;
  123.    path[0][7] <= 0;
  124.    path[0][8] <= 1;
  125.    path[0][9] <= 0;
  126.    path[0][10] <= 0;
  127.    
  128.    path[1][0] <= 2;
  129.    path[1][1] <= 0;
  130.    path[1][2] <= 0;
  131.    path[1][3] <= 0;
  132.    path[1][4] <= 1;
  133.    path[1][5] <= 0;
  134.    path[1][6] <= 0;
  135.    path[1][7] <= 1;
  136.    path[1][8] <= 0;
  137.    path[1][9] <= 2;
  138.    path[1][10] <= 2;
  139.  
  140.    path[2][0] <= 1;
  141.    path[2][1] <= 0;
  142.    path[2][2] <= 1;
  143.    path[2][3] <= 1;
  144.     path[2][4] <= 0;
  145.    path[2][5] <= 1;
  146.    path[2][6] <= 0;
  147.    path[2][7] <= 2;
  148.    
  149.    path[3][0] <= 1;
  150.    path[3][1] <= 0;
  151.    path[3][2] <= 1;
  152.    path[3][3] <= 0;
  153.     path[3][4] <= 1;
  154.    path[3][5] <= 0;
  155.    path[3][6] <= 1;
  156.    path[3][7] <= 0;
  157.    path[3][8] <= 2;
  158.    path[3][9] <= 2;
  159.    path[3][10] <= 0;
  160.    path[3][11] <= 0;  
  161.    
  162.    path_len[0] <= 10;
  163.    path_len[1] <= 10;
  164.    path_len[2] <= 7;
  165.    path_len[3] <= 11;
  166.    
  167.    
  168.    
  169.     clrPath[0][0] <= 0;
  170.     clrPath[0][1] <= 1;
  171.     clrPath[0][2] <= 3;
  172.     clrPath[0][3] <= 2;
  173.     clrPath[0][4] <= 0;
  174.  
  175.     clrPath[1][0] <= 0;
  176.     clrPath[1][1] <= 0;
  177.     clrPath[1][2] <= 1;
  178.     clrPath[1][3] <= 3;
  179.     clrPath[1][4] <= 2;
  180.     clrPath[1][5] <= 0;
  181.     clrPath[1][6] <= 0;
  182.  
  183.     clrPath[2][0] <= 0;
  184.     clrPath[2][1] <= 0;
  185.     clrPath[2][2] <= 0;
  186.     clrPath[2][3] <= 1;
  187.     clrPath[2][4] <= 3;
  188.     clrPath[2][5] <= 2;
  189.     clrPath[2][6] <= 0;
  190.     clrPath[2][7] <= 0;
  191.     clrPath[2][8] <= 0;
  192.  
  193.     clrPath[3][0] <= 0;
  194.     clrPath[3][1] <= 0;
  195.     clrPath[3][2] <= 0;
  196.     clrPath[3][3] <= 2;
  197.     clrPath[3][4] <= 3;
  198.     clrPath[3][5] <= 1;
  199.     clrPath[3][6] <= 0;
  200.     clrPath[3][7] <= 0;
  201.     clrPath[3][8] <= 0;
  202.  
  203.     clrPath[4][0] <= 0;
  204.     clrPath[4][1] <= 0;
  205.     clrPath[4][2] <= 2;
  206.     clrPath[4][3] <= 3;
  207.     clrPath[4][4] <= 1;
  208.     clrPath[4][5] <= 0;
  209.     clrPath[4][6] <= 0;
  210.  
  211.     clrPath[5][0] <= 0;
  212.     clrPath[5][1] <= 2;
  213.     clrPath[5][2] <= 3;
  214.     clrPath[5][3] <= 1;
  215.     clrPath[5][4] <= 0;
  216.  
  217.     clrPath[6][1] <= 2;
  218.     clrPath[6][2] <= 3;
  219.     clrPath[6][3] <= 1;
  220.  
  221.     clrPathlen[0] <= 4;
  222.     clrPathlen[1] <= 6;
  223.     clrPathlen[2] <= 8;
  224.     clrPathlen[3] <= 8;
  225.     clrPathlen[4] <= 6;
  226.     clrPathlen[5] <= 4;
  227.     clrPathlen[6] <= 3;
  228.     clrPathlen[7] <= 0;
  229.     clrPathlen[8] <= 0;
  230.    
  231.    
  232.     DepPath [0][0] <= 2;
  233.     DepPath [0][1] <= 0;
  234.     DepPath [0][2] <= 1;
  235.     DepPath [0][3] <= 1;
  236.     DepPath [0][4] <= 3;
  237.     DepPath [0][5] <= 2;
  238.     DepPath [0][6] <= 2;
  239.     DepPath [0][7] <= 0;
  240.     DepPath [0][8] <= 1;
  241.     //for DZP2
  242.     DepPath [1][0] <= 2;
  243.     DepPath [1][1] <= 1;
  244.     DepPath [1][2] <= 3;
  245.     DepPath [1][3] <= 2;
  246.     DepPath [1][4] <= 1;
  247.     //for DZM1
  248.     DepPath [2][0] <= 1;
  249.     DepPath [2][1] <= 1;
  250.     DepPath [2][2] <= 2;
  251.     DepPath [2][3] <= 3;
  252.     DepPath [2][4] <= 1;
  253.     DepPath [2][5] <= 2;
  254.     DepPath [2][6] <= 2;
  255.     //for DZM2
  256.     DepPath [3][0] <= 1;
  257.     DepPath [3][1] <= 1;
  258.     DepPath [3][2] <= 0;
  259.     DepPath [3][3] <= 2;
  260.     DepPath [3][4] <= 2;
  261.     DepPath [3][5] <= 3;
  262.     DepPath [3][6] <= 1;
  263.     DepPath [3][7] <= 0;
  264.     DepPath [3][8] <= 1; //This shall place the bot at start
  265.     //for DZM3
  266.     DepPath [4][0] <= 1;
  267.     DepPath [4][1] <= 0;
  268.     DepPath [4][2] <= 0;
  269.     DepPath [4][3] <= 0;
  270.     DepPath [4][4] <= 1;
  271.     DepPath [4][5] <= 1;
  272.     DepPath [4][6] <= 3;
  273.     DepPath [4][7] <= 1;
  274.     DepPath [4][8] <= 0;
  275.     DepPath [4][9] <= 0;
  276.     DepPath [4][10] <= 1; //This shall place the bot at start
  277.     //for DZV2
  278.     DepPath [5][0] <= 1;
  279.     DepPath [5][1] <= 2;
  280.     DepPath [5][2] <= 3;
  281.     DepPath [5][3] <= 1;
  282.     DepPath [5][4] <= 2;
  283.     //for DZV1
  284.     DepPath [6][0] <= 0;
  285.     DepPath [6][1] <= 1;
  286.     DepPath [6][2] <= 3;
  287.     DepPath [6][3] <= 2;
  288.     DepPath [6][4] <= 0;
  289.     //for DZN1
  290.     DepPath [7][0] <= 1;
  291.     DepPath [7][1] <= 0;
  292.     DepPath [7][2] <= 2;
  293.     DepPath [7][3] <= 1;
  294.     DepPath [7][4] <= 3;
  295.     DepPath [7][5] <= 2;
  296.     DepPath [7][6] <= 1;
  297.     DepPath [7][7] <= 0;
  298.     DepPath [7][8] <= 2;
  299.     //for DZN2
  300.     DepPath [8][0] <= 1;
  301.     DepPath [8][1] <= 0;
  302.     DepPath [8][2] <= 0;
  303.     DepPath [8][3] <= 2;
  304.     DepPath [8][4] <= 3;
  305.     DepPath [8][5] <= 1;
  306.     DepPath [8][6] <= 0;
  307.     DepPath [8][7] <= 0;
  308.     DepPath [8][8] <= 2;
  309.    
  310.     DepPath_len [0] <= 8;
  311.     DepPath_len [1] <= 4;
  312.     DepPath_len [2] <= 6;
  313.     DepPath_len [3] <= 8;
  314.     DepPath_len [4] <= 10;
  315.     DepPath_len [5] <= 4;
  316.     DepPath_len [6] <= 4;
  317.     DepPath_len [7] <= 8;
  318.     DepPath_len [8] <= 8;
  319.  
  320.      done <= 1;
  321. end
  322.    
  323.      led1 <= (s1);              //Led lights up when s1 has a greater value than 2000
  324.      led2 <= (s2);                  //Led lights up when s2 has a greater value than 2000
  325.      led3 <= (s3);                  //Led lights up when s3 has a greater value than 2000
  326.  
  327.     error <= (s1) - (s3);      //Relative error between sensors s1 and s3: values range from -1 to 1  P
  328.  
  329.     cumulative_error <= cumulative_error + error;  //Adds up the error to give a cumulative error  I
  330.      
  331.     if (cumulative_error > 10)     //Condition to reset the value of cumulative error to 10 if it crosses 10  
  332.     begin
  333.         cumulative_error <= 10;
  334.     end
  335.     if (cumulative_error < -10)    //Condition to reset the value of cumulative error to -10 if it crosses -10  
  336.     begin
  337.         cumulative_error <= -10;
  338.     end
  339.      
  340.      if ((s1==0) && s2 && (s3==0)) //Cumulative error resets to zero when the bot is on line i.e WBW
  341.      begin
  342.             cumulative_error <= 0;  
  343.             hl <=0;
  344.             id1 <= 0;
  345.      end
  346.      
  347.      difference <= error-preverror;                                  //forms the differential part D
  348.      correction <= ((10*error) + cumulative_error + (2*difference)); // kp = 10, ki = 1, kd =2
  349.       preverror <= error;  // Stores value of current error to previous error so that it can be used in next loop cycle
  350.      
  351.      
  352.      ml1 <= odc - correction;  // PID tuning for motor 1
  353.      ml2 <= odc + correction;  // PID tuning for motor 2
  354.  
  355.      if (ml1>70)      //Resetting value of ml1 to 70 if it crosses 70
  356.      begin
  357.          ml1 <= 70;
  358.      end
  359.      if (ml2>70)      //Resetting value of ml2 to 70 if it crosses 70
  360.      begin
  361.          ml2 <= 70;
  362.      end
  363.      if (ml1<30)       //Resetting value of ml1 to 30 if it becomes less than 30
  364.      begin
  365.          ml1 <= 30;
  366.      end
  367.      if (ml2<30)       //Resetting value of ml2 to 30 if it becomes less than 30
  368.      begin
  369.          ml2 <= 30;
  370.      end
  371.    
  372.     if ((s1==0) && s2 && (s3==0)) //ready for next node, flag resets to zero on line : WBW
  373.     begin
  374.         flag <= 0;
  375.           ml1r <= 0;
  376.           ml2r <= 0;
  377.     end
  378.  
  379.     if (s1 && s2 && s3 && flag == 0)  //detect node
  380.     begin
  381.             id1 <= 4;
  382.             hl <= 1;
  383.             nodecount <= nodecount + 1;
  384.          flag <= 1;   //node
  385.             flag2 <= 1;  //clr lock
  386.             clr_en <= 1; //color enable 1 disable
  387.     end
  388.    
  389.     if(flag == 1)                                  //if node
  390.     begin
  391.         if(state==0)                                //is arena?
  392.         begin
  393.             n<=pwm(path[path_no][nodecount]);         //follow arena
  394.         end else
  395.         if((state==1)&&(nodecount<10))                                //is pick?
  396.         begin
  397.             if((clrs[clrsN-1][0]==1)&&(clrs[clrsN-1][1]==0))
  398.             begin
  399.                 n<=pwm(clrPath[0+rcount][nodecount]);
  400.             end else if((clrs[clrsN-1][0]==0)&&(clrs[clrsN-1][1]==1))
  401.             begin
  402.                 n<=pwm(clrPath[3+gcount][nodecount]);
  403.             end else if((clrs[clrsN-1][0]==1)&&(clrs[clrsN-1][1]==1))
  404.             begin
  405.                 n<=pwm(clrPath[6+bcount][nodecount]);
  406.             end
  407.         end
  408.      if (state== 2)
  409.      begin
  410.         n<=pwm(DepPath[path_no][nodecount]);
  411.      end
  412.    
  413.     end
  414.    
  415.     if(flag == 0)    //assigning values of ml1 and ml2 (line condition) to mo1 and mo2 respectively
  416.     begin
  417.         if(path_no ==0 && nodecount ==5 )        //enable clr module at patch1
  418.             begin
  419.                 clr_en <= 0;
  420.                 if((Clr != 0)&&(flag2==1))
  421.                 begin
  422.                     clrsN <= clrsN +1;
  423.                     clrs[clrsN] <= Clr;
  424.                     dest[clrsN] <= 0;
  425.                     flag2 <= 0;
  426.                 end
  427.         end else if(path_no ==0 && nodecount ==7 )      //enable clr module only at patches
  428.         begin
  429.             clr_en <= 0;
  430.             if((Clr != 0)&&(flag2==1))
  431.             begin
  432.                 clrsN <= clrsN +1;
  433.                 clrs[clrsN] <= Clr;
  434.                 dest[clrsN] <= 0;
  435.                 flag2 <= 0;
  436.             end
  437.         end else if(path_no ==1 && nodecount ==5 )      //enable clr module only at patches
  438.         begin
  439.             clr_en <= 0;
  440.             if((Clr != 0)&&(flag2==1))
  441.             begin
  442.                 clrsN <= clrsN +1;
  443.                 clrs[clrsN] <= Clr;
  444.                 dest[clrsN] <= 0;
  445.                 flag2 <= 0;
  446.             end
  447.         end else if(path_no ==1 && nodecount ==6 )      //enable clr module only at patches
  448.         begin
  449.             clr_en <= 0;
  450.             if((Clr != 0)&&(flag2==1))
  451.             begin
  452.                 clrsN <= clrsN +1;
  453.                 clrs[clrsN] <= Clr;
  454.                 dest[clrsN] <= 0;
  455.                 flag2 <= 0;
  456.             end
  457.         end else if(path_no ==1 && nodecount ==7 )      //enable clr module only at patches
  458.         begin
  459.             clr_en <= 0;
  460.             if((Clr != 0)&&(flag2==1))
  461.             begin
  462.                 clrsN <= clrsN +1;
  463.                 clrs[clrsN] <= Clr;
  464.                 dest[clrsN] <= 0;
  465.                 flag2 <= 0;
  466.             end
  467.         end else if(path_no ==2 && nodecount ==1 )      //enable clr module only at patches
  468.         begin
  469.             clr_en <= 0;
  470.             if((Clr != 0)&&(flag2==1))
  471.             begin
  472.                 clrsN <= clrsN +1;
  473.                 clrs[clrsN] <= Clr;
  474.                 dest[clrsN] <= 0;
  475.                 flag2 <= 0;
  476.             end
  477.         end else if(path_no ==2 && nodecount ==6 )      //enable clr module only at patches
  478.         begin
  479.             clr_en <= 0;
  480.             if((Clr != 0)&&(flag2==1))
  481.             begin
  482.                 clrsN <= clrsN +1;
  483.                 clrs[clrsN] <= Clr;
  484.                 dest[clrsN] <= 0;
  485.                 flag2 <= 0;
  486.             end
  487.         end else if(path_no ==3 && nodecount ==5 )      //enable clr module only at patches
  488.         begin
  489.             clr_en <= 0;
  490.             if((Clr != 0)&&(flag2==1))
  491.             begin
  492.                 clrsN <= clrsN +1;
  493.                 clrs[clrsN] <= Clr;
  494.                 dest[clrsN] <= 0;
  495.                 flag2 <= 0;
  496.             end
  497.         end else if(path_no ==3 && nodecount ==7 )      //enable clr module only at patches
  498.         begin
  499.             clr_en <= 0;
  500.             if((Clr != 0)&&(flag2==1))
  501.             begin
  502.                 clrsN <= clrsN +1;
  503.                 clrs[clrsN] <= Clr;
  504.                 dest[clrsN] <= 0;
  505.                 flag2 <= 0;
  506.             end
  507.         end else
  508.         begin
  509.             clr_en <= 1;
  510.         end
  511.         if((state==0)&&(nodecount == (path_len[path_no]-1)))       //if about to reach end of path
  512.         begin
  513.             if(clrsN != 0)                           //detected color?
  514.             begin
  515.                 state <= 1;                             //go to pick
  516.             end else if(clrsN == 0)
  517.             begin
  518.                 nodecount <= -5'd1;                      //reset nodecount
  519.                 path_no <= path_no + 1;                  //change path
  520.             end
  521.         end
  522.         if(state==1)
  523.         begin
  524.             if(nodecount == path_len[path_no])
  525.             begin
  526.             state <= 2;
  527.             nodecount <= -5'd1;
  528.             end
  529.             nodecount <= nodecount+1;
  530.         end
  531.         if(state==2)
  532.         begin
  533.             if(nodecount == DepPath_len[path_no])
  534.             begin
  535.             state <= 0;
  536.             nodecount <= -5'd1;
  537.             end
  538.             nodecount <= nodecount+1;
  539.         end
  540.        
  541.         mo1 <= ml1;
  542.         mo1r<= ml1r;
  543.         mo2 <= ml2;
  544.         mo2r<= ml2r;
  545.     end else
  546.     begin            //assigning values of mn1 and mn2 (node condition) to mo1 and mo2 respectively
  547.         mo1 <= mn1;
  548.         mo1r<= mn1r;
  549.         mo2 <= mn2;
  550.         mo2r<= mn2r;
  551.     end
  552. end
  553.        
  554. assign Led1 = led1;
  555. assign Led2 = led2;
  556. assign Led3 = led3;
  557.  
  558. assign id = id1;
  559. assign HL1 = hl;
  560. assign m1 = mo1;
  561. assign m1b= mo1r;
  562. assign m2 = mo2;
  563. assign m2b= mo2r;
  564.  
  565. assign EM = en;
  566. assign Clr_en = clr_en;
  567. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement