LabelMachine.cs 10.1 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 LabelMachine : MachineBase, IRobot
    {
        private Config_Label _config;
        public new Config_Label 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 bool DisableLabel { get; set; }
        //public RunStatus runStatus { get; set; }
        //public bool UserPause { get; set; }
        public bool IgnoreSafecheck { get; set; }
        public bool IgnoreGratingSignal { get; set; }
        AxisBean Label_X_Axis;
        AxisBean Label_Y_Axis;
        AxisBean Label_Z_Axis;
        AxisBean Label_R_Axis;

        public MoveInfo SendOutMoveInfo;
        public MoveInfo VacuumMoveInfo;

        public MoveInfo TransMoveInfo;
        public bool Init(out string msg)
        {
            msg = "";
            try
            {
                string configFile = "config\\LabelConfig.csv";
                Config = new Config_Label(0, "", configFile);
                Config = (Config_Label)CSVConfigReader.LoadConfig<IO_Label_Type>(Config);

                #region 初始化伺服轴
                Label_X_Axis = new AxisBean(Config.Label_X_Axis, DeviceName);
                Label_Y_Axis = new AxisBean(Config.Label_Y_Axis, DeviceName);
                Label_Z_Axis = new AxisBean(Config.Label_Z_Axis, DeviceName);
                Label_R_Axis = new AxisBean(Config.Label_R_Axis, DeviceName);
                #endregion
                
                MoveInfo = new MoveInfo(DeviceName);
                SendOutMoveInfo = new MoveInfo("贴标送出");
                VacuumMoveInfo = new MoveInfo("贴标吸标");
                TransMoveInfo = new MoveInfo("移栽接收");
                SendOutMoveInfo.Hide = true;
                VacuumMoveInfo.Hide = true;
                ResetMoveInfo = MoveInfo;
                LedProcessInit();
            }
            catch (Exception e)
            {
                msg = e.Message;
                return false;
            }
            return true;
        }
        public override void Run()
        {
            base.Run();
            IOMove(IO_Label_Type.Camera_Led, IO_VALUE.HIGH);
        }
        public override void Stop()
        {
            IOMove(IO_Label_Type.Camera_Led, IO_VALUE.LOW);
            base.Stop();
        }
        protected override void LoopProcess()
        {
            mstart = true;
            while (mstart)
            {
                try
                {
                    canRunning = DeviceCheck();
                    if (canRunning)
                    {
                        //BtnProcess();
                        canRunning = SafeCheck();
                    }
                    Thread.Sleep(stepDelaytime);
                    if (!canRunning || !mstart)
                        continue;
                    if (runStatus == RunStatus.Running)
                    {
                        WorkProcess();
                        TransProcess();
                        SendOutProcess();
                        VacuumLabelProcess();
                    }
                    else if (runStatus == RunStatus.HomeReset)
                    {
                        HomeReset();
                    }
                }
                catch (Exception ex)
                {
                    Msg.add(ex.Message, MsgLevel.warning);
                    LogUtil.error(DeviceName + " " + ex.ToString());
                    Thread.Sleep(stepDelaytime*5);
                }
                finally
                {
                    ProcessMsgEventFire(Msg);
                    //ProcessMoveinfoEventFire(MoveInfo.List);
                    Msg.Clear();
                }
            }
            LogUtil.info($"{DeviceName} 主线程已退出.");
        }
        public bool DeviceCheck()
        {
            bool ok = true;
            if (IOManager.GetDIValue("", 0, 45).Equals(IO_VALUE.LOW))
            {
                Alarm(AlarmType.SuddenStop);
                Msg.add(crc.GetString("emergency_stop", "急停中"), MsgLevel.warning);
                Thread.Sleep(1000);
                ok = false;
            }
            else if (alarmType == AlarmType.SuddenStop)
            {
                Msg.add(crc.GetString("system_need_reset", "系统需要重置"), MsgLevel.warning);
                Thread.Sleep(1000);
                ok = false;
            }

            if (alarmType != AlarmType.SuddenStop)
            {
                TimeSpan span = DateTime.Now - checkAlarmTime;
                //在回原点,复位,出入库时,检测报警间隔减小
                if ((!runStatus.Equals(RunStatus.Stop) && span.TotalSeconds > 3) || span.TotalSeconds > 1)
                {
                    if (Config.moveAxisList != null)
                    {
                        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;
                                LogUtil.error(string.Join(",", HuichuanLibrary.HCBoardManager.GetAxisErrorDetail(configMoveAxis.GetAxisValue())));
                            }
                        }
                    }
                }
            }
            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("正在回原, 皮带线阻挡上升,贴标机构阻挡上升,z轴");
                    CylinderMove(ResetMoveInfo, IO_Label_Type.TrayStop_Down, IO_Label_Type.TrayStop_Up);
                    CylinderMove(ResetMoveInfo, IO_Label_Type.Label_Stop_Down, IO_Label_Type.Label_Stop_Up);
                    Label_Z_Axis.HomeMove(ResetMoveInfo);
                    break;
                case MoveStep.H02_HomeReset:
                    ResetMoveInfo.NextMoveStep(MoveStep.H03_HomeReset);
                    ResetMoveInfo.log("正在回原 y轴");
                    IOMove(IO_Label_Type.Line1_Run, IO_VALUE.HIGH);
                    IOMove(IO_Label_Type.Line2_Run, IO_VALUE.HIGH);
                    Label_X_Axis.HomeMove(ResetMoveInfo);
                    break;
                case MoveStep.H03_HomeReset:
                    ResetMoveInfo.NextMoveStep(MoveStep.H04_HomeReset);
                    ResetMoveInfo.log("正在回原 xyzr轴");
                    Label_R_Axis.HomeMove(ResetMoveInfo);
                    Label_Y_Axis.HomeMove(ResetMoveInfo);
                    break;
                case MoveStep.H04_HomeReset:
                    ResetMoveInfo.NextMoveStep(MoveStep.H05_HomeReset);
                    ResetMoveInfo.WaitList.Add(WaitResultInfo.WaitTime(500));
                    break;
                case MoveStep.H05_HomeReset:
                    ResetMoveInfo.NextMoveStep(MoveStep.H06_HomeReset);
                    ResetMoveInfo.log("正在回原 等待2秒钟");
                    
                    Label_R_Axis.AbsMove(ResetMoveInfo, Config.Label_R_P2, Config.Label_R_P2_speed);
                    Label_Z_Axis.AbsMove(ResetMoveInfo, Config.Label_Z_P1, Config.Label_Z_P1_speed);
                    ResetMoveInfo.WaitList.Add(WaitResultInfo.WaitTime(2000));
                    break;
                case MoveStep.H06_HomeReset:
                    ResetMoveInfo.NextMoveStep(MoveStep.H07_HomeReset);
                    ResetMoveInfo.log("正在回原 xyzr轴到待机点");
                    Label_Y_Axis.AbsMove(ResetMoveInfo, Config.Label_Y_P2, Config.Label_Y_P2_speed);
                    break;
                case MoveStep.H07_HomeReset:
                    ResetMoveInfo.NextMoveStep(MoveStep.HEND_HomeReset);
                    ResetMoveInfo.log("正在回原 线体停止,阻挡下降");
                    Label_X_Axis.AbsMove(ResetMoveInfo, Config.Label_X_P2, Config.Label_X_P2_speed);
                    IOMove(IO_Label_Type.Line1_Run, IO_VALUE.LOW);
                    IOMove(IO_Label_Type.Line2_Run, IO_VALUE.LOW);
                    CylinderMove(ResetMoveInfo, IO_Label_Type.TrayStop_Up, IO_Label_Type.TrayStop_Down);
                    CylinderMove(ResetMoveInfo, IO_Label_Type.Label_Stop_Up, IO_Label_Type.Label_Stop_Down);
                    break;
                case MoveStep.HEND_HomeReset:
                    ResetMoveInfo.log("回源完成");
                    ResetMoveInfo.EndMove();
                    MoveInfo.NewMove(MoveStep.Lbl_01_Wait_ATray);
                    TransMoveInfo.NewMove(MoveStep.Wait);
                    runStatus = RunStatus.Running;
                    break;
            }
        }
        public IO_VALUE Label_TaryStop_Check
        {
            get => IOValue(IO_Label_Type.Label_TaryStop_Check);
        }
        public void InLineRunControl(IO_VALUE iO_VALUE)
        {
            IOMove(IO_Label_Type.Line1_Run, iO_VALUE);
        }
    }
}