|
- //#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;
- using HBLConsole.GVL;
- using BPA.Models;
-
- namespace HBLConsole.MORKS
- {
- public class Control_MORKS : IControl
- {
- #region 单例模式
- private static Control_MORKS _instance;
- public static Control_MORKS Instance
- {
- get
- {
- if (_instance == null)
- _instance = new Control_MORKS();
- return _instance;
- }
- }
- public Control_MORKS()
- {
-
- }
- #endregion
- public GVL_MORKS mORKS { get; set; } = new GVL_MORKS();
- public void Init()
- {
- Test();
- ActionOperate.GetInstance.Register(new Action(() => { DeviceInit(); }), "InitCommand");
- }
-
- public object GetT()
- {
- return mORKS;
- }
-
- public void ConnectOk()
- {
- //WriteRecipeBoms();
- ReadData();
- Main();
- //ResetProgram();
- MessageLog.GetInstance.Show("MORKS 设备初始化完成");
- }
-
- /// <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
- }
-
- /// <summary>
- /// 复位程序
- /// </summary>
- private void ResetProgram()
- {
- ThreadOperate.GetInstance.StartLong(new Action(() =>
- {
- if (RTrig.GetInstance("ResetProgram").Start(mORKS.DeviceIniting))
- {
- ThreadOperate.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");
- }
-
- /// <summary>
- /// 数据读取
- /// </summary>
- public void ReadData()
- {
- ThreadOperate.GetInstance.StartLong(new Action(() =>
- {
- ModbusTcpHelper.GetInstance.Readbool(323, 3, new Action<bool[]>((bools) =>
- {
- mORKS.RobotTakeNoodle = bools[0];
- mORKS.RobotOutMeal = bools[1];
- mORKS.MoveTurntable = bools[2];
- }));
-
- ModbusTcpHelper.GetInstance.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];
- }));
-
- ModbusTcpHelper.GetInstance.Readbool(1136, 6, new Action<bool[]>((bools) =>
- {
- for (int i = 0; i < 6; i++)
- {
- mORKS.NoodleCookerStatus[i] = bools[i];
- }
- }));
-
- ModbusTcpHelper.GetInstance.Readbool(1144, 6, new Action<bool[]>((bools) =>
- {
- for (int i = 0; i < 6; i++)
- {
- mORKS.CookNoodlesComplete[i] = bools[i];
- }
- }));
-
- Thread.Sleep(500);
- }), "ReadPLCData");
- }
-
- public void SimOrder<T>(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}】");
- }
- }
- }
-
- /// <summary>
- /// 数据解析
- /// </summary>
- public void DataParse<T>(T order)
- {
- if (order is MorkOrderPush morkOrderPush)
- {
- BatchingInfo batching_M=null;//面条
- BatchingInfo batching_W=null;//碗
-
- foreach (var item in morkOrderPush.GoodBatchings)
- {
- var res = Json<BatchingInfoPar>.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) { batching_M = res; }
- else if (loc >= 10 && loc <= 11) { batching_W= res; }
- }
- }
- }
- if (batching_M != null && batching_W != null)
- {
- batching_W.BatchingLoc = "10";
- mORKS.RBTakeNoodleTask.Enqueue(new OrderLocInfo() { Loc = ushort.Parse(batching_M.BatchingLoc),SuborderId = morkOrderPush.SuborderId,BatchingId = batching_M.BatchingId });
-
- int index = Array.FindIndex(Json<BatchingInfoPar>.Data.recipeBoms.RecipeIds.ToArray(),p => p.RecipeId == morkOrderPush.RecipeId);
- index++;
- mORKS.TakeBowlTask.Enqueue(new OrderLocInfo() { Loc = ushort.Parse(batching_W.BatchingLoc),SuborderId = morkOrderPush.SuborderId,RecipeNumber = (index >= 1 && index <= 10) ? (ushort)index : (ushort)0 });
-
-
- }
- }
-
- }
-
- #region 临时变量
- /// <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>
- /// 出餐订单ID
- /// </summary>
- string OutMealId = string.Empty;
-
- /// <summary>
- /// 转台位置轮询
- /// </summary>
- List<ushort> TurntableLoc = new List<ushort>();
-
- #endregion
-
- public void Main()
- {
- ThreadOperate.GetInstance.StartLong(new Action(() =>
- {
- mORKS.AllowRun = mORKS.InitComplete && !mORKS.TemperatureReached;
- GeneralConfig.Healthy = mORKS.AllowRun;
-
- 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))
- {
- 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;
- }
- }
-
- /// <summary>
- /// 转台控制
- /// </summary>
- 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");
- mORKS.IsNoodles = true;
- }
- else
- {
- if (!mORKS.TurntableInterlock)
- {
- var result = Json<BatchingInfoPar>.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;
- }
- }
- }
- mORKS.IsNoodles = false;
- MessageLog.GetInstance.Show("转台位置缺少物料");
- }
- }
- }
- }
- }
-
- /// <summary>
- /// 取面任务
- /// </summary>
- 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;
- }
- }
-
- }
- }
-
- /// <summary>
- /// 出餐控制
- /// </summary>
- 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;
- }
- }
- }
- }
-
- /// <summary>
- /// 信号检测
- /// </summary>
- 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<MorkOrderPushPar>.Data.morkOrderPushes.FirstOrDefault(p => p.OrderPush.SuborderId == OutMealId);
- if (RemoveItem != null)
- {
- Json<MorkOrderPushPar>.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 控制函数
-
- /// <summary>
- /// 写入配方数据到 PLC
- /// </summary>
- private void WriteRecipeBoms()
- {
- List<ushort> recipeBoms = new List<ushort>();
- foreach (var item in Json<BatchingInfoPar>.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("成功写入配方数据");
- }
- }
- }
-
- /// <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
-
-
-
- }
-
- }
|