MainMachine.cs 7.7 KB
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DeviceLibrary
{
    public partial class MainMachine : IRobot
    {
        public string Name { get; set; } = "移载";
        public bool canRunning { get; set; } = false;
        public bool isBusy { get; set; } = false;
        public bool isAlarm { get; set; } = false;
        public RunStatus runStatus { get; set; } = RunStatus.Stop;
        public Robot_Config config { get; set; }
        public MoveInfo MoveInfo;


        public AxisBean Take_Middle_Axis;
        public AxisBean Take_UpDown_Axis;
        public AxisBean Left_Batch_Axis;
        public AxisBean Right_Batch_Axis;
        public AxisBean Label_X_Axis;
        public AxisBean Label_Y_Axis;
        public AxisBean Label_Z_Axis;
        public AxisBean Label_R_Axis;

        /// <summary>
        /// 开始运行的时间
        /// </summary>
        public DateTime StartTime { get; set; }

        /// <summary>
        /// 是否在急停中
        /// </summary>
        public bool isInSuddenDown = false;



        public MainMachine(Robot_Config _config) {
            config = _config;

            Take_Middle_Axis = new AxisBean(config.Take_Middle_Axis, Name);
            Take_UpDown_Axis = new AxisBean(config.Take_UpDown_Axis, Name);
            Left_Batch_Axis = new AxisBean(config.Left_Batch_Axis, Name);
            Right_Batch_Axis = new AxisBean(config.Right_Batch_Axis, Name);
            Label_X_Axis = new AxisBean(config.Label_X_Axis, Name);
            Label_Y_Axis = new AxisBean(config.Label_Y_Axis, Name);
            Label_Z_Axis = new AxisBean(config.Label_Z_Axis, Name);
            Label_R_Axis = new AxisBean(config.Label_R_Axis, Name);
        }

        public void Run() {
            while (true) {
                canRunning = DeviceCheck();
                if (canRunning)
                    canRunning = SafeCheck();
                
                Thread.Sleep(50);
                if (!canRunning)
                    continue;
                if (runStatus == RunStatus.Running)
                {
                    WorkProcess();
                }
                else if (runStatus == RunStatus.HomeReset)
                {
                    HomeReset();
                }                
            }
        }
        public void BeginHomeReset() {
            StopMove();
            Thread.Sleep(500);
            OpenAllServo();
            alarmType = AlarmType.None;
            runStatus = RunStatus.HomeReset;
            MoveInfo.NextMoveStep(MoveStep.H01_HomeReset);
            LogInfo(MoveInfo.MoveType + ":  开始回原 ");
            MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(1000));
        }
        void HomeReset()
        {
            if (CheckWait())
                return;

            switch (MoveInfo.MoveStep)
            {
                case MoveStep.H01_HomeReset:
                    MoveInfo.NextMoveStep(MoveStep.H02_HomeReset);
                    //MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(1000));
                    LogInfo(MoveInfo.MoveType + ":正在回原");
                    Take_Middle_Axis.HomeMove(MoveInfo);
                    Take_UpDown_Axis.HomeMove(MoveInfo);
                    Left_Batch_Axis.HomeMove(MoveInfo);
                    Right_Batch_Axis.HomeMove(MoveInfo);
                    break;
                case MoveStep.H02_HomeReset:
                    MoveInfo.NextMoveStep(MoveStep.H03_HomeReset);
                    LogInfo(MoveInfo.moveStep + ":正在回原");
                    Label_Z_Axis.HomeMove(MoveInfo);
                    Take_Middle_Axis.AbsMove(MoveInfo, config.Take_Middle_P1, config.Take_Middle_P1_speed);
                    Take_UpDown_Axis.AbsMove(MoveInfo, config.Take_UpDown_P1, config.Take_UpDown_P1_speed);
                    Right_Batch_Axis.AbsMove(MoveInfo, config.Right_Batch_P1, config.Right_Batch_P1_speed);
                    Left_Batch_Axis.AbsMove(MoveInfo, config.Left_Batch_P1, config.Left_Batch_P1_speed);
                    break;
                case MoveStep.H03_HomeReset:
                    MoveInfo.NextMoveStep(MoveStep.H04_HomeReset);
                    LogInfo(MoveInfo.moveStep + ":正在回原");
                    Label_Z_Axis.HomeMove(MoveInfo);
                    Label_X_Axis.HomeMove(MoveInfo);
                    Label_Y_Axis.HomeMove(MoveInfo);
                    Label_R_Axis.HomeMove(MoveInfo);
                    break;
                case MoveStep.H04_HomeReset:
                    MoveInfo.NextMoveStep(MoveStep.H05_HomeReset);
                    LogInfo(MoveInfo.moveStep + ":正在回原");
                    Label_Z_Axis.AbsMove(MoveInfo, config.Label_Z_P1, config.Label_Z_P1_speed);
                    Label_X_Axis.AbsMove(MoveInfo, config.Label_X_P1, config.Label_X_P1_speed);
                    Label_Y_Axis.AbsMove(MoveInfo, config.Label_Y_P1, config.Label_Y_P1_speed);
                    Label_R_Axis.AbsMove(MoveInfo, config.Label_R_P1, config.Label_R_P1_speed);
                    break;
                case MoveStep.H05_HomeReset:
                    MoveInfo.NextMoveStep(MoveStep.HEND_HomeReset);
                    LogInfo(MoveInfo.moveStep + ":正在回原 阻挡气缸判断");
                    if (IOManager.IOValue(IO_Type.LeftEnd_Check) == IO_VALUE.LOW)
                    {
                        CylinderMove(MoveInfo, IO_Type.LeftStopUP, IO_Type.LeftStopDown);
                    }
                    if (IOManager.IOValue(IO_Type.RightEnd_Check) == IO_VALUE.LOW)
                    {
                        CylinderMove(MoveInfo, IO_Type.RightStopUP, IO_Type.RightStopDown);
                    }
                    break;
                case MoveStep.HEND_HomeReset:
                    LogInfo(MoveInfo.moveStep + ": 完成");
                    MoveEndProcess();
                    break;
            }
        }

        bool SafeCheck() {
            List<string> msglist = new List<string>();
            if (IOValue(IO_Type.GratingSignal_Check).Equals(IO_VALUE.LOW))
            {
                msglist.Add("安全光栅被遮挡");
            }
            if (IOValue(IO_Type.HasNgBox).Equals(IO_VALUE.LOW))
            {
                msglist.Add("没有检测到NG料箱");
            }
            if (IOValue(IO_Type.HasPrinter).Equals(IO_VALUE.LOW))
            {
                msglist.Add("没有检测到NG料箱");
            }
            if (IOValue(IO_Type.LeftBackDoor_Check).Equals(IO_VALUE.LOW))
            {
                msglist.Add("左后门没有关闭");
            }
            if (IOValue(IO_Type.RightBackDoor_Check).Equals(IO_VALUE.LOW))
            {
                msglist.Add("右后门没有关闭");
            }
            return (msglist.Count == 0);
        }
        /// <summary>
        /// 最后一次气压检测变为0的时间
        /// </summary>
        DateTime lastAirCloseTime = DateTime.MinValue;
        bool DeviceCheck() {
            List<string> msglist = new List<string>();
            if (IOValue(IO_Type.SuddenStop_BTN).Equals(IO_VALUE.LOW))
            {
                msglist.Add("急停中");
            }
            if (IOValue(IO_Type.Airpressure_Check).Equals(IO_VALUE.LOW))
            {
                if (lastAirCloseTime == DateTime.MinValue)
                    lastAirCloseTime = DateTime.Now;

                TimeSpan span = DateTime.Now - lastAirCloseTime;
                if (span.TotalSeconds > RobotManage.Config.AirCheckSeconds)
                {
                    msglist.Add("急停中");
                }
            }
            else {
                lastAirCloseTime = DateTime.MinValue;
            }
            return (msglist.Count == 0);
        }

    }
}