终端一体化运控平台
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.
 
 
 

608 lines
23 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using BPA.Message.Enum;
  4. using BPASmartClient.Device;
  5. using BPASmartClient.EventBus;
  6. using BPASmartClient.Model;
  7. using BPASmartClient.Peripheral;
  8. using static BPASmartClient.EventBus.EventBus;
  9. using BPASmartClient.Helper;
  10. using System.Threading;
  11. using BPASmartClient.Message;
  12. using BPA.Message;
  13. using System.Linq;
  14. using BPASmartClient.Model.PLC;
  15. using System.Threading.Tasks;
  16. namespace BPASmartClient.MorkS
  17. {
  18. public class Control : BaseDevice
  19. {
  20. public override DeviceClientType DeviceType => DeviceClientType.MORKS;
  21. GVL_MORKS mORKS = new GVL_MORKS();
  22. /// <summary>
  23. /// 订单物料信息
  24. /// </summary>
  25. private OrderMaterialDelivery orderMaterialDelivery { get; set; } = new OrderMaterialDelivery();
  26. /// <summary>
  27. /// 配方数据信息
  28. /// </summary>
  29. private RecipeBoms recipeBoms { get; set; } = new RecipeBoms();
  30. int OrderCount;
  31. bool Initing;
  32. public override void StartMain()
  33. {
  34. ServerInit();
  35. DataParse();
  36. ReadPLCData();
  37. Main();
  38. //初始化中
  39. }
  40. public override void Stop()
  41. {
  42. }
  43. private void ServerInit()
  44. {
  45. //物料信息
  46. EventBus.EventBus.GetInstance().Subscribe<MaterialDeliveryEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  47. {
  48. if (@event == null) return;
  49. if (@event is MaterialDeliveryEvent material)
  50. {
  51. orderMaterialDelivery = material.orderMaterialDelivery;
  52. }
  53. });
  54. //配方数据信息
  55. EventBus.EventBus.GetInstance().Subscribe<RecipeBomEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  56. {
  57. if (@event == null) return;
  58. if (@event is RecipeBomEvent recipe)
  59. {
  60. recipeBoms = recipe.recipeBoms;
  61. }
  62. });
  63. }
  64. private void OrderChange(string subid, ORDER_STATUS oRDER_STATUS)
  65. {
  66. EventBus.EventBus.GetInstance().Publish(new OrderStatusChangedEvent() { Id = DeviceId, Status = oRDER_STATUS, SubOrderId = subid });
  67. }
  68. private void ReadData(string address, ushort len = 1, Action<bool[]> action = null)
  69. {
  70. EventBus.EventBus.GetInstance().Publish(new ReadModel() { Id = DeviceId, Address = address, Length = len }, (o) =>
  71. {
  72. if (o != null && o.Length > 0 && o[0] is bool[] bools)
  73. {
  74. action(bools);
  75. }
  76. });
  77. }
  78. private void ReadPLCData()
  79. {
  80. ThreadManage.GetInstance().StartLong(new Action(() =>
  81. {
  82. ReadData("M0.3", 3, new Action<bool[]>((bools) =>
  83. {
  84. mORKS.RobotTakeNoodle = bools[0];
  85. mORKS.RobotOutMeal = bools[1];
  86. mORKS.MoveTurntable = bools[2];
  87. }));
  88. ReadData("M100.0", 16, new Action<bool[]>((bools) =>
  89. {
  90. mORKS.InitComplete = bools[0];
  91. mORKS.TakeBowlIdle = bools[1];
  92. mORKS.TemperatureReached = bools[2];
  93. mORKS.AllowFallNoodle = bools[3];
  94. mORKS.RbTakeNoodleComplete = bools[4];
  95. mORKS.RbFallNoodleComplete = bools[5];
  96. mORKS.RbOutMealComplete = bools[6];
  97. mORKS.RobotIdle = bools[7];
  98. mORKS.TakeMealDetect = bools[8];
  99. mORKS.MissingBowl = bools[9];
  100. Initing = bools[10];
  101. mORKS.TurntableLowerLimit = bools[11];
  102. mORKS.MissingBowlSignal2 = bools[12];
  103. mORKS.TurntableUpLimit = bools[13];
  104. mORKS.FeedComplete = bools[14];
  105. mORKS.TurntableMoveInPlace = bools[15];
  106. }));
  107. ReadData("M235.0", 1, new Action<bool[]>((bools) => { mORKS.Error = bools[0]; }));
  108. //var errorStatus = ModbusTcpHelper.GetInstance.Read((ushort)ModbusTcpHelper.GetInstance.GetBoolAddress("M235.0"), ReadType.Coils, 1);
  109. //if (errorStatus != null && errorStatus is bool error) mORKS.Error = error;
  110. ReadData("M102.0", 7, new Action<bool[]>((bools) =>
  111. {
  112. for (int i = 0; i < 6; i++)
  113. {
  114. mORKS.NoodleCookerStatus[i] = bools[i];
  115. }
  116. mORKS.Feeding = bools[6];
  117. }));
  118. ReadData("M103.0", 6, new Action<bool[]>((bools) =>
  119. {
  120. for (int i = 0; i < 6; i++)
  121. {
  122. mORKS.CookNoodlesComplete[i] = bools[i];
  123. }
  124. }));
  125. ReadData("M550.0", 20, new Action<bool[]>((bools) =>
  126. {
  127. //alarm.MachineLeftLowTemperature = bools[0];
  128. //alarm.MachineRightLowTemperature = bools[1];
  129. //alarm.Supply1_LossBowl = bools[2];
  130. //alarm.Supply2_LossBowl = bools[3];
  131. //alarm.Supply1_ErrorOutBowl = bools[4];
  132. //alarm.Supply2_ErrorOutBowl = bools[5];
  133. //alarm.PushBowlCylinderError = bools[6];
  134. //alarm.NoodleMacCommunicateError = bools[7];
  135. //alarm.DosingMacCommunicateError = bools[8];
  136. //alarm.RobotMacCommunicateError = bools[9];
  137. //alarm.RobotInitError = bools[11];
  138. //alarm.RobotUrgentStop = bools[12];
  139. //alarm.RobotNotInRemoteMode = bools[13];
  140. //alarm.RobotNotInReady = bools[14];
  141. //alarm.RobotSelfInException = bools[15];
  142. }));
  143. EventBus.EventBus.GetInstance().Publish(new ReadModel() { Id = DeviceId, Address = "VW372", Length = 1 }, (o) =>
  144. {
  145. if (o != null && o.Length > 0 && o[0] is ushort value)
  146. {
  147. mORKS.TurntableFeedbackloc = value;
  148. }
  149. });
  150. //var ResLoc = ModbusTcpHelper.GetInstance.Read(286, ReadType.HoldingRegisters);
  151. //if (ResLoc != null)
  152. //{
  153. // if (ResLoc is ushort loc)
  154. // {
  155. // mORKS.TurntableFeedbackloc = loc;
  156. // }
  157. //}
  158. Thread.Sleep(500);
  159. }), "ReadPLCData", true);
  160. }
  161. /// <summary>
  162. /// 数据解析
  163. /// </summary>
  164. private void DataParse()
  165. {
  166. EventBus.EventBus.GetInstance().Subscribe<DoOrderEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBackHandle)
  167. {
  168. if (@event == null) return;
  169. if (@event is DoOrderEvent order)
  170. {
  171. OrderCount++;
  172. MessageLog.GetInstance.Show($"接收到{OrderCount}次订单");
  173. foreach (var item in order.MorkOrder.GoodBatchings)
  174. {
  175. var res = orderMaterialDelivery?.BatchingInfo?.FirstOrDefault(p => p.BatchingId == item.BatchingId);
  176. if (res != null)
  177. {
  178. if (ushort.TryParse(res.BatchingLoc, out ushort loc))
  179. {
  180. if (loc >= 1 && loc <= 5)
  181. {
  182. if (mORKS.RBTakeNoodleTask.FirstOrDefault(p => p.SuborderId == order.MorkOrder.SuborderId) == null)
  183. mORKS.RBTakeNoodleTask.Enqueue(new OrderLocInfo() { Loc = ushort.Parse(res.BatchingLoc), SuborderId = order.MorkOrder.SuborderId, BatchingId = res.BatchingId });
  184. }
  185. else if (loc >= 10 && loc <= 11)
  186. {
  187. int index = 0;
  188. if (recipeBoms != null)
  189. {
  190. index = Array.FindIndex(recipeBoms.RecipeIds?.ToArray(), p => p.RecipeId == order.MorkOrder.RecipeId);
  191. index++;
  192. }
  193. if (mORKS.TakeBowlTask.FirstOrDefault(p => p.SuborderId == order.MorkOrder.SuborderId) == null)
  194. mORKS.TakeBowlTask.Enqueue(new OrderLocInfo()
  195. {
  196. Loc = ushort.Parse(res.BatchingLoc),
  197. SuborderId = order.MorkOrder.SuborderId,
  198. RecipeNumber = (index >= 1 && index <= 10) ? (ushort)index : (ushort)0
  199. });
  200. }
  201. }
  202. }
  203. }
  204. }
  205. });
  206. }
  207. public void Main()
  208. {
  209. ThreadManage.GetInstance().StartLong(new Action(() =>
  210. {
  211. mORKS.AllowRun = mORKS.InitComplete;
  212. //IsHealth = mORKS.Error && mORKS.InitComplete;
  213. IsHealth = true;
  214. TakeBowlTask();
  215. TakeNoodleTask();
  216. OutNoodleTask();
  217. SingleDetect();
  218. TurntableControl();
  219. Thread.Sleep(100);
  220. }), "MainTask");
  221. }
  222. /// <summary>
  223. /// 取碗控制
  224. /// </summary>
  225. private void TakeBowlTask()
  226. {
  227. if (mORKS.AllowRun && mORKS.TakeBowlTask.Count > 0 && !mORKS.TakeBowlIdle && !mORKS.TakeBowlInterlock)
  228. {
  229. if (mORKS.TakeBowlTask.TryDequeue(out OrderLocInfo orderLocInfo))
  230. {
  231. mORKS.TakeBowlId = orderLocInfo.SuborderId;
  232. TakeBowlControl(orderLocInfo.Loc);
  233. SetRecipeNumber(orderLocInfo.RecipeNumber);
  234. OrderChange(mORKS.TakeBowlId, ORDER_STATUS.COOKING);
  235. MessageLog.GetInstance.Show($"订单【{ mORKS.TakeBowlId}】执行取碗控制,位置:[{orderLocInfo.Loc}]");
  236. }
  237. mORKS.TakeBowlInterlock = true;
  238. }
  239. }
  240. /// <summary>
  241. /// 转台控制
  242. /// </summary>
  243. private void TurntableControl()
  244. {
  245. //if (GeneralConfig.EnableLocalSimOrder)
  246. //{
  247. // //不做轮询,直接取面,模拟订单使用
  248. // if (mORKS.TurntableMoveInPlace && !mORKS.Feeding && mORKS.InitComplete && !mORKS.AllowTakeNoodle && mORKS.RBTakeNoodleTask.Count > 0)
  249. // {
  250. // if (mORKS.TurntableLowerLimit)
  251. // {
  252. // TurntableStart(mORKS.RBTakeNoodleTask.ElementAt(0).Loc);
  253. // mORKS.TurntableLocLists.Clear();
  254. // mORKS.AllowTakeNoodle = true;
  255. // MessageLog.GetInstance.Show($"控制机器人去转台【{mORKS.RBTakeNoodleTask.ElementAt(0).Loc}】号位置取面");
  256. // }
  257. // }
  258. //}
  259. //else
  260. {
  261. //正常轮询
  262. if (mORKS.TurntableMoveInPlace && !mORKS.Feeding && mORKS.InitComplete && !mORKS.AllowTakeNoodle && mORKS.RBTakeNoodleTask.Count > 0)
  263. {
  264. var result = orderMaterialDelivery.BatchingInfo.Where(p => p.BatchingId == mORKS.RBTakeNoodleTask.ElementAt(0).BatchingId).ToList();
  265. if (result != null)
  266. {
  267. var res = result.FirstOrDefault(P => P.BatchingLoc == mORKS.TurntableFeedbackloc.ToString());
  268. if (mORKS.TurntableLowerLimit && res != null)
  269. {
  270. TurntableStart(mORKS.TurntableFeedbackloc);
  271. mORKS.TurntableLocLists.Clear();
  272. mORKS.AllowTakeNoodle = true;
  273. MessageLog.GetInstance.Show($"控制机器人去转台【{mORKS.TurntableFeedbackloc}】号位置取面");
  274. }
  275. else
  276. {
  277. if (!mORKS.TurntableInterlock)
  278. {
  279. foreach (var item in result)
  280. {
  281. if (ushort.TryParse(item.BatchingLoc, out ushort loc))
  282. {
  283. if (mORKS.TurntableFeedbackloc != loc && !mORKS.TurntableLocLists.Contains(loc))
  284. {
  285. TurntableStart(loc);
  286. MessageLog.GetInstance.Show($"没有物料检测的启动转台控制,转台位置:[{loc}]");
  287. break;
  288. }
  289. else if (mORKS.TurntableFeedbackloc == loc && !mORKS.TurntableLocLists.Contains(loc)) mORKS.TurntableLocLists.Add(loc);
  290. }
  291. }
  292. }
  293. }
  294. }
  295. else MessageLog.GetInstance.Show("未找到可用的物料信息");
  296. }
  297. }
  298. //转台到位检测
  299. if (RTrig.GetInstance("TurntableInPlace").Start(mORKS.TurntableMoveInPlace && mORKS.CurrentLoc == mORKS.TurntableFeedbackloc))
  300. {
  301. mORKS.CurrentLoc = 0;
  302. mORKS.TurntableInterlock = false;
  303. MessageLog.GetInstance.Show("转台到位检测");
  304. }
  305. //补料完成检测
  306. if (RTrig.GetInstance("FeedComplete").Start(mORKS.FeedComplete))
  307. {
  308. if (!mORKS.AllowTakeNoodle && mORKS.TurntableLocLists.Count > 0)
  309. {
  310. mORKS.TurntableLocLists.Clear();
  311. mORKS.TurntableInterlock = false;
  312. MessageLog.GetInstance.Show("补料完成检测");
  313. }
  314. }
  315. }
  316. /// <summary>
  317. /// 取面任务
  318. /// </summary>
  319. private void TakeNoodleTask()
  320. {
  321. //取面控制
  322. if (mORKS.AllowRun && mORKS.RobotIdle && !mORKS.Feeding && !mORKS.RobotTaskInterlock && mORKS.AllowTakeNoodle && mORKS.TurntableMoveInPlace && !mORKS.TakeNoodleInterlock && !mORKS.OutNoodleing && mORKS.RBTakeNoodleTask.Count > 0)
  323. {
  324. int loc = Array.FindIndex(mORKS.NoodleCookerStatus, p => p == false);//查找煮面炉空闲位置
  325. if (loc >= 0 && loc <= 5)
  326. {
  327. if (mORKS.RBTakeNoodleTask.TryDequeue(out OrderLocInfo orderLocInfo))
  328. {
  329. mORKS.CookNodelId[loc] = orderLocInfo.SuborderId;
  330. SetFallNoodleLoc((ushort)(loc + 1));
  331. //机器人开始取面
  332. RobotTakeNoodle();
  333. OrderChange(orderLocInfo.SuborderId, ORDER_STATUS.COOKING);
  334. MessageLog.GetInstance.Show($"订单【{orderLocInfo.SuborderId}】,机器人倒面至【{loc + 1}】号煮面栏");
  335. //写入煮面时间
  336. //List<ushort> values = new List<ushort>();
  337. //values.Add(Json<KeepDataBase>.Data.parSets.ElementAt(loc).Minute);
  338. //values.Add(Json<KeepDataBase>.Data.parSets.ElementAt(loc).Second);
  339. //ModbusTcpHelper.GetInstance.Write((ushort)ModbusTcpHelper.GetInstance.GetWordAddress($"VW{116 + (loc * 6)}"), WriteType.HoldingRegisters, values.ToArray());
  340. mORKS.TakeNoodleInterlock = true;
  341. }
  342. }
  343. }
  344. }
  345. /// <summary>
  346. /// 出餐控制
  347. /// </summary>
  348. private void OutNoodleTask()
  349. {
  350. if (mORKS.AllowFallNoodle && mORKS.RobotTaskInterlock && !mORKS.TakeNoodleInterlock && mORKS.RobotIdle && !mORKS.TakeMealDetect)
  351. {
  352. int loc = Array.FindIndex(mORKS.CookNodelId, p => p == mORKS.IngredientsCompleteId && p.Length > 0);
  353. if (loc >= 0 && loc <= 5)
  354. {
  355. if (mORKS.CookNoodlesComplete[loc])
  356. {
  357. SetTakeNoodleLoc((ushort)(loc + 1));
  358. RobotOutMeal();
  359. CookNoodleStatusReset((ushort)(loc + 1));
  360. ResetAllowFallNoodle();
  361. mORKS.OutMealId = mORKS.IngredientsCompleteId;
  362. mORKS.IngredientsCompleteId = string.Empty;
  363. mORKS.CookNodelId[loc] = string.Empty;
  364. MessageLog.GetInstance.Show($"{loc + 1}号位置出餐控制");
  365. mORKS.OutNoodleing = true;
  366. }
  367. }
  368. }
  369. }
  370. /// <summary>
  371. /// 信号检测
  372. /// </summary>
  373. private void SingleDetect()
  374. {
  375. //允许倒面信号检测
  376. if (RTrig.GetInstance("AllowFallNoodle").Start(mORKS.AllowFallNoodle))
  377. {
  378. mORKS.IngredientsCompleteId = mORKS.TakeBowlId;
  379. mORKS.TakeBowlId = string.Empty;
  380. MessageLog.GetInstance.Show($"碗到位,允许到面,{mORKS.IngredientsCompleteId}");
  381. mORKS.TakeBowlInterlock = false;
  382. }
  383. //出餐完成信号检测
  384. if (RTrig.GetInstance("CompleteChange").Start(mORKS.RbOutMealComplete))
  385. {
  386. OrderChange(mORKS.OutMealId, ORDER_STATUS.COMPLETED_COOK);
  387. MessageLog.GetInstance.Show($"订单【{mORKS.OutMealId}】制作完成");
  388. mORKS.OutNoodleing = false;
  389. }
  390. //取餐完成逻辑处理
  391. if (DelayRTrig.GetInstance("CompleteChange1").Start(mORKS.RbOutMealComplete && !mORKS.TakeMealDetect, 2))
  392. {
  393. OrderChange(mORKS.OutMealId, ORDER_STATUS.COMPLETED_TAKE);
  394. MessageLog.GetInstance.Show($"订单【{mORKS.OutMealId}】取餐完成");
  395. ResetCookComplete();
  396. mORKS.OutMealId = string.Empty;
  397. }
  398. //机器人取面完成信号检测
  399. if (RTrig.GetInstance("TakeNoodleComplete").Start(mORKS.RbTakeNoodleComplete))
  400. {
  401. mORKS.TakeNoodleInterlock = false;
  402. mORKS.AllowTakeNoodle = false;
  403. mORKS.TurntableInterlock = false;
  404. MessageLog.GetInstance.Show("机器人取面完成信号检测");
  405. TakeNoodleCompleteReset();
  406. }
  407. int OutMealRequstCount = mORKS.CookNoodlesComplete.Where(p => p == true).ToList().Count;
  408. int mlCount = mORKS.NoodleCookerStatus.Where(p => p == true).ToList().Count;
  409. mORKS.RobotTaskInterlock = OutMealRequstCount > 0 && mORKS.AllowFallNoodle && (mlCount >= 2 || mORKS.RBTakeNoodleTask.Count == 0);
  410. }
  411. #region PLC 控制函数
  412. private void WriteData(string address, object value)
  413. {
  414. EventBus.EventBus.GetInstance().Publish(new WriteModel() { Id = DeviceId, Address = address, Value = value });
  415. }
  416. /// <summary>
  417. /// 写入配方数据到 PLC
  418. /// </summary>
  419. private void WriteRecipeBoms()
  420. {
  421. List<ushort> recipeBoms = new List<ushort>();
  422. if (this.recipeBoms == null) return;
  423. foreach (var item in this.recipeBoms.RecipeIds)
  424. {
  425. foreach (var rec in item.Recipes)
  426. {
  427. recipeBoms.Add((ushort)rec);
  428. }
  429. }
  430. if (recipeBoms.Count > 0)
  431. {
  432. //配方数据地址范围:VW2000 - VW2278
  433. WriteData("VW2000", recipeBoms.ToArray());
  434. }
  435. else { MessageLog.GetInstance.Show("配方数据为空"); }
  436. }
  437. /// <summary>
  438. /// 取面完成复位
  439. /// </summary>
  440. private void TakeNoodleCompleteReset()
  441. {
  442. WriteData("M100.4", false);
  443. }
  444. /// <summary>
  445. /// 指定煮面口状态复位
  446. /// </summary>
  447. /// <param name="num"></param>
  448. private void CookNoodleStatusReset(int num)
  449. {
  450. if (num >= 1 && num <= 6)
  451. {
  452. WriteData($"102.{num - 1}", false);
  453. MessageLog.GetInstance.Show($"{num}号煮面口占用复位");
  454. }
  455. }
  456. /// <summary>
  457. /// 写配方编号
  458. /// </summary>
  459. /// <param name="num"></param>
  460. private void SetRecipeNumber(ushort num)
  461. {
  462. WriteData("VW0", num);
  463. }
  464. /// <summary>
  465. /// 启动转台
  466. /// </summary>
  467. /// <param name="loc"></param>
  468. private void TurntableStart(ushort loc)
  469. {
  470. mORKS.CurrentLoc = loc;
  471. mORKS.TurntableInterlock = true;
  472. mORKS.TurntableLocLists.Add(loc);
  473. WriteData("VW0", loc);
  474. WriteData("M0.5", true);
  475. }
  476. /// <summary>
  477. /// 设置倒面位置
  478. /// </summary>
  479. /// <param name="loc"></param>
  480. private void SetFallNoodleLoc(ushort loc)
  481. {
  482. WriteData("VW4", loc);
  483. }
  484. /// <summary>
  485. /// 设置取面位置
  486. /// </summary>
  487. /// <param name="loc"></param>
  488. private void SetTakeNoodleLoc(ushort loc)
  489. {
  490. WriteData("VW6", loc);
  491. }
  492. /// <summary>
  493. /// 取碗控制
  494. /// </summary>
  495. /// <param name="loc"></param>
  496. private void TakeBowlControl(ushort loc)
  497. {
  498. if (loc == 10)//小碗
  499. {
  500. WriteData("M0.1", true);
  501. }
  502. else if (loc == 11)//大碗
  503. {
  504. WriteData("M0.2", true);
  505. }
  506. }
  507. /// <summary>
  508. /// 机器人取面
  509. /// </summary>
  510. private void RobotTakeNoodle()
  511. {
  512. WriteData("M0.3", true);
  513. }
  514. /// <summary>
  515. /// 机器人取餐
  516. /// </summary>
  517. private void RobotOutMeal()
  518. {
  519. WriteData("M0.4", true);
  520. }
  521. /// <summary>
  522. /// 制作完成信号复位
  523. /// </summary>
  524. private void ResetCookComplete()
  525. {
  526. WriteData("M100.6", false);
  527. }
  528. /// <summary>
  529. /// 复位允许取面信号
  530. /// </summary>
  531. private void ResetAllowFallNoodle()
  532. {
  533. WriteData("M100.3", false);
  534. }
  535. /// <summary>
  536. /// 设备初始化
  537. /// </summary>
  538. public async void DeviceInit()
  539. {
  540. WriteData("M0.0", true);
  541. await Task.Delay(1000);
  542. WriteData("M0.0", false);
  543. }
  544. #endregion
  545. }
  546. }