LiftMonitor.cs 4.4 KB
using OnlineStore;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DeviceLibrary
{
    public class LiftMonitor
    {
        string up;
        string down;
        string axisbreak;
        AxisBean axisBean;
        int upspeed;
        int downspeed;
        int StrokeLength = 270000;
        public LiftMonitor(string _up, string _down,string _break, AxisBean _axisBean,int _upspeed, int _downspeed= 0) {
            up = _up;
            down = _down;
            axisBean = _axisBean;
            upspeed = _upspeed;
            axisbreak = _break;
            downspeed = _downspeed;
            if (downspeed == 0) {
                downspeed = upspeed;
            }
        }
        public bool isAtTOP {
            get {
                return IOManager.IOValue(up.ToString()).Equals(IO_VALUE.HIGH);
            }
        }
        public bool isAtBOTTOM
        {
            get
            {
                return IOManager.IOValue(down.ToString()).Equals(IO_VALUE.HIGH);
            }
        }
        public void LiftUp(MoveInfo moveInfo) {

            if (moveInfo == null)
                moveInfo = new MoveInfo("Motor",false);
            if (IOManager.IOValue(up.ToString()).Equals(IO_VALUE.HIGH)) {
                moveInfo.log($"{axisBean.AxisName},已在位置,无需上升");
                return;
            }
            IOManager.IOMove(axisbreak,IO_VALUE.HIGH);
            Thread.Sleep(200);
            axisBean.RelMove(StrokeLength, (double)upspeed);
            DateTime d = DateTime.Now;
            moveInfo.log($"{axisBean.AxisName},LiftUp");
            if(moveInfo!=null)
                 moveInfo.WaitList.Add(WaitResultInfo.WaitAction(new Func<WaitResultInfo, bool>(WaitUp), $"等待顶升[{axisBean.Config.Explain}]机构上升"));
            Task.Run(()=> {
                while (!IOManager.IOValue(up.ToString()).Equals(IO_VALUE.HIGH)) {
                    Task.Delay(30);
                }
                axisBean.SuddenStop();
                IOManager.IOMove(axisbreak, IO_VALUE.LOW);
                var t = (DateTime.Now - d).TotalSeconds;
                moveInfo.log($"{axisBean.AxisName},上升到位,s:{t}");
            });
        }
        bool WaitUp(WaitResultInfo w)
        {
            if (IOManager.IOValue(up.ToString()).Equals(IO_VALUE.HIGH))
            {
                axisBean.SuddenStop();
                IOManager.IOMove(axisbreak, IO_VALUE.LOW);
                return true; 
            }

            if (!axisBean.IsBusy) {
                axisBean.RelMove(StrokeLength, (double)upspeed);
            }
            return false;
        }
        public void LiftDown(MoveInfo moveInfo)
        {
            if (moveInfo == null)
                moveInfo = new MoveInfo("Motor", false);
            if (IOManager.IOValue(down.ToString()).Equals(IO_VALUE.HIGH))
            {
                moveInfo.log($"{axisBean.AxisName},已在位置,无需下降");
                return;
            }
            IOManager.IOMove(axisbreak, IO_VALUE.HIGH);
            Thread.Sleep(200);
            axisBean.RelMove(-StrokeLength, (double)downspeed);
            DateTime d = DateTime.Now;
            moveInfo.log($"{axisBean.AxisName},LiftDown");
            if (moveInfo != null)
                moveInfo.WaitList.Add(WaitResultInfo.WaitAction(new Func<WaitResultInfo, bool>(WaitDown), $"等待顶升[{axisBean.Config.Explain}]机构下降"));

            Task.Run(() => {
                while (!IOManager.IOValue(down.ToString()).Equals(IO_VALUE.HIGH))
                {
                    Task.Delay(30);
                }
                axisBean.SuddenStop();
                IOManager.IOMove(axisbreak, IO_VALUE.LOW);
                var t = (DateTime.Now - d).TotalSeconds;
                moveInfo.log($"{axisBean.AxisName},下降到位,s:{t}");
            });
        }
        bool WaitDown(WaitResultInfo w)
        {
            if (IOManager.IOValue(down.ToString()).Equals(IO_VALUE.HIGH))
            {
                axisBean.SuddenStop();
                IOManager.IOMove(axisbreak, IO_VALUE.LOW);
                return true;
            }

            if (!axisBean.IsBusy)
            {
                axisBean.RelMove(-StrokeLength, (double)downspeed);
            }
            return false;
        }
    }
}