BoxAutoPoint.cs 12.8 KB
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OnlineStore.DeviceLibrary
{
    public class BoxAutoPoint
    { 
        private StoreMoveStep CurrStep;
        private BoxBean box = null;
        private System.Timers.Timer toolTimer = new System.Timers.Timer();
        public PToolInfo paramInfo = new PToolInfo();
        private string LogName = "";
        public BoxAutoPoint(BoxBean box)
        {
            toolTimer.Enabled = false;
            toolTimer.Interval = 500;
            toolTimer.AutoReset = true;
            toolTimer.Elapsed += ToolTimer_Elapsed;
            this.box = box;
            CurrStep = StoreMoveStep.Wait;
            LogName = box.Name + "校准点位 ";
        }

        public bool IsStop()
        {
            return !toolTimer.Enabled;
        }

        public void Start()
        {
            CurrStep=(StoreMoveStep.AP_01_InoutHome);
            LogUtil.info(LogName + CurrStep + "第" + paramInfo.CurrIndex + "列,旋转轴位置[" + paramInfo.GetCurrMiddleP() + "] :进出轴先原点返回");
            ConfigMoveAxis axis = box.Config.InOut_Axis;
            AxisManager.instance.HomeMove(axis.DeviceName, (short)axis.GetAxisValue(), axis.HomeHighSpeed, axis.HomeLowSpeed, axis.HomeAddSpeed);
            toolTimer.Interval = 500;
            toolTimer.Start();
        }
        private bool isInProcesss = false;

        private bool HomeIsEnd(ConfigMoveAxis moveAxis)
        {
            if (AxisManager.instance.IsHomeMoveEnd(moveAxis.DeviceName, moveAxis.GetAxisValue()))
            {
                //原点完成并且位置=0
                int outCount = AxisManager.instance.GetActualtPosition(moveAxis.DeviceName, moveAxis.GetAxisValue());
                int errorCount = Math.Abs(outCount);
                if (errorCount <= moveAxis.CanErrorCountMax)
                {
                    return true;
                }
            }
            return false;
        }
        private bool AbsMoveIsEnd(ConfigMoveAxis moveAxis, int targetP)
        {
            bool countError = false;
            return AxisManager.instance.AbsMoveIsEnd(moveAxis.DeviceName, moveAxis.GetAxisValue(), targetP, moveAxis.CanErrorCountMax, out countError);
        }
        private void ToolTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            TimeSpan span = DateTime.Now - paramInfo.LastTime;
            if (isInProcesss && span.TotalSeconds < 1)
            {
                return;
            }
            isInProcesss = true;

            int currIndex = paramInfo.CurrIndex;
            if (CurrStep.Equals(StoreMoveStep.AP_01_InoutHome))
            {
                if (HomeIsEnd(box.Config.InOut_Axis))
                {
                    CurrStep=(StoreMoveStep.AP_02_UpdownHome);

                    LogUtil.info(LogName + CurrStep + " "+ currIndex + ":升降轴原点返回");
                    ConfigMoveAxis axis = box.Config.UpDown_Axis;
                    AxisManager.instance.HomeMove(axis.DeviceName, (short)axis.GetAxisValue(), axis.HomeHighSpeed, axis.HomeLowSpeed, axis.HomeAddSpeed);
                }
            }
            else if (CurrStep.Equals(StoreMoveStep.AP_02_UpdownHome))
            {
                if (HomeIsEnd(box.Config.UpDown_Axis))
                {
                    CurrStep=(StoreMoveStep.AP_03_MiddleMove);

                    ConfigMoveAxis axis = box.Config.Middle_Axis;
                    int p = paramInfo.GetCurrMiddleP();
                    LogUtil.info(LogName + CurrStep + " " + currIndex + ":旋转轴移动到目标位置:" + p);
                    AxisManager.instance.AbsMove(axis.DeviceName, (short)axis.GetAxisValue(), p, box.Config.MiddleAxis_P1_Speed, axis.AddSpeed, axis.DelSpeed);
                }
            }
            else if (CurrStep.Equals(StoreMoveStep.AP_03_MiddleMove))
            {
                int p = paramInfo.GetCurrMiddleP();

                if (AbsMoveIsEnd(box.Config.Middle_Axis, p))
                {
                    CurrStep=(StoreMoveStep.AP_04_InoutToP);
                    ConfigMoveAxis iaxis = box.Config.InOut_Axis;

                    LogUtil.info(LogName + CurrStep + " " + currIndex + ":进出轴移动到目标位置:" + paramInfo.InoutTargetPosition);
                    AxisManager.instance.AbsMove(iaxis.DeviceName, (short)iaxis.GetAxisValue(), paramInfo.InoutTargetPosition, box.Config.InOutAxis_P1_Speed, iaxis.AddSpeed, iaxis.DelSpeed);
                }
            }
            else if (CurrStep.Equals(StoreMoveStep.AP_04_InoutToP))
            {
                if (AbsMoveIsEnd(box.Config.InOut_Axis, paramInfo.InoutTargetPosition))
                {
                    CurrStep=(StoreMoveStep.AP_05_UpdownMove);
                    ConfigMoveAxis axis = box.Config.UpDown_Axis;
                    LogUtil.info(LogName + CurrStep + " " + currIndex + ":升降轴移动到目标位置:" + paramInfo.UpdownTargetPosition);
                    AxisManager.instance.AbsMove(axis.DeviceName, (short)axis.GetAxisValue(), paramInfo.UpdownTargetPosition, paramInfo.UpdownSpeed, axis.AddSpeed, axis.DelSpeed);
                    toolTimer.Interval = 50;
                }
            }
            else if (CurrStep.Equals(StoreMoveStep.AP_05_UpdownMove))
            {
                try
                {
                    ConfigMoveAxis axis = box.Config.UpDown_Axis;
                    int moveS = AxisManager.instance.GetBusyStatus(axis.DeviceName, axis.GetAxisValue());
                    if (moveS.Equals(1))
                    {
                        IO_VALUE currValue = box.IOValue(IO_Type.CheckPos);
                        TimeSpan checkSpan = DateTime.Now - paramInfo.LastGetPTime;
                        //间隔两秒以上才有效
                        if (paramInfo.LastValue.Equals(IO_VALUE.LOW) && currValue.Equals(IO_VALUE.HIGH) && checkSpan.TotalSeconds > 2)
                        {
                            int currPos = AxisManager.instance.GetActualtPosition(axis.DeviceName, axis.GetAxisValue());
                            paramInfo.PositionList.Add(currPos);
                            int num = paramInfo.PositionList.Count;
                            int preValue = 0;
                            if (paramInfo.PositionList.Count > 1)
                            {
                                preValue = paramInfo.PositionList[num - 2];
                            }
                            LogUtil.info(LogName + CurrStep + " " + currIndex + "【" + num + "】【" + currPos + "】【" + Math.Abs(currPos - preValue) + "】");
                           
                            paramInfo.LastValue = IO_VALUE.HIGH;
                            paramInfo.LastGetPTime = DateTime.Now;
                        }

                        paramInfo.LastValue = currValue;
                    }
                    else
                    {
                        //TODO 下一个
                        SaveAndNext(true);
                    }
                }

                catch (Exception ex)
                {
                    LogUtil.error(LogName + "ToolTimer_Elapsed" + ex.ToString());
                }
            }
            isInProcesss = false;
        }

        private void SaveAndNext(bool needNext = true)
        {
            toolTimer.Stop();
            if (paramInfo.PositionList.Count > 0)
            {

                LogUtil.info(LogName + "当前第"+paramInfo.CurrIndex+"列运动结束,停止定时器,记录数据");
                List<string> strList = new List<string>();
                strList.Add("编号,旋转轴位置,标准位置,库位差值,升降轴库位出料前点P5,升降轴库位出料缓冲点P6,升降轴库位入料前点P3,升降轴库位入料缓冲点P4");
                int index = 1;
                foreach (int p in paramInfo.PositionList)
                {
                    int P3 = p + paramInfo.P3Offset;
                    int P4 = p + paramInfo.P4Offset;
                    int P5 = p + paramInfo.P5Offset;
                    int P6 = p + paramInfo.P6Offset;
                    int cha = 0;
                    if (index > 1)
                    {
                        cha = p - paramInfo.PositionList[index - 2];
                    }
                    string spilt = ",";
                    string resultstr = index + spilt + paramInfo.GetCurrMiddleP()+ spilt + p + spilt + cha+ spilt + P5 + spilt + P6 + spilt + P3 + spilt + P4 + "";
                    strList.Add(resultstr);
                    index++;
                }
                int count = paramInfo.CurrIndex + 1;
                string fileName = "position"+count+"_" + paramInfo.GetCurrMiddleP()+ ".csv";
                string filePath = paramInfo.FileTargetPath + fileName;
                try
                {
                    File.WriteAllLines(filePath, strList.ToArray(), Encoding.UTF8);
                    LogUtil.error(LogName + "保存位置到文件【" + filePath + "】成功");
                }
                catch (Exception ex)
                {
                    LogUtil.error(LogName + "保存位置到文件【" + filePath + "】出错:" + ex.ToString());
                }
            }
            else
            {
                LogUtil.info(LogName + "当前第" + paramInfo.CurrIndex + "列运动结束,停止定时器,暂无点位需要保存");
            }

            if (needNext)
            {
                int nextIndex = paramInfo.CurrIndex + 1;

                if (nextIndex < paramInfo.MiddlePositionList.Count)
                {
                    LogUtil.info(LogName + "开始下一列点位校准");
                    paramInfo.PositionList = new List<int>();
                    paramInfo.CurrIndex = nextIndex;
                    Start();
                }
                else
                {
                    LogUtil.info(LogName + "已经是最后一列,位置校准结束");
                }
            }
            //workMoveStatus(true);
        }
        public void StopMove()
        {
            SaveAndNext(false);
            ACServerManager.instance.SuddenStop(box.Config.UpDown_Axis.DeviceName, box.Config.UpDown_Axis.GetAxisValue());
            ACServerManager.instance.SuddenStop(box.Config.InOut_Axis.DeviceName, box.Config.InOut_Axis.GetAxisValue());
            ACServerManager.instance.SuddenStop(box.Config.Middle_Axis.DeviceName, box.Config.Middle_Axis.GetAxisValue());
        }
        public string CurrStr()
        {
            string str= "位置校准中:"+CurrStep + "\r\n" +
                "当前第" + (paramInfo.CurrIndex + 1) + "/" + paramInfo.MiddlePositionList.Count + "列,已校准库位" + paramInfo.PositionList.Count + "个";
            return str;
        }
    }
    public class PToolInfo
    {
        public int P3Offset = 0;
        public int P4Offset = 0;
        public int P5Offset = 0;
        public int P6Offset = 0;

        public int UpdownSpeed = 20;
        /// <summary>
        /// 升降轴开始位置
        /// </summary>
        public int UpdownStartPosition = 0;
        /// <summary>
        /// 升降轴目标位置
        /// </summary>
        public int UpdownTargetPosition = 0;
        /// <summary>
        /// 进出轴目标位置
        /// </summary>
        public int InoutTargetPosition = 0;

        /// <summary>
        /// 旋转轴位置
        /// </summary>
        public List<int> MiddlePositionList = new List<int>();

        /// <summary>
        /// 当前列
        /// </summary>
        public int CurrIndex = 0;

        public int GetCurrMiddleP()
        {
            return MiddlePositionList[CurrIndex];
        }

        public string FileTargetPath = "";

        public  List<int> PositionList = new List<int>();
        public IO_VALUE LastValue = IO_VALUE.LOW;
        public DateTime LastGetPTime = DateTime.Now;

        public DateTime LastTime = DateTime.Now;
        public string ParamStr()
        {
            string msgStr = "请确认以下对点位参数,点击“确定”按钮开始自动校准点位:\r\n";
            msgStr += "    进出轴前进位置:" + InoutTargetPosition + "\r\n";
            msgStr += "    升降轴目标位置:" + UpdownTargetPosition + ",速度:"+UpdownSpeed+"\r\n";
            msgStr += "    旋转轴位置列表:";

            foreach (int mP in MiddlePositionList)
            {
                msgStr += mP + ",";
            }

            if (msgStr.EndsWith(","))
            {
                msgStr = msgStr.Substring(0, msgStr.Length - 1);
            }
            msgStr += "\r\n";
            msgStr += "    点位偏移量:P3[" + P3Offset+"],P4["+P4Offset+"],P5["+P5Offset+"],P6["+P6Offset+"]\r\n";
            msgStr += "    文件保存位置:" + FileTargetPath;

            return msgStr;
        }
    }
}