PackageLine_S2Station.cs 18.4 KB

using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static OnlineStore.DeviceLibrary.HttpServer;

namespace OnlineStore.DeviceLibrary
{
    partial class PackageLine
    {
        string PkgShelfRFID = "";

        string BigShelfRFID = "";

        string TempBigRfid = "";

        PackageRobotTaskBean packgeRobotTaskBean;


        public StepInfo<S2_STATION_STEP> s2StationStep = new StepInfo<S2_STATION_STEP>(S2_STATION_STEP.NONE);
        /// <summary>
        /// 包装料架工位
        /// </summary>
        public enum S2_STATION_STEP
        {
            NONE,
            /// <summary>
            /// 等待料架
            /// </summary>
            WAIT_SHELF,
            /// <summary>
            /// 等待料架进入S2工位
            /// </summary>
            WAIT_SHELF_TO_S2,
            /// <summary>
            /// 等待读取RFID
            /// </summary>
            READ_RFID,
            /// <summary>
            /// 放行料架
            /// </summary>
            OUT_SHELF,
            /// <summary>
            /// 等待料架离开
            /// </summary>
            WAIT_OUT_S2,
            /// <summary>
            /// 等待顶升上升
            /// </summary>
            WAIT_STATION_UP,
            /// <summary>
            /// 等待顶升上升
            /// </summary>
            WAIT_STATION_DOWN,
            /// <summary>
            /// 等待机器人抓取
            /// </summary>
            WAIT_ROBOT,
            ROBOT_GET,
            WAIT_ROBOT_GET_OK,
            WAIT_ROBOT_PUT_FINISH,
            WAIT_ROBOT_PUT_OK,
        }

       

        /// <summary>
        /// 双层线大料工位
        /// </summary>
        private void ProcessS2Station()
        {
            ProcessShelfOutS2();
            if (s2StationStep.IsStep(S2_STATION_STEP.NONE))
            {
                bool hasShelf = IOValue(IO_Type.S2_StopCheck2).Equals(IO_VALUE.HIGH);
                if (hasShelf)
                {
                    s2StationStep.Msg ="工位2检测到料架,等待读取RFID";
                    s2StationStep.ToNextStep(S2_STATION_STEP.READ_RFID);
                } else
                {
                    s2StationStep.Msg ="工位2无料架,等待料架进入";
                    s2StationStep.ToNextStep(S2_STATION_STEP.WAIT_SHELF);
                }
            }
            else if (s2StationStep.IsStep(S2_STATION_STEP.WAIT_SHELF)) {
                bool hasWaitShelf = IOValue(IO_Type.S2_StopCheck1).Equals(IO_VALUE.HIGH);
                //有等待的料架
                if (hasWaitShelf)
                {
                    //验证料架是否可用
                    if (LineManager.Line.S2CanGetShelf)
                    {
                        s2StationStep.Msg = "工位2等待位发现料架,降下S2_StopCylinder1(2秒),打开工位2链条, 将其送入工位";
                        s2StationStep.ToNextStep(S2_STATION_STEP.WAIT_SHELF_TO_S2);
                        s2StationStep.AddIoWait(IO_Type.S2_StopCheck1, IO_VALUE.LOW);
                        s2StationStep.AddIoWait(IO_Type.S2_StopCheck2, IO_VALUE.HIGH);
                        IOMove(IO_Type.S2_LineRun, IO_VALUE.HIGH);
                        IOMove(IO_Type.S2_StopCylinder1, IO_VALUE.HIGH, 2000);
                    }
                    else
                    {
                        s2StationStep.Msg = "工位2等待位发现料架, 但工位一未使用完";
                    }
                }
            }
            else if (s2StationStep.IsStep(S2_STATION_STEP.WAIT_SHELF_TO_S2)) {
                
                if (s2StationStep.IsFinished())
                {
                    s2StationStep.Msg ="料架到达工位2, 停止工位2链条, 开始读取RFID";
                    IOMove(IO_Type.S2_LineRun, IO_VALUE.LOW);
                    s2StationStep.ToNextStep(S2_STATION_STEP.READ_RFID);
                }
                else
                {
                    KeepLineRun(IO_Type.S2_LineRun);
                }

            }
            else if (s2StationStep.IsStep(S2_STATION_STEP.READ_RFID))
            {
                if (s2StationStep.IsTimeOut(1000))
                {
                    string rfid = RFIDManager.ReadRFID(Config.S2_Rfid, true).NumStr();
                    if (rfid.Contains("C"))
                    {
                        ResultBean resultBean = HttpServer.GetPackInfo(rfid, PkgShelfRFID);
                        //发送指令到服务器
                        if (resultBean != null)
                        {
                            if (resultBean.code == 0)
                            {
                                PackageRobotTaskBean shelfTaskBean = resultBean.data;
                                if (shelfTaskBean.emptyInBig > 0)
                                {
                                    s2StationStep.Msg = "料架[" + rfid + "]剩余包装料空位数位[" + shelfTaskBean.emptyInBig + "]工位2顶升";
                                    //还有包装料要放,升起顶升
                                    BigShelfRFID = rfid;
                                    s2StationStep.ToNextStep(S2_STATION_STEP.WAIT_STATION_UP);
                                    s2StationStep.AddIoWait(IO_Type.S2_TopCylinderUp, IO_VALUE.HIGH);
                                    s2StationStep.AddIoWait(IO_Type.S2_TopCylinderDown, IO_VALUE.LOW);
                                    CylinderMove(null, IO_Type.S2_TopCylinderDown, IO_Type.S2_TopCylinderUp);
                                }
                                else
                                {
                                    s2StationStep.Msg = "工位2料架[" + rfid + "]已无空位,放行";
                                    s2StationStep.ToNextStep(S2_STATION_STEP.OUT_SHELF);
                                }
                            }
                            else
                            {
                                s2StationStep.Msg = "工位2料架[" + rfid + "] 未从服务器获取到有效信息[" + resultBean.msg + "],放行大料架";
                                s2StationStep.ToNextStep(S2_STATION_STEP.OUT_SHELF);
                            }
                        }
                        
                    }
                    else if (rfid.Contains("D"))
                    {
                        s2StationStep.Msg = "工位2料架[" + rfid + "]不是大料架,直接放行";
                        //不是大料架直接放行
                        s2StationStep.ToNextStep(S2_STATION_STEP.OUT_SHELF);
                    }
                    else
                    {
                        s2StationStep.Msg = "工位2料架RFID读取错误["+rfid+"],重新读取";
                        s2StationStep.ToNextStep(S2_STATION_STEP.READ_RFID);
                    }
                }
                
            }
            else if (s2StationStep.IsStep(S2_STATION_STEP.OUT_SHELF))
            {
                TempBigRfid = "";
                BigShelfRFID = "";

                s2StationStep.Msg ="工位2顶升下降";
                s2StationStep.ToNextStep(S2_STATION_STEP.WAIT_STATION_DOWN);
                s2StationStep.AddIoWait(IO_Type.S2_TopCylinderUp, IO_VALUE.LOW);
                s2StationStep.AddIoWait(IO_Type.S2_TopCylinderDown, IO_VALUE.HIGH);
                CylinderMove(null, IO_Type.S2_TopCylinderUp, IO_Type.S2_TopCylinderDown);
            }
            else if (s2StationStep.IsStep(S2_STATION_STEP.WAIT_STATION_DOWN)) {
                if (s2StationStep.IsFinished())
                {
                    s2StationStep.Msg ="工位2顶升下降完成,打开右侧下方线体, 开始放行料架";
                    s2StationStep.ToNextStep(S2_STATION_STEP.WAIT_OUT_S2);
                    s2StationStep.AddIoWait(IO_Type.S2_StopCheck2, IO_VALUE.LOW, 2000);
                    s2StationStep.AddTimeWait(12000);

                    KeepLineRun(IO_Type.S2_LineRun);
                    //阻挡2下降2秒
                    IOMove(IO_Type.S2_StopCylinder2, IO_VALUE.HIGH, 2000);
                    //打开右侧下方线体
                    KeepLineRun(IO_Type.R_LineRun);
                }
            }
            else if (s2StationStep.IsStep(S2_STATION_STEP.WAIT_OUT_S2)) {
                
                if (s2StationStep.IsFinished())
                {
                    if (outShelfStep.IsStep(SHELF_TO_LEAVE_STATION.WAIT_OUT_IO))
                    {
                        //上一个料架流出未完成
                        s2StationStep.Msg = "料架流出工位2完成,但上一个料架未到出口";
                    }
                    else
                    {
                        s2StationStep.Msg = "工位2放行料架完成,停止工位2线体";
                        s2StationStep.ToNextStep(S2_STATION_STEP.NONE);
                        IOMove(IO_Type.S2_LineRun, IO_VALUE.LOW);
                        //右侧下方线体是否停止,要根据情况来
                        outShelfStep.ToNextStep(SHELF_TO_LEAVE_STATION.SHELF_OUT_S2);
                    }
                }
                else
                {
                    KeepLineRun(IO_Type.R_LineRun);
                    if (s2StationStep.IsTimeOut(15000))
                    {
                        s2StationStep.Msg = "工位2放行料架15秒仍未完成,重新放行";
                        s2StationStep.ToNextStep(S2_STATION_STEP.OUT_SHELF);
                    }
                }
                
            }
            else if (s2StationStep.IsStep(S2_STATION_STEP.WAIT_STATION_UP))
            {
                if (s2StationStep.IsFinished())
                {
                    //顶升已经升起,等待机器人处理
                    s2StationStep.ToNextStep(S2_STATION_STEP.WAIT_ROBOT);
                }
            }
            else if (s2StationStep.IsStep(S2_STATION_STEP.WAIT_ROBOT))
            {
                packgeRobotTaskBean = null;
                string currentPkgShelfRfid = "";
                if (pkgStationStep.IsStep(PKG_STATION_STEP.WAIT_ROBOT))
                {
                    currentPkgShelfRfid = PkgShelfRFID;
                }
                //发送指令到服务器
                ResultBean resultBean = HttpServer.GetPackInfo(BigShelfRFID, currentPkgShelfRfid);
                if (resultBean != null)
                {
                    if(resultBean.code == 0)
                    {
                        PackageRobotTaskBean shelfTaskBean = resultBean.data;
                        if (pkgStationStep.IsStep(PKG_STATION_STEP.WAIT_ROBOT))
                        {
                            if (shelfTaskBean.reelInPackage <= 0)
                            {
                                s2StationStep.Msg = "包装料架[" + PkgShelfRFID + "]上已无料盘,放行";
                                pkgStationStep.ToNextStep(PKG_STATION_STEP.SHELF_EMPTY);
                            }
                        }

                        if (shelfTaskBean.emptyInBig <= 0)
                        {
                            s2StationStep.Msg = "工位2料架[" + BigShelfRFID + "]已无空位,放行";
                            s2StationStep.ToNextStep(S2_STATION_STEP.OUT_SHELF);
                        }

                        else
                        {
                            if (TempBigRfid.Equals("") || TempBigRfid.Equals(shelfTaskBean.bigRfid) || BigShelfRFID.Equals(shelfTaskBean.realBigRfid))
                            {
                                //未绑定过或与绑定的虚拟料架一致
                                if (pkgStationStep.IsStep(PKG_STATION_STEP.WAIT_ROBOT) && s2StationStep.IsStep(S2_STATION_STEP.WAIT_ROBOT))
                                {
                                    if (TempBigRfid.Equals(""))
                                    {
                                        TempBigRfid = shelfTaskBean.bigRfid;
                                    }
                                    packgeRobotTaskBean = shelfTaskBean;
                                    s2StationStep.ToNextStep(S2_STATION_STEP.ROBOT_GET);
                                    s2StationStep.Msg = "机器人准备抓取包装料架[" + PkgShelfRFID + "]库位[" + shelfTaskBean.packageLoc + "]上的料盘[" + shelfTaskBean.barcode + "]";
                                }
                            }
                            else
                            {
                                s2StationStep.Msg = "工位2料架" + BigShelfRFID + "[" + TempBigRfid + "]与服务器给定" + shelfTaskBean.bigRfid + "[" + shelfTaskBean.realBigRfid + "]不一致,放行大料架";
                                s2StationStep.ToNextStep(S2_STATION_STEP.OUT_SHELF);
                            }
                        }
                    }
                    else
                    {
                        s2StationStep.Msg = "工位2料架" + BigShelfRFID + "[" + TempBigRfid + "】 未从服务器获取到有效信息["+resultBean.msg+"],放行大料架";
                        s2StationStep.ToNextStep(S2_STATION_STEP.OUT_SHELF);
                    }
                    
                }
            }
            else if (s2StationStep.IsStep(S2_STATION_STEP.ROBOT_GET))
            {
                bool robotCanMove = IOValue(IO_Type.Pkg_Rotot_Forbidden_Check).Equals(IO_VALUE.LOW);
                if (robotCanMove)
                {
                    s2StationStep.ToNextStep(S2_STATION_STEP.WAIT_ROBOT_GET_OK);
                    s2StationStep.Msg ="机器人开始抓取包装料架[" + PkgShelfRFID + "]库位[" + packgeRobotTaskBean.packageLoc + "]上的料盘[" + packgeRobotTaskBean.barcode + "]";
                    ABBControl.SendCmd(Config.ABB3_IP, ABBControl.Cmd_moveget, "p" + packgeRobotTaskBean.packageLoc, "L", ABBControl.DefaultSpeed);
                }else
                {
                    s2StationStep.Msg = "包装线体机器人禁止区域有料架,暂停抓取";
                    s2StationStep.ToNextStep(S2_STATION_STEP.ROBOT_GET);
                }
            }
            else if (s2StationStep.IsStep(S2_STATION_STEP.WAIT_ROBOT_GET_OK))
            {
                string getResult = ABBControl.GetAbbCmd(Config.ABB3_IP);
                if (getResult.Equals(""))
                {
                    //如果包装料架已经空了,放行
                    if (packgeRobotTaskBean.reelInPackage == 1)
                    {
                        s2StationStep.Msg ="包装料架[" + PkgShelfRFID + "]最后一盘料已取出,放行";
                        pkgStationStep.ToNextStep(PKG_STATION_STEP.SHELF_EMPTY);
                    }

                    s2StationStep.ToNextStep(S2_STATION_STEP.WAIT_ROBOT_PUT_FINISH);
                    s2StationStep.Msg ="机器人开始将料盘[" + packgeRobotTaskBean.barcode + "]放入[" + packgeRobotTaskBean.realBigRfid + "]库位[" + packgeRobotTaskBean.bigLoc + "]";
                    ABBControl.SendCmd(Config.ABB3_IP, ABBControl.Cmd_moveput, "cp" + packgeRobotTaskBean.bigLoc, "L", ABBControl.DefaultSpeed);
                }
            } else if (s2StationStep.IsStep(S2_STATION_STEP.WAIT_ROBOT_PUT_FINISH))
            {
                string putResult = ABBControl.GetAbbCmd(Config.ABB3_IP);
                if (putResult.Contains("FINISHED"))
                {
                    s2StationStep.Msg ="机器人将料盘[" + packgeRobotTaskBean.barcode + "]放入" + packgeRobotTaskBean.bigRfid + "[" + packgeRobotTaskBean.realBigRfid + "]库位[" + packgeRobotTaskBean.bigLoc + "]完成";
                    s2StationStep.ToNextStep(S2_STATION_STEP.WAIT_ROBOT_PUT_OK);
                    //发送消息到服务器
                    HttpServer.PackFinish(packgeRobotTaskBean.barcode, packgeRobotTaskBean.packageRfid, packgeRobotTaskBean.packageLoc, BigShelfRFID, packgeRobotTaskBean.bigLoc);
                }
            }
            else if (s2StationStep.IsStep(S2_STATION_STEP.WAIT_ROBOT_PUT_OK))
            {
                string putResult = ABBControl.GetAbbCmd(Config.ABB3_IP);
                if (putResult.Equals(""))
                {
                    s2StationStep.Msg ="机器人将料盘[" + packgeRobotTaskBean.barcode + "]放入[" + packgeRobotTaskBean.realBigRfid + "]库位[" + packgeRobotTaskBean.bigLoc + "]结束";
                    s2StationStep.ToNextStep(S2_STATION_STEP.WAIT_ROBOT);
                }
            }
        }

