ProvidingEquip.cs 10.6 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 ProvidingEquip : EquipBase
    {

        public ProvidingEquip_Config Config;

        public ProvidingEquip(string cid, ProvidingEquip_Config config)
        {
            baseConfig = config;
            this.DeviceID = config.Id;
            this.Config = config;
            IsDebug = config.IsDebug.Equals(1);
            baseConfig = config;
            Name = (" " + "_出料_" + DeviceID % 100 + " ").ToUpper();
            Init(); 
            MoveInfo = new LineMoveInfo(DeviceID, "[" + Name.Trim() + "-Move]");
            SecondMoveInfo = new LineMoveInfo(DeviceID, "[" + Name.Trim() + "-SMove]");
            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,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.ReturnHome);
            StartReset(); 
            if ( isDebug)
            {
                mainTimer.Enabled = true;
            }
            return true ;
        }
         
        public override bool Reset()
        {
            StopMove();
            if (!RunAxis(true, UpdownAxis))
            {
                return false;
            } 
            LogInfo("开始重置:清理盘号,升降轴回原点,阻挡气缸上升 "); 
            runStatus = LineRunStatus.Reset;
            MoveInfo.NewMove(LineMoveType.Reset);
            StartReset(); 
            return true;
        }

       private void StartReset()
        {
            ResetClearData();
            lineStatus = LineStatus.ResetMove;
            MoveInfo.NextMoveStep(LineMoveStep.MH_CylinderUp);
            // CylinderMove(MoveInfo, IO_Type.UpDownCylinder_Down, IO_Type.UpDownCylinder_Up);
            UpdownHomeMove();
            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)
            {
                switch (MoveInfo.MoveStep)
                {
                    case LineMoveStep.MH_UpDownHome:
                        MoveInfo.NextMoveStep(LineMoveStep.MH_CylinderUp);
                        LogInfo(MoveInfo.MoveType + " :升降轴走到待机点");
                        UpdownUpMove();
                        MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(2000));
                        break;
                    case LineMoveStep.MH_CylinderUp:
                        MoveInfo.NextMoveStep(LineMoveStep.MH_OtherCylinderBack);
                        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_OtherCylinderBack:
                        MoveInfo.EndMove();
                        SecondMoveInfo.EndMove();
                        LogInfo(MoveInfo.MoveType + " 完成!");
                        runStatus = LineRunStatus.Runing;
                        if (IsDebug)
                        {
                            lineStatus = LineStatus.Debugging;
                        }
                        else
                        {
                            lineStatus = LineStatus.StoreOnline;
                        }
                        break;

                    default: break;
                }
            }
        }

        internal override void StopMove()
        {
            trayCheck2LowWait.Stop();
            trayCheckWait.Stop(); 
            MoveInfo.EndMove();
            SecondMoveInfo.EndMove();
            runStatus = LineRunStatus.Runing;

            LogInfo("停止运动: 上下气缸上升 ,阻挡气缸上升,顶升气缸下降    ");
             
            if (UseAxis)
            {
                UpdownAxis.SuddenStop();
            }
            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();
            CloseAxis(UpdownAxis);
            //停止运行时,把所有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);
            if (UseAxis.Equals(false))
            {
                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;
        }

        protected override void BaseTimerProcess()
        {
            if (isInSuddenDown || isNoAirCheck)
            {
                return;
            }
            BusyMoveProcess();
            IOTimeOutProcess();
            //判断流水线打开了才可以运行 
            if (SecondMoveInfo.MoveType.Equals(LineMoveType.None))
            {
                LineBean lineBean = LineManager.Line;
                if (LineManager.Line.CanProcessLine())
                {
                    StartCheckFixture();
                }
            }
        } 
        public override bool  StartInStoreMove(InOutParam param)
        {
            return false;
        } 
        protected override void InStoreProcess()
        { 
        }
        #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 UpdownDownP2Move(int trayHeight,int traySize)
        {
            if (UseAxis)
            {
                int position = Config.GetUpdownP2Detial(trayHeight,traySize);
                UpdownAxis.AbsMove(MoveInfo, position, Config.UpdownAxis_P2Speed);
            }
            else
            {
                CylinderMove(MoveInfo, IO_Type.UpDownCylinder_Up, IO_Type.UpDownCylinder_Down);
            }
        }
        private void UpdownDownP3Move(int trayHeight)
        {
            if (UseAxis)
            {
                int position = Config.GetUpdownP3(trayHeight);
                UpdownAxis.AbsMove(MoveInfo, position, Config.UpdownAxis_P3Speed);
            }
            else
            {
                CylinderMove(MoveInfo, IO_Type.UpDownCylinder_Up, IO_Type.UpDownCylinder_Down);
            }
        }
        private void UpdownHomeMove()
        {
            if (UseAxis)
            {
                MoveInfo.NextMoveStep(LineMoveStep.MH_UpDownHome);
                MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(2000));
                UpdownAxis.HomeMove(MoveInfo);
            }
            else
            {
                MoveInfo.NextMoveStep(LineMoveStep.MH_CylinderUp);
                MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(1000));
                CylinderMove(MoveInfo, IO_Type.UpDownCylinder_Down, IO_Type.UpDownCylinder_Up);
            }
        }
        #endregion

        /// <summary>
        /// 判断上料横移机构是否可以横移运动
        /// </summary>
        /// <returns></returns>
        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;
        }
    }
}