|
- using System;
- using System.Collections.Generic;
- using BPA.Message.Enum;
- using BPASmartClient.Device;
- using BPASmartClient.EventBus;
- using BPASmartClient.Model;
- using BPASmartClient.Peripheral;
- using static BPASmartClient.EventBus.EventBus;
- using BPASmartClient.Helper;
- using System.Threading;
- using BPASmartClient.Message;
- using BPA.Message;
- using System.Linq;
- using BPASmartClient.Model.PLC;
- using System.Threading.Tasks;
- using System.Reflection;
-
- namespace BPASmartClient.MorkS
- {
- public class Control : BaseDevice
- {
- public override DeviceClientType DeviceType => DeviceClientType.MORKS;
- GVL_MORKS mORKS = new GVL_MORKS();
- /// <summary>
- /// 订单物料信息
- /// </summary>
- private OrderMaterialDelivery orderMaterialDelivery { get; set; } = new OrderMaterialDelivery();
- /// <summary>
- /// 配方数据信息
- /// </summary>
- private RecipeBoms recipeBoms { get; set; } = new RecipeBoms();
- int OrderCount;
- bool Initing;
-
- public override void DoMain()
- {
- ServerInit();
- DataParse();
- ReadPLCData();
- Main();
-
- ThreadManage.GetInstance().StartLong(new Action(() =>
- {
- if (RTrig.GetInstance("ResetProgram").Start(Initing))
- {
- ThreadManage.GetInstance().StopTask("MainTask", new Action(() =>
- {
- ThreadManage.GetInstance().StopTask("ReadPLCData", new Action(() =>
- {
- mORKS = null;
- mORKS = new GVL_MORKS();
- ReadPLCData();
- Main();
- }));
- }));
- }
- Thread.Sleep(10);
- }), "ResetProgram");
- }
-
- public override void Stop()
- {
-
- }
-
- private void ServerInit()
- {
- //物料信息
- EventBus.EventBus.GetInstance().Subscribe<MaterialDeliveryEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
- {
- if (@event == null) return;
- if (@event is MaterialDeliveryEvent material)
- {
- orderMaterialDelivery = material.orderMaterialDelivery;
- }
- });
-
- //配方数据信息
- EventBus.EventBus.GetInstance().Subscribe<RecipeBomEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
- {
- if (@event == null) return;
- if (@event is RecipeBomEvent recipe)
- {
- recipeBoms = recipe.recipeBoms;
- WriteRecipeBoms();
- }
- });
- }
-
- private void OrderChange(string subid, ORDER_STATUS oRDER_STATUS)
- {
- EventBus.EventBus.GetInstance().Publish(new OrderStatusChangedEvent() { Id = DeviceId, Status = oRDER_STATUS, SubOrderId = subid });
- }
-
- private void ReadData(string address, ushort len = 1, Action<bool[]> action = null)
- {
- EventBus.EventBus.GetInstance().Publish(new ReadModel() { Id = DeviceId, Address = address, Length = len }, (o) =>
- {
- if (o != null && o.Length > 0 && o[0] is bool[] bools)
- {
- action(bools);
- }
- });
- }
-
- private void ReadPLCData()
- {
- ThreadManage.GetInstance().StartLong(new Action(() =>
- {
- var bools = (bool[])peripheralStatus["M0.3"];
- mORKS.RobotTakeNoodle = bools[0];
- mORKS.RobotOutMeal = bools[1];
- mORKS.MoveTurntable = bools[2];
-
- bools = (bool[])peripheralStatus["M100.0"];
- 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];
- Initing = bools[10];
- mORKS.TurntableLowerLimit = bools[11];
- mORKS.MissingBowlSignal2 = bools[12];
- mORKS.TurntableUpLimit = bools[13];
- mORKS.FeedComplete = bools[14];
- mORKS.TurntableMoveInPlace = bools[15];
-
- bools = (bool[])peripheralStatus["M235.0"];
- mORKS.Error = bools[0];
-
- bools = (bool[])peripheralStatus["M102.0"];
- for (int i = 0; i < 6; i++)
- {
- mORKS.NoodleCookerStatus[i] = bools[i];
- }
- mORKS.Feeding = bools[6];
-
- bools = (bool[])peripheralStatus["M103.0"];
- for (int i = 0; i < 6; i++)
- {
- mORKS.CookNoodlesComplete[i] = bools[i];
- }
-
- EventBus.EventBus.GetInstance().Publish(new ReadModel() { Id = DeviceId, Address = "VW372", Length = 1 }, (o) =>
- {
- if (o != null && o.Length > 0 && o[0] is ushort value)
- {
- mORKS.TurntableFeedbackloc = value;
- }
- });
-
- Thread.Sleep(500);
- }), "ReadPLCData", true);
-
- #region 替换为内部主动读取
- /*
- ThreadManage.GetInstance().StartLong(new Action(() =>
- {
-
-
- ReadData("M0.3", 3, new Action<bool[]>((bools) =>
- {
- mORKS.RobotTakeNoodle = bools[0];
- mORKS.RobotOutMeal = bools[1];
- mORKS.MoveTurntable = bools[2];
- }));
-
- ReadData("M100.0", 16, new Action<bool[]>((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];
- Initing = bools[10];
- mORKS.TurntableLowerLimit = bools[11];
- mORKS.MissingBowlSignal2 = bools[12];
- mORKS.TurntableUpLimit = bools[13];
- mORKS.FeedComplete = bools[14];
- mORKS.TurntableMoveInPlace = bools[15];
- }));
-
- ReadData("M235.0", 1, new Action<bool[]>((bools) => { mORKS.Error = bools[0]; }));
-
- ReadData("M102.0", 7, new Action<bool[]>((bools) =>
- {
- for (int i = 0; i < 6; i++)
- {
- mORKS.NoodleCookerStatus[i] = bools[i];
- }
- mORKS.Feeding = bools[6];
- }));
-
- ReadData("M103.0", 6, new Action<bool[]>((bools) =>
- {
- for (int i = 0; i < 6; i++)
- {
- mORKS.CookNoodlesComplete[i] = bools[i];
- }
- }));
-
- //ReadData("M550.0", 20, new Action<bool[]>((bools) =>
- //{
- // //alarm.MachineLeftLowTemperature = bools[0];
- // //alarm.MachineRightLowTemperature = bools[1];
- // //alarm.Supply1_LossBowl = bools[2];
- // //alarm.Supply2_LossBowl = bools[3];
- // //alarm.Supply1_ErrorOutBowl = bools[4];
- // //alarm.Supply2_ErrorOutBowl = bools[5];
- // //alarm.PushBowlCylinderError = bools[6];
- // //alarm.NoodleMacCommunicateError = bools[7];
- // //alarm.DosingMacCommunicateError = bools[8];
- // //alarm.RobotMacCommunicateError = bools[9];
- // //alarm.RobotInitError = bools[11];
- // //alarm.RobotUrgentStop = bools[12];
- // //alarm.RobotNotInRemoteMode = bools[13];
- // //alarm.RobotNotInReady = bools[14];
- // //alarm.RobotSelfInException = bools[15];
- //}));
-
- EventBus.EventBus.GetInstance().Publish(new ReadModel() { Id = DeviceId, Address = "VW372", Length = 1 }, (o) =>
- {
- if (o != null && o.Length > 0 && o[0] is ushort value)
- {
- mORKS.TurntableFeedbackloc = value;
- }
- });
-
- Thread.Sleep(500);
- }), "ReadPLCData", true);
- */
- #endregion
- }
-
- /// <summary>
- /// 数据解析
- /// </summary>
- private void DataParse()
- {
- EventBus.EventBus.GetInstance().Subscribe<DoOrderEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBackHandle)
- {
- if (@event == null) return;
- if (@event is DoOrderEvent order)
- {
- if (order.MorkOrder.GoodBatchings == null) return;
- OrderCount++;
- MessageLog.GetInstance.Show($"接收到{OrderCount}次订单");
- foreach (var item in order.MorkOrder.GoodBatchings)
- {
- var res = orderMaterialDelivery?.BatchingInfo?.FirstOrDefault(p => p.BatchingId == item.BatchingId);
- if (res != null)
- {
- if (ushort.TryParse(res.BatchingLoc, out ushort loc))
- {
- if (loc >= 1 && loc <= 5)
- {
- if (mORKS.RBTakeNoodleTask.FirstOrDefault(p => p.SuborderId == order.MorkOrder.SuborderId) == null)
- mORKS.RBTakeNoodleTask.Enqueue(new OrderLocInfo() { Loc = ushort.Parse(res.BatchingLoc), SuborderId = order.MorkOrder.SuborderId, BatchingId = res.BatchingId });
- }
- else if (loc >= 10 && loc <= 11)
- {
- int index = 0;
- if (recipeBoms != null)
- {
- index = Array.FindIndex(recipeBoms.RecipeIds?.ToArray(), p => p.RecipeId == order.MorkOrder.RecipeId);
- index++;
- }
- if (mORKS.TakeBowlTask.FirstOrDefault(p => p.SuborderId == order.MorkOrder.SuborderId) == null)
- mORKS.TakeBowlTask.Enqueue(new OrderLocInfo()
- {
- Loc = ushort.Parse(res.BatchingLoc),
- SuborderId = order.MorkOrder.SuborderId,
- RecipeNumber = (index >= 1 && index <= 10) ? (ushort)index : (ushort)0
- });
- }
- }
- }
- }
- }
- });
- }
-
-
- public void Main()
- {
- ThreadManage.GetInstance().StartLong(new Action(() =>
- {
- mORKS.AllowRun = mORKS.InitComplete;
- //IsHealth = mORKS.Error && mORKS.InitComplete;
- IsHealth = true;
- TakeBowlTask();
-
- TakeNoodleTask();
-
- OutNoodleTask();
-
- SingleDetect();
-
- TurntableControl();
-
- Thread.Sleep(100);
-
- }), "MainTask");
- }
-
-
- /// <summary>
- /// 取碗控制
- /// </summary>
- private void TakeBowlTask()
- {
- if (mORKS.AllowRun && mORKS.TakeBowlTask.Count > 0 && !mORKS.TakeBowlIdle && !mORKS.TakeBowlInterlock)
- {
- if (mORKS.TakeBowlTask.TryDequeue(out OrderLocInfo orderLocInfo))
- {
- mORKS.TakeBowlId = orderLocInfo.SuborderId;
- TakeBowlControl(orderLocInfo.Loc);
- SetRecipeNumber(orderLocInfo.RecipeNumber);
- OrderChange(mORKS.TakeBowlId, ORDER_STATUS.COOKING);
- MessageLog.GetInstance.Show($"订单【{ mORKS.TakeBowlId}】执行取碗控制,位置:[{orderLocInfo.Loc}]");
- }
- mORKS.TakeBowlInterlock = true;
- }
- }
-
- /// <summary>
- /// 转台控制
- /// </summary>
- private void TurntableControl()
- {
- //if (GeneralConfig.EnableLocalSimOrder)
- //{
- // //不做轮询,直接取面,模拟订单使用
- // if (mORKS.TurntableMoveInPlace && !mORKS.Feeding && mORKS.InitComplete && !mORKS.AllowTakeNoodle && mORKS.RBTakeNoodleTask.Count > 0)
- // {
- // if (mORKS.TurntableLowerLimit)
- // {
- // TurntableStart(mORKS.RBTakeNoodleTask.ElementAt(0).Loc);
- // mORKS.TurntableLocLists.Clear();
- // mORKS.AllowTakeNoodle = true;
- // MessageLog.GetInstance.Show($"控制机器人去转台【{mORKS.RBTakeNoodleTask.ElementAt(0).Loc}】号位置取面");
- // }
- // }
- //}
- //else
- {
- //正常轮询
- if (mORKS.TurntableMoveInPlace && !mORKS.Feeding && mORKS.InitComplete && !mORKS.AllowTakeNoodle && mORKS.RBTakeNoodleTask.Count > 0)
- {
- var result = orderMaterialDelivery.BatchingInfo.Where(p => p.BatchingId == mORKS.RBTakeNoodleTask.ElementAt(0).BatchingId).ToList();
- if (result != null)
- {
- var res = result.FirstOrDefault(P => P.BatchingLoc == mORKS.TurntableFeedbackloc.ToString());
- if (mORKS.TurntableLowerLimit && res != null)
- {
- TurntableStart(mORKS.TurntableFeedbackloc);
- mORKS.TurntableLocLists.Clear();
- mORKS.AllowTakeNoodle = true;
- MessageLog.GetInstance.Show($"控制机器人去转台【{mORKS.TurntableFeedbackloc}】号位置取面");
- }
- else
- {
- if (!mORKS.TurntableInterlock)
- {
- foreach (var item in result)
- {
- if (ushort.TryParse(item.BatchingLoc, out ushort loc))
- {
- if (mORKS.TurntableFeedbackloc != loc && !mORKS.TurntableLocLists.Contains(loc))
- {
- TurntableStart(loc);
- MessageLog.GetInstance.Show($"没有物料检测的启动转台控制,转台位置:[{loc}]");
- break;
- }
- else if (mORKS.TurntableFeedbackloc == loc && !mORKS.TurntableLocLists.Contains(loc)) mORKS.TurntableLocLists.Add(loc);
- }
- }
- }
- }
- }
- else MessageLog.GetInstance.Show("未找到可用的物料信息");
- }
- }
-
- //转台到位检测
- if (RTrig.GetInstance("TurntableInPlace").Start(mORKS.TurntableMoveInPlace && mORKS.CurrentLoc == mORKS.TurntableFeedbackloc))
- {
- mORKS.CurrentLoc = 0;
- mORKS.TurntableInterlock = false;
- MessageLog.GetInstance.Show("转台到位检测");
- }
-
- //补料完成检测
- if (RTrig.GetInstance("FeedComplete").Start(mORKS.FeedComplete))
- {
- if (!mORKS.AllowTakeNoodle && mORKS.TurntableLocLists.Count > 0)
- {
- mORKS.TurntableLocLists.Clear();
- mORKS.TurntableInterlock = false;
- MessageLog.GetInstance.Show("补料完成检测");
- }
- }
-
- }
-
- /// <summary>
- /// 取面任务
- /// </summary>
- private void TakeNoodleTask()
- {
- //取面控制
- if (mORKS.AllowRun && mORKS.RobotIdle && !mORKS.Feeding && !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))
- {
- mORKS.CookNodelId[loc] = orderLocInfo.SuborderId;
- SetFallNoodleLoc((ushort)(loc + 1));
- //机器人开始取面
- RobotTakeNoodle();
- OrderChange(orderLocInfo.SuborderId, ORDER_STATUS.COOKING);
- MessageLog.GetInstance.Show($"订单【{orderLocInfo.SuborderId}】,机器人倒面至【{loc + 1}】号煮面栏");
- //写入煮面时间
- //List<ushort> values = new List<ushort>();
- //values.Add(Json<KeepDataBase>.Data.parSets.ElementAt(loc).Minute);
- //values.Add(Json<KeepDataBase>.Data.parSets.ElementAt(loc).Second);
- //ModbusTcpHelper.GetInstance.Write((ushort)ModbusTcpHelper.GetInstance.GetWordAddress($"VW{116 + (loc * 6)}"), WriteType.HoldingRegisters, values.ToArray());
-
- mORKS.TakeNoodleInterlock = true;
- }
- }
-
- }
- }
-
- /// <summary>
- /// 出餐控制
- /// </summary>
- private void OutNoodleTask()
- {
- if (mORKS.AllowFallNoodle && mORKS.RobotTaskInterlock && !mORKS.TakeNoodleInterlock && mORKS.RobotIdle && !mORKS.TakeMealDetect)
- {
- int loc = Array.FindIndex(mORKS.CookNodelId, p => p == mORKS.IngredientsCompleteId && p.Length > 0);
- if (loc >= 0 && loc <= 5)
- {
- if (mORKS.CookNoodlesComplete[loc])
- {
- SetTakeNoodleLoc((ushort)(loc + 1));
- RobotOutMeal();
- CookNoodleStatusReset((ushort)(loc + 1));
- ResetAllowFallNoodle();
- mORKS.OutMealId = mORKS.IngredientsCompleteId;
- mORKS.IngredientsCompleteId = string.Empty;
- mORKS.CookNodelId[loc] = string.Empty;
- MessageLog.GetInstance.Show($"{loc + 1}号位置出餐控制");
- mORKS.OutNoodleing = true;
- }
- }
- }
- }
-
- /// <summary>
- /// 信号检测
- /// </summary>
- private void SingleDetect()
- {
- //允许倒面信号检测
- if (RTrig.GetInstance("AllowFallNoodle").Start(mORKS.AllowFallNoodle))
- {
- mORKS.IngredientsCompleteId = mORKS.TakeBowlId;
- mORKS.TakeBowlId = string.Empty;
- MessageLog.GetInstance.Show($"碗到位,允许到面,{mORKS.IngredientsCompleteId}");
- mORKS.TakeBowlInterlock = false;
- }
-
- //出餐完成信号检测
- if (RTrig.GetInstance("CompleteChange").Start(mORKS.RbOutMealComplete))
- {
- OrderChange(mORKS.OutMealId, ORDER_STATUS.COMPLETED_COOK);
- MessageLog.GetInstance.Show($"订单【{mORKS.OutMealId}】制作完成");
- mORKS.OutNoodleing = false;
- }
-
- //取餐完成逻辑处理
- if (DelayRTrig.GetInstance("CompleteChange1").Start(mORKS.RbOutMealComplete && !mORKS.TakeMealDetect, 2))
- {
- OrderChange(mORKS.OutMealId, ORDER_STATUS.COMPLETED_TAKE);
- MessageLog.GetInstance.Show($"订单【{mORKS.OutMealId}】取餐完成");
- ResetCookComplete();
- mORKS.OutMealId = string.Empty;
- }
-
- //机器人取面完成信号检测
- if (RTrig.GetInstance("TakeNoodleComplete").Start(mORKS.RbTakeNoodleComplete))
- {
- mORKS.TakeNoodleInterlock = false;
- mORKS.AllowTakeNoodle = false;
- mORKS.TurntableInterlock = false;
- MessageLog.GetInstance.Show("机器人取面完成信号检测");
- TakeNoodleCompleteReset();
- }
-
- 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 控制函数
-
- private void WriteData(string address, object value)
- {
- EventBus.EventBus.GetInstance().Publish(new WriteModel() { Id = DeviceId, Address = address, Value = value });
- }
-
- /// <summary>
- /// 写入配方数据到 PLC
- /// </summary>
- private void WriteRecipeBoms()
- {
- List<ushort> recipeBoms = new List<ushort>();
- if (this.recipeBoms == null) return;
- foreach (var item in this.recipeBoms.RecipeIds)
- {
- foreach (var rec in item.Recipes)
- {
- recipeBoms.Add((ushort)rec);
- }
- }
- if (recipeBoms.Count > 0)
- {
- //配方数据地址范围:VW2000 - VW2278
- WriteData("VW2000", recipeBoms.ToArray());
- }
- else { MessageLog.GetInstance.Show("配方数据为空"); }
- }
-
-
- /// <summary>
- /// 取面完成复位
- /// </summary>
- private void TakeNoodleCompleteReset()
- {
- WriteData("M100.4", false);
-
- }
-
- /// <summary>
- /// 指定煮面口状态复位
- /// </summary>
- /// <param name="num"></param>
- private void CookNoodleStatusReset(int num)
- {
- if (num >= 1 && num <= 6)
- {
- WriteData($"102.{num - 1}", false);
- MessageLog.GetInstance.Show($"{num}号煮面口占用复位");
- }
-
- }
-
- /// <summary>
- /// 写配方编号
- /// </summary>
- /// <param name="num"></param>
- private void SetRecipeNumber(ushort num)
- {
- WriteData("VW0", num);
- }
-
- /// <summary>
- /// 启动转台
- /// </summary>
- /// <param name="loc"></param>
- private void TurntableStart(ushort loc)
- {
- mORKS.CurrentLoc = loc;
- mORKS.TurntableInterlock = true;
- mORKS.TurntableLocLists.Add(loc);
- WriteData("VW0", loc);
- WriteData("M0.5", true);
- }
-
- /// <summary>
- /// 设置倒面位置
- /// </summary>
- /// <param name="loc"></param>
- private void SetFallNoodleLoc(ushort loc)
- {
- WriteData("VW4", loc);
- }
-
- /// <summary>
- /// 设置取面位置
- /// </summary>
- /// <param name="loc"></param>
- private void SetTakeNoodleLoc(ushort loc)
- {
- WriteData("VW6", loc);
- }
-
- /// <summary>
- /// 取碗控制
- /// </summary>
- /// <param name="loc"></param>
- private void TakeBowlControl(ushort loc)
- {
- if (loc == 10)//小碗
- {
- WriteData("M0.1", true);
- }
- else if (loc == 11)//大碗
- {
- WriteData("M0.2", true);
- }
- }
-
- /// <summary>
- /// 机器人取面
- /// </summary>
- private void RobotTakeNoodle()
- {
- WriteData("M0.3", true);
- }
-
- /// <summary>
- /// 机器人取餐
- /// </summary>
- private void RobotOutMeal()
- {
- WriteData("M0.4", true);
- }
-
- /// <summary>
- /// 制作完成信号复位
- /// </summary>
- private void ResetCookComplete()
- {
- WriteData("M100.6", false);
- }
-
- /// <summary>
- /// 复位允许取面信号
- /// </summary>
- private void ResetAllowFallNoodle()
- {
- WriteData("M100.3", false);
- }
-
- /// <summary>
- /// 设备初始化
- /// </summary>
- public async void DeviceInit()
- {
- WriteData("M0.0", true);
- await Task.Delay(1000);
- WriteData("M0.0", false);
- }
- #endregion
-
-
- }
- }
|