        private StepInfo<SHELF_TO_LEAVE_STATION> outShelfStep = new StepInfo<SHELF_TO_LEAVE_STATION>(SHELF_TO_LEAVE_STATION.NONE);
        private enum SHELF_TO_LEAVE_STATION
        {
            NONE, SHELF_OUT_S2, WAIT_LINE_RUN_TIME,WAIT_OUT_IO
        }

        private void ProcessShelfOutS2()
        {
            if (outShelfStep.IsStep(SHELF_TO_LEAVE_STATION.SHELF_OUT_S2)){
                bool hasShelfToLeave = IOValue(IO_Type.R_StopCheck2).Equals(IO_VALUE.HIGH);
                KeepLineRun(IO_Type.R_LineRun);
                if (hasShelfToLeave)
                {
                    outShelfStep.ToNextStep(SHELF_TO_LEAVE_STATION.WAIT_LINE_RUN_TIME);
                    //出料口已有料架,转动60秒
                    outShelfStep.Msg= "料架流出工位2时,出口已有料架,下层线体转动60秒";
                    outShelfStep.AddTimeWait(600000);
                }
                else
                {
                    //出料口无料架,等待出料口信号持续3秒
                    outShelfStep.ToNextStep(SHELF_TO_LEAVE_STATION.WAIT_OUT_IO);
                    outShelfStep.Msg = "料架流出工位2时,出口无料架,下层线体转动到料架到达出口";
                    outShelfStep.AddIoWait(IO_Type.R_StopCheck2, IO_VALUE.HIGH, 3000);
                }
            }else if (outShelfStep.IsStep(SHELF_TO_LEAVE_STATION.WAIT_LINE_RUN_TIME) || outShelfStep.IsStep(SHELF_TO_LEAVE_STATION.WAIT_OUT_IO))
            {
                if (outShelfStep.IsFinished())
                {
                    outShelfStep.Msg = "料架流出工位2完成,停止下层线体";
                    IOMove(IO_Type.R_LineRun, IO_VALUE.LOW);
                    outShelfStep.ToNextStep(SHELF_TO_LEAVE_STATION.NONE);
                }
                else
                {
                    //检查线体是否停止,如果停止重新启动
                    KeepLineRun(IO_Type.R_LineRun);
                }
            }
        }
    }

}