XRayMachine.cs 8.3 KB
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DeviceLibrary
{
    public partial class XRayMachine : MachineBase, IRobot
    {
        private Config_XRay _config;
        public new Config_XRay Config
        {
            get => _config;
            set
            {
                base.Config = value;
                _config = value;
            }
        }
        public override string DeviceName { get; set; } = "点料";
        public bool canRunning { get; set; }
        public bool isBusy { get; set; }
        public bool isAlarm { get; set; }
        //public RunStatus runStatus { get; set; }
        //public override bool UserPause { get; set; }
        public bool IgnoreSafecheck { get; set; }
        public bool IgnoreGratingSignal { get; set; }

        AxisBean Line_Entry_Axis;
        AxisBean Line_In_Axis;

        public bool Init(out string msg)
        {
            
            msg = "";
            try
            {
                string configFile = "config\\XrayConfig.csv";
                Config = new Config_XRay(0, "", configFile);
                Config = (Config_XRay)CSVConfigReader.LoadConfig<IO_XRay_Type>(Config);
                LogUtil.info(DeviceName + " config load finish");
                #region 初始化伺服轴
                Line_Entry_Axis = new AxisBean(Config.Line_Entry_Axis, DeviceName);
                Line_In_Axis = new AxisBean(Config.Line_In_Axis, DeviceName);
                #endregion
                MoveInfo = new MoveInfo(DeviceName);
                ResetMoveInfo = MoveInfo;
                LedProcessInit();
                IOMonitor.RegisterIO(IO_XRay_Type.Exit_Open, Config, IO_VALUE.LOW, delegate () { XRayDoorOpen(); });
                IOMonitor.RegisterIO(IO_XRay_Type.Entry_Open, Config, IO_VALUE.LOW, delegate () { XRayDoorOpen(); });
                LogUtil.info(DeviceName + " init end");
            }
            catch (Exception e){
                msg = e.Message;
                return false;
            }
            return true;
        }
        void XRayDoorOpen() {
            if (RobotManage.XRay.IsRayOpen)
            {
                LogUtil.info(DeviceName + " X光机防护门打开");
                RobotManage.XRay.Stop();
                IOMove(IO_XRay_Type.Xray_Lock, IO_VALUE.LOW);
            }
            else {
                LogUtil.info(DeviceName + " X光没有打开");
            }
        }
        protected override void LoopProcess()
        {
            mstart = true;
            while (mstart)
            {
                try
                {
                    canRunning = DeviceCheck();
                    if (canRunning)
                    {
                        canRunning = SafeCheck();
                    }
                    Thread.Sleep(stepDelaytime);
                    if (!canRunning || !mstart)
                        continue;
                    if (runStatus == RunStatus.Running)
                    {
                        FeedingProcess();

                        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} 主线程已退出.");
        }


        /// <summary>
        /// 最后一次气压检测变为0的时间
        /// </summary>
        DateTime lastAirCloseTime = DateTime.MinValue;        
        public bool DeviceCheck() {
            //return true;
            bool ok = true;
            if (IOValue(IO_XRay_Type.SuddenStop_BTN).Equals(IO_VALUE.LOW))
            {
                Alarm(AlarmType.SuddenStop);
                Msg.add("急停中", MsgLevel.warning);
                ok = false;

            }
            else if (alarmType == AlarmType.SuddenStop)
            {
                Msg.add("系统需要重置", MsgLevel.info);
                ok = false;
            }
            if (IOValue(IO_XRay_Type.Airpressure_Check).Equals(IO_VALUE.LOW))
            {
                if (lastAirCloseTime == DateTime.MinValue)
                    lastAirCloseTime = DateTime.Now;

                TimeSpan span = DateTime.Now - lastAirCloseTime;
                if (span.TotalSeconds > Config.AirCheckSeconds)
                {
                    ok = false;
                    Msg.add("气压不足", MsgLevel.warning);
                }
            }
            else
            {
                lastAirCloseTime = DateTime.MinValue;
            }
            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($"{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_XRay_Type.Location_Cylinder_Down, IO_XRay_Type.Location_Cylinder_Up,IO_VALUE.LOW);
                    CylinderMove(ResetMoveInfo, IO_XRay_Type.Entry_Close, IO_XRay_Type.Entry_Open);
                    CylinderMove(ResetMoveInfo, IO_XRay_Type.Exit_Close, IO_XRay_Type.Exit_Open);
                    break;
                case MoveStep.H02_HomeReset:
                    ResetMoveInfo.NextMoveStep(MoveStep.H03_HomeReset);
                    Line_Entry_Axis.RelMove(ResetMoveInfo, Config.Line_Entry_Relative*2, Config.Line_Entry_Relative_speed);
                    Line_In_Axis.RelMove(ResetMoveInfo, Config.Line_In_Relative*2, Config.Line_In_Relative_speed);
                    ResetMoveInfo.log("正在回原");
                    break;
                case MoveStep.H03_HomeReset:
                    ResetMoveInfo.NextMoveStep(MoveStep.HEND_HomeReset);
                    ResetMoveInfo.log("正在回原, 门关闭");
                    CylinderMove(ResetMoveInfo, IO_XRay_Type.Entry_Close, IO_XRay_Type.Entry_Open,IO_VALUE.LOW);
                    CylinderMove(ResetMoveInfo, IO_XRay_Type.Exit_Close, IO_XRay_Type.Exit_Open, IO_VALUE.LOW);
                    CylinderMove(ResetMoveInfo, IO_XRay_Type.Location_Cylinder_Down, IO_XRay_Type.Location_Cylinder_Up, IO_VALUE.HIGH);
                    break;
                case MoveStep.HEND_HomeReset:
                    ResetMoveInfo.log("回源完成");
                    ResetMoveInfo.EndMove();
                    //MoveInfo.NewMove(MoveStep.Feeding_05_Goto_Xray);
                    runStatus = RunStatus.Running;
                    break;
            }
        }
    }
}