Advertisement
Sidsh

LSA Backup

Feb 10th, 2022
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.06 KB | None | 0 0
  1. module LSA(
  2. output [7:0]m1, //motor1 PIN_D3
  3. output [7:0]m2, //motor2 PIN_C3
  4. input s1, //12-bit output of ch. 5 (parallel)
  5. input s2, //12-bit output of ch. 6 (parallel)
  6. input s3, //12-bit output of ch. 7 (parallel)
  7. input clk_50, //50 MHz clock
  8. input reset,
  9. output Led1, //Led used to indicate position of bot i.e. node or line
  10. output Led2,
  11. output Led3,
  12. output HL1, // to control uart
  13. output [2:0] id
  14. );
  15.  
  16. reg signed[7:0]error = 0;
  17. reg signed[7:0]difference = 0;
  18. reg signed[7:0]correction = 0;
  19. reg signed[7:0]cumulative_error = 0;
  20. reg signed[7:0]preverror = 0;
  21. reg [3:0]nodecount = -4'd1; //No. of nodes bot has traversed, initially set to -1
  22. //reg [3:0]flag=0;
  23. reg flag = 0;
  24.  
  25. reg led1=0;
  26. reg led2=0;
  27. reg led3=0;
  28.  
  29. reg [7:0]odc =50; //optimum duty cycle
  30. reg [7:0]mo1 = 50; // pwm to motor1, initially set to 50
  31. reg [7:0]mo2 = 50; // pwm to motor2, initially set to 50
  32. reg [7:0]ml1 = 50; // pwm to motor1 when it is on line
  33. reg [7:0]ml2 = 50; // pwm to motor2 when it is on line
  34. reg [7:0]mn1 = 50; // pwm to motor1 when it is on node
  35. reg [7:0]mn2 = 50; // pwm to motor2 when it is on node
  36. //reg [11:0]thr = 2000; //Condition to differentiate between white and black surface
  37. reg [7:0]r_COUNT = 0; //counter
  38. reg clk_1 = 0; //1 MHz Clock
  39.  
  40. reg [2:0] id1 =0; //to control uart
  41. reg hl;
  42.  
  43. /*reg [9:0]s1v1=0;
  44. reg [9:0]s1v2=0;
  45. reg [9:0]s1v3=0;
  46. reg [9:0]s1v4=0;
  47. reg [9:0]s1v5=0;
  48. reg [9:0]s1v6=0;
  49.  
  50. reg [9:0]s2v1=0;
  51. reg [9:0]s2v2=0;
  52. reg [9:0]s2v3=0;
  53. reg [9:0]s2v4=0;
  54. reg [9:0]s2v5=0;
  55. reg [9:0]s2v6=0;
  56.  
  57. reg [9:0]s3v1=0;
  58. reg [9:0]s3v2=0;
  59. reg [9:0]s3v3=0;
  60. reg [9:0]s3v4=0;
  61. reg [9:0]s3v5=0;
  62. reg [9:0]s3v6=0;
  63.  
  64. reg [11:0]s1avg=0;
  65. reg [11:0]s2avg=0;
  66. reg [11:0]s3avg=0;*/
  67.  
  68. always @(posedge clk_50) begin //Scaling down of 50MHz clock to 1 MHz
  69.  
  70. if(reset == 0)
  71. begin
  72. r_COUNT <= 0;
  73. end
  74.  
  75. if (r_COUNT == 49 )begin //Check if count reaches 49
  76. r_COUNT <= 0; //Reset counter if it reaches 49
  77. end else begin
  78. r_COUNT <= r_COUNT + 1; //increment counter by 1 till 49
  79. end
  80. clk_1 <= (r_COUNT < 25);
  81. end
  82.  
  83. always @(posedge clk_1) begin
  84.  
  85.  
  86. if(reset == 0)
  87. begin
  88. nodecount <=-4'd1 ; //Initially node count set to -1
  89. end
  90.  
  91. led1 <= (s1); //Led lights up when s1 has a greater value than 2000
  92. led2 <= (s2); //Led lights up when s2 has a greater value than 2000
  93. led3 <= (s3); //Led lights up when s3 has a greater value than 2000
  94.  
  95. error <= (s1) - (s3); //Relative error between sensors s1 and s3: values range from -1 to 1 P
  96.  
  97. cumulative_error <= cumulative_error + error; //Adds up the error to give a cumulative error I
  98.  
  99. if (cumulative_error > 10) //Condition to reset the value of cumulative error to 10 if it crosses 10
  100. begin
  101. cumulative_error <= 10;
  102. end
  103.  
  104. if (cumulative_error < -10) //Condition to reset the value of cumulative error to -10 if it crosses -10
  105. begin
  106. cumulative_error <= -10;
  107. end
  108.  
  109. if ((s1==0) && s2 && (s3==0)) //Cumulative error resets to zero when the bot is on line i.e WBW
  110. begin
  111. cumulative_error <= 0;
  112. hl <=0;
  113. id1 <= 0;
  114. end
  115.  
  116. difference <= error-preverror; //forms the differential part D
  117. correction <= ((10*error) + cumulative_error + (2*difference)); // kp = 10, ki = 1, kd =2
  118.  
  119. preverror <= error; // Stores value of current error to previous error so that it can be used in next loop cycle
  120.  
  121.  
  122. ml1 <= odc - correction; // PID tuning for motor 1
  123. ml2 <= odc + correction; // PID tuning for motor 2
  124.  
  125. if (ml1>70) //Resetting value of ml1 to 70 if it crosses 70
  126. begin
  127. ml1 <= 70;
  128. end
  129. if (ml2>70) //Resetting value of ml2 to 70 if it crosses 70
  130. begin
  131. ml2 <= 70;
  132. end
  133. if (ml1<30) //Resetting value of ml1 to 30 if it becomes less than 30
  134. begin
  135. ml1 <= 30;
  136. end
  137. if (ml2<30) //Resetting value of ml2 to 30 if it becomes less than 30
  138. begin
  139. ml2 <= 30;
  140. end
  141.  
  142.  
  143. /* s1v1 <= s1v2;
  144. s1v2 <= s1v3;
  145. s1v3 <= s1v4;
  146. s1v4 <= s1v5;
  147. s1v5 <= s1v6;
  148. s1v6 <= s1[11:2];
  149. s1avg <= s1v1 + s1v2 + s1v3 + s1v4 + s1v5 + s1v6;
  150.  
  151. s2v1 <= s2v2;
  152. s2v2 <= s2v3;
  153. s2v3 <= s2v4;
  154. s2v4 <= s2v5;
  155. s2v5 <= s2v6;
  156. s2v6 <= s2[11:2];
  157. s2avg <= s2v1 + s2v2 + s2v3 + s2v4 + s2v5 + s2v6;
  158.  
  159. s3v1 <= s3v2;
  160. s3v2 <= s3v3;
  161. s3v3 <= s3v4;
  162. s3v4 <= s3v5;
  163. s3v5 <= s3v6;
  164. s3v6 <= s3[11:2];
  165. s3avg <= s3v1 + s3v2 + s3v3 + s3v4 + s3v5 + s3v6; */
  166.  
  167. /*if(s1>thr && s2>thr && s3>thr)
  168. begin
  169. flag = 1;
  170. mo1 <= 0;
  171. mo2 <= 63;
  172. end*/
  173. /*if(s1>thr && s2<thr && s3<thr && flag == 1) //end turning
  174. begin
  175. flag <= 0;
  176. mo1 <= odc - correction;
  177. mo2 <= odc + correction;
  178. end
  179. /*if(s1<thr && s2>thr && s3<thr && flag == 1)
  180. begin
  181. flag <= 0;
  182. mo1 <= odc - correction;
  183. mo2 <= odc + correction;
  184. end*/
  185. if ((s1==0) && s2 && (s3==0)) //ready for next node, flag resets to zero on line : WBW
  186. begin
  187. flag <= 0;
  188. end
  189. /*else begin
  190. flag <= 1;
  191. end*/
  192.  
  193. // Only detect node once.
  194. if (s1 && s2 && s3 && flag == 0) //detect node
  195. begin
  196. if (nodecount == 7) // Nodecount resets to zero if it crosses 7
  197. begin
  198. id1 <= 4;
  199. hl <= 1;
  200. nodecount <= 0;
  201. flag <= 1; //flag set to 1 when it detects a node
  202. end else
  203. begin
  204. id1 <= 4;
  205. hl <= 1;
  206. nodecount <= nodecount + 1;
  207. flag <= 1;
  208. end
  209. end
  210.  
  211. /*if (s1>thr && s2>thr && s3>thr)
  212. begin
  213. if(flag == 3)
  214. begin
  215. if (nodecount == 7)
  216. begin
  217. nodecount <= 0;
  218. flag <= 0;
  219. flag2 <= 1;
  220. end else
  221. begin
  222. nodecount <= nodecount + 1;
  223. flag <=0;
  224. flag2 <= 1;
  225. end
  226. end else
  227. begin
  228. flag <= flag + 1;
  229. end
  230. end else
  231. begin
  232. flag <= 0;
  233. end*/
  234.  
  235. if(flag == 1) //Detection of node and applying pwm accordingly
  236. begin
  237. case (nodecount)
  238. 1: begin
  239. mn1 <= odc;
  240. mn2 <= odc;
  241. end
  242. 2: begin
  243. mn1 <= 5;
  244. mn2 <= 60;
  245. end
  246. 3: begin
  247. mn1 <= odc;
  248. mn2 <= odc;
  249. end
  250. 4: begin
  251. mn1 <= 5;
  252. mn2 <= 60;
  253. end
  254. 5: begin
  255. mn1 <= odc;
  256. mn2 <= odc;
  257. end
  258. 6: begin
  259. mn1 <= odc;
  260. mn2 <= odc;
  261. end
  262. 7: begin
  263. mn1 <= 5;
  264. mn2 <= 60;
  265. end
  266. 0: begin
  267. mn1 <= odc;
  268. mn2 <= odc;
  269. end
  270. endcase
  271. /*
  272. mn1 <= 5;
  273. mn2 <= 60;*/
  274. end
  275.  
  276. if(flag == 0) //assigning values of ml1 and ml2 (line condition) to mo1 and mo2 respectively
  277. begin
  278. mo1 <= ml1;
  279. mo2 <= ml2;
  280. end else
  281. begin //assigning values of mn1 and mn2 (node condition) to mo1 and mo2 respectively
  282. mo1 <= mn1;
  283. mo2 <= mn2;
  284. end
  285. end
  286.  
  287. assign Led1 = led1;
  288. assign Led2 = led2;
  289. assign Led3 = led3;
  290.  
  291. assign id = id1;
  292. assign HL1 = hl;
  293. assign m1 = mo1;
  294. assign m2 = mo2;
  295.  
  296. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement