InletEquip_InStore.cs 13.1 KB
using Asa;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

namespace OnlineStore.DeviceLibrary
{
    partial class InletEquip
    {
        /// <summary>
        /// 等待机器人在安全位置
        /// </summary>
        /// <param name="moveInfo"></param>
        /// <param name="wait"></param>
        /// <returns></returns>
        protected override bool CheckWaitResult(DeviceMoveInfo moveInfo, WaitResultInfo wait)
        {
            if (wait.WaitType.Equals(WaitEnum.W102_FeedScanCode))
            {
                if (LastCodeList.Count > 0)
                {
                    wait.IsEnd = true;
                }
            }
            else if (wait.WaitType.Equals(WaitEnum.W201_RobotMove))
            {
                if (StoreManager.Client.robotEquip.RobotMoveEnd(curcmd))
                {
                    wait.IsEnd = true;
                }
            }
            return wait.IsEnd;
        }
        /// <summary>
        /// 当前命令
        /// </summary>
        private string curcmd = "";
        private List<string> LastCodeList = new List<string>();
        #region 自动入料
        private DateTime startInTime = DateTime.Now;
        public override bool StartInstore(InOutParam param)
        {
            if (!NoAlarm())
            {
                LogInfo("报警中,无法开始自动入料:" + param.PosInfo.ToStr());
                return false;
            }

            if (MoveInfo.MoveType.Equals(MoveType.None))
            {
                runStatus = RunStatus.Busy;
                MoveInfo.NewMove(MoveType.InStore, param);
                startInTime = DateTime.Now;
                {
                    MoveInfo.NextMoveStep(StepEnum.Inlet_01_Wait);
                    MoveLog($"开始入料{MoveInfo.SLog}");
                }
                return true;
            }
            return false;
        }

