T1Machine.cs 14.3 KB
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DeviceLibrary
{
    using crc = OnlineStore.CodeResourceControl;
    public partial class T1Machine : MachineBase, IRobot
    {
        private Config_T1 _config;
        public new Config_T1 Config
        {
            get => _config; set
            {
                base.Config = value;
                _config = value;
            }
        }
        public override string DeviceName { get; } = "出料机构";
        public bool canRunning { get; set; }
        public bool isBusy { get; set; }
        public bool isAlarm { get; set; }
        //public RunStatus runStatus { get; set; }
        //public bool UserPause { get; set; }
        public bool IgnoreSafecheck { get; set; }
        public bool IgnoreGratingSignal { get; set; }
        public AxisBean T_Batch_Axis;
        public AxisBean T_Pan_Axis;
        public AxisBean T_Updown_Axis;
        AxisBean T_TrayPos_Axis;
        public AxisBean T_Y_Axis;
        MoveInfo ShelfOutMoveInfo;
        MoveInfo ShelfInMoveInfo;
        public LineRunMonitor ShelfOutLine;
        public LineRunMonitor ShelfInLine;

        public event EventHandler<Bitmap> TrayStringLocation;

        public Point CenterPos = Point.Empty;
        public bool Init(out string msg)
        {
            msg = "";
            try
            {
                string configFile = "config\\T1Config.csv";
                Config = new Config_T1(0, "", configFile);
                Config = (Config_T1)CSVConfigReader.LoadConfig<IO_T1_Type>(Config);

                #region 初始化伺服轴
                T_Batch_Axis = new AxisBean(Config.T_Batch_Axis, DeviceName);
                T_Pan_Axis = new AxisBean(Config.T_Pan_Axis, DeviceName);
                T_Updown_Axis = new AxisBean(Config.T_Updown_Axis, DeviceName);
                T_TrayPos_Axis = new AxisBean(Config.T_TrayPos_Axis, DeviceName);
                T_Y_Axis = new AxisBean(Config.T_Y_Axis, DeviceName);
                #endregion

                MoveInfo = new MoveInfo(DeviceName);
                ResetMoveInfo = MoveInfo;
                ShelfOutMoveInfo = new MoveInfo("出料串线体");
                ShelfInMoveInfo = new MoveInfo("入料串线体");

                ShelfOutLine = new LineRunMonitor("ShelfOutLine", Config.DOList[IO_T1_Type.Line_Out_Run].GetIOAddr());
                ShelfInLine = new LineRunMonitor("ShelfInLine", Config.DOList[IO_T1_Type.Empty_Line_Run].GetIOAddr());

                RobotManage.wistonAgvClient.EmptyShelfInReady += WistonAgvClient_EmptyShelfInReady;
                RobotManage.wistonAgvClient.FullShelfOutReady += WistonAgvClient_FullShelfOutReady;
                CenterPos = new Point(Config.String_Center_X, Config.String_Center_Y);
                LastStringCenter = CenterPos;
                LedProcessInit();
                IOMonitor.RegisterIO(IO_T1_Type.Empty_LineIn_Check, Config, IO_VALUE.HIGH, delegate () { EmptyStringIN(); });
                IOMonitor.RegisterIO(IO_T1_Type.EmptyString_In_Check, Config, IO_VALUE.LOW, delegate () {
                    //if (IOValue(IO_T1_Type.Empty_LineIn_Check).Equals(IO_VALUE.LOW))
                    {
                        //RobotManage.wistonAgvClient.NeedEnter();
                    }
                });
                IOMonitor.RegisterIO(IO_T1_Type.T1_Out_Check, Config, IO_VALUE.HIGH, delegate () {
                    //if (IOValue(IO_T1_Type.Empty_LineIn_Check).Equals(IO_VALUE.LOW))
                    {
                        RobotManage.wistonAgvClient.NeedLeave();
                    }
                });

            }
            catch (Exception e)
            {
                msg = e.Message;
                return false;
            }
            return true;
        }

        private void WistonAgvClient_FullShelfOutReady(object sender, EventArgs e)
        {
            this.loginfo("收到满料串Agv抵达信号");
            ShelfOutLine.LineRun("FullStringOut", 40);
            Thread.Sleep(1000);
            IOMove(IO_T1_Type.T1_Out_Stop, IO_VALUE.HIGH, false, 2000);
            WaitIo(IO_T1_Type.T1_Out_Check, IO_VALUE.LOW, 30 * 1000);
            ShelfOutLine.LineRun("FullStringOut", 15);
            RobotManage.wistonAgvClient.FinishLeave();
        }

        private void WistonAgvClient_EmptyShelfInReady(object sender, EventArgs e)
        {
            this.loginfo("收到空料串Agv抵达信号");
            ShelfInLine.LineRun("EmptyStringIN_agv", 40);
            WaitIo(IO_T1_Type.Empty_LineIn_Check, IO_VALUE.HIGH, 30 * 1000);
            ShelfInLine.LineRun("EmptyStringIN_agv", 15);
            RobotManage.wistonAgvClient.FinishEnter();
        }

        protected override void LoopProcess()
        {
            mstart = true;
            while (mstart)
            {
                try
                {
                    canRunning = DeviceCheck();
                    if (canRunning)
                    {
                        //BtnProcess();
                        canRunning = SafeCheck();
                    }
                    Thread.Sleep(stepDelaytime);
                    if (!canRunning || !mstart)
                        continue;
                    if (runStatus == RunStatus.Running)
                    {
                        IOMonitorFUNC();
                        WorkProcess();
                        ShelfOutProcess();
                        ShelfInProcess();

                    }
                    else if (runStatus == RunStatus.HomeReset)
                    {
                        HomeReset();
                    }
                }
                catch (Exception ex)
                {
                    Msg.add(ex.Message, MsgLevel.warning);
                    LogUtil.error(DeviceName + " " + ex.ToString());
                }
                finally
                {
                    ProcessMsgEventFire(Msg);
                    //ProcessMoveinfoEventFire(MoveInfo.List);
                    Msg.Clear();
                }
            }
            LogUtil.info($"{DeviceName} 主线程已退出.");
        }
        public bool DeviceCheck()
        {
            bool ok = true;
            if (IOValue(IO_T1_Type.SuddenStop_BTN).Equals(IO_VALUE.LOW))
            {
                Alarm(AlarmType.SuddenStop);
                Msg.add(crc.GetString("emergency_stop", "急停中"), MsgLevel.warning);
                Thread.Sleep(1000);
                ok = false;
            }
            else if (alarmType == AlarmType.SuddenStop)
            {
                Msg.add(crc.GetString("system_need_reset", "系统需要重置"), MsgLevel.info);
                Thread.Sleep(1000);
                ok = false;
            }

            if (alarmType != AlarmType.SuddenStop)
            {
                TimeSpan span = DateTime.Now - checkAlarmTime;
                //在回原点,复位,出入库时,检测报警间隔减小
                if ((!runStatus.Equals(RunStatus.Stop) && span.TotalSeconds > 3) || span.TotalSeconds > 1)
                {
                    foreach (ConfigMoveAxis configMoveAxis in Config.moveAxisList)
                    {
                        if (AxisManager.GetAlarmStatus(configMoveAxis.DeviceName, configMoveAxis.GetAxisValue()) == 1)
                        {
                            Msg.add(crc.GetString("axis_run_alert", "{0}:运动报警", configMoveAxis.Explain), MsgLevel.warning);
                            ok = false;
                            LogUtil.error(string.Join(",", HuichuanLibrary.HCBoardManager.GetAxisErrorDetail(configMoveAxis.GetAxisValue())));
                        }
                    }

                    //if (RobotManage.electricGripper.ErrorCode > 0)
                    //{
                    //    Msg.add($"电夹爪出错 ErrorCode:{RobotManage.electricGripper.ErrorCode}", MsgLevel.warning);
                    //    ok = false;
                    //}
                }
            }
            return ok;
        }
        public void BeginHomeReset(bool firstRun = false)
        {
            if (!firstRun)
            {
                StopMove();
                Thread.Sleep(500);
            }
            OpenAllServo();
            alarmType = AlarmType.None;
            runStatus = RunStatus.HomeReset;
            ResetMoveInfo.NewMove(MoveStep.H01_HomeReset);
            ResetMoveInfo.log("开始回原");
            ResetMoveInfo.WaitList.Add(WaitResultInfo.WaitTime(1000));
        }
        private void HomeReset()
        {
            if (CheckWait(ResetMoveInfo))
                return;

            switch (ResetMoveInfo.MoveStep)
            {
                case MoveStep.H01_HomeReset:
                    ResetMoveInfo.NextMoveStep(MoveStep.H02_HomeReset);
                    ResetMoveInfo.log("正在回原, 皮带线阻挡上升,贴标机构阻挡上升");
                    CylinderMove(MoveInfo, IO_T1_Type.End_Lift_Cylinder_Down, IO_T1_Type.End_Lift_Cylinder_Up, IO_VALUE.LOW);
                    CylinderMove(MoveInfo, IO_T1_Type.EmptyString_Lift_Down, IO_T1_Type.EmptyString_Lift_Up, IO_VALUE.LOW);
                    CylinderMove(MoveInfo, IO_T1_Type.T1_Lift_Down, IO_T1_Type.T1_Lift_Up, IO_VALUE.HIGH);
                    RobotManage.electricGripper.HomeReset();
                    //CylinderMove(ResetMoveInfo, IO_Label_Type.TrayStop_Down, IO_Label_Type.TrayStop_Up);
                    //CylinderMove(ResetMoveInfo, IO_Label_Type.Label_Stop_Down, IO_Label_Type.Label_Stop_Up);
                    break;
                case MoveStep.H02_HomeReset:
                    ResetMoveInfo.NextMoveStep(MoveStep.H03_HomeReset);
                    ResetMoveInfo.log("正在回原 所有线体运转");
                    IOMove(IO_T1_Type.Line4_Run, IO_VALUE.HIGH);
                    IOMove(IO_T1_Type.Empty_Line_Run, IO_VALUE.HIGH);
                    IOMove(IO_T1_Type.Line_Out_Run, IO_VALUE.HIGH);
                    break;
                case MoveStep.H03_HomeReset:
                    ResetMoveInfo.NextMoveStep(MoveStep.H04_HomeReset);
                    ResetMoveInfo.log("正在回原 所有轴回原点");
                    T_Batch_Axis.HomeMove(ResetMoveInfo);
                    T_Pan_Axis.HomeMove(ResetMoveInfo);
                    T_Updown_Axis.HomeMove(ResetMoveInfo);
                    T_TrayPos_Axis.HomeMove(ResetMoveInfo);
                    T_Y_Axis.HomeMove(ResetMoveInfo);
                    break;
                case MoveStep.H04_HomeReset:
                    ResetMoveInfo.NextMoveStep(MoveStep.H05_HomeReset);
                    ResetMoveInfo.log("正在回原 所有线体停止");
                    IOMove(IO_T1_Type.Line4_Run, IO_VALUE.LOW);
                    IOMove(IO_T1_Type.Empty_Line_Run, IO_VALUE.LOW);
                    IOMove(IO_T1_Type.Line_Out_Run, IO_VALUE.LOW);
                    MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(1000));
                    break;
                case MoveStep.H05_HomeReset:
                    ResetMoveInfo.NextMoveStep(MoveStep.H06_HomeReset);
                    ResetMoveInfo.log("正在回原 所有轴到待机点");
                    T_Batch_Axis.AbsMove(ResetMoveInfo, Config.BatchAxis_P1, Config.BatchAxis_P1_speed);
                    T_Pan_Axis.AbsMove(ResetMoveInfo, Config.Pan_P1, Config.Pan_P1_speed);
                    T_Updown_Axis.AbsMove(ResetMoveInfo, Config.UpdownAxis_P1, Config.UpdownAxis_P1_speed);
                    T_TrayPos_Axis.AbsMove(ResetMoveInfo, Config.TrayPos_P1, Config.TrayPos_P1_speed);
                    T_Y_Axis.AbsMove(ResetMoveInfo, Config.Y_P1, Config.Y_P1_speed);
                    break;
                case MoveStep.H06_HomeReset:
                    ResetMoveInfo.NextMoveStep(MoveStep.HEND_HomeReset);
                    ResetMoveInfo.log("正在回原 顶升下降, 料串叉后退");
                    LocationDown(ResetMoveInfo);
                    CylinderMove(ResetMoveInfo, IO_T1_Type.Fork_Cylinder_Bck, IO_T1_Type.Fork_Cylinder_Fwd, IO_VALUE.LOW);
                    break;
                case MoveStep.HEND_HomeReset:
                    ResetMoveInfo.log("回源完成");
                    ResetMoveInfo.EndMove();
                    MoveInfo.NewMove(MoveStep.T1_01_WaitReel);
                    ShelfInMoveInfo.NewMove(MoveStep.Shelf_EmptyIn_WaitWorkLeave);
                    //ShelfOutMoveInfo.NewMove(MoveStep.Shelf_Out_Wait);
                    runStatus = RunStatus.Running;
                    break;
            }
        }

        /// <summary>
        /// 提升工位定位销上升
        /// </summary>
        public void LocationUp(MoveInfo MoveInfo)
        {
            IOMove(IO_T1_Type.T1_Location_Cylinder_Down, IO_VALUE.LOW);
            IOMove(IO_T1_Type.T1_Location_Cylinder_Up, IO_VALUE.HIGH);
            if (MoveInfo != null)
            {
                MoveInfo.WaitList.Add(WaitResultInfo.WaitIO(IO_T1_Type.T1_Location1_Cylinder_Up, IO_VALUE.HIGH));
                MoveInfo.WaitList.Add(WaitResultInfo.WaitIO(IO_T1_Type.T1_Location2_Cylinder_Up, IO_VALUE.HIGH));
            }
        }
        /// <summary>
        /// 提升工位定位销下降
        /// </summary>
        public void LocationDown(MoveInfo MoveInfo)
        {
            IOMove(IO_T1_Type.T1_Location_Cylinder_Down, IO_VALUE.HIGH);
            IOMove(IO_T1_Type.T1_Location_Cylinder_Up, IO_VALUE.LOW);
            if (MoveInfo != null)
            {
                MoveInfo.WaitList.Add(WaitResultInfo.WaitIO(IO_T1_Type.T1_Location1_Cylinder_Down, IO_VALUE.HIGH));
                MoveInfo.WaitList.Add(WaitResultInfo.WaitIO(IO_T1_Type.T1_Location2_Cylinder_Down, IO_VALUE.HIGH));
            }
        }

        bool calledagv = false;
        void IOMonitorFUNC()
        {
            if (!calledagv && IOValue(IO_T1_Type.Empty_LineIn_Check).Equals(IO_VALUE.LOW)&& IOValue(IO_T1_Type.EmptyString_In_Check).Equals(IO_VALUE.LOW))
            {
                calledagv = true;
                if (!ConfigHelper.Config.Get("wiston_disable_agv_NeedEnter", false))
                    RobotManage.wistonAgvClient.NeedEnter();
            }
            else if (IOValue(IO_T1_Type.Empty_LineIn_Check).Equals(IO_VALUE.HIGH) || IOValue(IO_T1_Type.EmptyString_In_Check).Equals(IO_VALUE.HIGH))
            {
                calledagv = false;
            }
        }
    }
}