Hubert_M

Untitled

May 28th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.24 KB | None | 0 0
  1. using System;
  2. using System.Timers;
  3. using System.ComponentModel.Design;
  4. using System.Threading;
  5. using System.Windows;
  6. using System.Net.Sockets;
  7. using System.Threading.Tasks;
  8.  
  9.  
  10. namespace Obiekt1
  11. {
  12. class Tank
  13. {
  14. public double WYP = 0.63;
  15. public static double PI = 3.14;
  16. private static double G = 9.81;
  17. private static double TM = 6;
  18. private static double QM = 11;
  19. private static double X1 = 0.6;
  20. private static double X2 = 1.3;
  21. private static double X3 = 1.9;
  22.  
  23. public int x1 = 0, x2 = 0, x3 = 0;
  24. public static double hT = 2; // wysokość zbiornika w m2
  25. static double r = 0.5; // promień zbiornika w m
  26. public static double sT = PI * r * r; // powierzchnia podstawy zbiornika w m2
  27. public double vT = sT * hT; // objętość zbiornika w m3
  28. public double hW = 0.0; // poziom wody w zbiorniku w m
  29. static double vW = 0; // objętość wody w zbiorniku w m3 50litra max objętość
  30. public int overflowed = 0; // czujnik przelania
  31.  
  32. public void check()
  33. {
  34. if (hW > hT)
  35. {
  36. hW = 2;
  37. overflowed = 1;
  38. }
  39. else overflowed = 0;
  40.  
  41. if (hW >= X1)
  42. x1 = 1;
  43. else
  44. x1 = 0;
  45.  
  46. if (hW >= X2)
  47. x2 = 1;
  48. else
  49. x2 = 0;
  50.  
  51. if (hW >= X3)
  52. x3 = 1;
  53. else
  54. x3 = 0;
  55.  
  56. vW = sT * hW;
  57. }
  58.  
  59. }
  60.  
  61. class Valve
  62. {
  63. private static double PI = 3.14;
  64. private static double QM = 11;
  65.  
  66. public int status = 0; // 1 nalewa/ 0 nic nie robi
  67. static double r = 0.03; // promień przekorju zaworu w m
  68. static double sV = PI * r * r; // przekrój otworu w m2
  69. public double rSV = sV; // Obecny przekrój
  70.  
  71. public void qV(double x)
  72. {
  73. if (x != QM)
  74. {
  75. rSV = sV / x;
  76. }
  77. else
  78. {
  79. rSV = 0;
  80. }
  81. }
  82. }
  83.  
  84. class Heater
  85. {
  86. static int status = 0; // 1 grzeje/ o nie grzeje
  87. static int p;
  88. }
  89.  
  90. class Program
  91. {
  92.  
  93. private static System.Timers.Timer aTimer;
  94. private static System.Timers.Timer bTimer;
  95.  
  96. public static double TM = 6;
  97. public static double G = 9.81;
  98. public static double WYP = 0.63;
  99. static double PI = 3.14;
  100. static double hT = 2; // wysokość zbiornika w m2
  101. static double r = 0.5; // promień zbiornika w m
  102. static double sT = PI * r * r; // powierzchnia podstawy zbiornika w m2
  103. static double vT = sT * hT; // objętość zbiornika w m3
  104. static double hW = 0.0; // poziom wody w zbiorniku w m
  105. static double vW = 0; // objętość wody w zbiorniku w m3 50litra max objętość
  106. static double sV = PI * r * r; // przekrój otworu w m2
  107. static double rSV = sV; // Obecny przekrój
  108.  
  109. static async void Connect(Tank t1, Tank t2, Tank t3, Tank t4, Valve v1, Valve v2, Valve v3, Valve v4, Valve v5, Valve v6)
  110. {
  111. try
  112. {
  113. String server = "127.0.0.1";
  114. Int32 port = 16666;
  115. TcpClient client = new TcpClient(server, port);
  116. while (true)
  117. {
  118.  
  119.  
  120.  
  121. // Translate the passed message into ASCII and store it as a Byte array.
  122.  
  123. string message = t1.x1.ToString() + ":" + t1.x2.ToString() + ":" + t1.x3.ToString() + ":" +
  124. t2.x1.ToString() + ":" +
  125. t2.x2.ToString() + ":" + t2.x3.ToString() + ":" + t3.x1.ToString() + ":" +
  126. t3.x2.ToString() + ":" +
  127. t3.x3.ToString() + ":" + t4.x1.ToString() + ":" + t4.x2.ToString() + ":" +
  128. t4.x3.ToString();
  129. Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
  130.  
  131. // Get a client stream for reading and writing.
  132. // Stream stream = client.GetStream();
  133.  
  134. NetworkStream stream = client.GetStream();
  135.  
  136. // Send the message to the connected TcpServer.
  137.  
  138. stream.Write(data, 0, data.Length);
  139.  
  140. //Console.WriteLine("Sent: {0}", message);
  141.  
  142. // Receive the TcpServer.response.
  143.  
  144. // Buffer to store the response bytes.
  145. data = new Byte[256];
  146.  
  147. // String to store the response ASCII representation.
  148. String responseData = String.Empty;
  149.  
  150. // Read the first batch of the TcpServer response bytes.
  151. Int32 bytes = stream.Read(data, 0, data.Length);
  152. responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
  153. v1.status = Convert.ToInt32(responseData.Split(':')[0]);
  154. v2.status = Convert.ToInt32(responseData.Split(':')[1]);
  155. v3.status = Convert.ToInt32(responseData.Split(':')[2]);
  156. v4.status = Convert.ToInt32(responseData.Split(':')[3]);
  157. v5.status = Convert.ToInt32(responseData.Split(':')[4]);
  158. v6.status = Convert.ToInt32(responseData.Split(':')[5]);
  159.  
  160. //Console.WriteLine("Received: {0}", responseData);
  161. }
  162.  
  163.  
  164. }
  165. catch (ArgumentNullException e)
  166. {
  167. Console.WriteLine("ArgumentNullException: {0}", e);
  168. }
  169. catch (SocketException e)
  170. {
  171. Console.WriteLine("SocketException: {0}", e);
  172. }
  173.  
  174. }
  175.  
  176.  
  177. static void Main(string[] args)
  178. {
  179.  
  180. Tank t1 = new Tank();
  181. Tank t2 = new Tank();
  182. Tank t3 = new Tank();
  183. Tank t4 = new Tank();
  184. Valve v1 = new Valve();
  185. Valve v2 = new Valve();
  186. Valve v3 = new Valve();
  187. Valve v4 = new Valve();
  188. Valve v5 = new Valve();
  189. Valve v6 = new Valve();
  190.  
  191. //Connect(t1, t2, t3, t4, v1, v2, v3, v4, v5, v6);
  192. SetTimer(t1, t2, t3, t4, v1, v2, v3, v4, v5, v6);
  193. Connect(t1, t2, t3, t4, v1, v2, v3, v4, v5, v6);
  194. //SetTimer2(t1, t2, t3, t4, v1, v2, v3, v4, v5, v6);
  195. //SetTimer(t1, t2, t3, t4, v1, v2, v3, v4, v5, v6);
  196. //SetTimer2(t1, t2, t3, t4, v1, v2, v3, v4, v5, v6);
  197.  
  198. while (true) { }
  199.  
  200.  
  201. }
  202.  
  203. private static void SetTimer(Tank t1, Tank t2, Tank t3, Tank t4, Valve v1, Valve v2, Valve v3, Valve v4, Valve v5, Valve v6)
  204. {
  205. // Create a timer with a two second interval.
  206. aTimer = new System.Timers.Timer(100);
  207. // Hook up the Elapsed event for the timer.
  208. aTimer.Elapsed += (source, e) => OnTimedEvent(source, e, t1, t2, t3, t4, v1, v2, v3, v4, v5, v6);
  209. aTimer.AutoReset = true;
  210. aTimer.Enabled = true;
  211. }
  212.  
  213. private static void SetTimer2(Tank t1, Tank t2, Tank t3, Tank t4, Valve v1, Valve v2, Valve v3, Valve v4, Valve v5, Valve v6)
  214. {
  215. bTimer = new System.Timers.Timer(100);
  216. // Hook up the Elapsed event for the timer.
  217. bTimer.Elapsed += (source, e) => OnTimedEvent2(source, e, t1, t2, t3, t4, v1, v2, v3, v4, v5, v6);
  218. bTimer.AutoReset = true;
  219. bTimer.Enabled = true;
  220. }
  221.  
  222. private static async void OnTimedEvent(Object source, ElapsedEventArgs e, Tank t1, Tank t2, Tank t3, Tank t4, Valve v1, Valve v2, Valve v3, Valve v4, Valve v5, Valve v6)
  223. {
  224. var taska = Task.Run(() => loop(t1, t2, t3, t4, v1, v2, v3, v4, v5, v6));
  225. //var taskb = Task.Run(() => Connect(t1, t2, t3, t4, v1, v2, v3, v4, v5, v6));
  226. }
  227.  
  228.  
  229. private static void OnTimedEvent2(Object source, ElapsedEventArgs e, Tank t1, Tank t2, Tank t3, Tank t4, Valve v1, Valve v2, Valve v3, Valve v4, Valve v5, Valve v6)
  230. {
  231. Connect(t1, t2, t3, t4, v1, v2, v3, v4, v5, v6);
  232. }
  233.  
  234.  
  235. long currentTime = 0;
  236. long previousTime = 0;
  237. long deltaTime = 0;
  238. double[] data;
  239.  
  240.  
  241. static double d1 = 0; // różnica w poziomie wodym w całym cyklu
  242. static double d2 = 0; // różnica w poziomie wodym w całym cyklu
  243. static double d3 = 0; // różnica w poziomie wodym w całym cyklu
  244. static double d4 = 0; // różnica w poziomie wodym w całym cyklu
  245.  
  246.  
  247. public static double bind(Tank t, Valve v, int s, double h = 1000)
  248. {
  249. double d, q;
  250. if (v.rSV == 0)
  251. return 0;
  252. if (h == 1000)
  253. {
  254. q = WYP * v.rSV * Math.Sqrt(2 * G * (h / 1000));
  255. }
  256. else
  257. {
  258. q = WYP * v.rSV * Math.Sqrt(2 * G * h);
  259. } // natężenie wypływu w m3/s
  260.  
  261. d = TM * q / Tank.sT;
  262. if (h < 1000)
  263. if (d >= h)
  264. d = h;
  265. if (d >= h) d = h;
  266. if (s == -1)
  267. {
  268. return -d;
  269. }
  270.  
  271. return d;
  272. }
  273.  
  274.  
  275.  
  276. static void loop(Tank t1, Tank t2, Tank t3, Tank t4, Valve v1, Valve v2, Valve v3, Valve v4, Valve v5, Valve v6)
  277. {
  278. v1.qV(3);
  279. v2.qV(3);
  280. v3.qV(3);
  281. v4.qV(3);
  282. v5.qV(3);
  283. v6.qV(3);
  284. //v1.status = 1;
  285.  
  286. if (v1.status == 1)
  287. {
  288. d1 += bind(t1, v1, 1);
  289. }
  290. else
  291. {
  292. d1 += 0;
  293. }
  294.  
  295. if (v2.status == 1)
  296. {
  297. d1 += bind(t1, v2, -1, t1.hW);
  298. }
  299. else
  300. {
  301. d1 += 0;
  302. }
  303.  
  304. if (v2.status == 1)
  305. {
  306. d2 += bind(t2, v2, 1, t1.hW);
  307.  
  308. }
  309. else
  310. {
  311. d2 += 0;
  312.  
  313. }
  314.  
  315. t1.hW += d1;
  316. d1 = 0;
  317. if (v3.status == 1)
  318. {
  319. d1 += bind(t1, v3, -1, t1.hW);
  320. }
  321.  
  322. else
  323. {
  324.  
  325. d1 += 0;
  326.  
  327. }
  328.  
  329. if (v4.status == 1)
  330. {
  331. d2 += bind(t2, v4, -1, t2.hW);
  332. }
  333. else
  334. {
  335. d2 += 0;
  336. }
  337.  
  338. if (v3.status == 1)
  339. {
  340. d3 += bind(t3, v3, 1, t1.hW);
  341. }
  342. else
  343. {
  344. d3 += 0;
  345. }
  346.  
  347.  
  348. if (v5.status == 1)
  349. {
  350. d3 += bind(t3, v5, -1, t3.hW);
  351. }
  352. else
  353. {
  354. d3 += 0;
  355. }
  356.  
  357. if (v4.status == 1)
  358. {
  359. d4 += bind(t4, v4, 1, t2.hW);
  360. }
  361. else
  362. {
  363. d4 += 0;
  364. }
  365.  
  366. if (v5.status == 1)
  367. {
  368. d4 += bind(t4, v5, 1, t3.hW);
  369. }
  370. else
  371. {
  372. d4 += 0;
  373. }
  374.  
  375. if (v6.status == 1)
  376. {
  377. d4 += bind(t4, v6, -1, t4.hW);
  378. }
  379. else
  380. {
  381. d4 += 0;
  382. }
  383.  
  384.  
  385. t1.hW += d1;
  386. t2.hW += d2;
  387. t3.hW += d3;
  388. t4.hW += d4;
  389.  
  390. t1.check();
  391. t2.check();
  392. t3.check();
  393. t4.check();
  394. if (t1.overflowed == 1)
  395. {
  396. t1.hW = 2;
  397. }
  398.  
  399. if (t2.overflowed == 1)
  400. {
  401. t2.hW = 2;
  402. }
  403.  
  404. d1 = 0;
  405. d2 = 0;
  406. d3 = 0;
  407. d4 = 0;
  408. Console.WriteLine("v1 : " + v1.status);
  409. Console.WriteLine("v2 : " + v2.status);
  410. Console.WriteLine("v2 poziom : " + t2.hW);
  411. }
  412.  
  413. }
  414. }
Advertisement
Add Comment
Please, Sign In to add comment