TrayStop.cs 7.0 KB
using OnlineStore;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeviceLibrary
{
    public class TrayStop : DeviceBase, IDevice
    {
        public static Dictionary<string, TrayStop> DeviceList = new Dictionary<string, TrayStop>();

        public static void Init(Robot_Config config, Dictionary<string, DeviceGroup> devices,out string msg) {
            msg = "";
            foreach (var key in devices.Keys) {
                if (!devices[key].DeviceType.StartsWith("RT"))
                    continue;
                TrayStop sideMove = new TrayStop(devices[key],out string m);
                DeviceList.Add(devices[key].GroupName, sideMove);
                msg += m;
            }
        }
        DeviceGroup DeviceGroup;
        MoveInfo MoveInfo;
        public PuYueRFID_C2S RFID_1 = null;
        CylinderManger Location;
        AxisBean axis;
        AxisBean axis2;
        public TrayStop(DeviceGroup device,out string msg) {
            
            msg = "";
            Name = device.Name+"("+ device.GroupName + ")";
            DeviceGroup = device;
            GroupName = DeviceGroup.GroupName;
            Msg = new MsgService(GroupName);
            MoveInfo = new MoveInfo(GroupName);
            if (!string.IsNullOrEmpty(DeviceGroup.RFID_1))
            {
                RFID_1 = new PuYueRFID_C2S(DeviceGroup.RFID_1);
                if (!RFID_1.Open()) {
                    msg += DeviceGroup.GroupName + " RFID 1:" + DeviceGroup.RFID_1 +","+crc.GetString("Res0183","打开失败")+ "\r\n";
                }
            }
            Location = new CylinderManger($"托盘顶升", GroupName, IO_Type.MI_Location_Up, IO_Type.MI_Location_Down);

            var axisc = RobotManage.Config.moveAxisList.Find(ma => ma.GetAxisValue() == RobotManage.DeviceGroup[GroupName].AxisID);
            if (axisc != null)
            axis = new AxisBean(axisc, GroupName);
            axisc = RobotManage.Config.moveAxisList.Find(ma => ma.GetAxisValue() == RobotManage.DeviceGroup[GroupName].Axis2ID);
            if (axisc != null)
                axis2 = new AxisBean(axisc, GroupName);
        }
        public DeviceStateE DeviceState { get; set; }
        public void Start()
        {
            DeviceState = DeviceStateE.HomeReset;
            MoveInfo.NewMove(MoveStep.H01_HomeReset);
            MoveInfo.log("开始回原");
        }

        public void Stop()
        {
            DeviceState = DeviceStateE.Stop;
            MoveInfo.log("停止运行");
        }
        public void Process()
        {
            if (DeviceState == DeviceStateE.HomeReset)
                ResetProcess();
            else if (DeviceState == DeviceStateE.Run)
            {
                 OneWayProcess();
            }
        }
        int StopBufDelayMS = 500;
        int StopDelayMS = 1000;
        public void OneWayProcess()
        {
            if (CheckWait(MoveInfo))
                return;

            switch (MoveInfo.MoveStep)
            {
                case MoveStep.Wait:
                    if (IOValue(IO_Type.MI_Front_Check).Equals(IO_VALUE.HIGH))
                    {
                        MoveInfo.NextMoveStep(MoveStep.SideMove_01);
                        Location.ToLow(MoveInfo);
                        
                    }
                    else {
                        Msg.add(crc.GetString("Res0184","空闲中"), MsgLevel.info);
                    }
                    break;
                case MoveStep.TrayStop_01:
                    MoveInfo.NextMoveStep(MoveStep.SideMove_02);
                    IOMove(IO_Type.AMH_Front_Stop, IO_VALUE.HIGH, StopDelayMS);
                    MoveInfo.CanWhileCount = 3;
                    MoveInfo.WaitList.Add(WaitResultInfo.WaitIO(IO_Type.MI_In_Check, IO_VALUE.HIGH));
                    break;
                case MoveStep.TrayStop_02:
                    if (RFID_1.ReadByte(0x20, 16, out byte[] data))
                    {
                        var rfid = Encoding.ASCII.GetString(data).Trim();
                        var through = TrayManager.Process(rfid, DeviceGroup.addr_1, out TrayInfo trayInfo);
                        if (through)
                            MoveInfo.NextMoveStep(MoveStep.TrayStop_03);
                        else
                            MoveInfo.NextMoveStep(MoveStep.TrayStop_WaitLoadLeave);
                        MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(500));
                    }
                    else if (MoveInfo.CanWhileCount > 0)
                    {
                        MoveInfo.CanWhileCount--;
                        MoveInfo.log($"RFid读取失败,重试第{3 - MoveInfo.CanWhileCount}次");
                        MoveInfo.WaitList.Add(WaitResultInfo.WaitMsg(crc.GetString("Res0016","RFid读取失败"), MsgLevel.warning));
                    }
                    else
                    {
                        MoveInfo.log("RFid读取失败,直接通过");
                        MoveInfo.NextMoveStep(MoveStep.SideMove_04);
                    }
                    break;
                case MoveStep.TrayStop_03:
                    MoveInfo.NextMoveStep(MoveStep.TrayStop_04);
                    IOMove(IO_Type.AMH_Tray_Stop, IO_VALUE.HIGH, StopDelayMS);
                    MoveInfo.WaitList.Add(WaitResultInfo.WaitIO(IO_Type.MI_In_Check, IO_VALUE.LOW));
                    break;
                case MoveStep.TrayStop_04:
                    MoveInfo.NextMoveStep(MoveStep.Wait);
                    MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(500));
                    MoveInfo.log("托盘已放行");
                    break;
                case MoveStep.TrayStop_WaitLoadLeave:
                    break;
                case MoveStep.TrayStop_LoadLeaved:
                    MoveInfo.NextMoveStep(MoveStep.Wait);
                    Location.ToLow(MoveInfo);
                    MoveInfo.log("载荷离开, 顶升下降");
                    break;
            }
        }
        public void ResetProcess()
        {
            if (CheckWait(MoveInfo))
                return;

            switch (MoveInfo.MoveStep)
            {
                case MoveStep.Wait:
                    MoveInfo.NextMoveStep(MoveStep.SideMove_01);
                    MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(2000));
                    break;
                case MoveStep.SideMove_01:
                    MoveInfo.NextMoveStep(MoveStep.SideMove_02);
                    CylinderMove(MoveInfo, IO_Type.MI_Location_Down, IO_Type.MI_Location_Up, IO_VALUE.LOW);                    
                    break;
                case MoveStep.SideMove_02:
                    MoveInfo.NextMoveStep(MoveStep.SideMove_03);
                    IOMove(IO_Type.AMH_Tray_Stop, IO_VALUE.HIGH, StopDelayMS);
                    MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(1500));
                    break;
                case MoveStep.SideMove_03:
                    MoveInfo.EndMove();
                    DeviceState = DeviceStateE.Run;
                    break;
            }
        }
    }
}