InputEquip_Robot.cs 11.6 KB
using JAKA;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static JAKA.JKTYPE;

namespace OnlineStore.DeviceLibrary
{
    /// <summary>
    /// 机器人
    /// </summary>
    partial class InputEquip
    {
        public JAKAServer RobotServer;
        string robotIp = "";
        void initRobot()
        {
            robotIp = ConfigAppSettings.GetValue("RobotIp", "192.168.104.21");
            RobotServer = new JAKAServer(new string[] { robotIp });
            RobotServer.Start();
        }
        /// <summary>
        /// 机器人移动成功
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        public bool RobotMoveEnd(string cmd)
        {
            JAKA.JAKABean bean;
            if (!GetRobotInfo(out bean))
            {
                return false;
            }
            if (!bean.RobotData.Online)
            {
                SetWarnMsg("无法读取机器人状态,因机器人离线");
                Alarm(AlarmType.RobotError);
                robotOnline = false;
                return false;
            }
            else
            {
                ClearSpecifiedAlarm("无法读取机器人状态,因机器人离线");
            }
            try
            {
                string[] recv = bean.RobotData.RecvMsg.Split(';');
                if (GetCurCmd().Equals(cmd) && recv != null && recv.Length == 2
                    && recv[0].Equals(cmd) && recv[1].Equals(JakaCmd.MoveDone)
                    && (bean.RobotData.RobotStatus.inpos == 1))
                {
                    LogUtil.info($"RobotMoveEnd:【{cmd}】");
                    return true;
                }
            }
            catch (Exception ex)
            {
                LogUtil.error($"RobotMoveEnd:【{cmd}】", ex);
                return false;
            }
            return false;
        }
        #region 点位命令
        void To入料口()
        {
            string cmd = JakaPoint.入料口.ToString();
            if (RobotServer.RobotCmds.ContainsKey(cmd))
            {
                SendCmd(cmd);
            }
        }
        void From入料口回原()
        {
            string cmd = $"{JakaPoint.入料口.ToString()}{JAKAServer._回原}";
            if (RobotServer.RobotCmds.ContainsKey(cmd))
            {
                SendCmd(cmd);
            }
        }
        void To打印机()
        {
            string cmd = JakaPoint.打印机.ToString();
            if (RobotServer.RobotCmds.ContainsKey(cmd))
            {
                SendCmd(cmd);
            }
        }
        void From打印机回原()
        {
            string cmd = $"{JakaPoint.打印机.ToString()}{JAKAServer._回原}";
            if (RobotServer.RobotCmds.ContainsKey(cmd))
            {
                SendCmd(cmd);
            }
        }
        void ToA上暂存区()
        {
            string cmd = JakaPoint.A上暂存区.ToString();
            if (RobotServer.RobotCmds.ContainsKey(cmd))
            {
                SendCmd(cmd);
            }
        }
        void FromA上暂存区回原()
        {
            string cmd = $"{JakaPoint.A上暂存区.ToString()}{JAKAServer._回原}";
            if (RobotServer.RobotCmds.ContainsKey(cmd))
            {
                SendCmd(cmd);
            }
        }
        void ToA下皮带末端(int round = 0)
        {
            string cmd = JakaPoint.A下皮带末端.ToString();
            if (RobotServer.RobotCmds.ContainsKey(cmd))
            {
                SendCmd(cmd, round);
            }
        }
        void FromA下皮带末端回原()
        {
            string cmd = $"{JakaPoint.A下皮带末端.ToString()}{JAKAServer._回原}";
            if (RobotServer.RobotCmds.ContainsKey(cmd))
            {
                SendCmd(cmd);
            }
        }
        void ToB上暂存区()
        {
            string cmd = JakaPoint.B上暂存区.ToString();
            if (RobotServer.RobotCmds.ContainsKey(cmd))
            {
                SendCmd(cmd);
            }
        }
        void FromB上暂存区回原()
        {
            string cmd = $"{JakaPoint.B上暂存区.ToString()}{JAKAServer._回原}";
            if (RobotServer.RobotCmds.ContainsKey(cmd))
            {
                SendCmd(cmd);
            }
        }
        void ToB下皮带末端(int round=0)
        {
            string cmd = JakaPoint.B下皮带末端.ToString();
            if (RobotServer.RobotCmds.ContainsKey(cmd))
            {
                SendCmd(cmd, round);
            }
        }
        void FromB下皮带末端回原()
        {
            string cmd = $"{JakaPoint.B下皮带末端.ToString()}{JAKAServer._回原}";
            if (RobotServer.RobotCmds.ContainsKey(cmd))
            {
                SendCmd(cmd);
            }
        }
        void AToA周转1_内侧()
        {
            string cmd = JakaPoint.A下到A周转箱1_内侧.ToString();
            if (RobotServer.RobotCmds.ContainsKey(cmd))
            {
                SendCmd(cmd);
            }
        }
        void BToA周转1_内侧()
        {
            string cmd = JakaPoint.B下到A周转箱1_内侧.ToString();
            if (RobotServer.RobotCmds.ContainsKey(cmd))
            {
                SendCmd(cmd);
            }
        }
        void FromA周转1_内侧回原()
        {
            string cmd = $"{JakaPoint.A下到A周转箱1_内侧.ToString()}{JAKAServer._回原}";
            if (RobotServer.RobotCmds.ContainsKey(cmd))
            {
                SendCmd(cmd);
            }
        }
        void AToA周转2_外侧()
        {
            string cmd = JakaPoint.A下到A周转箱2_外侧.ToString();
            if (RobotServer.RobotCmds.ContainsKey(cmd))
            {
                SendCmd(cmd);
            }
        }
        void BToA周转2_外侧()
        {
            string cmd = JakaPoint.B下到A周转箱2_外侧.ToString();
            if (RobotServer.RobotCmds.ContainsKey(cmd))
            {
                SendCmd(cmd);
            }
        }
        void FromA周转2_外侧回原()
        {
            string cmd = $"{JakaPoint.A下到A周转箱2_外侧.ToString()}{JAKAServer._回原}";
            if (RobotServer.RobotCmds.ContainsKey(cmd))
            {
                SendCmd(cmd);
            }
        }

