Advertisement
Sidsh

SM Path Traversal 1.1

Feb 28th, 2022
194
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][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. function automatic pwm;
  207.     input [1:0]n;
  208.     begin
  209.         case(n)
  210.         0:begin
  211.             mn1 = odc;
  212.             mn1r=0;
  213.             mn2 = odc;
  214.             mn2r=0;
  215.           end
  216.         1:begin
  217.             mn1 = 5;
  218.             mn1r=0;
  219.             mn2 = 56;
  220.             mn2r=0;
  221.           end
  222.         2:begin
  223.             mn1 = 58;
  224.             mn1r=0;
  225.             mn2 = 5;
  226.             mn2r=0;
  227.           end
  228.         3:begin
  229.             mn1=0;
  230.             mn1r=odc;
  231.             mn2=odc;
  232.             mn2r=0;
  233.           end
  234.         endcase
  235.         pwm = 1;
  236.      end
  237.     endfunction
  238.  
  239. always @(posedge clk_1) begin    
  240.    
  241.    
  242.     if(reset == 0)
  243.      begin
  244.         nodecount <=-4'd1 ;         //Initially node count set to -1
  245.      end
  246.    
  247.      led1 <= (s1);              //Led lights up when s1 has a greater value than 2000
  248.      led2 <= (s2);              //Led lights up when s2 has a greater value than 2000
  249.      led3 <= (s3);              //Led lights up when s3 has a greater value than 2000
  250.  
  251.     error <= (s1) - (s3);    //Relative error between sensors s1 and s3: values range from -1 to 1  P
  252.  
  253.     cumulative_error <= cumulative_error + error;  //Adds up the error to give a cumulative error  I
  254.      
  255.     if (cumulative_error > 10)      //Condition to reset the value of cumulative error to 10 if it crosses 10  
  256.     begin
  257.         cumulative_error <= 10;
  258.     end
  259.  
  260.      if (cumulative_error < -10)    //Condition to reset the value of cumulative error to -10 if it crosses -10  
  261.     begin
  262.         cumulative_error <= -10;
  263.     end
  264.      
  265.      if ((s1==0) && s2 && (s3==0)) //Cumulative error resets to zero when the bot is on line i.e WBW
  266.      begin
  267.             cumulative_error <= 0;  
  268.             hl <=0;
  269.             id1 <= 0;
  270.      end
  271.      
  272.      difference <= error-preverror;   //forms the differential part D
  273.      correction <= ((10*error) + cumulative_error + (2*difference)); // kp = 10, ki = 1, kd =2
  274.      
  275.       preverror <= error;  // Stores value of current error to previous error so that it can be used in next loop cycle
  276.      
  277.      
  278.      ml1 <= odc - correction;  // PID tuning for motor 1
  279.      ml2 <= odc + correction;  // PID tuning for motor 2
  280.  
  281.      if (ml1>70)      //Resetting value of ml1 to 70 if it crosses 70
  282.      begin
  283.          ml1 <= 70;
  284.      end
  285.      if (ml2>70)      //Resetting value of ml2 to 70 if it crosses 70
  286.      begin
  287.          ml2 <= 70;
  288.      end
  289.      if (ml1<30)       //Resetting value of ml1 to 30 if it becomes less than 30
  290.      begin
  291.          ml1 <= 30;
  292.      end
  293.      if (ml2<30)       //Resetting value of ml2 to 30 if it becomes less than 30
  294.      begin
  295.          ml2 <= 30;
  296.      end
  297.    
  298.     if ((s1==0) && s2 && (s3==0)) //ready for next node, flag resets to zero on line : WBW
  299.     begin
  300.         flag <= 0;
  301.           ml1r <= 0;
  302.           ml2r <= 0;
  303.     end
  304.     /*if ((s1==0) && (s2==0) && (s3==0)&& (flag ==0))
  305.     begin
  306.         flag <= 0;
  307.         n=pwm(3);
  308.         ml1 <=0;
  309.         ml1r<=odc;
  310.         ml2 <=0;
  311.         ml2r<=odc;
  312.     end*/
  313.    
  314.     if (s1 && s2 && s3 && flag == 0)  //detect node
  315.     begin
  316.             id1 <= 4;
  317.             hl <= 1;
  318.             nodecount <= nodecount + 1;
  319.          flag <= 1;
  320.             i <= i+2;
  321.             j <= j+2;
  322.     end
  323.    
  324.     if(flag == 1)    //Detection of node and applying pwm accordingly
  325.     begin
  326.     //n<=pwm(path[nodecount]);
  327.     if(flag1==0)
  328.     begin
  329.         n<=pwm(path[path_no][i:j]);
  330.         if(nodecount==path_len[path_no])
  331.         begin
  332.             path_no <= path_no + 1;
  333.             nodecount <= -5'd1;
  334.             if(clrsN > 0)
  335.             begin
  336.                 flag1 <= 1;
  337.             end
  338.         end
  339.     end else
  340.     if(flag1 == 1)
  341.     begin
  342.         if(clrs[clrsN] == 1)
  343.         begin
  344.         n<=pwm(clrPath[0+rcount][nodecount]);
  345.             if(nodecount==clrPathlen[0+rcount])
  346.             begin
  347.                 flag1 <= 2;
  348.                 nodecount <= -5'd1;
  349.                 rcount <= rcount + 1;
  350.             end
  351.         end
  352.     end else
  353.     if(
  354.        
  355.    
  356.     /*if(path_no == && nodecount == )      //enable clr module only at patches
  357.     begin
  358.         clr_en <= 0;
  359.     end else
  360.     begin
  361.         clr_en <= 1;
  362.     end*/
  363.    
  364.     if(clr_en == 1 && Clr != 0)
  365.     begin
  366.         clrsN <= clrsN +1;
  367.         clrs[clrsN] <= Clr;
  368.     end
  369.    
  370.     case (nodecount)
  371.     0: begin
  372.         n<=pwm(0);
  373.         end
  374.     1: begin            
  375.         n<=pwm(0);
  376.         end
  377.     2: begin
  378.         n<=pwm(0);
  379.         end
  380.     3: begin
  381.         n<=pwm(0);
  382.         end
  383.     4: begin
  384.         n<=pwm(1);
  385.         end
  386.     5: begin
  387.         n<=pwm(0);
  388.         end
  389.     6: begin
  390.         n<=pwm(2);
  391.         end
  392.     7: begin
  393.         n<=pwm(0);
  394.         end
  395.     8: begin
  396.         n<=pwm(2);
  397.         end
  398.     9: begin
  399.         n<=pwm(2);
  400.         end
  401.     10:begin
  402.         n<=pwm(0);
  403.         end
  404.     11:begin
  405.         n<=pwm(0);
  406.         end
  407.     12:begin
  408.         n<=pwm(0);
  409.         end
  410.     13:begin
  411.         n<=pwm(2);
  412.         end
  413.     14:begin
  414.         en <= 1;
  415.         n<=pwm(3);
  416.         id1 <= 5;
  417.         hl <= 1;
  418.         end
  419.     15:begin
  420.         n<=pwm(1);
  421.         end
  422.     16:begin
  423.         n<=pwm(0);
  424.         end
  425.     17:begin
  426.         n<=pwm(1);
  427.         end
  428.     18:begin
  429.         n<=pwm(0);
  430.         end
  431.     19:begin
  432.         n<=pwm(2);
  433.         end
  434.     20:begin
  435.         n<=pwm(1);
  436.         end
  437.     21:begin
  438.         en <= 0;
  439.         n<=pwm(3);
  440.         id1 <= 6;
  441.         hl <= 1;
  442.         end
  443.     endcase
  444.     end
  445.    
  446.     if(flag == 0)    //assigning values of ml1 and ml2 (line condition) to mo1 and mo2 respectively
  447.     begin
  448.         mo1 <= ml1;
  449.         mo1r<= ml1r;
  450.         mo2 <= ml2;
  451.         mo2r<= ml2r;
  452.     end else
  453.     begin            //assigning values of mn1 and mn2 (node condition) to mo1 and mo2 respectively
  454.         mo1 <= mn1;
  455.         mo1r<= mn1r;
  456.         mo2 <= mn2;
  457.         mo2r<= mn2r;
  458.     end
  459. end
  460.        
  461. assign Led1 = led1;
  462. assign Led2 = led2;
  463. assign Led3 = led3;
  464.  
  465. assign id = id1;
  466. assign HL1 = hl;
  467. assign m1 = mo1;
  468. assign m1b= mo1r;
  469. assign m2 = mo2;
  470. assign m2b= mo2r;
  471.  
  472. assign EM = en;
  473. assign Clr_en = flag;
  474. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement