XRayMachine.Feeding.cs 9.6 KB
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CodeLibrary;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
namespace DeviceLibrary
{
    using crc = OnlineStore.CodeResourceControl;
    public partial class XRayMachine
    {

        MoveInfo FeedingMoveinfo = new MoveInfo("入料");
        void FeedingProcess()
        {
            if (CheckWait(FeedingMoveinfo))
                return;
            switch (FeedingMoveinfo.MoveStep)
            {
                case MoveStep.Feeding_01_Wait_Detect:
                    if (TrayWidth == 0 || TrayHight == 0)
                    {
                        TrayMonitor();
                    }
                    else
                    {
                        FeedingMoveinfo.NextMoveStep(MoveStep.Feeding_02_Take_Picture);
                        FeedingMoveinfo.ReelParam.PlateW = TrayWidth;
                        FeedingMoveinfo.ReelParam.PlateH = TrayHight;
                        FeedingMoveinfo.ReelParam.PlateRawH = TrayRawHight;
                        FeedingMoveinfo.log($"已稳定检测到料盘TrayWidth={TrayWidth},TrayHight={TrayHight},TrayRawHight={TrayRawHight}.");
                    }
                    break;
                case MoveStep.Feeding_02_Take_Picture:
                    FeedingMoveinfo.NextMoveStep(MoveStep.Feeding_03_Get_Reelinfo);
                    ScanCode();
                    break;
                case MoveStep.Feeding_03_Get_Reelinfo:
                    if (ScanTask.IsCompleted)
                    {
                        FeedingMoveinfo.NextMoveStep(MoveStep.Feeding_04_Wait_Xray_Free);
                        //78.10222.20L-016651|-20200826|900|W78016651352000A6|SAMSUNG
                        var x = ScanTask.Result;
                        //x.Add(new CodeInfo("test", 1000, 1000));
                        if (x.Count != 1)
                        {
                            FeedingMoveinfo.log($"未识别到有效二维码,等待人工取走, Count={x.Count}");
                            FeedingMoveinfo.ReelParam.IsNg = true;
                        }
                        else
                        {
                            FeedingMoveinfo.ReelParam.IsNg = false;
                            FeedingMoveinfo.ReelParam.codeInfos = x;
                            FeedingMoveinfo.ReelParam.WareCode = x[0].CodeStr;
                            FeedingMoveinfo.log($"已完成扫码,等待料盘被通过按钮确认,Count={x.Count}");
                        }
                        
                    }
                    else if (FeedingMoveinfo.IsTimeOut(10))
                    {
                        FeedingMoveinfo.NextMoveStep(MoveStep.Feeding_03_Get_Reelinfo);
                        FeedingMoveinfo.log($"等待扫码超时,重新扫码");
                    }

                    break;
                case MoveStep.Feeding_04_Wait_Xray_Free:
                    if (MoveInfo.MoveStep == MoveStep.Wait)
                    {
                        FeedingMoveinfo.NextMoveStep(MoveStep.Feeding_05_Wait_Man_Check);
                        FeedingMoveinfo.log($"Xray已空闲,允许进入");
                        
                    }
                    else if (FeedingMoveinfo.ReelParam.IsNg)
                    {
                        FeedingMoveinfo.NextMoveStep(MoveStep.Feeding_05_Wait_Man_Check);
                        FeedingMoveinfo.log($"NG料,进入人工取走步骤");
                    }
                    else if (MoveInfo.IsTimeOut(10))
                    {
                        FeedingMoveinfo.log($"等待Xray可以进入");
                        Msg.add(crc.GetString("wait_xx_free", "等待{0}空闲", RobotManage.xrayMachine.DeviceName), MsgLevel.info);
                    }
                    break;
                case MoveStep.Feeding_05_Wait_Man_Check:
                    if (FeedingMoveinfo.ReelParam.IsNg)
                    {
                        if (IOValue(IO_XRay_Type.Tray_Check).Equals(IO_VALUE.HIGH))
                        {
                            Msg.add(crc.GetString("scancode_fail_takeout","未识别出料盘, 请取走"), MsgLevel.warning);
                        }
                        else if (IOMonitor.IODebound(IO_XRay_Type.Tray_Check, Config, IO_VALUE.LOW, 1000))
                        {
                            FeedingMoveinfo.NextMoveStep(MoveStep.Feeding_10_Wait_Reset);
                            FeedingMoveinfo.log($"料盘已被人工取走");
                        }
                    }
                    else
                    {
                        if (true|| IOValue(IO_XRay_Type.Manual_Confirm_BTN).Equals(IO_VALUE.HIGH))
                        {
                            FeedingMoveinfo.NextMoveStep(MoveStep.Feeding_06_Goto_Xray);
                            FeedingMoveinfo.log($"按下入料按钮, 定位气缸下降");
                            CylinderMove(FeedingMoveinfo, IO_XRay_Type.Location_Cylinder_Down, IO_XRay_Type.Location_Cylinder_Up, IO_VALUE.LOW);
                        }
                        else if (IOMonitor.IODebound(IO_XRay_Type.Tray_Check, Config, IO_VALUE.LOW, 2000))
                        {
                            FeedingMoveinfo.NextMoveStep(MoveStep.Feeding_10_Wait_Reset);
                            FeedingMoveinfo.log($"料盘已被人工取走");
                        }
                    }
                    break;
                case MoveStep.Feeding_06_Goto_Xray:
                    FeedingMoveinfo.NextMoveStep(MoveStep.Feeding_10_Wait_Reset);
                    MoveInfo.NewMove(MoveStep.XRay_01_LocationDown);
                    MoveInfo.ReelParam = FeedingMoveinfo.ReelParam;
                    FeedingMoveinfo.WaitList.Add(WaitResultInfo.WaitTime(7000));
                    break;
                case MoveStep.Feeding_10_Wait_Reset:
                    FeedingMoveinfo.NewMove(MoveStep.Feeding_01_Wait_Detect);
                    TrayWidth = 0;
                    TrayHight = 0;
                    SensorDebounce.Reset();                    
                    CylinderMove(FeedingMoveinfo, IO_XRay_Type.Location_Cylinder_Down, IO_XRay_Type.Location_Cylinder_Up, IO_VALUE.HIGH);
                    break;
                default:
                    FeedingMoveinfo.NextMoveStep(MoveStep.Feeding_10_Wait_Reset);
                    break;
            }


        }

