You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

370 lines
14 KiB

  1. using System;
  2. using HBLConsole.Interface;
  3. using HBLConsole.Service;
  4. using HBLConsole.Communication;
  5. using BPA.Message;
  6. using HBLConsole.Model;
  7. using System.Linq;
  8. using System.Threading;
  9. using HBLConsole.Factory;
  10. using BPA.Message.Enum;
  11. namespace HBLConsole.MORKD
  12. {
  13. public class Control_MORKD : IControl
  14. {
  15. GVL_MORKD mORKD = new GVL_MORKD();
  16. public void ConnectOk()
  17. {
  18. Main();
  19. ReadData();
  20. }
  21. public object GetT()
  22. {
  23. return mORKD;
  24. }
  25. public void DataParse<T>(T order)
  26. {
  27. if (order is MorkOrderPush morkOrderPush)
  28. {
  29. foreach (var item in morkOrderPush.GoodBatchings)
  30. {
  31. var res = Json<BatchingInfoPar>.Data.orderMaterialDelivery.BatchingInfo.FirstOrDefault(p => p.BatchingId == item.BatchingId);
  32. if (res != null)
  33. {
  34. if (ushort.TryParse(res.BatchingLoc, out ushort loc))
  35. {
  36. if (loc >= 1 && loc <= 5)
  37. {
  38. mORKD.RBTakeNoodleTask.Enqueue(new OrderLocInfo() { Loc = loc, SuborderId = morkOrderPush.SuborderId, BatchingId = res.BatchingId });
  39. }
  40. else if (loc >= 6 && loc <= 10)
  41. {
  42. mORKD.TakeSoupTask.Enqueue(new OrderLocInfo() { Loc = loc, SuborderId = morkOrderPush.SuborderId });
  43. }
  44. else if (loc >= 11 && loc <= 12)
  45. {
  46. mORKD.TakeBowlTask.Enqueue(new OrderLocInfo() { Loc = loc, SuborderId = morkOrderPush.SuborderId });
  47. }
  48. }
  49. }
  50. }
  51. }
  52. }
  53. public void Init()
  54. {
  55. ActionOperate.GetInstance.Register(new Action(() => { mORKD.InitControl(); }), "InitCommand");
  56. }
  57. public void Main()
  58. {
  59. ThreadOperate.GetInstance.StartLong(new Action(() =>
  60. {
  61. mORKD.AllowRun = mORKD.InitComplete && !mORKD.TemperatureReached;
  62. TakeBowlTask();
  63. TakeNoodleTask();
  64. OutNoodleTask();
  65. SingleDetect();
  66. TurntableControl();
  67. OutSoupTask();
  68. TakeSoupTask();
  69. Thread.Sleep(100);
  70. }), "MainTask");
  71. }
  72. public void ReadData()
  73. {
  74. ModbusTcpHelper.GetInstance.Readbool(1120, 29, new Action<bool[]>((bools) =>
  75. {
  76. mORKD.InitComplete = bools[0];
  77. mORKD.TurntableInPlace = bools[1];
  78. mORKD.RBTakeNoodleComplete = bools[2];
  79. for (int i = 0; i < 2; i++)
  80. {
  81. mORKD.AxisIdle[i] = bools[3 + i];
  82. mORKD.AxisAllowInvertedNoodle[i] = bools[5 + i];
  83. mORKD.AxisAllowInvertedSoup[i] = bools[7 + i];
  84. mORKD.SoupHeatComplete[i] = bools[9 + i];
  85. mORKD.BreakMechanismIdle[i] = bools[25 + i];
  86. }
  87. for (int i = 0; i < 6; i++)
  88. {
  89. mORKD.CookNoodleBasketIdle[i] = bools[11 + i];
  90. mORKD.CookNoodleComplete[i] = bools[17 + i];
  91. }
  92. mORKD.TakeNoodleRobotIdle = bools[23];
  93. mORKD.TakeSoupRobotIdle = bools[24];
  94. mORKD.TakeSoupComplete = bools[27];
  95. mORKD.PutNoodleTakeMealComplete = bools[28];
  96. }));
  97. ModbusTcpHelper.GetInstance.Readbool(1200, 11, new Action<bool[]>((bools) =>
  98. {
  99. mORKD.TurntableLowerLimit = bools[0];
  100. mORKD.TurntableUpLimit = bools[1];
  101. for (int i = 0; i < 5; i++)
  102. {
  103. mORKD.SoupMaterialShortage[i] = bools[2 + i];
  104. }
  105. for (int i = 0; i < 3; i++)
  106. {
  107. mORKD.OutMealDetect[i] = bools[7 + i];
  108. }
  109. mORKD.TemperatureReached = bools[10];
  110. }));
  111. }
  112. public void SimOrder<T>(T simOrder)
  113. {
  114. if (simOrder != null)
  115. {
  116. if (simOrder is SimOrderData simOrderData)
  117. {
  118. string subId = Guid.NewGuid().ToString();
  119. mORKD.RBTakeNoodleTask.Enqueue(new OrderLocInfo() { Loc = simOrderData.NoodleLoc, SuborderId = subId });
  120. mORKD.TakeBowlTask.Enqueue(new OrderLocInfo() { Loc = simOrderData.BowlLoc, SuborderId = subId });
  121. mORKD.TakeSoupTask.Enqueue(new OrderLocInfo() { Loc = simOrderData.SoupLoc, SuborderId = subId });
  122. MessageLog.GetInstance.Show($"添加订单:面条位置【{simOrderData.NoodleLoc}】,浇头位置【{simOrderData.SoupLoc}】,碗位置【{simOrderData.BowlLoc}】");
  123. }
  124. }
  125. }
  126. /// <summary>
  127. /// 取碗控制
  128. /// </summary>
  129. private void TakeBowlTask()
  130. {
  131. mORKD.AxisIdleIndex = mORKD.AxisIdle.GetIndex(true);
  132. mORKD.AxisIdleLockIndex = mORKD.AxisIdleLock.GetIndex(false);
  133. if (mORKD.AxisIdleLockIndex == mORKD.AxisIdleIndex)
  134. {
  135. if (mORKD.AxisIdleIndex >= 0 && mORKD.AxisIdleIndex <= 1)
  136. {
  137. if (mORKD.AllowRun && mORKD.TakeBowlTask.Count > 0)
  138. {
  139. if (mORKD.TakeBowlTask.TryDequeue(out OrderLocInfo orderLocInfo))
  140. {
  141. mORKD.AxisAllowInvertedNoodleID[mORKD.AxisIdleIndex] = orderLocInfo.SuborderId;
  142. mORKD.DropBowlStart((ushort)mORKD.AxisIdleIndex, orderLocInfo.Loc);
  143. mORKD.AxisIdleLock[mORKD.AxisIdleLockIndex] = true;
  144. MessageLog.GetInstance.Show($"执行取碗控制,位置:[{orderLocInfo.Loc}]");
  145. }
  146. }
  147. }
  148. }
  149. }
  150. /// <summary>
  151. /// 转台控制
  152. /// </summary>
  153. private void TurntableControl()
  154. {
  155. if (mORKD.InitComplete && !mORKD.TurntableInterlock && !mORKD.AllowTakeNoodle && mORKD.RBTakeNoodleTask.Count > 0)
  156. {
  157. var result = Json<BatchingInfoPar>.Data.orderMaterialDelivery.BatchingInfo.Where(p => p.BatchingId == mORKD.RBTakeNoodleTask.ElementAt(0).BatchingId).ToList();
  158. if (result != null)
  159. {
  160. foreach (var item in result)
  161. {
  162. if (ushort.TryParse(item.BatchingLoc, out ushort loc))
  163. {
  164. if (!mORKD.TurntableLocLists.Contains(loc))
  165. {
  166. mORKD.TurntableStart(loc);
  167. return;
  168. }
  169. }
  170. }
  171. }
  172. }
  173. //转台到位检测
  174. if (RTrig.GetInstance("TurntableInPlace").Start(mORKD.TurntableInPlace))
  175. {
  176. if (mORKD.TurntableInterlock)
  177. {
  178. if (mORKD.TurntableLowerLimit)
  179. {
  180. mORKD.TurntableLocLists.Clear();
  181. mORKD.AllowTakeNoodle = true;
  182. }
  183. else
  184. {
  185. mORKD.TurntableInterlock = false;
  186. mORKD.AllowTakeNoodle = false;
  187. }
  188. }
  189. }
  190. }
  191. /// <summary>
  192. /// 取面任务
  193. /// </summary>
  194. private void TakeNoodleTask()
  195. {
  196. //取面控制
  197. if (mORKD.AllowRun && mORKD.TakeNoodleRobotIdle && !mORKD.RobotTaskInterlock && mORKD.AllowTakeNoodle)
  198. {
  199. if (mORKD.TurntableInPlace && !mORKD.OutNoodleing && mORKD.RBTakeNoodleTask.Count > 0)
  200. {
  201. int loc = mORKD.CookNoodleBasketIdle.GetIndex(false);//查找煮面炉空闲位置
  202. if (loc >= 0 && loc <= 5)
  203. {
  204. if (mORKD.RBTakeNoodleTask.TryDequeue(out OrderLocInfo orderLocInfo))
  205. {
  206. mORKD.RBTakeNoodleStart((ushort)(loc + 1), orderLocInfo.Loc);
  207. mORKD.CookNodelId[loc] = orderLocInfo.SuborderId;
  208. SimpleFactory.GetInstance.OrderChanged(orderLocInfo.SuborderId, ORDER_STATUS.COOKING);
  209. MessageLog.GetInstance.Show($"订单【{orderLocInfo.SuborderId}】,煮面栏:[{loc + 1}]");
  210. mORKD.TakeNoodleInterlock = true;
  211. }
  212. }
  213. }
  214. }
  215. //机器人取面完成信号检测
  216. if (RTrig.GetInstance("TakeNoodleComplete").Start(mORKD.RBTakeNoodleComplete))
  217. {
  218. mORKD.TakeNoodleInterlock = false;
  219. mORKD.AllowTakeNoodle = false;
  220. MessageLog.GetInstance.Show("转台取面完成");
  221. mORKD.TakeNoodleCompleteReset();
  222. }
  223. }
  224. /// <summary>
  225. /// 出餐控制
  226. /// </summary>
  227. private void OutNoodleTask()
  228. {
  229. for (ushort i = 0; i < 6; i++)
  230. {
  231. if (RTrig.GetInstance($"CookNoodleCompleteTask{i}").Start(mORKD.CookNoodleComplete[i]))
  232. {
  233. mORKD.CookNoodleCompleteTask.Enqueue(i);
  234. }
  235. }
  236. if (mORKD.RobotTaskInterlock && !mORKD.TakeNoodleInterlock && mORKD.TakeNoodleRobotIdle)
  237. {
  238. if (mORKD.CookNoodleCompleteTask.Count > 0)
  239. {
  240. string id = mORKD.CookNodelId[mORKD.CookNoodleCompleteTask.ElementAt(0)];
  241. int index = mORKD.AxisAllowInvertedNoodleID.GetIndex(id);
  242. if (index >= 0 && index <= 1)
  243. {
  244. if (mORKD.AxisAllowInvertedNoodle[index])
  245. {
  246. if (mORKD.CookNoodleCompleteTask.TryDequeue(out ushort loc))
  247. {
  248. mORKD.OutNoodleing = true;
  249. mORKD.RBOutNoodleStart((ushort)(loc + 1), (ushort)(index + 1));
  250. mORKD.AxisIdleLock[index] = false;
  251. mORKD.AxisAllowInvertedSoupID[index] = mORKD.AxisAllowInvertedNoodleID[index];
  252. mORKD.AxisAllowInvertedNoodleID[index] = string.Empty;
  253. MessageLog.GetInstance.Show($"从{loc + 1}号位置取面");
  254. MessageLog.GetInstance.Show($"倒入{index + 1}号碗位置");
  255. }
  256. }
  257. }
  258. }
  259. }
  260. int mlCount = mORKD.CookNoodleBasketIdle.Where(p => p == true).ToList().Count;
  261. mORKD.RobotTaskInterlock = mORKD.CookNoodleCompleteTask.Count > 0 && (mlCount >= 2 || mORKD.RBTakeNoodleTask.Count == 0);
  262. }
  263. /// <summary>
  264. /// 取浇头控制
  265. /// </summary>
  266. private void TakeSoupTask()
  267. {
  268. int index = mORKD.BreakMechanismIdle.GetIndex(true);
  269. if (index >= 0 && index <= 1)
  270. {
  271. if (mORKD.TakeSoupTask.Count > 0 && !mORKD.AllowTakeSoup && mORKD.TakeSoupRobotIdle && mORKD.AllowRun)
  272. {
  273. if (mORKD.TakeSoupTask.TryDequeue(out OrderLocInfo orderLocInfo))
  274. {
  275. mORKD.SoupHeatCompleteID[index] = orderLocInfo.SuborderId;
  276. mORKD.RBTakeSoupStart(orderLocInfo.Loc, (ushort)(index + 1));
  277. SimpleFactory.GetInstance.OrderChanged(orderLocInfo.SuborderId, ORDER_STATUS.COOKING);
  278. }
  279. }
  280. }
  281. if (RTrig.GetInstance("TakeSoupComplete").Start(mORKD.TakeSoupComplete))
  282. {
  283. mORKD.AllowTakeSoup = false;
  284. MessageLog.GetInstance.Show("取浇头完成");
  285. mORKD.TakeSoupCompleteReset();
  286. }
  287. }
  288. /// <summary>
  289. /// 浇头加热完成出浇头控制
  290. /// </summary>
  291. private void OutSoupTask()
  292. {
  293. if (!mORKD.AllowTakeSoup && !mORKD.AllowPutSoup && mORKD.TakeSoupRobotIdle)
  294. {
  295. mORKD.PutNoodleLoc = mORKD.OutMealDetect.GetIndex(false);
  296. if (mORKD.PutNoodleLoc >= 0 && mORKD.PutNoodleLoc <= 2)
  297. {
  298. for (int i = 0; i < 2; i++)
  299. {
  300. if (mORKD.SoupHeatComplete[i] && mORKD.SoupHeatCompleteID[i].Length > 0)
  301. {
  302. for (int m = 0; m < 2; m++)
  303. {
  304. if (mORKD.AxisAllowInvertedSoup[m] && mORKD.AxisAllowInvertedSoupID[m].Length > 0)
  305. {
  306. if (mORKD.SoupHeatCompleteID[i] == mORKD.AxisAllowInvertedSoupID[m])
  307. {
  308. mORKD.HeatCompleteTakeSoupStart((ushort)(i + 1), (ushort)(m + 1), (ushort)(mORKD.PutNoodleLoc + 1));
  309. mORKD.OutMealId[mORKD.PutNoodleLoc] = mORKD.AxisAllowInvertedSoupID[m];
  310. mORKD.AxisAllowInvertedSoupID[m] = string.Empty;
  311. mORKD.SoupHeatCompleteID[i] = string.Empty;
  312. }
  313. }
  314. }
  315. }
  316. }
  317. }
  318. }
  319. if (RTrig.GetInstance("PutNoodleTakeMealComplete").Start(mORKD.PutNoodleTakeMealComplete))
  320. {
  321. mORKD.AllowPutSoup = false;
  322. SimpleFactory.GetInstance.OrderChanged(mORKD.OutMealId[mORKD.PutNoodleLoc], ORDER_STATUS.COMPLETED_COOK);
  323. mORKD.PutNoodleTakeMealCompleteReset();
  324. }
  325. }
  326. /// <summary>
  327. /// 信号检测
  328. /// </summary>
  329. private void SingleDetect()
  330. {
  331. }
  332. }
  333. }