InOutDevice.cs 11.2 KB
using CodeLibrary;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeviceLibrary
{
    public partial class InOutDevice
    {
        public static Dictionary<InOutSideE,InOutDevice> InOutDeviceList = new Dictionary<InOutSideE, InOutDevice>();


        #region io定义
        string DO_Agv_Rsp = IO_Type.Right_Agv_Rsp;
        string DO_Lift_Break = IO_Type.Right_Lift_Break;
        string DO_Moto_Run = IO_Type.Right_Moto_Run;
        string DO_Moto_Rev = IO_Type.Right_Moto_Rev;
        string DO_F_Stop_Up = IO_Type.Right_F_Stop_Up;
        string DO_F_Stop_Down = IO_Type.Right_F_Stop_Down;
        string DO_M_Stop_Up = IO_Type.Right_M_Stop_Up;
        string DO_M_Stop_Down = IO_Type.Right_M_Stop_Down;
        string DO_Door_Up = IO_Type.Right_Door_Up;
        string DO_Door_Down = IO_Type.Right_Door_Down;

        string IO_Entry_SafetyGrating = IO_Type.Right_Entry_SafetyGrating;
        string IO_Side_SafetyGrating = IO_Type.Right_Side_SafetyGrating;
        string IO_OverHead_Check = IO_Type.Right_OverHead_Check;
        string IO_AgvIn_Req = IO_Type.Right_AgvIn_Req;
        string IO_AgvOut_Req = IO_Type.Right_AgvOut_Req;
        string IO_F_Stop_In = IO_Type.Right_F_Stop_In;
        string IO_F_Stop_Out = IO_Type.Right_F_Stop_Out;
        string IO_F_Stop_Up = IO_Type.Right_F_Stop_Up;
        string IO_F_Stop_Down = IO_Type.Right_F_Stop_Down;
        string IO_M_Stop_In = IO_Type.Right_M_Stop_In;
        string IO_M_Stop_Out = IO_Type.Right_M_Stop_Out;
        string IO_M_Stop_Up = IO_Type.Right_M_Stop_Up;
        string IO_M_Stop_Down = IO_Type.Right_M_Stop_Down;
        string IO_EndMaterialTop_Check = IO_Type.Right_EndMaterialTop_Check;
        string IO_EndMaterialBottom_Check = IO_Type.Right_EndMaterialBottom_Check;
        string IO_Lift_Up = IO_Type.Right_Lift_Up;
        string IO_Lift_Down = IO_Type.Right_Lift_Down;
        string IO_Door_Up = IO_Type.Right_Door_Up;
        string IO_Door_Down = IO_Type.Right_Door_Down;
        #endregion

        public InOutSideE InOutSide;
        public InOutModeE InOutMode;
        public InOutDirectionE CurrnetDirection = InOutDirectionE.NONE;
        public MoveInfo MoveInfo;
        public LiftMonitor Lift;
        public LineRunMonitor Line;
        Robot_Config Config;
        MainMachine mainMachine;
        public string Name;
        public DoorInfo DoorInfo=new DoorInfo(doorIndexE.LeftIn);
        public InOutDevice(InOutSideE inOutSide, InOutModeE inOutMode, Robot_Config _Config, MainMachine _mainMachine)
        {
            AxisBean Lift_Moto;
            Name = "右侧出入口";

            InOutSide = inOutSide;
            InOutMode = inOutMode;
            Config = _Config;
            mainMachine = _mainMachine;
            if (InOutSide == InOutSideE.Left)
            {
                Lift_Moto = new AxisBean(Config.Left_Lift_Moto, Name);
                Name = "左侧出入口";
                #region io定义
                DO_Agv_Rsp = IO_Type.Left_Agv_Rsp;
                DO_Lift_Break = IO_Type.Left_Lift_Break;
                DO_Moto_Run = IO_Type.Left_Moto_Run;
                DO_Moto_Rev = IO_Type.Left_Moto_Rev;
                DO_F_Stop_Up = IO_Type.Left_F_Stop_Up;
                DO_F_Stop_Down = IO_Type.Left_F_Stop_Down;
                DO_M_Stop_Up = IO_Type.Left_M_Stop_Up;
                DO_M_Stop_Down = IO_Type.Left_M_Stop_Down;
                DO_Door_Up = IO_Type.Left_Door_Up;
                DO_Door_Down = IO_Type.Left_Door_Down;

                IO_Entry_SafetyGrating = IO_Type.Left_Entry_SafetyGrating;
                IO_Side_SafetyGrating = IO_Type.Left_Side_SafetyGrating;
                IO_OverHead_Check = IO_Type.Left_OverHead_Check;
                IO_AgvIn_Req = IO_Type.Left_AgvIn_Req;
                IO_AgvOut_Req = IO_Type.Left_AgvOut_Req;
                IO_F_Stop_In = IO_Type.Left_F_Stop_In;
                IO_F_Stop_Out = IO_Type.Left_F_Stop_Out;
                IO_F_Stop_Up = IO_Type.Left_F_Stop_Up;
                IO_F_Stop_Down = IO_Type.Left_F_Stop_Down;
                IO_M_Stop_In = IO_Type.Left_M_Stop_In;
                IO_M_Stop_Out = IO_Type.Left_M_Stop_Out;
                IO_M_Stop_Up = IO_Type.Left_M_Stop_Up;
                IO_M_Stop_Down = IO_Type.Left_M_Stop_Down;
                IO_EndMaterialTop_Check = IO_Type.Left_EndMaterialTop_Check;
                IO_EndMaterialBottom_Check = IO_Type.Left_EndMaterialBottom_Check;
                IO_Lift_Up = IO_Type.Left_Lift_Up;
                IO_Lift_Down = IO_Type.Left_Lift_Down;
                IO_Door_Up = IO_Type.Left_Door_Up;
                IO_Door_Down = IO_Type.Left_Door_Down;
                #endregion
            }
            else
            {
                Lift_Moto = new AxisBean(Config.Right_Lift_Moto, Name);
            }
            MoveInfo = new MoveInfo(Name);
            MoveInfo.SetStateDelegate(State);

            

            Lift = new LiftMonitor(IO_Lift_Up, IO_Lift_Down, DO_Lift_Break, Lift_Moto, Config.InLift_Moto_Speed, Config.InLift_Moto_DownSpeed);
            Line = new LineRunMonitor(Name, Config.DOList[DO_Moto_Run].GetIOAddr(), Config.DOList[DO_Moto_Rev].GetIOAddr());
            
            InOutDeviceList.Add(inOutSide,this);
        }
        bool SafeCheck()
        {
            bool ok = true;
            if (IOValue(IO_Entry_SafetyGrating).Equals(IO_VALUE.LOW))
            {
                if (!mainMachine.IgnoreSafecheck && !mainMachine.IgnoreGratingSignal
                    && IOValue(IO_AgvIn_Req).Equals(IO_VALUE.LOW) && IOValue(IO_AgvOut_Req).Equals(IO_VALUE.LOW))
                {
                    ok = false;
                    Line.Pause();
                    //Lift.Pause();
                }
                Msg.add(Name + "正面安全光栅被遮挡" + (ok ? "[已忽略]" : ""), MsgLevel.warning);
            }

            if (IOValue(IO_Side_SafetyGrating).Equals(IO_VALUE.LOW))
            {
                if (!mainMachine.IgnoreSafecheck && !mainMachine.IgnoreGratingSignal)
                {
                    ok = false;
                    Line.Pause();
                }
                Msg.add(Name + "侧面光栅被遮挡" + (ok ? "[已忽略]" : ""), MsgLevel.warning);
            }

            if (ok)
            {
                Line.Resume();
                //Lift.Resume();
            }
            return ok;
        }
        public void Progress()
        {
            if (!SafeCheck())
                return;



            if (MoveInfo.MoveStep == MoveStep.Wait)
            {
                if (CanIn(out string msg))
                {

                    if (IOValue(IO_AgvIn_Req).Equals(IO_VALUE.HIGH) || (IOValue(IO_F_Stop_In).Equals(IO_VALUE.HIGH) && IOValue(IO_Entry_SafetyGrating).Equals(IO_VALUE.LOW)))
                    {
                        MoveInfo.NewMove(MoveStep.AgvIn01);
                        MoveInfo.log("检测到入口周转箱信号,开始入库流程");
                        CurrnetDirection = InOutDirectionE.IN;
                        return;
                    }
                }
                else if (IOValue(IO_AgvIn_Req).Equals(IO_VALUE.HIGH)){
                    Msg.add(Name + msg, MsgLevel.warning);
                }
                
                CurrnetDirection = InOutDirectionE.NONE;
                return;
            }

            if (CurrnetDirection == InOutDirectionE.IN)
                InProcess();
            else if (CurrnetDirection == InOutDirectionE.OUT)
                OutProcess();

            if (CurrnetDirection == InOutDirectionE.IN)
            {
                if (InOutSide == InOutSideE.Left)
                    DoorInfo.doorIndex = doorIndexE.LeftIn;
                else if (InOutSide == InOutSideE.Right)
                    DoorInfo.doorIndex = doorIndexE.RightIn;
                DoorInfo.hSerial = MoveInfo.MoveParam.hSerial;
                DoorInfo.status = doorStatusE.inStore;
                DoorInfo.hasContainer = false;
            }
            else if (CurrnetDirection == InOutDirectionE.OUT)
            {
                if (InOutSide == InOutSideE.Left)
                    DoorInfo.doorIndex = doorIndexE.LeftOut;
                else if (InOutSide == InOutSideE.Right)
                    DoorInfo.doorIndex = doorIndexE.RightOut;

                DoorInfo.hSerial = MoveInfo.MoveParam.hSerial;
                DoorInfo.status = doorStatusE.outStore;
                DoorInfo.hasContainer = true;

                if (MoveInfo.MoveStep == MoveStep.OutWaitAgv)
                {
                    DoorInfo.status = doorStatusE.fullBoxNeedLeave;
                }
            }
            else
            {
                if (CanIn(out string msg))
                {
                    if (InOutSide == InOutSideE.Left)
                        DoorInfo.doorIndex = doorIndexE.LeftIn;
                    else if (InOutSide == InOutSideE.Right)
                        DoorInfo.doorIndex = doorIndexE.RightIn;

                    DoorInfo.status = doorStatusE.needBox;
                    DoorInfo.hasContainer = false;
                }
            }

            
        }
        public bool CanIn(out string msg)
        {
            //if (CurrnetDirection == InOutDirectionE.IN && MoveInfo.MoveStep>= MoveStep.In09) {
            //    return true;
            //}
            msg = "";
            if (MoveInfo.MoveStep != MoveStep.Wait)
            {
                msg = Name+"忙碌中";
                return false;
            }

            if (InOutMode == InOutModeE.OnlyIN || InOutMode == InOutModeE.Both)
            {
                return true;
            }
            msg = Name+"设定不允许入库";
            return false;
        }
        
        public bool CanOut()
        {
            if (MoveInfo.MoveStep != MoveStep.Wait)
                return false;
            if (InOutMode == InOutModeE.OnlyOUT || InOutMode == InOutModeE.Both)
            {
                //CurrnetDirection = InOutDirectionE.OUT;
                //MoveInfo.NewMove(MoveStep.Out01);
                return true;
            }

            return false;
        }
        string State()
        {
            string state = "";
            if (MoveInfo.MoveStep >= MoveStep.In02)
            {
                return MoveInfo.MoveParam.ToStr();
            }
            else if (MoveInfo.MoveStep >= MoveStep.Wait)
            {
                state = "空闲中";
            }
            else if (MoveInfo.MoveStep < MoveStep.In02)
            {
                state = "入库扫描";
            }

            return state;
        }
        public void FrontStop(MoveInfo moveInfo, IO_VALUE iO_VALUE)
        {
            MainMachine.CylinderMove(moveInfo, DO_F_Stop_Down, DO_F_Stop_Up, iO_VALUE);
        }
        public void MiddleStop(MoveInfo moveInfo, IO_VALUE iO_VALUE)
        {
            MainMachine.CylinderMove(moveInfo, DO_M_Stop_Down, DO_M_Stop_Up, iO_VALUE);
        }
        public IO_VALUE IOValue(string ioType)
        {
            return MainMachine.IOValue(ioType);
        }
        public void IOMove(string IoType, IO_VALUE value, bool isCheck = false, int msTime = 0)
        {
            MainMachine.IOMove(IoType, value, isCheck, msTime);
        }
       
    }


}