FilterMachine.cs 8.5 KB
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DeviceLibrary
{
    using crc = OnlineStore.CodeResourceControl;

    public partial class FilterMachine : MachineBase, IRobot
    {
        private Config_Filter _config;
        public new Config_Filter Config
        {
            get => _config;
            set
            {
                base.Config = value;
                _config = value;
            }
        }
        public override string DeviceName { get; } = "分盘线";
        public bool canRunning { get; set; }
        public bool isBusy { get; set; }
        public bool isAlarm { get; set; }
        //public RunStatus runStatus { get; set; }
        //public bool UserPause { get; set; }
        public bool IgnoreSafecheck { get; set; }
        public bool IgnoreGratingSignal { get; set; }
        public int NGBox_Count { get => nGBox_Count; set { nGBox_Count = value; ConfigHelper.Config.Set("NGBox_Count", value); } }
        public int MSDBox_Count { get => mSDBox_Count; set { mSDBox_Count = value; ConfigHelper.Config.Set("MSDBox_Count", value); } }
        public int PaperBox_Count { get => paperBox_Count; set { paperBox_Count = value; ConfigHelper.Config.Set("PaperBox_Count", value); } }

        private int nGBox_Count = 0;
        private int mSDBox_Count = 0;
        private int paperBox_Count = 0;

        public bool Init(out string msg)
        {
            msg = "";
            try
            {
                string configFile = "config\\FilterConfig.csv";
                Config = new Config_Filter(0, "", configFile);
                Config = (Config_Filter)CSVConfigReader.LoadConfig<IO_Filter_Type>(Config);

                MoveInfo = new MoveInfo(DeviceName);
                ResetMoveInfo = MoveInfo;

                NGBox_Count = ConfigHelper.Config.Get("NGBox_Count", 0);
                MSDBox_Count = ConfigHelper.Config.Get("MSDBox_Count", 0);
                PaperBox_Count = ConfigHelper.Config.Get("PaperBox_Count", 0);
            }
            catch (Exception e)
            {
                msg = e.Message;
                return false;
            }
            return true;
        }
        protected override void LoopProcess()
        {
            mstart = true;
            while (mstart)
            {
                try
                {
                    //HomeReset();
                    canRunning = DeviceCheck();
                    if (canRunning)
                    {
                        //BtnProcess();
                        canRunning = SafeCheck();
                    }
                    Thread.Sleep(stepDelaytime);
                    if (!canRunning || !mstart)
                        continue;
                    if (runStatus == RunStatus.Running)
                    {
                        WorkProcess();
                    }
                    else if (runStatus == RunStatus.HomeReset)
                    {
                        HomeReset();
                    }
                }
                catch (Exception ex)
                {
                    Msg.add(ex.Message, MsgLevel.warning);
                    LogUtil.error(DeviceName + " " + ex.ToString());
                }
                finally
                {
                    ProcessMsgEventFire(Msg);
                    //ProcessMoveinfoEventFire(MoveInfo.List);
                    Msg.Clear();
                }
            }
            LogUtil.info($"{DeviceName} 主线程已退出.");
        }
        public bool DeviceCheck()
        {
            bool ok = true;
            if (alarmType == AlarmType.SuddenStop)
            {
                Msg.add(crc.GetString("system_need_reset","系统需要重置"), MsgLevel.warning);
                ok = false;
            }

            if (NGBox_Count >= Config.NG_BOX_MAXCOUNT) {
                Msg.add(crc.GetString("ng_box_full", "NG箱已满"), MsgLevel.warning);
                ok = false;
            }
            if (MSDBox_Count >= Config.MSD_BOX_MAXCOUNT)
            {
                Msg.add(crc.GetString("msd_box_full", "MSD箱已满"), MsgLevel.warning);
                ok = false;
            }
            if (PaperBox_Count >= Config.PAPER_BOX_MAXCOUNT)
            {
                Msg.add(crc.GetString("paper_box_full", "Paper箱已满"), MsgLevel.warning);
                ok = false;
            }

            if (alarmType != AlarmType.SuddenStop)
            {
                TimeSpan span = DateTime.Now - checkAlarmTime;
                //在回原点,复位,出入库时,检测报警间隔减小
                if ((!runStatus.Equals(RunStatus.Stop) && span.TotalSeconds > 3) || span.TotalSeconds > 1)
                {
                    foreach (ConfigMoveAxis configMoveAxis in Config.moveAxisList)
                    {
                        if (AxisManager.GetAlarmStatus(configMoveAxis.DeviceName, configMoveAxis.GetAxisValue()) == 1)
                        {
                            Msg.add(crc.GetString("axis_run_alert","{0}:运动报警", configMoveAxis.Explain), MsgLevel.warning);
                            ok = false;
                        }
                    }
                }
            }
            return ok;
        }

        public void BeginHomeReset(bool firstRun = false)
        {
            if (!firstRun)
            {
                StopMove();
                Thread.Sleep(500);
            }
            OpenAllServo();
            alarmType = AlarmType.None;
            runStatus = RunStatus.HomeReset;
            ResetMoveInfo.NewMove(MoveStep.H01_HomeReset);
            ResetMoveInfo.log("开始回原");
            ResetMoveInfo.WaitList.Add(WaitResultInfo.WaitTime(1000));
        }
        private void HomeReset()
        {
            if (CheckWait(ResetMoveInfo))
                return;

            switch (ResetMoveInfo.MoveStep)
            {
                case MoveStep.H01_HomeReset:
                    ResetMoveInfo.NextMoveStep(MoveStep.H02_HomeReset);
                    ResetMoveInfo.log("正在回原, 所有阻挡上升");
                    CylinderMove(ResetMoveInfo, IO_Filter_Type.NG_TaryStop_Down, IO_Filter_Type.NG_TaryStop_Up, IO_VALUE.HIGH);
                    CylinderMove(ResetMoveInfo, IO_Filter_Type.Paper_TaryStop_Down, IO_Filter_Type.Paper_TaryStop_Up, IO_VALUE.HIGH);
                    CylinderMove(ResetMoveInfo, IO_Filter_Type.MSD_TaryStop_Down, IO_Filter_Type.MSD_TaryStop_Up, IO_VALUE.HIGH);
                    break;
                case MoveStep.H02_HomeReset:
                    ResetMoveInfo.NextMoveStep(MoveStep.H03_HomeReset);
                    ResetMoveInfo.log("正在回原");
                    IOMove(IO_Filter_Type.Line3_Run, IO_VALUE.HIGH);
                    break;
                case MoveStep.H03_HomeReset:
                    ResetMoveInfo.NextMoveStep(MoveStep.H04_HomeReset);
                    ResetMoveInfo.log("正在回原");
                    CylinderMove(ResetMoveInfo, IO_Filter_Type.NG_GetOut_Bck, IO_Filter_Type.NG_GetOut_Fwd, IO_VALUE.LOW);
                    CylinderMove(ResetMoveInfo, IO_Filter_Type.Paper_GetOut_Bck, IO_Filter_Type.Paper_GetOut_Fwd, IO_VALUE.LOW);
                    CylinderMove(ResetMoveInfo, IO_Filter_Type.MSD_GetOut_Bck, IO_Filter_Type.MSD_GetOut_Fwd, IO_VALUE.LOW);
                    break;
                case MoveStep.H04_HomeReset:
                    ResetMoveInfo.NextMoveStep(MoveStep.H05_HomeReset);
                    MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(1000));
                    break;
                case MoveStep.H05_HomeReset:
                    ResetMoveInfo.NextMoveStep(MoveStep.HEND_HomeReset);
                    ResetMoveInfo.log("正在回原, 所有阻挡下升");
                    CylinderMove(ResetMoveInfo, IO_Filter_Type.NG_TaryStop_Down, IO_Filter_Type.NG_TaryStop_Up, IO_VALUE.LOW);
                    CylinderMove(ResetMoveInfo, IO_Filter_Type.Paper_TaryStop_Down, IO_Filter_Type.Paper_TaryStop_Up, IO_VALUE.LOW);
                    CylinderMove(ResetMoveInfo, IO_Filter_Type.MSD_TaryStop_Down, IO_Filter_Type.MSD_TaryStop_Up, IO_VALUE.LOW);
                    IOMove(IO_Filter_Type.Line3_Run, IO_VALUE.LOW);
                    break;
                case MoveStep.HEND_HomeReset:
                    ResetMoveInfo.log("回源完成");
                    ResetMoveInfo.EndMove();
                    MoveInfo.NewMove(MoveStep.Filter_01_WaitReel);
                    runStatus = RunStatus.Running;
                    break;
            }
        }
    }
}