AutomaticBaiting_Partial.cs 11.2 KB
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OnlineStore.DeviceLibrary
{
    public partial class AutomaticBaiting 
    {

        private static int StartMovePosition = 0;
        private static int EndMovePosition = 0;
        public static int AutoAxisIsMove = 0;
        private static void ACAxisSpeedMove(ConfigMoveAxis moveAxis, int targetSpeed)
        {
            AutoAxisIsMove = 1;
            StartMovePosition = ACServerManager.GetActualtPosition(moveAxis.DeviceName,moveAxis.GetAxisValue());
            EndMovePosition = StartMovePosition;
            StoreMove.WaitList.Add(WaitResultInfo.WaitAutoAxisStop(moveAxis));  
            ACServerManager.SpeedMove(moveAxis.DeviceName, moveAxis.GetAxisValue(), targetSpeed);
        }

        private static void ACAxisHomeMove(ConfigMoveAxis moveAxis)
        {
            moveAxis.TargetPosition = 0;
            LogUtil.debug(Name + moveAxis.DisplayStr + "speed[" + moveAxis.TargetSpeed + "]开始原点返回");
            StoreMove.WaitList.Add(WaitResultInfo.WaitAxis(moveAxis, true)); 
            ACServerManager.HomeMove(moveAxis.DeviceName, (short)moveAxis.GetAxisValue(), moveAxis.HomeHighSpeed);
        }
        private static  bool ACHomeMoveIsEnd(ConfigMoveAxis moveAxis, out string msg)
        {
            msg = "";
            if (ACServerManager.IsHomeMoveEnd(moveAxis.DeviceName, moveAxis.GetAxisValue()))
            {
                //原点完成并且位置=0
                int outCount = ACServerManager.GetActualtPosition(moveAxis.DeviceName, moveAxis.GetAxisValue());
                int errorCount = Math.Abs(outCount);
                if (errorCount > moveAxis.CanErrorCountMax)
                {
                    ////判断是否需要重新运动
                    //if (StoreMove.CanWhileCount > 0)
                    //{
                    //    LogUtil.error(Name+ moveAxis.DisplayStr + "收到原点完成信号,当前位置[" + outCount + "],重新回原点,剩余[" + StoreMove.CanWhileCount + "]次"); 
                    //    ACServerManager.HomeMove(moveAxis.DeviceName, moveAxis.GetAxisValue(), moveAxis.HomeHighSpeed);
                    //    StoreMove.CanWhileCount--;
                    //}
                    //else
                    //{
                        msg = Name + " storeMoveStep=" + StoreMove.MoveStep + moveAxis.DisplayStr + "收到原点完成信号,当前位置[" + outCount + "],误差过大,需要报警";
                        LogUtil.error(  msg);
                        return false;
                    //} 
                } 
                return true;
            }
            return false;
        }
        private static void CheckWait()
        {
            List<WaitResultInfo> list = StoreMove.WaitList; 
            if (list.Count <= 0)
            {
                StoreMove.EndStepWait();
                return;
            } 
            //当等待超过一分钟时,需要打印提示 
            TimeSpan span = DateTime.Now - StoreMove.LastSetpTime;
            string NotOkMsg = "";
            bool isOk = !StoreMove.OneWaitCanEndStep; 
            foreach (WaitResultInfo wait in list)
            {
                if (wait.IsEnd)
                {
                    continue;
                }
                NotOkMsg = wait.ToStr();
                if (wait.WaitType == 1)
                {
                    wait.IsEnd = AxisMoveISEnd(wait);
                }
                else if (wait.WaitType == 2)
                {
                    wait.IsEnd = KND.IOValue(wait.IoType).Equals(wait.IoValue);
                    int timeOutMs = StoreManager.Config.IOSingle_TimerOut;

                    if ((!wait.IsEnd) && span.TotalMilliseconds > timeOutMs)
                    {
                        ConfigIO io = StoreManager.Config.getWaitIO(wait.IoType);
                        WarnMsg = Name + " 等待信号" + io.DisplayStr + "=" + wait.IoValue + "超时!";
                        StoreManager.Store.Alarm(StoreAlarmType.IoSingleTimeOut, io.ElectricalDefinition, WarnMsg, StoreMove.MoveType);
                        LogUtil.error(WarnMsg);
                        isOk = false;
                        break;
                    }
                }
                else if (wait.WaitType == 3)
                {
                    wait.IsEnd = (span.TotalMilliseconds >= wait.TimeMSeconds);
                }
                else if (wait.WaitType == 5)
                {
                }
                else if (wait.WaitType == 6)
                {
                    IO_VALUE value = (IO_VALUE)ACServerManager.GetHomeSingle(wait.AxisInfo.DeviceName, wait.AxisInfo.GetAxisValue());
                    wait.IsEnd = wait.IoValue.Equals(value);
                }
                else if (wait.WaitType == 7)
                {
                }
                else if (wait.WaitType == 8)
                {
                    IO_VALUE value = (IO_VALUE)ACServerManager.GetLimitNegativeSingle(wait.AxisInfo.DeviceName, wait.AxisInfo.GetAxisValue());
                    wait.IsEnd = wait.IoValue.Equals(value);
                }
                else if (wait.WaitType == 9)
                {
                    IO_VALUE value = (IO_VALUE)ACServerManager.GetLimitPositiveSingle(wait.AxisInfo.DeviceName, wait.AxisInfo.GetAxisValue());
                    wait.IsEnd = wait.IoValue.Equals(value);
                }
                else if (wait.WaitType == 10)
                {
                    wait.IsEnd = AutoAxisIsEnd(wait);
                }
                else if (wait.WaitType == 11)
                {
                    wait.IsEnd = !String.IsNullOrEmpty(LastCode);
                }
                if (wait.IsEnd)
                {
                    if (StoreMove.OneWaitCanEndStep)
                    {
                        isOk = true;
                        break;
                    }
                }
                else
                {
                    if (!StoreMove.OneWaitCanEndStep)
                    {
                        isOk = false;
                        break;
                    }
                }
            }
            if (isOk)
            {
                StoreMove.EndStepWait();
            }
            else if (span.TotalSeconds > StoreMove.TimeOutSeconds)
            {
                if (NotOkMsg.Equals(""))
                {
                    WarnMsg = Name + "等待超时【" + StoreMove.MoveType + "】【" + StoreMove.MoveStep + "】已等待【" + Math.Round(span.TotalSeconds) + "】秒";
                    foreach (WaitResultInfo wait in list)
                    {
                        WarnMsg = WarnMsg + "\r\n" + wait.ToStr();
                    }
                    LogUtil.error(WarnMsg);
                }
                else
                {
                    WarnMsg = Name + "【" + StoreMove.MoveType + "】【" + StoreMove.MoveStep + "】等待超时  [" + NotOkMsg
                        + "]已等待[" + Math.Round(span.TotalSeconds, 1) + "]秒";
                    LogUtil.error(WarnMsg);
                }
                StoreManager.Store.Alarm(StoreAlarmType.IoSingleTimeOut, "", WarnMsg, StoreMove.MoveType);
            }
        }

        private static bool AxisMoveISEnd(WaitResultInfo wait)
        {
            string msg = "";
            if (wait.IsHomeMove)
            {
                wait.IsEnd = ACHomeMoveIsEnd(wait.AxisInfo, out msg);
            }
            if (!wait.IsEnd)
            {
                IO_VALUE value = (IO_VALUE)ACServerManager.GetHomeSingle(wait.AxisInfo.DeviceName, wait.AxisInfo.GetAxisValue());
                if (value.Equals(IO_VALUE.HIGH))
                {
                    ACServerManager.SuddenStop(wait.AxisInfo.DeviceName, wait.AxisInfo.GetAxisValue());
                    LogUtil.error(Name + wait.ToStr() + "原点返回过程中到达 负限位,直接停止运动");
                    StoreManager.Store.Alarm(StoreAlarmType.AxisMoveError, StoreManager.Store.GetAlarmCodeByAxis(wait.AxisInfo).ToString(), WarnMsg, StoreMove.MoveType);
                }
            }
            if (!msg.Equals(""))
            { 
                WarnMsg = msg;
                StoreManager.Store.Alarm(StoreAlarmType.AxisMoveError, StoreManager.Store.GetAlarmCodeByAxis(wait.AxisInfo).ToString(), WarnMsg, StoreMove.MoveType); 
            }
            return wait.IsEnd;
        }

        private static bool AutoAxisIsEnd(WaitResultInfo wait)
        {
            bool result = false;
            if (KND.IOValue(IO_Type.TrayCheck_LoadMaterial).Equals(IO_VALUE.HIGH))
            {
                result = true;
            }
            else
            {
                IO_VALUE LimitN = (IO_VALUE)ACServerManager.GetLimitNegativeSingle(wait.AxisInfo.DeviceName, wait.AxisInfo.GetAxisValue());
                if (LimitN.Equals(IO_VALUE.HIGH))
                {
                    result = true;
                }
            }
            if (result)
            {
                AutoAxisIsMove = 0;
                ACServerManager.SuddenStop(wait.AxisInfo.DeviceName, wait.AxisInfo.GetAxisValue());
            }
            return result;
        }

        public static  int GetSize()
        {
            if (KND.IOValue(IO_Type.WidthCheck1).Equals(IO_VALUE.HIGH))
            {
                if (KND.IOValue(IO_Type.WidthCheck2).Equals(IO_VALUE.HIGH))
                {
                    return 13;
                }
                else
                {
                    return 7;
                }
            } 
            return 0;
        }
         
        #region Halcon扫码枪代码
        public bool IsTestCamera = false;
        private static string spiltStr = "##";
        public static void GetCameraCode()
        {
            Task.Factory.StartNew(delegate ()
            { 
                KND.IOMove(IO_Type.CameraLight_Power, IO_VALUE.HIGH);
                string message = "";
                List<string> codeList = CodeManager.CameraScan();
                if (codeList.Count <= 0)
                {
                    codeList = CodeManager.CameraScan();
                }
                foreach (string str in codeList)
                {
                    message = message + "=1+0x0-" + LastSize + "x" + LastHeight + " =" + str + spiltStr;
                }
                LastCode = message;
                if (LastCode.Equals(""))
                {
                    LogUtil.error("未扫到二维码,请拿走料盘");
                    WarnMsg = "未扫到二维码,请拿走料盘";
                }
                KND.IOMove(IO_Type.CameraLight_Power, IO_VALUE.LOW);
                StoreManager.Store.onCodeReceived(message);
            }); 
        } 
        #endregion

        private static void CylinderMove(string highType, string lowType, bool isWait)
        {
            if (isWait)
            {
                StoreMove.WaitList.Add(WaitResultInfo.WaitIO(lowType, IO_VALUE.LOW));
                StoreMove.WaitList.Add(WaitResultInfo.WaitIO(highType, IO_VALUE.HIGH));
            }
            KND.IOMove(lowType, IO_VALUE.LOW);
            KND.IOMove(highType, IO_VALUE.HIGH);
        }
    }
}