MoveEquip.cs 13.3 KB
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
{
    /// <summary>
    /// 流水线自动料仓-进仓装置类
    /// </summary>
    public partial class MoveEquip : EquipBase
    {
        public MoveEquip_Config Config;

        public MoveEquip(string cid, MoveEquip_Config config)
        {
            this.DeviceID = config.Id;
            this.Config = config;
            baseConfig = config;
            IsDebug = config.IsDebug.Equals(1);
            Name = (" " + ""+ GetNByID() + "进仓" + DeviceID.ToString().PadLeft(2, '0') + " " ).ToUpper();

            Init();
             
            MoveInfo = new LineMoveInfo(DeviceID, "[" + Name.Trim() + "-Move]");
            SecondMoveInfo = new LineMoveInfo(DeviceID, "[" + Name.Trim() + "-SMove]");

            UseAxis = config.UpDownUseAxis.Equals(1); 
            UpdownAxis = new AxisBean(config.UpDown_Axis, Name);
            RFIDIP = RFIDManager.GetRFIP(DeviceID);
        }
        private string GetNByID()
        {
            string name = "";
            int index = DeviceID;
            if (index >= 0)
            {
                int num =( index+1) / 2;
                int subNum = index % 2;
                if (subNum.Equals(0))
                {
                    subNum = 2;
                }
                if (num <= 6)
                {

                }
                else
                {
                    subNum = 3 - subNum;
                }
                name = "D" + num + "-" + subNum;
            }
            return name;
        }
        public override bool StartRun(bool isDebug = false)
        {
            if (CanStartRun().Equals(false))
            {
                return false;
            }

            if (!RunAxis(true, UpdownAxis))
            {
                return false;
            }

            mainTimer.Enabled = false;
            MoveInfo.EndMove();
            SecondMoveInfo.EndMove();
            runStatus = LineRunStatus.HomeMoving;
            LogInfo("开始 原点返回: 升降轴回原点,阻挡气缸上升 ");
            MoveInfo.NewMove(LineMoveType.RHome);
            StartReset();
            if (isDebug)
            {
                mainTimer.Enabled = true;
            }
            return true;
        }


        public override bool Reset()
        {
            StopMove("复位");
            if (!RunAxis(true, UpdownAxis))
            {
                return false;
            }
            LogInfo("开始重置: 升降轴回原点,阻挡气缸上升 ");

            runStatus = LineRunStatus.Reset;
            SecondMoveInfo.EndMove();
            MoveInfo.NewMove(LineMoveType.Reset);
            StartReset();
            return true;
        }

        private void StartReset()
        {
            ResetClearData(); 
            //移载装置原点状态:顶升气缸下降端,前后气缸后退端,上下气缸上升端,夹料气缸放松端,阻挡气缸输入=0     
            UpdownHomeMove(); 
            if (IsDebug)
            {
                IOMove(IO_Type.StopDown1, IO_VALUE.HIGH);
                IOMove(IO_Type.StopDown2, IO_VALUE.HIGH);
            }
            else
            {
                IOMove(IO_Type.StopDown1, IO_VALUE.LOW);
                IOMove(IO_Type.StopDown2, IO_VALUE.LOW);
            }
            isInPro = false;
        }

        protected override void ResetProcess()
        {
            if (MoveInfo.IsInWait)
            {
                CheckWait(MoveInfo);
            }
            else if (SecondMoveInfo.IsInWait)
            {
                CheckWait(SecondMoveInfo);
            }
            if (!MoveInfo.IsInWait && !SecondMoveInfo.IsInWait)
            {
                //流水线各装置复原位,夹料气缸状态不变
                //阻挡气缸全部=0
                //上下气缸上升,、
                //上升到位,顶升气缸下降,前后气缸回退
                //复位时夹紧气缸需要发送,不然后面出入库会有问题
                switch (MoveInfo.MoveStep)
                {
                    case LineMoveStep.MH_01_UpDownHome:
                        MoveInfo.NextMoveStep(LineMoveStep.MH_02_CylinderUp);
                        DebugInfo(MoveInfo.MoveType + " : 升降轴走到待机点");
                        UpdownUpMove();
                        MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(2000));
                        break;
                    case LineMoveStep.MH_02_CylinderUp:

                        MoveInfo.NextMoveStep(LineMoveStep.MH_03_OtherBack);
                        DebugInfo(MoveInfo.MoveType + " : 上升气缸到位,顶升气缸下降,前后气缸后退,夹紧气缸放松");
                        CylinderMove(MoveInfo, IO_Type.TopCylinder_Up, IO_Type.TopCylinder_Down);
                        CylinderMove(MoveInfo, IO_Type.BeforeAfterCylinder_Before, IO_Type.BeforeAfterCylinder_After);
                        CylinderMove(MoveInfo, IO_Type.ClampCylinder_Work, IO_Type.ClampCylinder_Relax);
                        break;

                    case LineMoveStep.MH_03_OtherBack:
                        MoveInfo.NextMoveStep(LineMoveStep.MH_04_ClampCheck);
                        DebugInfo(MoveInfo.MoveType + " : 等待夹爪气缸料盘检测无料");
                        MoveInfo.WaitList.Add(WaitResultInfo.WaitIO(IO_Type.ClampCylinder_Check, IO_VALUE.LOW));
                        break;
                    case LineMoveStep.MH_04_ClampCheck:

                        LogInfo(MoveInfo.MoveType + " 完成!");
                        runStatus = LineRunStatus.Runing;
                        MoveInfo.EndMove();
                        SecondMoveInfo.EndMove();
                     
                        break;
                    default: break;
                }
            }
        }
         
        internal override void StopMove(string stopDes = "")
        {
            if (!String.IsNullOrEmpty(stopDes))
            { 
                if (MoveInfo.MoveType.Equals(LineMoveType.InStore) || MoveInfo.MoveType.Equals(LineMoveType.OutStore))
                {
                    LogInfo("["+stopDes + "]时中止运动: "+MoveInfo.ToStr());
                }
            }

            trayCheck2LowWait.Stop();
            trayCheckWait.Stop();

            MoveInfo.EndMove();
            runStatus = LineRunStatus.Busy;
            SecondMoveInfo.EndMove();

            LogInfo("停止运动:关闭所有DO  ");

            //UpdownUpMove();
            //CylinderMove(MoveInfo, IO_Type.TopCylinder_Up, IO_Type.TopCylinder_Down);
            //IOMove(IO_Type.StopDown1, IO_VALUE.LOW);
            //IOMove(IO_Type.StopDown2, IO_VALUE.LOW);
            if (UseAxis)
            {
                UpdownAxis.SuddenStop();
                CloseAxis(UpdownAxis);
            }
            CloseAllDO();
        }


        public override void StopRun()
        {
            if (mainTimer != null)
            {
                mainTimer.Enabled = false;
            }
            StopMove();
            //停止运行时,把所有IO 置零
            //CheckAndMove(IO_Type.StopDown1, IO_VALUE.LOW);
            //CheckAndMove(IO_Type.StopDown2, IO_VALUE.LOW);
            //CheckAndMove(IO_Type.TopCylinder_Up, IO_VALUE.LOW);
            //CheckAndMove(IO_Type.TopCylinder_Down, IO_VALUE.LOW);
            //CheckAndMove(IO_Type.BeforeAfterCylinder_Before, IO_VALUE.LOW);
            //CheckAndMove(IO_Type.BeforeAfterCylinder_After, IO_VALUE.LOW);
            //if (UseAxis.Equals(false))
            //{
            //    CheckAndMove(IO_Type.UpDownCylinder_Up, IO_VALUE.LOW);
            //    CheckAndMove(IO_Type.UpDownCylinder_Down, IO_VALUE.LOW);
            //}
            //CheckAndMove(IO_Type.ClampCylinder_Work, IO_VALUE.LOW);
            //CheckAndMove(IO_Type.ClampCylinder_Relax, IO_VALUE.LOW);
            runStatus = LineRunStatus.Wait;
        }
      
        protected override void BaseTimerProcess()
        {
            if (isInSuddenDown || isNoAirCheck)
            {
                return;
            }
            BusyMoveProcess();
            IOTimeOutProcess();
            //判断流水线打开了才可以运行 
            if (SecondMoveInfo.MoveType.Equals(LineMoveType.None))
            {
                if (LineManager.Line.CanProcessLine())
                { 
                    CheckFixture();
                }
            }
            if (UseAxis&& runStatus >= LineRunStatus.Runing)
            {
                CheckAxisAlarm(UpdownAxis);
            }
            OutStoreListPro();
        }
         
        private void OutStoreListPro()
        {
            //料仓出库完成,进仓装置开始出库检测
            if (IsDebug.Equals(false) && MoveInfo.MoveType.Equals(LineMoveType.None) && runStatus.Equals(LineRunStatus.Runing))
            {
                InOutParam waitOutParma = null;
                bool ok = waitOutStoreList.TryDequeue(out waitOutParma);

                if (ok)
                {
                    LogUtil.info(Name + "执行排队中的出库【" + waitOutParma.ToStr() + "】");
                    //出库
                    bool result = StartOutStoreMove(waitOutParma);
                    if (!result)
                    {
                        LogUtil.info(Name + " 执行出库【" + waitOutParma.ToStr() + "】失败,重新加入等待队列");
                        AddWaitOutInfo(waitOutParma);
                    }
                }
            }
        }
        public void AddWaitOutInfo(InOutParam param)
        {
            //判断是否已经加入,不重复加
            List<InOutParam> paramList = (from m in waitOutStoreList where m.PosId.Equals(param.PosId) select m).ToList<InOutParam>();
            if (paramList.Count > 0)
            {
                LogUtil.error(Name + "出库【" + param.ToStr() + "】加入等待出库队列失败,此库位已存在列表中");
            }
            else
            {
                waitOutStoreList.Enqueue(param);
            }
        }
       
        public bool CanBeforeAfter()
        {
            if (UseAxis)
            {
                int currPosition = UpdownAxis.GetAclPosition() - Config.UpDownAxisP1;
                if (Math.Abs(currPosition) < Math.Abs(Config.UpDown_Axis.CanErrorCountMax * 10))
                {
                    return true;
                }
            }
            else
            {
                if (IOValue(IO_Type.UpDownCylinder_Down).Equals(IO_VALUE.LOW) && IOValue(IO_Type.UpDownCylinder_Up).Equals(IO_VALUE.HIGH))
                {
                    return true;
                }
            }
            return false;
        }

        #region 上下气缸伺服运动

        private bool UpdownIsUp()
        {
            if (UseAxis)
            {
                return true;
            }
            else
            {
                return CylinderIsOk(IO_Type.UpDownCylinder_Down, IO_Type.UpDownCylinder_Up);
            }
        }

        private void UpdownUpMove()
        {
            if (UseAxis)
            {
                MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(1000));
                UpdownAxis.AbsMove(MoveInfo, Config.UpDownAxisP1, Config.UpdownAxis_P1Speed);
            }
            else
            {
                MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(300));
                CylinderMove(MoveInfo, IO_Type.UpDownCylinder_Down, IO_Type.UpDownCylinder_Up);
            }
        }

        private void UpdownDownP2Move(int trayHeight,int traySize)
        {
            if (UseAxis)
            {
                MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(1000));
                int position = Config.GetUpdownP2Detial(trayHeight,traySize);
                UpdownAxis.AbsMove(MoveInfo, position, Config.UpdownAxis_P2Speed);
            }
            else
            {
                MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(300));
                CylinderMove(MoveInfo, IO_Type.UpDownCylinder_Up, IO_Type.UpDownCylinder_Down);
            }
        }
        private void UpdownDownP3Move(int trayHeight)
        {
            if (UseAxis)
            {
                MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(1000));
                int position = Config.GetUpdownP3(trayHeight);
                UpdownAxis.AbsMove(MoveInfo, position, Config.UpdownAxis_P3Speed);
            }
            else
            {
                MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(300));
                CylinderMove(MoveInfo, IO_Type.UpDownCylinder_Up, IO_Type.UpDownCylinder_Down);
            }
        }
        private void UpdownHomeMove()
        {
            if (UseAxis)
            {
                MoveInfo.NextMoveStep(LineMoveStep.MH_01_UpDownHome);
                MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(1000));
                UpdownAxis.HomeMove(MoveInfo);
            }
            else
            {
                MoveInfo.NextMoveStep(LineMoveStep.MH_02_CylinderUp);
                MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(200));
                CylinderMove(MoveInfo, IO_Type.UpDownCylinder_Down, IO_Type.UpDownCylinder_Up);
            }
        }
         
        #endregion

    }
}