ElectricGripper.cs 4.6 KB
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 OnlineStore.DeviceLibrary
{
    public class ElectricGripper
    {
        private int subType = 0;
        public Neotel.Rmaxis axis;
        private int Force=50;
        public Enum GripperType = GripperTypeE.None;
        public ElectricGripper(int force){
            Force = force;
        }
        public bool OpenPort(string port)
        {
            axis = new Neotel.Rmaxis();
            bool rtn  = axis.OpenPort(port);
            //var s = "版本号\r\n" + axis.GetVersion();
            return rtn;
        }
        public void ClosePort() {
            axis.ClosePort();
        }
        public bool Clamp(StoreMoveInfo moveInfo = null,int plateW=7)
        {
            GripperType = GripperTypeE.Gripper;
            if (!IsBusy)
            {
                //if (plateW > 7)
                try
                {
                    var f= (float)ConfigAppSettings.GetNumValue("ClampOpen", 5);
                    axis.Push(Force, f, 15);
                }
                catch(Exception e) {
                    LogUtil.info(e.ToString());
                    return false;
                }
                if (moveInfo!=null)
                    moveInfo.WaitList.Add(WaitResultInfo.WaitTime(100));
                return true;
            }
            if (moveInfo != null)
                moveInfo.WaitList.Add(WaitResultInfo.WaitAction(new Func<WaitResultInfo, bool>(WaitAction)));
            return false;
        }
        public bool Release(StoreMoveInfo moveInfo = null)
        {
            GripperType = GripperTypeE.Release;
            if (!IsBusy)
            {
                //axis.StopAxis();
                //Thread.Sleep(500);
                axis.MoveAbsolute(0, 30, 500, 500, 0.1f);
                if (moveInfo != null)
                    moveInfo.WaitList.Add(WaitResultInfo.WaitTime(100));
                return true;
            }
            else
            {
                //axis.StopAxis();
                //Thread.Sleep(500);
                //axis.GoHome();
                if (moveInfo != null)
                    moveInfo.WaitList.Add(WaitResultInfo.WaitAction(new Func<WaitResultInfo, bool>(WaitAction)));
                return false;
            }
        }
        public void HomeReset(StoreMoveInfo moveInfo = null)
        {
            GripperType = GripperTypeE.Reset;
            //if (!IsBusy)
            //{
            axis.StopAxis();
            axis.ResetError();
            axis.GoHome();
            // }
            //if (moveInfo != null)
            //    moveInfo.WaitList.Add(WaitResultInfo.WaitAction(new Func<WaitResultInfo, bool>(WaitAction)));
        }
        public bool IsBusy {
            get {
                LogUtil.info($"ElectricGripper IsReached:{axis.IsReached},IsMoving:{axis.IsMoving},Pos:{axis.GetPosition()}");
                //return false;
                try
                {
                    return !axis.IsReached;
                }
                catch (Exception e)
                {
                    LogUtil.info(e.ToString());
                    return true;
                }
                
            }
        }
        public bool IsClamp {
            get
            {
                
                return !axis.IsPushEmpty;
            }
        }
        public bool IsPushEmpty { get => axis.IsPushEmpty; }
        public bool IsMoving { get => axis.IsMoving; }
        public bool IsReached { get => axis.IsReached; }
        public int ErrorCode { get => axis.ErrorCode; }

        bool WaitAction(WaitResultInfo w)
        {
            if (this.IsBusy)
                return false;

            if (this.GripperType.Equals(ElectricGripper.GripperTypeE.Gripper))
            {
                if (this.IsClamp)
                    return true;
                else
                {
                    this.Clamp();
                    return false; ;
                }
            }
            else if (this.GripperType.Equals(ElectricGripper.GripperTypeE.Release))
            {
                return this.Release();
            }
            else if (this.GripperType.Equals(ElectricGripper.GripperTypeE.Reset))
            {
                return this.IsBusy;
            }
            else {
                return true;
            }
        }
        public enum GripperTypeE
        {
            Gripper,
            Release,
            Reset,
            None
        }
    }
}