        void AToB周转1_内侧()
        {
            string cmd = JakaPoint.A下到B周转箱1_内侧.ToString();
            if (RobotServer.RobotCmds.ContainsKey(cmd))
            {
                SendCmd(cmd);
            }
        }
        void BToB周转1_内侧()
        {
            string cmd = JakaPoint.B下到B周转箱1_内侧.ToString();
            if (RobotServer.RobotCmds.ContainsKey(cmd))
            {
                SendCmd(cmd);
            }
        }
        void FromB周转1_内侧回原()
        {
            string cmd = $"{JakaPoint.A下到B周转箱1_内侧.ToString()}{JAKAServer._回原}";
            if (RobotServer.RobotCmds.ContainsKey(cmd))
            {
                SendCmd(cmd);
            }
        }
        void AToB周转2_外侧()
        {
            string cmd = JakaPoint.A下到B周转箱2_外侧.ToString();
            if (RobotServer.RobotCmds.ContainsKey(cmd))
            {
                SendCmd(cmd);
            }
        }
        void BToB周转2_外侧()
        {
            string cmd = JakaPoint.B下到B周转箱2_外侧.ToString();
            if (RobotServer.RobotCmds.ContainsKey(cmd))
            { 
                SendCmd(cmd);
            }
        }
        void FromB周转2_外侧回原()
        {
            string cmd = $"{JakaPoint.A下到B周转箱2_外侧.ToString()}{JAKAServer._回原}";
            if (RobotServer.RobotCmds.ContainsKey(cmd))
            {
                SendCmd(cmd);
            }
        }
        void ToNG()
        {
            string cmd = JakaPoint.NG.ToString();
            if (RobotServer.RobotCmds.ContainsKey(cmd))
            {
                SendCmd(cmd);
            }
        }
        void To紧急料()
        {
            string cmd = JakaPoint.到紧急料区域.ToString();
            if (RobotServer.RobotCmds.ContainsKey(cmd))
            {
                SendCmd(cmd);
            }
        }
        void BToNG()
        {
            string cmd = JakaPoint.B下到NG.ToString();
            if (RobotServer.RobotCmds.ContainsKey(cmd))
            {
                SendCmd(cmd);
            }
        }
        void BTo紧急料()
        {
            string cmd = JakaPoint.B下到紧急料区域.ToString();
            if (RobotServer.RobotCmds.ContainsKey(cmd))
            {
                SendCmd(cmd);
            }
        }
        void FromNG箱回原()
        {
            string cmd = $"{JakaPoint.到NG口.ToString()}{JAKAServer._回原}";
            if (RobotServer.RobotCmds.ContainsKey(cmd))
            {
                SendCmd(cmd);
            }
        }
        void From紧急料回原()
        {
            string cmd = $"{JakaPoint.到紧急料区域.ToString()}{JAKAServer._回原}";
            if (RobotServer.RobotCmds.ContainsKey(cmd))
            {
                SendCmd(cmd);
            }
        }
        #endregion
        #region 机器人
        public string curcmd = "";
        DateTime sendTime = DateTime.Now;
        bool robotOnline = false;
        /// <summary>
        /// 获取机器人当前命令
        /// </summary>
        /// <returns></returns>
        string GetCurCmd()
        {
            JAKA.JAKABean bean;
            if (!GetRobotInfo(out bean))
            {
                return "";
            }
            return bean.RobotData.CurCmd;
        }
        /// <summary>
        /// 机器人是否在安全位置
        /// </summary>
        /// <returns></returns>
        bool IsInSafePos()
        {
            JAKA.JAKABean bean;
            if (!GetRobotInfo(out bean))
            {
                return false;
            }
            if (bean?.GetDigitalOutput(IOType.IO_CABINET, 0, out bool insafe) ?? false)
                return insafe;
            return false;
        }
        /// <summary>
        /// 发送命令
        /// </summary>
        /// <param name="cmd"></param>
        void SendCmd(string cmd, int round = 0)
        {
            MoveInfo.WaitList.Add(WaitResultInfo.WaitRobotMove(cmd));
            JAKA.JAKABean bean;
            if (!GetRobotInfo(out bean))
            {
                return;
            }
            if (!bean.RobotData.Online)
            {
                SetWarnMsg($"无法发送命令,因机器人离线:{robotIp}");
                return;
            }
            else
            {
                ClearSpecifiedAlarm("无法发送命令,因机器人离线");
            }
            curcmd = cmd;
            RobotServer.SendCmd(bean.RemoteEndPoint, cmd, round);
            sendTime = DateTime.Now;
            LogUtil.info($"Send RobotMove Cmd:【{cmd}】【{round}】");
        }
        public bool GetRobotInfo(out JAKA.JAKABean bean)
        {
            bean = RobotServer.GetJAKABean(robotIp);
            if (bean == null)
            {
                SetWarnMsg($"无法获取机器人信息,因不存在的机器人:{robotIp}");
                return false;
            }
            return true;
        }
        /// <summary>
        /// 机器人运动中止
        /// </summary>
        public void MotionAbort()
        {
            if (GetRobotInfo(out JAKA.JAKABean bean))
            {
                bean.MotionAbort();
            }
        }
        #endregion
    }
}