        /// <summary>
        /// 扫码线程
        /// </summary>
        Task<List<CodeInfo>> ScanTask;
        void ScanCode()
        {
            FeedingMoveinfo.log("开始扫码");
            try
            {
                ScanTask = Task.Run(new Func<List<CodeInfo>>(() =>
                {
                    //IOMove(IO_XRay_Type.Camera_Led, IO_VALUE.HIGH);
                    //Task.Delay(10).Wait();
                    List<CodeInfo> LastCodeList = CodeManager.CameraScan(new List<string> { Config.CameraName });
                    //IOMove(IO_XRay_Type.Camera_Led, IO_VALUE.LOW);
                    return LastCodeList;
                }));
            }
            catch (Exception ex)
            {
                LogUtil.error("扫码出错:", ex);
            }

        }
        int TrayWidth = 0;
        int TrayHight = 0;
        int TrayRawHight = 0;
        void TrayMonitor() {
            if (IOValue(IO_XRay_Type.Tray_Check).Equals(IO_VALUE.LOW))
            {
                SensorDebounce.Reset();
                return;
            }
            int overhight = 70;
            var Tray15_L = AdioValue(IO_XRay_Type.Tray15_L_ADIO);
            var Tray15_R = AdioValue(IO_XRay_Type.Tray15_R_ADIO);
            var Tray13_L = AdioValue(IO_XRay_Type.Tray13_L_ADIO);
            var Tray13_R = AdioValue(IO_XRay_Type.Tray13_R_ADIO);
            var Tray7_L = AdioValue(IO_XRay_Type.Tray7_L_ADIO);
            var Tray7_R = AdioValue(IO_XRay_Type.Tray7_R_ADIO);


            var Tray7= Math.Max(Tray7_L, Tray7_R);
            var Tray13 = Math.Max(Tray13_L, Tray13_R);
            var Tray15 = Math.Max(Tray15_L, Tray15_R);
            //LogUtil.OutputDebugString($"Tray7:{Tray7},Tray13:{Tray13},Tray15:{Tray15}");
            int v=0;
            if (Tray15 < overhight)
            {
                if (SensorDebounce.Debounce(Tray15, out v))
                    TrayWidth = 15;
            }
            else if (Tray13 < overhight)
            {
                if (SensorDebounce.Debounce(Tray13, out v))
                    TrayWidth = 13;
            }
            else if (Tray7 < overhight)
            {
                if (SensorDebounce.Debounce(Tray7, out v))
                    TrayWidth = 7;
            }
            if (v>0)
                FeedingMoveinfo.log($"Sensor Tray7:{Tray7},Tray13:{Tray13},Tray15:{Tray15}");
            TrayHight = CalcHight(v);
            TrayRawHight = v;

        }
        
        int AdioValue(string ioType) {
            var pi = Config.GetType().GetProperty(ioType + "_base");
            var basevalue = (int)pi.GetValue(Config);
            var value = IOManager.GetADIOValue(ioType, Config);
            var c = ((int)value - basevalue) / Config.Tray_ADIO_Value;
            return c;
        }
        int CalcHight(int height) {
            int LastHeight = 0;
            List<int> heightList = new List<int> {0, 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;
                }
            }
            return LastHeight;
        }
    }
}