//#define test using BPA.Message; using HBLConsole.Communication; using HBLConsole.Factory; using HBLConsole.Interface; using HBLConsole.Model; using HBLConsole.Service; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using BPA.Message.Enum; namespace HBLConsole.MORKS { public class Control_MORKS : IControl { public GVL_MORKS mORKS { get; set; } = new GVL_MORKS(); public void Init() { Test(); ActionManagerment.GetInstance.Register(new Action(() => { DeviceInit(); }), "InitCommand"); } public void ConnectOk() { //WriteRecipeBoms(); ReadData(); Main(); //ResetProgram(); MessageLog.GetInstance.Show("MORKS 设备初始化完成"); } /// /// 测试程序 /// private void Test() { #if test ThreadManagerment.GetInstance.StartLong(new Action(() => { while (Json.GetInstance.Base.morkOrderPushes.Count > 0) { var result = Json.GetInstance.Base.morkOrderPushes.ElementAt(0); SimpleFactory.GetInstance.OrderChanged(result.OrderPush.SuborderId, ORDER_STATUS.COOKING); MessageLog.GetInstance.Show($"{result.OrderPush.GoodsName},{ORDER_STATUS.COOKING}"); Thread.Sleep(5000); SimpleFactory.GetInstance.OrderChanged(result.OrderPush.SuborderId, ORDER_STATUS.COMPLETED_COOK); MessageLog.GetInstance.Show($"{result.OrderPush.GoodsName},{ORDER_STATUS.COMPLETED_COOK}"); Thread.Sleep(5000); SimpleFactory.GetInstance.OrderChanged(result.OrderPush.SuborderId, ORDER_STATUS.COMPLETED_TAKE); MessageLog.GetInstance.Show($"{result.OrderPush.GoodsName},{ORDER_STATUS.COMPLETED_TAKE}"); Thread.Sleep(5000); Json.GetInstance.Base.morkOrderPushes.RemoveAt(0); } Thread.Sleep(500); }), "test"); #endif } /// /// 复位程序 /// private void ResetProgram() { ThreadManagerment.GetInstance.StartLong(new Action(() => { if (RTrig.GetInstance("ResetProgram").Start(mORKS.DeviceIniting)) { ThreadManagerment.GetInstance.StopTask("MainTask", new Action(() => { mORKS.AllowRun = false; TakeBowlId = string.Empty; IngredientsCompleteId = string.Empty; CookNodelId = new string[6] { string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, }; mORKS.RobotTaskInterlock = false; OutMealId = string.Empty; mORKS.TakeBowlInterlock = false; mORKS.TakeNoodleInterlock = false; mORKS.OutNoodleing = false; Main(); })); } Thread.Sleep(10); }), "ResetProgram"); } /// /// 数据读取 /// public void ReadData() { ThreadManagerment.GetInstance.StartLong(new Action(() => { ModbusTcpHelper.GetInstance.Readbool(323, 3, new Action((bools) => { mORKS.RobotTakeNoodle = bools[0]; mORKS.RobotOutMeal = bools[1]; mORKS.MoveTurntable = bools[2]; })); ModbusTcpHelper.GetInstance.Readbool(1120, 16, new Action((bools) => { mORKS.InitComplete = bools[0]; mORKS.TakeBowlIdle = bools[1]; mORKS.TemperatureReached = bools[2]; mORKS.AllowFallNoodle = bools[3]; mORKS.RbTakeNoodleComplete = bools[4]; mORKS.RbFallNoodleComplete = bools[5]; mORKS.RbOutMealComplete = bools[6]; mORKS.RobotIdle = bools[7]; mORKS.TakeMealDetect = bools[8]; mORKS.MissingBowl = bools[9]; mORKS.DeviceIniting = bools[10]; mORKS.TurntableLowerLimit = bools[11]; mORKS.MissingBowlSignal2 = bools[12]; mORKS.TurntableUpLimit = bools[13]; mORKS.TurntableMoveInPlace = bools[15]; })); ModbusTcpHelper.GetInstance.Readbool(1136, 6, new Action((bools) => { for (int i = 0; i < 6; i++) { mORKS.NoodleCookerStatus[i] = bools[i]; } })); ModbusTcpHelper.GetInstance.Readbool(1144, 6, new Action((bools) => { for (int i = 0; i < 6; i++) { mORKS.CookNoodlesComplete[i] = bools[i]; } })); Thread.Sleep(500); }), "ReadPLCData"); } public void SimOrder(T simOrder) { if (simOrder != null) { if (simOrder is SimOrderData simOrderData) { string subId = Guid.NewGuid().ToString(); mORKS.RBTakeNoodleTask.Enqueue(new OrderLocInfo() { Loc = simOrderData.NoodleLoc, SuborderId = subId }); mORKS.TakeBowlTask.Enqueue(new OrderLocInfo() { Loc = simOrderData.BowlLoc, SuborderId = subId }); MessageLog.GetInstance.Show($"添加订单:面条位置【{simOrderData.NoodleLoc}】,碗位置【{simOrderData.BowlLoc}】"); } } } /// /// 数据解析 /// public void DataParse(T order) { if (order is MorkOrderPush morkOrderPush) { foreach (var item in morkOrderPush.GoodBatchings) { var res = Json.Data.orderMaterialDelivery.BatchingInfo.FirstOrDefault(p => p.BatchingId == item.BatchingId); if (res != null) { if (ushort.TryParse(res.BatchingLoc, out ushort loc)) { if (loc >= 1 && loc <= 5) { mORKS.RBTakeNoodleTask.Enqueue(new OrderLocInfo() { Loc = loc, SuborderId = morkOrderPush.SuborderId, BatchingId = res.BatchingId }); } else if (loc >= 10 && loc <= 11) { int index = Array.FindIndex(Json.Data.recipeBoms.RecipeIds.ToArray(), p => p.RecipeId == morkOrderPush.RecipeId); index++; mORKS.TakeBowlTask.Enqueue(new OrderLocInfo() { Loc = loc, SuborderId = morkOrderPush.SuborderId, RecipeNumber = (index >= 1 && index <= 10) ? (ushort)index : (ushort)0 }); } } } } } } #region 临时变量 /// /// 取碗订单ID /// string TakeBowlId = string.Empty; /// /// 配料完成订单ID /// string IngredientsCompleteId = string.Empty; /// /// 煮面口对应的订单ID /// string[] CookNodelId = new string[6] { string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, }; /// /// 出餐订单ID /// string OutMealId = string.Empty; /// /// 转台位置轮询 /// List TurntableLoc = new List(); #endregion public void Main() { ThreadManagerment.GetInstance.StartLong(new Action(() => { mORKS.AllowRun = mORKS.InitComplete && !mORKS.TemperatureReached; TakeBowlTask(); TakeNoodleTask(); OutNoodleTask(); SingleDetect(); TurntableControl(); Thread.Sleep(100); }), "MainTask"); } /// /// 取碗控制 /// private void TakeBowlTask() { if (mORKS.AllowRun && mORKS.TakeBowlTask.Count > 0 && !mORKS.TakeBowlIdle && !mORKS.TakeBowlInterlock) { if (mORKS.TakeBowlTask.TryDequeue(out OrderLocInfo orderLocInfo)) { TakeBowlId = orderLocInfo.SuborderId; TakeBowlControl(orderLocInfo.Loc); SetRecipeNumber(orderLocInfo.RecipeNumber); SimpleFactory.GetInstance.OrderChanged(TakeBowlId, ORDER_STATUS.COOKING); MessageLog.GetInstance.Show($"订单【{TakeBowlId}】执行取碗控制,位置:[{orderLocInfo.Loc}]"); } mORKS.TakeBowlInterlock = true; } } /// /// 转台控制 /// private void TurntableControl() { if (mORKS.TurntableMoveInPlace && mORKS.InitComplete && !mORKS.AllowTakeNoodle && mORKS.RBTakeNoodleTask.Count > 0) { if (mORKS.TurntableLowerLimit) { SetTurntableLoc(mORKS.RBTakeNoodleTask.ElementAt(0).Loc); MoveTurntable(); mORKS.AllowTakeNoodle = true; TurntableLoc.Clear(); MessageLog.GetInstance.Show("转台位置OK"); } else { if (!mORKS.TurntableInterlock) { var result = Json.Data.orderMaterialDelivery.BatchingInfo.Where(p => p.BatchingId == mORKS.RBTakeNoodleTask.ElementAt(0).BatchingId).ToList(); if (result != null) { foreach (var item in result) { if (ushort.TryParse(item.BatchingLoc, out ushort loc)) { if (TurntableLoc.Contains(loc)) { SetTurntableLoc(loc); MoveTurntable(); mORKS.TurntableInterlock = true; TurntableLoc.Add(loc); return; } } } MessageLog.GetInstance.Show("转台位置缺少物料"); } } } } } /// /// 取面任务 /// private void TakeNoodleTask() { //取面控制 if (mORKS.AllowRun && mORKS.RobotIdle && !mORKS.RobotTaskInterlock && mORKS.AllowTakeNoodle && mORKS.TurntableMoveInPlace && !mORKS.TakeNoodleInterlock && !mORKS.OutNoodleing && mORKS.RBTakeNoodleTask.Count > 0) { int loc = Array.FindIndex(mORKS.NoodleCookerStatus, p => p == false);//查找煮面炉空闲位置 if (loc >= 0 && loc <= 5) { if (mORKS.RBTakeNoodleTask.TryDequeue(out OrderLocInfo orderLocInfo)) { //设置转台位置 SetTurntableLoc(orderLocInfo.Loc); //设置倒面位置 CookNodelId[loc] = orderLocInfo.SuborderId; SetFallNoodleLoc((ushort)(loc + 1)); //机器人开始取面 RobotTakeNoodle(); SimpleFactory.GetInstance.OrderChanged(orderLocInfo.SuborderId, ORDER_STATUS.COOKING); MessageLog.GetInstance.Show($"订单【{orderLocInfo.SuborderId}】,转台:[{orderLocInfo}],煮面栏:[{loc + 1}]"); mORKS.TakeNoodleInterlock = true; } } } } /// /// 出餐控制 /// private void OutNoodleTask() { if (mORKS.AllowFallNoodle && mORKS.RobotTaskInterlock && !mORKS.TakeNoodleInterlock && mORKS.RobotIdle && !mORKS.TakeMealDetect) { int loc = Array.FindIndex(CookNodelId, p => p == IngredientsCompleteId && p.Length > 0); if (loc >= 0 && loc <= 5) { if (mORKS.CookNoodlesComplete[loc]) { SetTakeNoodleLoc((ushort)(loc + 1)); RobotOutMeal(); CookNoodleStatusReset((ushort)(loc + 1)); ResetAllowFallNoodle(); OutMealId = IngredientsCompleteId; IngredientsCompleteId = string.Empty; CookNodelId[loc] = string.Empty; MessageLog.GetInstance.Show($"{loc + 1}号位置出餐控制"); mORKS.OutNoodleing = true; } } } } /// /// 信号检测 /// private void SingleDetect() { //允许倒面信号检测 if (RTrig.GetInstance("AllowFallNoodle").Start(mORKS.AllowFallNoodle)) { IngredientsCompleteId = TakeBowlId; TakeBowlId = string.Empty; MessageLog.GetInstance.Show($"允许到面,{IngredientsCompleteId}"); mORKS.TakeBowlInterlock = false; } //出餐完成信号检测 if (RTrig.GetInstance("CompleteChange").Start(mORKS.RbOutMealComplete)) { SimpleFactory.GetInstance.OrderChanged(OutMealId, ORDER_STATUS.COMPLETED_COOK); MessageLog.GetInstance.Show($"订单【{OutMealId}】制作完成"); mORKS.OutNoodleing = false; } //取餐完成逻辑处理 if (DelayRTrig.GetInstance("CompleteChange1").Start(mORKS.RbOutMealComplete && !mORKS.TakeMealDetect, 2)) { SimpleFactory.GetInstance.OrderChanged(OutMealId, ORDER_STATUS.COMPLETED_TAKE); MessageLog.GetInstance.Show($"订单【{OutMealId}】取餐完成"); var RemoveItem = Json.Data.morkOrderPushes.FirstOrDefault(p => p.OrderPush.SuborderId == OutMealId); if (RemoveItem != null) { Json.Data.morkOrderPushes.Remove(RemoveItem); } ResetCookComplete(); OutMealId = string.Empty; } //机器人取面完成信号检测 if (RTrig.GetInstance("TakeNoodleComplete").Start(mORKS.RbTakeNoodleComplete)) { mORKS.TakeNoodleInterlock = false; mORKS.AllowTakeNoodle = false; MessageLog.GetInstance.Show("取面完成"); TakeNoodleCompleteReset(); } //转台到位检测 if (RTrig.GetInstance("TurntableInPlace").Start(mORKS.TurntableMoveInPlace)) { mORKS.TurntableInterlock = false; } int OutMealRequstCount = mORKS.CookNoodlesComplete.Where(p => p == true).ToList().Count; int mlCount = mORKS.NoodleCookerStatus.Where(p => p == true).ToList().Count; mORKS.RobotTaskInterlock = OutMealRequstCount > 0 && mORKS.AllowFallNoodle && (mlCount >= 2 || mORKS.RBTakeNoodleTask.Count == 0); } #region PLC 控制函数 /// /// 写入配方数据到 PLC /// private void WriteRecipeBoms() { List recipeBoms = new List(); foreach (var item in Json.Data.recipeBoms.RecipeIds) { foreach (var rec in item.Recipes) { recipeBoms.Add((ushort)rec); } } if (recipeBoms.Count > 0) { if (ModbusTcpHelper.GetInstance.Write(1100, WriteType.HoldingRegisters, recipeBoms.ToArray())) { MessageLog.GetInstance.Show("成功写入配方数据"); } } } /// /// 转台移动 /// private void MoveTurntable() { ModbusTcpHelper.GetInstance.Write(325, WriteType.Coils, true); } /// /// 取面完成复位 /// private void TakeNoodleCompleteReset() { ModbusTcpHelper.GetInstance.Write(1124, WriteType.Coils, false); } /// /// 指定煮面口状态复位 /// /// private void CookNoodleStatusReset(int num) { if (num >= 1 && num <= 6) { ushort addRess = (ushort)(1136 + num - 1); ModbusTcpHelper.GetInstance.Write(addRess, WriteType.Coils, false); MessageLog.GetInstance.Show($"{num}号煮面口占用复位"); } } /// /// 写配方编号 /// /// private void SetRecipeNumber(ushort num) { ModbusTcpHelper.GetInstance.Write(100, WriteType.HoldingRegisters, num); } /// /// 设置转台位置 /// /// private void SetTurntableLoc(ushort loc) { ModbusTcpHelper.GetInstance.Write(101, WriteType.HoldingRegisters, loc); } /// /// 设置倒面位置 /// /// private void SetFallNoodleLoc(ushort loc) { ModbusTcpHelper.GetInstance.Write(102, WriteType.HoldingRegisters, loc); } /// /// 设置取面位置 /// /// private void SetTakeNoodleLoc(ushort loc) { ModbusTcpHelper.GetInstance.Write(103, WriteType.HoldingRegisters, loc); } /// /// 取碗控制 /// /// private void TakeBowlControl(ushort loc) { if (loc == 10) { ModbusTcpHelper.GetInstance.Write(321, WriteType.Coils, true); } else if (loc == 11) { ModbusTcpHelper.GetInstance.Write(322, WriteType.Coils, true); } } /// /// 机器人取面 /// private void RobotTakeNoodle() { ModbusTcpHelper.GetInstance.Write(323, WriteType.Coils, true); } /// /// 机器人取餐 /// private void RobotOutMeal() { ModbusTcpHelper.GetInstance.Write(324, WriteType.Coils, true); var result = ModbusTcpHelper.GetInstance.Read(324, ReadType.Coils); if (result is bool res) while (!res) { ModbusTcpHelper.GetInstance.Write(324, WriteType.Coils, true); } } /// /// 制作完成信号复位 /// private void ResetCookComplete() { ModbusTcpHelper.GetInstance.Write(1126, WriteType.Coils, false); } /// /// 复位允许取面信号 /// private void ResetAllowFallNoodle() { ModbusTcpHelper.GetInstance.Write(1123, WriteType.Coils, false); } /// /// 设备初始化 /// public async void DeviceInit() { ModbusTcpHelper.GetInstance.Write(320, WriteType.Coils, true); await Task.Delay(1000); ModbusTcpHelper.GetInstance.Write(320, WriteType.Coils, false); } #endregion } }