|
- #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.Business.Devices
- {
- public class MORKS : IBusiness
- {
- private volatile static MORKS _Instance;
- public static MORKS GetInstance => _Instance ?? (_Instance = new MORKS());
- private MORKS() { }
-
- public GVL.MORKS mORKS { get; set; } = new GVL.MORKS();
-
- /// <summary>
- /// 写入配方数据到 PLC
- /// </summary>
- private void WriteRecipeBoms()
- {
- List<ushort> recipeBoms = new List<ushort>();
- foreach (var item in Json<BatchingInfoPar>.GetInstance.Base.recipeBoms.RecipeIds)
- {
- foreach (var rec in item.Recipes)
- {
- recipeBoms.Add((ushort)rec);
- }
- }
- if (ModbusTcpHelper.GetInstance.Write(1100, WriteType.HoldingRegisters, recipeBoms.ToArray()))
- {
- MessageLog.GetInstance.Show("成功写入配方数据");
- }
- }
-
- public void Init()
- {
- //Modbus TCP连接成功
- ActionManagerment.GetInstance.Register(new Action(() =>
- {
- WriteRecipeBoms();
- ReadPlcData();
- DataParse();
- Main();
- //ResetProgram();
- }), "ConnectOk");
-
- //获取物料信息
- SimpleFactory.GetInstance.GetBatchingInfo();
-
- //Modbus Tcp 连接
- ModbusTcpHelper.GetInstance.ModbusTcpConnect("192.168.1.11", 508);
-
- //模拟订单
- SimOrder();
- Test();
- Heartbeat();
- MessageLog.GetInstance.Show("MORKS 设备初始化完成");
- }
-
- List<BatchingInfo> batchingInfos = new List<BatchingInfo>();
-
- private void Heartbeat()
- {
- HeartbeatReport.GetInstance.GetMessage = new Action(() =>
- {
- HeartbeatReport.GetInstance.deviceStatus.BatchingInfo = batchingInfos;
- #if test
- HeartbeatReport.GetInstance.deviceStatus.Healthy = DeviceHealthy.UnHealth;
- #else
- HeartbeatReport.GetInstance.deviceStatus.Healthy = AllowRun ? DeviceHealthy.UnHealth : DeviceHealthy.Health;
- #endif
-
- });
- }
-
- /// <summary>
- /// 测试程序
- /// </summary>
- private void Test()
- {
- #if test
- ThreadManagerment.GetInstance.StartLong(new Action(() =>
- {
- while (Json<MorkOrderPushPar>.GetInstance.Base.morkOrderPushes.Count > 0)
- {
- var result = Json<MorkOrderPushPar>.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<MorkOrderPushPar>.GetInstance.Base.morkOrderPushes.RemoveAt(0);
- }
- Thread.Sleep(500);
- }), "test");
- #endif
- }
-
- private void Readbool(ushort startAddress, ushort len, Action<bool[]> action)
- {
- object result;
- result = ModbusTcpHelper.GetInstance.Read(startAddress, ReadType.Coils, len);
- if (result != null)
- {
- if (result is bool[] bools)
- {
- if (bools.Length == len)
- {
- action(bools);
- }
- }
- }
- }
-
- /// <summary>
- /// 复位程序
- /// </summary>
- private void ResetProgram()
- {
- ThreadManagerment.GetInstance.StartLong(new Action(() =>
- {
- if (RTrig.GetInstance("ResetProgram").Start(mORKS.DeviceIniting))
- {
- ThreadManagerment.GetInstance.StopTask("MainTask", new Action(() =>
- {
- AllowRun = false;
- TakeBowlId = string.Empty;
- IngredientsCompleteId = string.Empty;
- CookNodelId = new string[6] { string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, };
- RobotTaskInterlock = false;
- OutMealId = string.Empty;
- TakeBowlInterlock = false;
- TakeNoodleInterlock = false;
- OutNoodleing = false;
- Main();
- }));
- }
- Thread.Sleep(10);
- }), "ResetProgram");
- }
-
- /// <summary>
- /// 数据读取
- /// </summary>
- private void ReadPlcData()
- {
- ThreadManagerment.GetInstance.StartLong(new Action(() =>
- {
- Readbool(323, 3, new Action<bool[]>((o) =>
- {
- mORKS.RobotTakeNoodle = o[0];
- mORKS.RobotOutMeal = o[1];
- mORKS.MoveTurntable = o[2];
- }));
-
- Readbool(1120, 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];
- mORKS.DeviceIniting = bools[10];
- mORKS.TurntableLowerLimit = bools[11];
- mORKS.MissingBowlSignal2 = bools[12];
- mORKS.TurntableUpLimit = bools[13];
- mORKS.TurntableMoveInPlace = bools[15];
- }));
-
- Readbool(1136, 6, new Action<bool[]>((bools) =>
- {
- for (int i = 0; i < 6; i++)
- {
- mORKS.NoodleCookerStatus[i] = bools[i];
- }
- }));
-
- Readbool(1144, 6, new Action<bool[]>((bools) =>
- {
- for (int i = 0; i < 6; i++)
- {
- mORKS.CookNoodlesComplete[i] = bools[i];
- }
- }));
-
- Thread.Sleep(500);
- }), "ReadPLCData");
- }
-
- private void SimOrder()
- {
- ActionManagerment.GetInstance.Register(new Action(() =>
- {
- string subId = Guid.NewGuid().ToString();
- ushort NoodleLoc = 1;//(ushort)(new Random().Next(1, 5));
- ushort BolwLoc = (ushort)(new Random().Next(10, 11));
- mORKS.RBTakeNoodleTask.Enqueue(new GVL.OrderLocInfo() { Loc = NoodleLoc, SuborderId = subId });
- mORKS.TakeBowlTask.Enqueue(new GVL.OrderLocInfo() { Loc = BolwLoc, SuborderId = subId });
- MessageLog.GetInstance.Show($"添加订单:面条位置【{NoodleLoc}】,碗位置【{BolwLoc}】");
- }), "SimOrder");
- }
-
- /// <summary>
- /// 数据解析
- /// </summary>
- private void DataParse()
- {
- ActionManagerment.GetInstance.Register(new Action<object>((o) =>
- {
- if (o is MorkOrderPush morkOrderPush)
- {
- foreach (var item in morkOrderPush.GoodBatchings)
- {
- var res = Json<BatchingInfoPar>.GetInstance.Base.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 GVL.OrderLocInfo() { Loc = loc, SuborderId = morkOrderPush.SuborderId, BatchingId = res.BatchingId });
- }
- else if (loc >= 10 && loc <= 11)
- {
- mORKS.TakeBowlTask.Enqueue(new GVL.OrderLocInfo() { Loc = loc, SuborderId = morkOrderPush.SuborderId, RecipeNumber = (ushort)morkOrderPush.RecipeId });
- }
- }
- }
- }
- }
- }), "MorksParse");
- }
-
- #region 临时变量
- /// <summary>
- /// 允许运行
- /// </summary>
- bool AllowRun = false;
-
- /// <summary>
- /// 取碗订单ID
- /// </summary>
- string TakeBowlId = string.Empty;
-
- /// <summary>
- /// 配料完成订单ID
- /// </summary>
- string IngredientsCompleteId = string.Empty;
-
- /// <summary>
- /// 煮面口对应的订单ID
- /// </summary>
- string[] CookNodelId = new string[6] { string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, };
-
- /// <summary>
- /// //机器人任务互锁信号
- /// </summary>
- bool RobotTaskInterlock = false;
-
- /// <summary>
- /// 出餐订单ID
- /// </summary>
- string OutMealId = string.Empty;
-
- /// <summary>
- /// 取碗互锁信号
- /// </summary>
- bool TakeBowlInterlock = false;
-
- /// <summary>
- /// 取面互锁信号
- /// </summary>
- bool TakeNoodleInterlock = false;
-
- /// <summary>
- /// 出面中
- /// </summary>
- bool OutNoodleing = false;
-
- /// <summary>
- /// 允许取面
- /// </summary>
- bool AllowTakeNoodle = false;
-
- /// <summary>
- /// 转台位置轮询
- /// </summary>
- List<ushort> TurntableLoc = new List<ushort>();
-
- /// <summary>
- /// 转台互锁信号
- /// </summary>
- bool TurntableInterlock = false;
- #endregion
-
- private void Main()
- {
- ThreadManagerment.GetInstance.StartLong(new Action(() =>
- {
- AllowRun = mORKS.InitComplete && !mORKS.TemperatureReached;
-
- if (AllowRun)
- {
- TakeBowlTask();
-
- TakeNoodleTask();
- }
- OutNoodleTask();
-
- SingleDetect();
-
- TurntableControl();
-
- Thread.Sleep(100);
-
- }), "MainTask");
- }
-
- /// <summary>
- /// 取碗控制
- /// </summary>
- private void TakeBowlTask()
- {
- if (mORKS.TakeBowlTask.Count > 0 && !mORKS.TakeBowlIdle && !TakeBowlInterlock)
- {
- if (mORKS.TakeBowlTask.TryDequeue(out GVL.OrderLocInfo orderLocInfo))
- {
- TakeBowlId = orderLocInfo.SuborderId;
- TakeBowlControl(orderLocInfo.Loc);
- SetRecipeNumber(orderLocInfo.RecipeNumber);
- SimpleFactory.GetInstance.OrderChanged(TakeBowlId, ORDER_STATUS.COOKING);
- MessageLog.GetInstance.Show($"订单【{TakeBowlId}】执行取碗控制,位置:[{orderLocInfo.Loc}]");
- }
- TakeBowlInterlock = true;
- }
- }
-
- /// <summary>
- /// 转台控制
- /// </summary>
- private void TurntableControl()
- {
- if (mORKS.TurntableMoveInPlace && mORKS.InitComplete && !AllowTakeNoodle && mORKS.RBTakeNoodleTask.Count > 0)
- {
-
- if (mORKS.TurntableLowerLimit)
- {
- if (mORKS.RBTakeNoodleTask.Count > 0)
- {
- SetTurntableLoc(mORKS.RBTakeNoodleTask.ElementAt(0).Loc);
- MoveTurntable();
- }
- AllowTakeNoodle = true;
- TurntableLoc.Clear();
- MessageLog.GetInstance.Show("转台位置OK");
- }
- else
- {
-
- if (mORKS.RBTakeNoodleTask.Count > 0 && !TurntableInterlock)
- {
- var result = Json<BatchingInfoPar>.GetInstance.Base.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();
- TurntableInterlock = true;
- TurntableLoc.Add(loc);
- return;
- }
- }
- }
- MessageLog.GetInstance.Show("缺少指定物料");
- }
- }
- }
- }
- }
-
- /// <summary>
- /// 取面任务
- /// </summary>
- private void TakeNoodleTask()
- {
- //取面控制
- if (!RobotTaskInterlock)
- {
- if (mORKS.RobotIdle && AllowTakeNoodle && mORKS.TurntableMoveInPlace && !TakeNoodleInterlock && !OutNoodleing && mORKS.RBTakeNoodleTask.Count > 0)
- {
- if (mORKS.RBTakeNoodleTask.TryDequeue(out GVL.OrderLocInfo orderLocInfo))
- {
- //设置转台位置
- SetTurntableLoc(orderLocInfo.Loc);
- //设置倒面位置
- int loc = Array.FindIndex(mORKS.NoodleCookerStatus, p => p == false);//查找煮面炉空闲位置
- if (loc >= 0 && loc <= 5)
- {
- 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}]");
- TakeNoodleInterlock = true;
- }
- }
- }
- }
-
- /// <summary>
- /// 出餐控制
- /// </summary>
- private void OutNoodleTask()
- {
- if (RobotTaskInterlock)
- {
- if (mORKS.AllowFallNoodle && mORKS.RobotIdle && !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}号位置出餐控制");
- OutNoodleing = true;
- }
- }
- }
- }
- }
-
- /// <summary>
- /// 信号检测
- /// </summary>
- private void SingleDetect()
- {
- //允许倒面信号检测
- if (RTrig.GetInstance("AllowFallNoodle").Start(mORKS.AllowFallNoodle))
- {
- IngredientsCompleteId = TakeBowlId;
- TakeBowlId = string.Empty;
- MessageLog.GetInstance.Show($"允许到面,{IngredientsCompleteId}");
- TakeBowlInterlock = false;
- }
-
- //出餐完成信号检测
- if (RTrig.GetInstance("CompleteChange").Start(mORKS.RbOutMealComplete))
- {
- SimpleFactory.GetInstance.OrderChanged(OutMealId, ORDER_STATUS.COMPLETED_COOK);
- MessageLog.GetInstance.Show($"订单【{OutMealId}】制作完成");
- 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<MorkOrderPushPar>.GetInstance.Base.morkOrderPushes.FirstOrDefault(p => p.OrderPush.SuborderId == OutMealId);
- if (RemoveItem != null)
- {
- Json<MorkOrderPushPar>.GetInstance.Base.morkOrderPushes.Remove(RemoveItem);
- }
- ResetCookComplete();
- OutMealId = string.Empty;
- }
-
- //机器人取面完成信号检测
- if (RTrig.GetInstance("TakeNoodleComplete").Start(mORKS.RbTakeNoodleComplete))
- {
- TakeNoodleInterlock = false;
- AllowTakeNoodle = false;
- MessageLog.GetInstance.Show("取面完成");
- TakeNoodleCompleteReset();
- }
-
- //转台到位检测
- if (RTrig.GetInstance("TurntableInPlace").Start(mORKS.TurntableMoveInPlace))
- {
- TurntableInterlock = false;
- }
-
- int OutMealRequstCount = mORKS.CookNoodlesComplete.Where(p => p == true).ToList().Count;
- int mlCount = mORKS.NoodleCookerStatus.Where(p => p == true).ToList().Count;
- RobotTaskInterlock = OutMealRequstCount > 0 && mORKS.AllowFallNoodle && (mlCount >= 2 || mORKS.RBTakeNoodleTask.Count == 0);
-
- }
-
-
- #region PLC 控制函数
- /// <summary>
- /// 转台移动
- /// </summary>
- private void MoveTurntable()
- {
- ModbusTcpHelper.GetInstance.Write(325, WriteType.Coils, true);
- }
-
- /// <summary>
- /// 取面完成复位
- /// </summary>
- private void TakeNoodleCompleteReset()
- {
- ModbusTcpHelper.GetInstance.Write(1124, WriteType.Coils, false);
- }
-
- /// <summary>
- /// 指定煮面口状态复位
- /// </summary>
- /// <param name="num"></param>
- 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}号煮面口占用复位");
- }
-
- /// <summary>
- /// 写配方编号
- /// </summary>
- /// <param name="num"></param>
- private void SetRecipeNumber(ushort num)
- {
- ModbusTcpHelper.GetInstance.Write(100, WriteType.HoldingRegisters, num);
- }
-
- /// <summary>
- /// 设置转台位置
- /// </summary>
- /// <param name="loc"></param>
- private void SetTurntableLoc(ushort loc)
- {
- ModbusTcpHelper.GetInstance.Write(101, WriteType.HoldingRegisters, loc);
- }
-
- /// <summary>
- /// 设置倒面位置
- /// </summary>
- /// <param name="loc"></param>
- private void SetFallNoodleLoc(ushort loc)
- {
- ModbusTcpHelper.GetInstance.Write(102, WriteType.HoldingRegisters, loc);
- }
-
- /// <summary>
- /// 设置取面位置
- /// </summary>
- /// <param name="loc"></param>
- private void SetTakeNoodleLoc(ushort loc)
- {
- ModbusTcpHelper.GetInstance.Write(103, WriteType.HoldingRegisters, loc);
- }
-
- /// <summary>
- /// 取碗控制
- /// </summary>
- /// <param name="loc"></param>
- 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);
- }
- }
-
- /// <summary>
- /// 机器人取面
- /// </summary>
- private void RobotTakeNoodle()
- {
- ModbusTcpHelper.GetInstance.Write(323, WriteType.Coils, true);
- }
-
- /// <summary>
- /// 机器人取餐
- /// </summary>
- 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);
- }
- }
-
- /// <summary>
- /// 制作完成信号复位
- /// </summary>
- private void ResetCookComplete()
- {
- ModbusTcpHelper.GetInstance.Write(1126, WriteType.Coils, false);
- }
-
- /// <summary>
- /// 复位允许取面信号
- /// </summary>
- private void ResetAllowFallNoodle()
- {
- ModbusTcpHelper.GetInstance.Write(1123, WriteType.Coils, false);
- }
-
- /// <summary>
- /// 设备初始化
- /// </summary>
- public async void DeviceInit()
- {
- ModbusTcpHelper.GetInstance.Write(320, WriteType.Coils, true);
- await Task.Delay(1000);
- ModbusTcpHelper.GetInstance.Write(320, WriteType.Coils, false);
-
- }
- #endregion
-
-
-
- }
-
- }
|