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

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

        int clampTimes = 0;
        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()
        {
            try
            {
                axis.ClosePort();
            }
            catch { }
        }
        public bool Clamp(MoveInfo moveInfo = null)
        {
            GripperType = GripperTypeE.Gripper;
            if (moveInfo != null)
                moveInfo.WaitList.Add(WaitResultInfo.WaitAction(new Func<WaitResultInfo, bool>(WaitAction),crc.GetString("Res0192","夹爪夹紧")));
            if (!IsBusy)
            {
                axis.ResetError();
                axis.Push(ConfigHelper.Config.Get("ElectricGripperPushForce",40), 6f, 20);
                clampTimes++;
                if (moveInfo != null)
                    moveInfo.WaitList.Add(WaitResultInfo.WaitTime(100));

                return true;
            }
            else {
                if (axis.ErrorCode > 0) {
                    LogUtil.info($"ElectricGripper ErrorCode:{axis.ErrorCode}");
                    axis.ResetError();
                }
                return false;
            }
        }
        public bool Release(MoveInfo moveInfo = null)
        {
            GripperType = GripperTypeE.Release;
            HomeReset();
            return true;
            if (!IsBusy)
            {
                axis.StopAxis();
                axis.ResetError();
                axis.MoveAbsolute(0, 50, 100, 100, 0.5f);
                clampTimes = 0;
                if (moveInfo != null)
                    moveInfo.WaitList.Add(WaitResultInfo.WaitTime(100));
                return true;
            }
            else
            {
                if (moveInfo != null)
                    moveInfo.WaitList.Add(WaitResultInfo.WaitAction(new Func<WaitResultInfo, bool>(WaitAction), crc.GetString("Res0193","夹爪放松")));
                return false;
            }
        }
        public void HomeReset(MoveInfo moveInfo = null)
        {
            GripperType = GripperTypeE.Reset;
            //ClosePort();
            //OpenPort(ConfigHelper.Config.Get("ElectricGripperPort"));
            axis.StopAxis();
            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.IsMoving;
            }
        }
        public bool IsClamp
        {
            get
            {
                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
        }
    }
}