        private void NextMoveStep(StepEnum step, string msg)
        {
            MoveInfo.NextMoveStep(step);
            MoveLog($" {MoveInfo.SLog}:{msg}");
        }
        InOutPosInfo lastPosInfo = new InOutPosInfo();
        Task getTrayInfoTask = null;
        Task getTargetInfoTask = null;
        protected override void InstoreProcess()
        {
            if (MoveInfo.IsInWait)
            {
                CheckWait(MoveInfo);
            }
            if (MoveInfo.IsInWait)
            {
                return;
            }

            if (MoveInfo.IsStep(StepEnum.Inlet_01_Wait))
            {
                NextMoveStep(StepEnum.Inlet_02_CheckTray, "检查是否有料盘");
            }
            else if (MoveInfo.IsStep(StepEnum.Inlet_02_CheckTray))
            {
                if (HasTrayInPosition())
                {
                    NextMoveStep(StepEnum.Inlet_05_GetTrayInfo, "有料盘,扫码,准备获取料盘信息");
                    AllowFeedIn(false);
                    Inlet_05_GetTrayInfo();
                }
                else//无料盘
                {
                    NextMoveStep(StepEnum.Inlet_03_AllowFeedIn, "无料盘,允许料盘进入,链条运行");
                    AllowFeedIn(true);
                    InletRun(true);
                    AddWaitTrayInPosition(true);
                    AddWaitTime(5000);
                }
            }
            else if (MoveInfo.IsStep(StepEnum.Inlet_03_AllowFeedIn))
            {
                NextMoveStep(StepEnum.Inlet_04_TrayInPosition, "料盘到位,拒绝料盘进入,链条停止");
                AllowFeedIn(false);
                InletRun(false);

            }
            else if (MoveInfo.IsStep(StepEnum.Inlet_04_TrayInPosition))
            {
                NextMoveStep(StepEnum.Inlet_05_GetTrayInfo, "料盘已到位,扫码,准备获取料盘信息");
                Inlet_05_GetTrayInfo();
            }
            else if (MoveInfo.IsStep(StepEnum.Inlet_05_GetTrayInfo))
            {
                if (getTrayInfoTask != null && getTrayInfoTask.IsCompleted)
                {
                    NextMoveStep(StepEnum.Inlet_06_JackUp, "获取料盘信息完成,顶升抬起");
                    Inlet_06_JackUp();
                }
            }
            else if (MoveInfo.IsStep(StepEnum.Inlet_06_JackUp))
            {
                if (MoveInfo.MoveParam.PosInfo.IsNG)
                {
                    NextMoveStep(StepEnum.Inlet_08_WaitTrayLeave, "条码NG,等待料盘离开");
                    AddWaitTrayInPosition(false);
                    SetCurCmd(RobotEquip.GetCmd_InletUp());
                }
                else
                {
                    NextMoveStep(StepEnum.Inlet_07_GetTargetInfo, "从服务器获取入库库位");
                    GetTargetInfo();
                }

            }
            else if (MoveInfo.IsStep(StepEnum.Inlet_07_GetTargetInfo))
            {
                if (getTargetInfoTask != null && getTargetInfoTask.IsCompleted && lastPosInfo != null)
                {
                    getPosIdMsg = "";
                    ClearTimeoutAlarm("获取库位号超时");
                    //InOutPosInfo oldPos = MoveInfo.MoveParam.PosInfo;
                    //if ((!LastPosInfo.PlateH.Equals(oldPos.PlateH)) || (!LastPosInfo.PlateW.Equals(oldPos.PlateW)))
                    //{
                    //    MoveLog(" " + MoveInfo.SLog + " 原有料盘尺寸:【" + oldPos.PlateW + "X" + oldPos.PlateH + "】服务器返回尺寸【" + LastPosInfo.PlateW + "X" + LastPosInfo.PlateH + "】   ");
                    //}
                    MoveInfo.MoveParam.PosInfo = lastPosInfo;
                    NextMoveStep(StepEnum.Inlet_08_WaitTrayLeave, "获取目标仓位成功,等待料盘离开");
                    AddWaitTrayInPosition(false);
                    SetCurCmd(RobotEquip.GetCmd_InletUp());
                }
                else if (MoveInfo.IsTimeOut(15))
                {
                    MoveTimeOut(MoveInfo, "获取库位号超时 " + getPosIdMsg);
                }
            }
            else if (MoveInfo.IsStep(StepEnum.Inlet_08_WaitTrayLeave))
            {
                NextMoveStep(StepEnum.Inlet_09_WaitRobotInSafe, "料盘离开入料口,等待机器人到安全位置");
                BufferDataManager.TrayInRobotInfo = MoveInfo.MoveParam.PosInfo;
                SetCurCmd(RobotEquip.GetCmd_Standby());
                AddWaitTime(10000);
                MoveInfo.OneWaitCanEndStep=true;
            }
            else if (MoveInfo.IsStep(StepEnum.Inlet_09_WaitRobotInSafe))
            {
                NextMoveStep(StepEnum.Inlet_10_JackDown, "机器人到达安全位置,顶升下降");
                Inlet_10_JackDown();
            }
            else if (MoveInfo.IsStep(StepEnum.Inlet_10_JackDown))
            {
                curcmd = ""; 
                MoveInfo.EndMove();

            }

        }
        #endregion
        private void SetCurCmd(string cmd)
        {
            curcmd = cmd;
            MoveInfo.WaitList.Add(WaitResultInfo.WaitRobotMove());
        }
        private void Inlet_05_GetTrayInfo()
        {
            bool isScan = ConfigAppSettings.GetIntValue(Setting_Init.NeedScanCode).Equals(1);
            LastCodeList = new List<string>();
            getTrayInfoTask = Task.Factory.StartNew(() =>
            {
                if (isScan)
                {
                    MoveInfo.OneWaitCanEndStep = true;
                    MoveInfo.WaitList.Add(WaitResultInfo.WaitFeedScanCode());
                    MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(6000));
                    try
                    {
                        LastCodeList = CodeManager.CameraScan(Config.ScanCodeCamera, Name);
                        if (LastCodeList.Count <= 0)
                        {
                            LastCodeList = CodeManager.CameraScan(Config.ScanCodeCamera, Name);
                        }
                    }
                    catch (Exception ex)
                    {
                        LogUtil.error("扫码出错", ex);
                    }
                }

                string code = CodeManager.ProcessCode(LastCodeList);
                MoveInfo.MoveParam.PosInfo.barcode = code;
                if (string.IsNullOrEmpty(code))
                {
                    MoveInfo.MoveParam.PosInfo.IsNG = true;
                    MoveInfo.MoveParam.PosInfo.NgMsg = "无有效条码NG";
                }
                //获取料盘宽度
                MoveInfo.MoveParam.PosInfo.PlateW = GetWidth();

                //获取厚度
                MoveInfo.MoveParam.PosInfo.PlateH = GetHeight();
                //扫码

            });
            AddWaitTime(3000);
        }
        private int GetWidth()
        {
            int width = 7;
            double val = IOManager.GetADVal(0) * Config.Coeff_Width;
            if (Math.Abs(val - 7) < Math.Abs(val - 13))
                return width;
            else
                return 13;
        }
        private int GetHeight()
        {
            int height = 8;
            return height;
        }
        private int LastResult = 0;
        private string getPosIdMsg = "";
        private void GetTargetInfo()
        {
            InOutPosInfo pos = MoveInfo.MoveParam.PosInfo;
            lastPosInfo = null;
            List<string> codeList = Regex.Split(pos.barcode, "##", RegexOptions.IgnoreCase).ToList();
            getTargetInfoTask = Task.Factory.StartNew(() =>
            {
                //更新托盘条码信息 
                try
                {
                    int count = 1;
                    while (MoveInfo.MoveType.Equals(MoveType.InStore))
                    {
                        int ms = 5000;


                        string lastPosId = "";

                        //从服务器获取库位号
                        GetPosResult result = SServerManager.GetPosId(Name, codeList, pos.PlateH, pos.PlateW, pos.rfid, lastPosId);
                        LastResult = result.Result;
                        if (result.IsTimeOut)
                        {
                            if (count < 5)
                            {
                                ms = 2000;
                            }
                            Thread.Sleep(ms);
                            LogUtil.error(Name + "【" + pos.barcode + "】第[" + count + "]次 " + MoveInfo.SLog + " 超时,等待" + ms + "后重新获取");
                        }
                        else if (result.Result.Equals(99) || result.Result.Equals(100))
                        {
                            if (count < 5)
                            {
                                ms = 3000;
                            }
                            getPosIdMsg = result.Msg;
                            Thread.Sleep(ms);
                            LogUtil.error(Name + "【" + pos.barcode + "】第[" + count + "]次  " + MoveInfo.SLog + "  结果【" + result.Result + "】,等待" + ms + "后重新获取");
                        }
                        else if (!result.Msg.Equals(""))
                        {
                            lastPosInfo = result.Param;
                            LogUtil.error(Name + "【" + pos.barcode + "】第[" + count + "]次  " + MoveInfo.SLog + "  入库NG:" + result.Msg);
                            break;
                        }
                        else
                        {
                            lastPosInfo = result.Param;
                            break;
                        }

                        if (count >= 3)
                        {
                            //自动NG
                            lastPosInfo = new InOutPosInfo(pos.barcode, "", MoveInfo.MoveParam.PosInfo.PlateW, MoveInfo.MoveParam.PosInfo.PlateH);
                            lastPosInfo.IsNG = true;
                            lastPosInfo.NgMsg = "获取库位超过三次,直接NG";
                            break;
                        }
                        count++;
                    }
                }
                catch (Exception ex)
                {
                    LogUtil.error(Name + "【" + pos.barcode + "】 " + MoveInfo.SLog + " 获取库位号报错:" + ex.ToString());
                }
            });
        }
        /// <summary>
        /// 入料线体是否有料盘
        /// </summary>
        /// <returns></returns>
        public bool HasTrayInPosition()
        {
            return IOValue(IO_Type.Inlet_TrayCheck).Equals(IO_VALUE.HIGH);
        }
        private void Inlet_06_JackUp()
        {
            if (MoveInfo.MoveParam.PosInfo != null && MoveInfo.MoveParam.PosInfo.PlateW.Equals(7))
            {
                Inch7JackUp(true);
            }
            else if (MoveInfo.MoveParam.PosInfo != null && MoveInfo.MoveParam.PosInfo.PlateW.Equals(13))
            {
                Inch13JackUp(true);
            }
        }
        private void Inlet_10_JackDown()
        {
            Inch7JackUp(false);
            Inch13JackUp(false);
        }
    }
}