ElectricGripper.cs 5.1 KB
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DeviceLibrary
{
    public class ElectricGripper
    {
        private Neotel.Rmaxis axis;
        private string Port = "";
        public Enum GripperType = GripperTypeE.None;

        int clampTimes = 0;
        public ElectricGripper(string port) {
            Port = port;
        }

        public bool OpenPort()
        {
            if(axis==null)
                axis = new Neotel.Rmaxis();
            if (axis.IsPortOpen)
                return true;
            bool rtn = axis.OpenPort(Port);
            //var s = "版本号\r\n" + axis.GetVersion();
            return rtn;
        }
        public void ClosePort()
        {
            axis.ClosePort();
        }
        public bool Clamp(MoveInfo moveInfo = null)
        {
            OpenPort();
            GripperType = GripperTypeE.Gripper;
            
            if (moveInfo != null)
                moveInfo.WaitList.Add(WaitResultInfo.WaitAction(new Func<WaitResultInfo, bool>(WaitAction),"夹爪夹紧"));
            LogUtil.info($"ElectricGripper Clamp");
            if (axis.ErrorCode > 0)
            {
                LogUtil.info("ElectricGripper Clamp error:" + axis.ErrorCode.ToString() + axis.ErrorString);
                axis.ResetError();
            }
            if (IsBusy)
            {
                axis.StopAxis();
                Thread.Sleep(500);
            }
            if (!IsBusy)
            {
                axis.Push(50, 5.5f, 5);
                clampTimes++;
                if (moveInfo != null)
                    moveInfo.WaitList.Add(WaitResultInfo.WaitTime(100));

                return true;
            }
            else {
                return false;
            }
        }
        public bool Release(MoveInfo moveInfo = null)
        {
            OpenPort();
            GripperType = GripperTypeE.Release;
            LogUtil.info($"ElectricGripper Release");
            if (axis.ErrorCode > 0)
            {
                LogUtil.info("ElectricGripper Release error:" + axis.ErrorCode.ToString() + axis.ErrorString);
                axis.ResetError();
            }
            if (IsBusy)
            {
                axis.StopAxis();
                Thread.Sleep(500);
            }
            if (!IsBusy)
            {
                axis.MoveAbsolute(0.2f, 5, 15, 15, 1f);
                clampTimes = 0;
                if (moveInfo != null)
                    moveInfo.WaitList.Add(WaitResultInfo.WaitTime(100));
                return true;
            }
            else
            {
                if (axis.ErrorCode > 0)
                {
                    LogUtil.info($"ElectricGripper ErrCode:{axis.ErrorCode}");
                    axis.ResetError();
                    Thread.Sleep(100);
                }
                if (moveInfo != null)
                    moveInfo.WaitList.Add(WaitResultInfo.WaitAction(new Func<WaitResultInfo, bool>(WaitAction), "夹爪放松"));
                return false;
            }
        }
        public void HomeReset(MoveInfo moveInfo = null)
        {
            OpenPort();
            GripperType = GripperTypeE.Reset;
            //if (!IsBusy)
            //{
            axis.ResetError();
            axis.GoHome();
            clampTimes = 0;
            // }
            //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}");
                //return false;
                return !axis.IsReached;
            }
        }
        public int ErrorCode
        {
            get
            {
                return axis.ErrorCode;
            }
        }
        public bool IsClamp
        {
            get
            {
                LogUtil.OutputDebugString($"ElectricGripper IsPushEmpty:{axis.IsPushEmpty}");
                return !axis.IsPushEmpty;
            }
        }

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

            if (this.GripperType.Equals(ElectricGripper.GripperTypeE.Gripper))
            {
                if (this.IsClamp)
                    return true;
                else
                {
                    if (clampTimes<=2)
                        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
        }
    }
}