InputEquip.cs 13.4 KB
using Asa;
using DeviceLib;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace OnlineStore.DeviceLibrary
{
    public partial class InputEquip : EquipBase
    {
        public InputEquip_Config Config;
     
        public AxisBean MiddleAxis = null;
        public AxisBean UpdownAxis = null;
        public AxisBean InOutAxis = null;
   
        private AxisBean[] moveAxisArray;

        public BatchMoveBean LeftBatchMove = null;
        public BatchMoveBean RightBatchMove = null;

        public bool AutoInput = ConfigAppSettings.GetIntValue(Setting_Init.AutoInput).Equals(1);
        public int DefautTargetP = 0;

        public InputEquip(string cid, InputEquip_Config config)
        {
            this.DeviceID = config.Id;
            baseConfig = config;
            this.Config = config;
            IsDebug = config.IsDebug.Equals(1);
            Name = (" " + "入料模块" + " ").ToUpper();
            Init();
            ledProcessTimer.Elapsed += LedProcess;
            IoCheckTimer.Elapsed += IoCheckTimerProcess; 
            Config.SetAxisParam();

            MiddleAxis = new AxisBean(RobotManager.Config.Take_Middle_Axis, Name);
            UpdownAxis = new AxisBean(RobotManager.Config.Take_UpDown_Axis, Name);
            InOutAxis = new AxisBean(RobotManager.Config.Take_InOut_Axis, Name);

            AxisBean LeftBatchAxis = new AxisBean(RobotManager.Config.Left_Batch_Axis, Name);
            AxisBean RightBatchAxis = new AxisBean(RobotManager.Config.Right_Batch_Axis, Name);
            moveAxisArray = new AxisBean[] { MiddleAxis, UpdownAxis, InOutAxis };

            MoveInfo = new RobotMoveInfo(Name);
            SecMoveInfo = new RobotMoveInfo( "[" + Name.Trim() + "-SMove]");

            LeftBatchMove = new BatchMoveBean(Config, LeftBatchAxis,   1);
            RightBatchMove = new BatchMoveBean(config, RightBatchAxis,   2);
        }


        public override bool StartRun()
        {
            if (CanStartRun().Equals(false))
            {
                return false;
            }

            if (!OpenAllAxis())
            {
                CloseAllAxis();
                return false;
            }
            SetAllTimer(false);
            MoveInfo.EndMove();
            SecMoveInfo.EndMove();

            LogInfo("StartRun : 开始启动 ");
            runStatus = RobotRunStatus.HomeMoving;
            MoveInfo.NewMove(RobotMoveType.RHome);

            StartReset();
            SetAllTimer(true);

            return true;
        }

        public override bool Reset()
        {
            StopMove(); 
        
            if (!OpenAllAxis())
            {
                CloseAllAxis();
                return false;
            }  
            LogInfo("Reset  开始重置  ");
            runStatus = RobotRunStatus.Reset;
            MoveInfo.NewMove(RobotMoveType.Reset);
            StartReset();
            return true;

        }

        private void StartReset()
        { 
            //复位时设置状态为none
            AgvClient.SetStatus(Config.LeftAgvName);
            AgvClient.SetStatus(Config.RightAgvName);
 
         //   TimerMaxSeconds = 10;
            SetWarnMsg("");
            alarmType = AlarmType.None;
            isInSuddenDown = false;
            isNoAirCheck = false; 
            MoveInfo.NextMoveStep(StepEnum.IR01_Wait); 
            CylinderMove(MoveInfo, IO_Type.Clamping_Work, IO_Type.Clamping_Relax);
        }

        protected override void ResetProcess()
        {
            if (MoveInfo.IsInWait)
            {
                CheckWait(MoveInfo);
            }
            if (MoveInfo.IsInWait)
            {
                return;
            }
            if (MoveInfo.IsStep(StepEnum.IR01_Wait))
            {
                MoveInfo.NextMoveStep(StepEnum.IR02_InoutHome);
                WorkLog("复位:进出轴回原点");
                InOutAxis.HomeMove(MoveInfo);
            }
            else if (MoveInfo.IsStep(StepEnum.IR02_InoutHome))
            {
                MoveInfo.NextMoveStep(StepEnum.IR03_InoutToP1);
                WorkLog("复位:进出轴到待机点P1");
                InOutAxis.AbsMove(MoveInfo, Config.InoutAxis_P1, Config.InoutAxis_P1Speed);
            }
            else if (MoveInfo.IsStep(StepEnum.IR03_InoutToP1))
            {
                MoveInfo.NextMoveStep(StepEnum.IR04_UpdownAxisHome);
                MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(1000));
                WorkLog("复位:升降轴原点返回,左右批量轴开始回原点");
                UpdownAxis.HomeMove(MoveInfo); 
                LeftBatchMove.Reset();
                RightBatchMove.Reset();
            }
            else if (MoveInfo.IsStep(StepEnum.IR04_UpdownAxisHome))
            {
                MoveInfo.NextMoveStep(StepEnum.IR05_UpdownToP1);
                WorkLog("复位:升降轴运动到待机点P1");
                UpdownAxis.AbsMove(MoveInfo, Config.UpdownAxis_P1, Config.UpdownAxis_P1Speed);
            }
            else if (MoveInfo.IsStep(StepEnum.IR05_UpdownToP1))
            {
                MoveInfo.NextMoveStep(StepEnum.IR06_MiddleAxisHome);
                WorkLog("复位:旋转轴开始回原点"); 
                MiddleAxis.HomeMove(MoveInfo); 
            }
            else if (MoveInfo.IsStep(StepEnum.IR06_MiddleAxisHome))
            {
                MoveInfo.NextMoveStep(StepEnum.IR07_MiddleToP1);
                WorkLog("复位:旋转轴到待机点P1"); 
                MiddleAxis.AbsMove(MoveInfo, Config.MiddleAxis_P1, Config.MiddleAxis_P1Speed);

            }
            else if (MoveInfo.IsStep(StepEnum.IR07_MiddleToP1))
            {
                MoveInfo.NextMoveStep(StepEnum.IR08_Clamping_Relax);
                WorkLog("复位:夹爪放松");
                CylinderMove(MoveInfo, IO_Type.Clamping_Work, IO_Type.Clamping_Relax);
            }
            else if (MoveInfo.IsStep(StepEnum.IR08_Clamping_Relax))
            {
                MoveInfo.NextMoveStep(StepEnum.IR09_WaitBatchMove);
                WorkLog("复位:等待左右批量轴模块复位完成");
            }
            else if (MoveInfo.IsStep(StepEnum.IR09_WaitBatchMove))
            {
                if (LeftBatchMove.MoveInfo.MoveType.Equals(RobotMoveType.None) && RightBatchMove.MoveInfo.MoveType.Equals(RobotMoveType.None))
                {
                    WorkLog("复位完成");
                    runStatus = RobotRunStatus.Runing;
                    MoveInfo.EndMove();
                }
                else if (MoveInfo.IsTimeOut(180))
                {
                    if (!LeftBatchMove.MoveInfo.MoveType.Equals(RobotMoveType.None))
                    {
                        WarnMsg =Name+ "等待" + LeftBatchMove.Name + "复位完成超时[" + MoveInfo.TimeOutSeconds + "]秒";
                        LogUtil.error(  WarnMsg);
                        Alarm(AlarmType.IoSingleTimeOut);
                    }
                    else
                    {
                        WarnMsg = Name + "等待" + LeftBatchMove.Name + "复位完成超时[" + MoveInfo.TimeOutSeconds + "]秒";
                        LogUtil.error(  WarnMsg);
                        Alarm(AlarmType.IoSingleTimeOut);
                    } 
                }
            } 
        }

        internal override void StopMove()
        {
            //StopMove时设置状态为none
            AgvClient.SetStatus(Config.LeftAgvName);
            AgvClient.SetStatus(Config.RightAgvName);
             
            MoveInfo.EndMove();
            SecMoveInfo.EndMove();
             
            LeftBatchMove.StopMove();
            RightBatchMove.StopMove();

            UpdownAxis.SuddenStop();
            MiddleAxis.SuddenStop();
            InOutAxis.SuddenStop();

            CloseAllAxis();
        }

        public override void StopRun()
        {
            SetAllTimer(false);
            runStatus = RobotRunStatus.Wait; 
            StopMove();
        }


        protected override void BaseTimerProcess()
        {
            if (isInSuddenDown || isNoAirCheck)
            {
                return;
            }
            BusyMoveProcess();
            IOTimeOutProcess();
            //判断流水线打开了才可以运行 
            if (MoveInfo.MoveType.Equals(RobotMoveType.None) && NoErrorAlarm())
            {
                //若左侧或右侧在等待扫码结束的状态,需要开始去取料 
                if (LeftBatchMove.MoveInfo.MoveType.Equals(RobotMoveType.Working) && LeftBatchMove.MoveInfo.IsStep(StepEnum.IB10_ScanOK))
                {
                    string code = LeftBatchMove.MoveInfo.MoveParam.WareCode;
                    StartWorking(new WorkParam(1, 0, code));
                }
                else if (RightBatchMove.MoveInfo.MoveType.Equals(RobotMoveType.Working) && RightBatchMove.MoveInfo.IsStep(StepEnum.IB10_ScanOK))
                {
                    string code = RightBatchMove.MoveInfo.MoveParam.WareCode;
                    StartWorking(new WorkParam(2, 0, code));
                }
            }
            if (SecMoveInfo.MoveType.Equals(RobotMoveType.None) && NoErrorAlarm())
            {
                if (RobotManager.robot.runStatus <= RobotRunStatus.Wait)
                {
                    // StartCheckFixture();
                }
            }

            LeftBatchMove.TimerProcess();
            RightBatchMove.TimerProcess();

            if (NoErrorAlarm())
            {
                CheckAxisAlarm();
            }
        }


        internal bool CanStartWork()
        {
            //是否可以开始新的料串入料
            if (LeftBatchMove.MoveInfo.MoveType.Equals(RobotMoveType.Working))
            {
                StepEnum step = LeftBatchMove.MoveInfo.MoveStep; 
                if (step < StepEnum.IB21_BatchToP1)
                {
                    return false;
                }
            }
            else if (RightBatchMove.MoveInfo.MoveType.Equals(RobotMoveType.Working))
            {
                StepEnum step = RightBatchMove.MoveInfo.MoveStep; 
                if (step < StepEnum.IB21_BatchToP1)
                {
                    return false;
                }
            }
            return true ;
        }
  
        private DateTime checkAlarmTime = DateTime.Now;
        public void CheckAxisAlarm()
        {
            if (alarmType.Equals(AlarmType.AxisAlarm) || alarmType.Equals(AlarmType.AxisMoveError))
            {
                return;
            }
            TimeSpan span = DateTime.Now - checkAlarmTime;
            //在回原点,复位,出入库时,检测报警间隔减小
            if (MoveInfo.MoveType.Equals(RobotMoveType.None))
            {
                if (span.TotalSeconds < 3)
                {
                    return;
                }
            }
            else if (span.TotalSeconds < 1)
            {
                return;
            }

            checkAlarmTime = DateTime.Now;
            foreach (AxisBean axisInfo in moveAxisArray)
            {
                if (AxisIsAlarm(axisInfo))
                { 
                    return;
                }
            }
            if (AxisIsAlarm(LeftBatchMove.BatchAxis,false ))
            {
                LeftBatchMove.WarnMsg = WarnMsg;
                LeftBatchMove.Alarm(AlarmType.AxisAlarm);
                return;
            }
            if (AxisIsAlarm(RightBatchMove.BatchAxis,false ))
            {
                RightBatchMove.WarnMsg = WarnMsg;
                RightBatchMove.Alarm(AlarmType.AxisAlarm);
                return;
            }
        }


        public bool OpenAllAxis(bool isCheck = true)
        {

            if (RunMultiAxis(isCheck, IO_Type.TakeAxis_ON, IO_Type.Take_Axis_Break, moveAxisArray))
            {
                if (RunAxis(isCheck, LeftBatchMove.BatchAxis) && RunAxis(isCheck, RightBatchMove.BatchAxis))
                {
                    return true;
                }
            }
            return false;
        }

        public void CloseAllAxis()
        {
            IOMove(IO_Type.Take_Axis_Break, IO_VALUE.LOW);
            IOMove(IO_Type.LeftBatchAxis_Break, IO_VALUE.LOW);
            IOMove(IO_Type.RightBatchAxis_Break, IO_VALUE.LOW);

            CloseMultiAxis(IO_Type.TakeAxis_ON, IO_Type.Take_Axis_Break, moveAxisArray);
            CloseAxis(LeftBatchMove.BatchAxis);
            CloseAxis(RightBatchMove.BatchAxis);
        } 
        public override string GetMoveStr()
        {
            string msg = "";
            int tLength = 15; 
          
            msg += "左侧料串:" + LeftBatchMove.CurrShelfId + "    右侧料串:"+RightBatchMove.CurrShelfId+" \r\n";
        
            msg += "runS:  " + runStatus + "\n"; 
            msg += "alarm:  " + alarmType + "      " + LeftBatchMove.alarmType +"      " + RightBatchMove.alarmType +"\n";
            msg += "Move:" + MoveInfo.MoveType + " "+MoveInfo.MoveStep + "\n";
            if (MoveInfo.MoveType.Equals(RobotMoveType.Working))
            {
                msg += "料盘信息:" + MoveInfo.MoveParam.ToStr() + "\n";
            }
            else
            {
                msg+= "\n";
            }
            msg += "左侧上料 :" + LeftBatchMove.GetMoveStr()+"\n";
            msg += "右侧上料:" + RightBatchMove.GetMoveStr() + "\n";
            return msg;
        } 
    }
    
}