MoveEquip.cs 12.0 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 = (" " + "_进仓_" + DeviceID.ToString().PadLeft(2,'0') + " ").ToUpper();
            
            Init();
             
            MoveInfo = new LineMoveInfo(DeviceID, "进仓-" + DeviceID.ToString().PadLeft(2, '0') + "-MoveInfo");
            SecondMoveInfo = new LineMoveInfo(DeviceID, "进仓-" + DeviceID.ToString().PadLeft(2, '0') + "-SecondMoveInfo");
     
            UseAxis = config.UpDownUseAxis.Equals(1);
            if (UseAxis)
            {
                Config.UpDown_Axis.TargetSpeed = Config.UpdownAxis_TargetSpeed;
                Config.UpDown_Axis.AddSpeed = Config.UpdownAxis_AddSpeed;
                Config.UpDown_Axis.DelSpeed = Config.UpdownAxis_DelSpeed;
                Config.UpDown_Axis.HomeAddSpeed = Config.UpdownAxis_HomeAddSpeed;
                Config.UpDown_Axis.HomeHighSpeed = Config.UpdownAxis_HomeHighSpeed;
                Config.UpDown_Axis.HomeLowSpeed = Config.UpdownAxis_HomeLowSpeed;

                Config.UpDown_Axis.ServerOnDO = IO_Type.UpDownAxis_ServoOn;
                Config.UpDown_Axis.BreakOnDO = IO_Type.UpDownAxis_BreakOn;
            }
            UpdownAxis=new AxisBean (config.UpDown_Axis);
        }
         
        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("开始原点返回:  (上下气缸回原点,阻挡1气缸上升  )开始");
            MoveInfo.NewMove(LineMoveType.ReturnHome);
            StartReset();
            if ( isDebug)
            {
                mainTimer.Enabled = true;
            }
            return true;
        }
          
       
        public override bool Reset()
        { 
            StopMove();
            if (!RunAxis(true, UpdownAxis))
            {
                return false;
            } 
            LogInfo("开始重置:清空托盘号,(上下气缸回原点,阻挡1气缸上升  )开始;");
         
            runStatus = LineRunStatus.Reset;
            SecondMoveInfo.EndMove();
            MoveInfo.NewMove(LineMoveType.Reset); 
            StartReset();
            return true;
        }

        private void StartReset()
        {
            ResetClearData();
            lineStatus = LineStatus.ResetMove;
            //移载装置原点状态:顶升气缸下降端,前后气缸后退端,上下气缸上升端,夹料气缸放松端,阻挡气缸输入=0     
            UpdownHomeMove();
            //  CylinderMove(MoveInfo, IO_Type.UpDownCylinder_Down, IO_Type.UpDownCylinder_Up);
            if (IsDebug)
            {
                IOMove(IO_Type.StopCylinder_Down1, IO_VALUE.HIGH);
                IOMove(IO_Type.StopCylinder_Down2, IO_VALUE.HIGH);
            }
            else
            { 
                IOMove(IO_Type.StopCylinder_Down1, IO_VALUE.LOW);
                IOMove(IO_Type.StopCylinder_Down2, 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_UpDownHomeMove:
                        MoveInfo.NextMoveStep(LineMoveStep.MH_UpDownCylinder_Up);
                        LogInfo(MoveInfo.MoveType+ " :  (上下轴原点返回完成,上下轴走到待机点  )开始");
                        UpdownUpMove();
                        break;
                    case LineMoveStep.MH_UpDownCylinder_Up:

                        MoveInfo.NextMoveStep(LineMoveStep.MH_OtherCylinder_Back);
                        LogInfo(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_Slack, IO_Type.ClampCylinder_Tighten);
                        break;

                    case LineMoveStep.MH_OtherCylinder_Back:

                        LogInfo(MoveInfo.MoveType + " :完成!");
                        runStatus = LineRunStatus.Runing;
                        MoveInfo.EndMove();
                        SecondMoveInfo.EndMove();
                        if (IsDebug)
                        {
                            lineStatus = LineStatus.Debugging;
                        }
                        else
                        { 
                            lineStatus = LineStatus.StoreOnline;
                        }
                        break; 
                    default: break;
                }
            }
        }
  
        /// <summary>
        /// 停止运动
        /// </summary>
        internal override void StopMove()
        {
            //如果正在出库中,需要减去托盘号
            if (MoveInfo.MoveType.Equals(LineMoveType.OutStore))
            {
                LogInfo("停止运动时出库执行中,减去托盘数;");
                //减去需要的盘数
                TrayManager.DelNeedEmptyTrayNum();
                MoveInfo.EndMove();
            }
            runStatus = LineRunStatus.Busy;
            SecondMoveInfo.EndMove();

            LogInfo("停止运动:(上下气缸上升端,阻挡气缸输入=0  )开始 ");
             
            UpdownUpMove();
            CylinderMove(MoveInfo, IO_Type.TopCylinder_UP, IO_Type.TopCylinder_Down);
            IOMove(IO_Type.StopCylinder_Down1, IO_VALUE.LOW);
            IOMove(IO_Type.StopCylinder_Down2, IO_VALUE.LOW);

        }


        public override void StopRun()
        { 
            if (mainTimer != null)
            {
                mainTimer.Enabled = false;
            }
            StopMove();
            //停止运行时,把所有IO 置零
            IOMove(IO_Type.StopCylinder_Down1, IO_VALUE.LOW);
            IOMove(IO_Type.StopCylinder_Down2, IO_VALUE.LOW);
            IOMove(IO_Type.TopCylinder_UP, IO_VALUE.LOW);
            IOMove(IO_Type.TopCylinder_Down, IO_VALUE.LOW);
            IOMove(IO_Type.BeforeAfterCylinder_Before, IO_VALUE.LOW);
            IOMove(IO_Type.BeforeAfterCylinder_After, IO_VALUE.LOW);
            IOMove(IO_Type.UpDownCylinder_Up, IO_VALUE.LOW);
            IOMove(IO_Type.UpDownCylinder_Down, IO_VALUE.LOW);
            IOMove(IO_Type.ClampCylinder_Slack, IO_VALUE.LOW);
            IOMove(IO_Type.ClampCylinder_Tighten, IO_VALUE.LOW);
            runStatus = LineRunStatus.Wait;
        }


        public override void TimerProcess()
        {
            BusyMoveProcess();
            //判断流水线打开了才可以运行 
            if (SecondMoveInfo.MoveType.Equals(LineMoveType.None))
            {
                if (LineManager.Line.CanProcessLine())
                {
                    // LogUtil.info("StartCheckFixture");
                    StartCheckFixture();
                }
            }
            IOTimeOutProcess();
            OutStoreListPro();
        }

        /// <summary>
        /// 出库队列处理
        /// </summary>
        private void OutStoreListPro()
        {
            //料仓出库完成,进仓装置开始出库检测
            if (IsDebug.Equals(false) && MoveInfo.MoveType.Equals(LineMoveType.None) && runStatus.Equals(LineRunStatus.Runing))
            {
                InOutParam waitOutParma = null;
                lock (waitOutListLock)
                {
                    if (waitOutStoreList.Count > 0)
                    {
                        waitOutParma = waitOutStoreList[0];
                        waitOutStoreList.RemoveAt(0);
                    }
                }
                if (waitOutParma != null)
                {
                    LogUtil.info(Name + "开始 执行排队中的出库【" + waitOutParma.ToStr() + "】");
                    //出库
                    bool result = StartOutStoreMove(waitOutParma);
                    if (!result)
                    {
                        LogUtil.info(Name + " 执行排队中的出库【" + waitOutParma.ToStr() + "】失败,重新加入等待队列");
                        AddWaitOutInfo(waitOutParma);
                    }
                }
            }
        }
        public void AddWaitOutInfo(InOutParam param)
        {
            lock (waitOutListLock)
            {
                //判断是否已经加入,不重复加
                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.Add(param);
                }
            }
        }

        #region 上下气缸伺服运动

        private void UpdownUpMove()
        {
            if (UseAxis)
            {
            UpdownAxis.AbsMove(MoveInfo,   Config.UpDownAxisP1, Config.UpdownAxis_P1Speed);
            }
            else
            {
                CylinderMove(MoveInfo, IO_Type.UpDownCylinder_Down, IO_Type.UpDownCylinder_Up);
            }
        }

        private void UpdownDownMove(int trayHeight)
        {
            if (UseAxis)
            {
                int position = Config.GetUpdownP2(trayHeight);
                UpdownAxis.AbsMove(MoveInfo, position, Config.UpdownAxis_DownSpeed);
            }
            else
            {
                CylinderMove(MoveInfo, IO_Type.UpDownCylinder_Up, IO_Type.UpDownCylinder_Down);
            }
        }
        private void UpdownDownBoxMove(int trayHeight)
        {
            if (UseAxis)
            {
                int position = Config.GetUpdownP3(trayHeight);
                UpdownAxis.AbsMove(MoveInfo, position, Config.UpdownAxis_DownSpeed);
            }
            else
            {
                CylinderMove(MoveInfo, IO_Type.UpDownCylinder_Up, IO_Type.UpDownCylinder_Down);
            }
        }
        private void UpdownHomeMove()
        {
            if (UseAxis)
            {
                MoveInfo.NextMoveStep(LineMoveStep.MH_UpDownHomeMove);
                UpdownAxis.HomeMove(MoveInfo);
            }
            else
            {
                MoveInfo.NextMoveStep(LineMoveStep.MH_UpDownCylinder_Up);
                CylinderMove(MoveInfo, IO_Type.UpDownCylinder_Down, IO_Type.UpDownCylinder_Up);
            }
        }
        #endregion

    }
}