ElectricGripper.cs 3.2 KB
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OnlineStore.DeviceLibrary
{
    public class ElectricGripper
    {
        private int subType = 0;
        private Neotel.Rmaxis axis;
        public Enum GripperType = GripperTypeE.None;
        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 void Clamp(StoreMoveInfo moveInfo = null)
        {
            GripperType = GripperTypeE.Gripper;
            if (!IsBusy)
            {
                axis.Push(30, 7, 500);
                if (moveInfo!=null)
                    moveInfo.WaitList.Add(WaitResultInfo.WaitTime(100));
            }
            if (moveInfo != null)
                moveInfo.WaitList.Add(WaitResultInfo.WaitAction(new Func<WaitResultInfo, bool>(WaitAction)));
        }
        public bool Release(StoreMoveInfo moveInfo = null)
        {
            GripperType = GripperTypeE.Release;
            if (!IsBusy)
            {
                axis.GoHome();
                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)));
                return false;
            }
        }
        public void HomeReset(StoreMoveInfo moveInfo = null) {
            GripperType = GripperTypeE.Reset;
            //if (!IsBusy)
            //{
                axis.ResetError();
                axis.GoHome();
           // }
            //if (moveInfo != null)
            //    moveInfo.WaitList.Add(WaitResultInfo.WaitAction(new Func<WaitResultInfo, bool>(WaitAction)));
        }
        public bool IsBusy {
            get {
                return !axis.IsReached;
            }
        }
        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
                {
                    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
        }
    }
}