MainMachine_InCar.cs 16.8 KB
using CodeLibrary;
using OnlineStore;
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
{
    partial class MainMachine
    {

        /// <summary>
        /// 通知料盘已取走
        /// </summary>
        void ReelGetted(bool success = true)
        {
            if (InCarMoveInfo.MoveStep == MoveStep.InCarReadyGet)
                if (success)
                    InCarMoveInfo.NextMoveStep(MoveStep.InCarReelGot);
                else
                    InCarMoveInfo.NextMoveStep(MoveStep.InCarReelGetFail);
            else
                InCarMoveInfo.log($"无法执行ReelGetted,因为当前步骤为:{InCarMoveInfo.MoveStep}");
        }

        bool InHasCar { get => (IOValue(IO_Type.Loading_Car_Check).Equals(IO_VALUE.HIGH) || IOValue(IO_Type.Loading_Car1315_Check).Equals(IO_VALUE.HIGH)); }

        public CarTypeE InCarType = CarTypeE.None;
        public int InCurrentStringNum = 0;
        bool innewreel = true;
        int ReelGetFail = 0;


        const bool oldtestheight = true;
        void InCarProcess()
        {
            if (CheckWait(InCarMoveInfo))
                return;
            
            switch (InCarMoveInfo.MoveStep)
            {
                case MoveStep.Wait:
                    InCarType = CarTypeE.None;
                    var hascar = InHasCar;
                    if (hascar)
                        Loading_NOCar_Led.LedState = LedState.blink;
                    else
                        Loading_NOCar_Led.LedState = LedState.off;
                    if (hascar && IOValue(IO_Type.Loading_CarLock_Btn).Equals(IO_VALUE.HIGH))
                    {
                        InCarMoveInfo.NextMoveStep(MoveStep.InCar_01);
                        InCarMoveInfo.log("检测到小车到位");
                    }
                    else if (hascar && IOValue(IO_Type.Loading_Car_Location_Up).Equals(IO_VALUE.HIGH))
                    {
                        InCarMoveInfo.NextMoveStep(MoveStep.InCar_02);
                        InCarMoveInfo.log("小车已经到位");
                    }
                    break;
                case MoveStep.InCar_01:
                    InCarMoveInfo.NextMoveStep(MoveStep.InCar_02);
                    InCarMoveInfo.log($"提升定位气缸");
                    CylinderMove(InCarMoveInfo, IO_Type.Loading_Car_Location_Down, IO_Type.Loading_Car_Location_Up, IO_VALUE.HIGH);
                    break;
                case MoveStep.InCar_02:
                    var car7 = IOValue(IO_Type.Loading_Car_Check).Equals(IO_VALUE.HIGH);
                    var car13 = IOValue(IO_Type.Loading_Car1315_Check).Equals(IO_VALUE.HIGH);
                    if (car7 || car13)
                    {
                        if (car7)
                            InCarType = CarTypeE.Car7;
                        else
                            InCarType = CarTypeE.Car13;

                        InCarMoveInfo.log($"检测到料车尺寸:{InCarType}");
                        InCarMoveInfo.NextMoveStep(MoveStep.InCar_03);
                        Loading_NOCar_Led.LedState = LedState.on;
                    }
                    else {
                        InCarMoveInfo.log($"小车丢失,重新检测");
                        CylinderMove(InCarMoveInfo, IO_Type.Loading_Car_Location_Down, IO_Type.Loading_Car_Location_Up, IO_VALUE.LOW);
                        InCarMoveInfo.NextMoveStep(MoveStep.Wait);
                    }
                    break;
                case MoveStep.InCar_03:
                    InCarMoveInfo.NextMoveStep(MoveStep.InCar_04);
                    innewreel = true;
                    InCarMoveInfo.log($"新料串");
                    break;
                case MoveStep.InCar_04:
                    InCarMoveInfo.NextMoveStep(MoveStep.InCar_05);
                    InCarMoveInfo.log($"批量轴高速上升到顶部检测点");
                    BatchAxisToP2(InCarMoveInfo, Loading_Batch_Axis, INGetReelCheckIOList(),IO_VALUE.HIGH, Config.Loading_Batch_Axis_P2, Config.Loading_Batch_Axis_P1_speed);
                    break;
                case MoveStep.InCar_05:
                    InCarMoveInfo.NextMoveStep(MoveStep.InCar_06);
                    InCurrentStringNum = GetTopStringNum(INGetReelCheckIOList());
                    if (Loading_Batch_Axis.IsInPosition(Config.Loading_Batch_Axis_P2) && InCurrentStringNum==0)
                    {
                        InCarMoveInfo.log($"料车料盘检测为空");
                        InCarMoveInfo.NextMoveStep(MoveStep.InCarOut_01);
                        return;
                    }
                    var tpos1 = Loading_Batch_Axis.GetAclPosition() - Config.Loading_Batch_Axis_MMtoPOS * 10;
                    if (tpos1 < Config.Loading_Batch_Axis_P1)
                        tpos1 = Config.Loading_Batch_Axis_P1;
                    Loading_Batch_Axis.AbsMove(InCarMoveInfo, tpos1, Config.Loading_Batch_Axis_P1_speed);
                    InCarMoveInfo.log($"批量轴下降1cm");
                    break;
                case MoveStep.InCar_06:
                    InCarMoveInfo.NextMoveStep(MoveStep.InCar_07);
                    BatchAxisToP2(InCarMoveInfo, Loading_Batch_Axis, INGetReelCheckIOList(), IO_VALUE.HIGH, Config.Loading_Batch_Axis_P2, Config.Loading_Batch_Axis_P2_speed);
                    InCarMoveInfo.WaitList.Add(WaitResultInfo.WaitMsg("进料车料串正在上升",MsgLevel.warning));
                    break;
                case MoveStep.InCar_07:
                    InCarMoveInfo.NextMoveStep(MoveStep.InCarReadyGet);
                    InCurrentStringNum = GetTopStringNum(INGetReelCheckIOList());
                    if (Loading_Batch_Axis.IsInPosition(Config.Loading_Batch_Axis_P2) && InCurrentStringNum == 0)
                    {
                        InCarMoveInfo.log($"料车料盘检测为空");
                        InCarMoveInfo.NextMoveStep(MoveStep.InCarOut_01);
                        return;
                    }
                    InCarMoveInfo.log($"料车料盘检测到新的当前串[{InCurrentStringNum}串]");
                    if (InCurrentStringNum == 0) {
                        InCarMoveInfo.NextMoveStep(MoveStep.InCar_06);
                        InCarMoveInfo.log($"再次上升");
                        return;
                    }

                    //if (!innewreel)
                    //     SetReelHeight(GetHeight(InCarMoveInfo));
                    //innewreel = false;
                    break;               
                case MoveStep.InCarReadyGet:
                    Msg.add("入口料串取料准备就绪", MsgLevel.info);
                    break;
                case MoveStep.InCarReelGot:
                    InCarMoveInfo.log($"料已取走,开始测高");
                    InCarMoveInfo.NextMoveStep(MoveStep.InCarReelGet_01);
                    break;
                case MoveStep.InCarReelGet_01:
                    InCarMoveInfo.NextMoveStep(MoveStep.InCarReelGet_02);
                    BatchAxisToP2(InCarMoveInfo, Loading_Batch_Axis, INGetReelCheckIOList(InCurrentStringNum), IO_VALUE.HIGH, Config.Loading_Batch_Axis_P2, Config.Loading_Batch_Axis_P2_speed);
                    InCarMoveInfo.log($"批量轴[{InCurrentStringNum}串]上升检测信号:{INGetReelCheckIOList(InCurrentStringNum)[0]}");
                    break;
                case MoveStep.InCarReelGet_02:
                    InCarMoveInfo.NextMoveStep(MoveStep.InCarReelGet_03);
                    SetReelHeight(GetHeight(InCarMoveInfo));
                    var topnum = GetTopStringNum(INGetReelCheckIOList());
                    if (InCurrentStringNum == topnum)
                    {
                        InCarMoveInfo.log($"最高侧等于当前侧:{InCurrentStringNum}");
                    }
                    else {
                        
                        InCarMoveInfo.log($"最高侧不等于当前侧:{InCurrentStringNum}!={topnum}");
                    }
                    break;
                case MoveStep.InCarReelGet_03:
                    InCarMoveInfo.NextMoveStep(MoveStep.InCarReelGet_04);
                    BatchAxisToP2(InCarMoveInfo, Loading_Batch_Axis, INGetReelCheckIOList(), IO_VALUE.LOW, Config.Loading_Batch_Axis_P1, Config.Loading_Batch_Axis_P1_speed);
                    InCarMoveInfo.log($"下降到所有传感器灭");
                    break;
                case MoveStep.InCarReelGet_04:

                    if (GetTopStringNum(INGetReelCheckIOList()) != 0)
                    {
                        InCarMoveInfo.NextMoveStep(MoveStep.InCarReelGet_03);
                        InCarMoveInfo.log($"还未到底, 继续下降");
                    }
                    else
                    {
                        InCarMoveInfo.NextMoveStep(MoveStep.InCar_06);
                        InCarMoveInfo.log($"完成[{InCurrentStringNum}串]盘上料");
                    }
                    break;
                case MoveStep.InCarReelGetFail:
                    InCarMoveInfo.log($"料盘取走失败,料盘下降");
                    ReelGetFail++;
                    InCarMoveInfo.NextMoveStep(MoveStep.InCarReelGetFail_02);
                    var tpos2 = Loading_Batch_Axis.GetAclPosition() - Config.Loading_Batch_Axis_MMtoPOS * 20;
                    if (tpos2 < 0)
                        tpos2 = 0;
                    Loading_Batch_Axis.AbsMove(InCarMoveInfo, tpos2, Config.Loading_Batch_Axis_P1_speed);
                    break;
                case MoveStep.InCarReelGetFail_02:                    
                    InCarMoveInfo.NextMoveStep(MoveStep.InCar_06);
                    InCarMoveInfo.log($"[{InCurrentStringNum}串]料盘取走失败,重新上升");
                    break;
                
                case MoveStep.InCarOut_01:
                    InCarMoveInfo.NextMoveStep(MoveStep.InCarOut_02);
                    incarProcessFinish = true;
                    InCarMoveInfo.log($"料串下降到P1点");
                    Loading_Batch_Axis.AbsMove(InCarMoveInfo, Config.Loading_Batch_Axis_P1, Config.Loading_Batch_Axis_P1_speed);                    
                    InCarMoveInfo.WaitList.Add(WaitResultInfo.WaitMsg("料串正在下降", MsgLevel.warning));
                    break;
                case MoveStep.InCarOut_02:
                    InCarMoveInfo.NextMoveStep(MoveStep.InCarOut_Released);
                    InCarMoveInfo.log($"顶升下降");
                    CylinderMove(InCarMoveInfo, IO_Type.Loading_Car_Location_Down, IO_Type.Loading_Car_Location_Up, IO_VALUE.LOW);
                    break;
                case MoveStep.InCarOut_Released:
                    InCarMoveInfo.log($"料串已回到待机点");
                    InCarMoveInfo.NewMove(MoveStep.Wait);                    
                    break;
                default:
                    InCarMoveInfo.log($"未找到对应步骤:{InCarMoveInfo.MoveStep}");
                    break;
            }
        }

        string InCarProcessState() {
            string state = "";

            if (InCarType == CarTypeE.Car13)
                state = "13/15寸盘小车";
            else if (InCarType == CarTypeE.Car7)
            {                
                state = "7寸盘小车";
            }
            else if (InHasCar)
                state = "检测到小车,尚未锁定";

            if (InCarMoveInfo.MoveStep == MoveStep.Wait)
            {
                return "空闲中";
            }
            else if (InCarMoveInfo.MoveStep < MoveStep.InCarReadyGet)
            {
                return state + "," + "准备中";
            }
            else if (InCarMoveInfo.MoveStep >= MoveStep.InCarOut_01)
            {
                return state + "," + "释放中";
            }
            else if (InCarMoveInfo.MoveStep > MoveStep.InCar_06)
            {
                return state;
            }

            return state;
        }


        int StartMovePosition = 0;
        void BatchAxisToP2(MoveInfo moveInfo, AxisBean axisBean, string[] CheckIoList, IO_VALUE iO_VALUE, int targetpos, int speed)
        {
            int targetP2 = targetpos;
            int targetSpeed = speed;
            moveInfo.log($"BatchAxis 目标P2: {targetP2},{speed},IO_VALUE={iO_VALUE},axisBean:{axisBean.Config.Explain},CheckIoList:{string.Join(",",CheckIoList)}");
            moveInfo.TimeOutSeconds = 200;
            moveInfo.CanWhileCount = 0;
            //  需要增加定时器,获取验证信号并停止伺服
            if (axisBean== Loading_Batch_Axis)
                StartMovePosition = axisBean.GetAclPosition();
            moveInfo.WaitList.Add(WaitResultInfo.WaitBatchAxisMove(axisBean.Config, targetP2, targetSpeed));
            axisBean.Config.TargetPosition = targetP2;
            axisBean.AbsMove(null, targetP2, targetSpeed);
            //开始检测信号
            axisBean.BatchAxisStartCheck(CheckIoList, iO_VALUE);
        }
        
        int CalcHeight(int mm)
        {
            LogUtil.info($"获取mm:{mm}");
            List<int> heightList = new List<int> { 8, 12, 16, 24, 32, 44, 56 };
            heightList.Reverse();
            //heightList = (from m in heightList orderby m descending select m).ToList<int>();
            //float minCha = height;
            foreach (int h in heightList)
            {
                if (mm > h + 1)
                {
                    return h;
                }
            }
            return 8;
        }
        string LastCode = "";
        private int GetHeight(MoveInfo moveInfo)
        {
            int LastHeight = 0;
            int AxisChangeValue = Config.Loading_Batch_Axis_MMtoPOS;
            //计算高度
            int EndMovePosition = Loading_Batch_Axis.GetAclPosition();

            float height = (float)Math.Ceiling(1F * Math.Abs(EndMovePosition - StartMovePosition) / AxisChangeValue);
            string buchongStr = "";
            if (Loading_Batch_Axis.IsInPosition(Config.Loading_Batch_Axis_P2))
            {
                buchongStr = $"(最后一盘料已补充{Config.LastTrayAddHeight})";
                height += Config.LastTrayAddHeight;
            }
            LastHeight = (int)height;
            if (LastHeight <= 8) { LastHeight = 8; }
            string msg = Name + " 计算盘高:上升前 [" + StartMovePosition + "]实时[ " + EndMovePosition + "]差值[" + (EndMovePosition - StartMovePosition) + "]系数[" + AxisChangeValue + "] 计算后" + buchongStr + "[" + height + "]" + ",归类为【" + LastHeight + "mm】条码【" + LastCode + "】";
            LogUtil.info(msg);
            if (LastHeight > 80)
                LastHeight = 80;
            return LastHeight;

            if (moveInfo.MoveParam.PlateW == 15)
            {
                height -= 2;
            }
            else if (moveInfo.MoveParam.PlateW == 13)
            {
                //height -= 3;
            }
            //如果检测出<=15,都按照8计算
            if (height < 12)
            {
                LastHeight = 8;
            }
            else
            {
                List<int> heightList = new List<int> { 8, 12, 16, 24, 32, 44, 56 };
                //heightList = (from m in heightList orderby m descending select m).ToList<int>();
                //float minCha = height;
                foreach (int h in heightList)
                {
                    if (height > h)
                    {
                        LastHeight = h;
                    }
                }
            }
            if (LastHeight <= 8) { LastHeight = 8; }
            //string code = CodeManager.ProcessCode(LastCodeList);
            msg = Name + " 计算盘高:上升前 [" + StartMovePosition + "]实时[ " + EndMovePosition + "]差值[" + (EndMovePosition - StartMovePosition) + "]系数[" + AxisChangeValue + "] 计算后" + buchongStr + "[" + height + "]" + ",归类为【" + LastHeight + "mm】条码【" + LastCode + "】";
            LogUtil.info(msg);
            return LastHeight;
        }

        string[] INGetReelCheckIOList(int currentnum = 0) {
            if (currentnum > 0)
                return new string[] { "Loading_Batch_ReelCheck_T" + currentnum.ToString() };

            if (InCarType == CarTypeE.Car7)
            {
                return new string[] { IO_Type.Loading_Batch_ReelCheck_T1, IO_Type.Loading_Batch_ReelCheck_T2, IO_Type.Loading_Batch_ReelCheck_T4, IO_Type.Loading_Batch_ReelCheck_T5 };
            }
            else
            {
                return new string[] { IO_Type.Loading_Batch_ReelCheck_T3 };
            }        
        }
        int GetTopStringNum(string[]  checkiolist) {
            foreach (var io in checkiolist) {
                if (IOValue(io).Equals(IO_VALUE.HIGH))
                {
                    return int.Parse(io.Substring(io.Length - 1));
                }
            }
            return 0;
        }
    }
}