RobotEquip.cs 15.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Concurrent;
using JAKA;
using System.Threading.Tasks;

namespace OnlineStore.DeviceLibrary
{
    public partial class RobotEquip : EquipBase
    {

        /// <summary>
        /// 行走机构
        /// </summary>
        public AxisBean MoveAxis = null;
        /// <summary>
        /// 压紧轴
        /// </summary>
        public AxisBean CompressAxis = null;
        /// <summary>
        /// 轴列表
        /// </summary>
        public AxisBean[] axisArray;
        public JAKAServer JAKAServer;
        public Client_Config Config;
        public InOutPosInfo lastPosInfo;
        public string executeTime = "";
        public DeviceStatus deviceStatus = DeviceStatus.ResetMove;
        public string CID = "";
        /// <summary>
        /// 等待出库的队列
        /// </summary>
        public ConcurrentQueue<InOutParam> waitOutStoreList = new ConcurrentQueue<InOutParam>();
        public RobotEquip(Client_Config config, int id)
        {
            baseConfig = config;
            this.Config = config;
            this.DeviceID = id;
            CID = config.CID;
            IsDebug = config.IsDebug.Equals(1);
            Name = (" " + "机器人" + " ").ToUpper();
            Init();
            InitConnectServerTimer();
            ledProcessTimer.Elapsed += LedProcess;
            IoCheckTimer.Elapsed += IoCheckTimerProcess;
            MoveInfo = new DeviceMoveInfo(" 机器人 ");
            InitAllAxis();
            lastPosInfo = new InOutPosInfo();
            LoadCameraConfig();
            string[] clientIps = new string[] { config.Robot_IP };
            JAKAServer = new JAKAServer(clientIps);
            StartRobotService();
        }
        public void StartRobotService()
        {
            Task.Factory.StartNew(delegate
            {
                JAKAServer.Start();
                PowerOn(true);
                System.Threading.Thread.Sleep(2000);
                EnableRobot(true);
                System.Threading.Thread.Sleep(2000);
            });

        }
        public void StopRobotService()
        {
            try
            {
                Task.Factory.StartNew(delegate
                {
                    EnableRobot(false);
                    System.Threading.Thread.Sleep(2000);
                    PowerOn(false);
                    JAKAServer.Stop();
                });
            }
            catch (Exception e)
            {
                LogUtil.error("StopRobotService 出错", e);
            }

        }
        internal void InitAllAxis()
        {
            MoveAxis = new AxisBean(Config.MoveAxis, Name);
            CompressAxis = new AxisBean(Config.CompressAxis, Name);
            axisArray = new AxisBean[] {
                MoveAxis,CompressAxis
            };
        }

        public override bool StartRun()
        {
            if (CanStartRun().Equals(false))
            {
                return false;
            }
            bool rtn = false;
            Task task = Task.Factory.StartNew(new Action(() => { rtn = OpenAllAxis(); }));
            task.Wait();
            if (!rtn)
            {
                CloseAllAxis();
                return false;
            }
            SetAllTimer(false);
            SetConnectServerTimer(false);
            MoveInfo.EndMove();
            SetBoxStatus(DeviceStatus.ResetMove, RunStatus.HomeMoving);
            MoveInfo.NewMove(MoveType.RHome);
            LogInfo("开始 原点返回:");
            StartReset();
            SetConnectServerTimer(true);
            SetAllTimer(true);
            return true;
        }

        public override bool Reset()
        {
            StopMove();
            bool rtn = false;
            Task task = Task.Factory.StartNew(new Action(() => { rtn = OpenAllAxis(); }));
            task.Wait();
            if (!rtn)
            {
                CloseAllAxis();
                return false;
            }
            mainTimer.Enabled = false;
            SetBoxStatus(DeviceStatus.ResetMove, RunStatus.Reset);
            MoveInfo.NewMove(MoveType.Reset);

            LogInfo("开始重置:  ");
            StartReset();
            mainTimer.Enabled = true;
            return true;
        }


        #region 复位处理

        private void StartReset()
        {
            ClearWarnMsg();
            alarmType = AlarmType.None;
            isInSuddenDown = false;
            isNoAirpressure_Check = false;
            MoveInfo.NextMoveStep(StepEnum.Robot_Reset_01_Wait);
            LogInfo($"复位 {MoveInfo.SLog} :轴开始复位");
            StartProg(true);
            curcmd = "";
            MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(2000));
            isInPro = false;
        }

        protected override void ResetProcess()
        {
            if (MoveInfo.MoveType.Equals(MoveType.Reset) || MoveInfo.MoveType.Equals(MoveType.RHome))
            {
                MoveResetPro();
            }
        }
        static int axisDiff = 20000;
        private bool CheckTongsPos()
        {
            return false;
        }
        private void MoveResetPro()
        {
            if (MoveInfo.IsInWait)
            {
                CheckWait(MoveInfo);
            }
            if (MoveInfo.IsInWait)
            {
                return;
            }
            switch (MoveInfo.MoveStep)
            {
                case StepEnum.Robot_Reset_01_Wait:
                    NextMoveStep(StepEnum.Robot_Reset_02_CheckArmState, "检查机器人是否在原点");
                    break;
                case StepEnum.Robot_Reset_02_CheckArmState:
                    NextMoveStep(StepEnum.Robot_Reset_03_ArmToP1, "机器人回原点");
                    SendCmd(GetCmd_Home());
                    break;
                case StepEnum.Robot_Reset_03_ArmToP1:
                    NextMoveStep(StepEnum.Robot_Reset_04_CheckTray, "检查机械臂是否有料盘");
                    break;
                case StepEnum.Robot_Reset_04_CheckTray:
                    if (BufferDataManager.TrayInRobotInfo == null)
                    {
                        NextMoveStep(StepEnum.Robot_Reset_05_AllAxisToP1, "压紧轴无料盘,行走机构和压紧轴回待机点");
                        MoveAxis.AbsMove(MoveInfo, Config.MoveAxis_P1, Config.MoveAxis_P1_Speed);
                        CompressAxis.AbsMove(MoveInfo, Config.CompressAxis_P1, Config.CompressAxis_P1_Speed);
                    }
                    else
                    {
                        NextMoveStep(StepEnum.Robot_Reset_06_MoveAxisToP1, "压紧轴有料盘,行走机构回待机点");
                        MoveAxis.AbsMove(MoveInfo, Config.MoveAxis_P1, Config.MoveAxis_P1_Speed);
                    }
                    break;
                case StepEnum.Robot_Reset_05_AllAxisToP1:
                    NextMoveStep(StepEnum.Robot_Reset_08_Finish, "复位完成");
                    break;
                case StepEnum.Robot_Reset_06_MoveAxisToP1:
                    NextMoveStep(StepEnum.Robot_Reset_07_ClearCompress, "将压紧轴上的料盘送到指定位置");
                    break;
                case StepEnum.Robot_Reset_07_ClearCompress:

                    if (BufferDataManager.TrayInRobotInfo != null)
                    {
                        MoveInfo.EndMove();
                        runStatus = RunStatus.Runing;
                        BufferDataManager.TrayInRobotInfo = null;
                    }
                    else
                    {
                        NextMoveStep(StepEnum.Robot_Reset_08_Finish, "复位完成");
                    }
                    break;
                case StepEnum.Robot_Reset_08_Finish:
                    MoveInfo.EndMove();
                    runStatus = RunStatus.Runing;
                    break;
            }
        }


        #endregion
        /// <summary>
        /// 是否在安全位置
        /// </summary>
        /// <returns></returns>
        public bool IsInSafePosition()
        {
            return true;
        }
        private void NextMoveStep(StepEnum step, string msg)
        {
            MoveInfo.NextMoveStep(step);
            LogInfo($" {MoveInfo.SLog}:{msg}");
        }
        private string preWriteStr = "";
        private string curWriteStr = "";
        void LogInOutErrInfo(string msg)
        {
            curWriteStr = msg;
            if (curWriteStr != preWriteStr)
            {
                preWriteStr = curWriteStr;
                LogUtil.error(curWriteStr);
            }

        }
        int SigLastTime = 2;//5秒
        protected override void OnTimerProcess()
        {
            if (!runStatus.Equals(RunStatus.Runing))
                return;
        }
        private void CheckInStoreProcess()
        {
            if (StoreManager.Client.inletEquip.UpdownIsUp() && BufferDataManager.TrayInInletInfo != null)
            {
                LogInfo("机器人执行入库【" + BufferDataManager.TrayInInletInfo.ToStr() + "】");
                StartInstore(new InOutParam(BufferDataManager.TrayInInletInfo));
            }
        }
        private void ExecuteOutListProcess()
        {
            try
            {
                if (waitOutStoreList.Count > 0)
                {
                    InOutParam param = null;
                    bool result = waitOutStoreList.TryDequeue(out param);
                    if (result && param != null)
                    {
                        LogInfo("执行排队的出库【" + param.PosInfo.ToStr() + "】");
                        StartExecuctOut(param);
                        return;
                    }

                }
            }
            catch (Exception ex)
            {
                LogUtil.error(Name + "处理出库排队列表出错:" + ex.ToString());
            }
        }
        protected override void BaseTimerProcess()
        {
            if (isInSuddenDown || isNoAirpressure_Check)
            {
                return;
            }
            SecurityAccessProcess();
            NGBoxProcess();
            if (runStatus.Equals(RunStatus.Runing))
            {
                ////ExecuteOutListProcess();
                if (StoreManager.IsConnectServer)
                {
                    GetRobotTask();
                }
                CheckInStoreProcess();
            }
            BusyMoveProcess();
            IOTimeOutProcess();
            if (NoErrorAlarm())
            {
                CheckAxisAlarm();
            }
        }
        protected override void IOTimeOutProcess()
        {
            try
            {
                TimeSpan span = DateTime.Now - preIoTimerOutTime;
                if (span.TotalSeconds > 3 && alarmType.Equals(AlarmType.IoSingleTimeOut))
                {
                    preIoTimerOutTime = DateTime.Now;

                    if (runStatus < RunStatus.Runing || isInSuddenDown || isNoAirpressure_Check)
                    {
                        return;
                    }
                    ////若BOX和出料都没有在等待Io的过程中则此Io超时异常可能已经处理过
                    if (MoveInfo.IsInWait.Equals(false) && NoErrorAlarm())
                    {
                        LogUtil.info(Name + "清理信号超时报警【" + WarnMsg + "】 ");
                        alarmType = AlarmType.None;
                        ClearWarnMsg();
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("IOTimeOutProcess出错:", ex);
            }
        }
        internal override void StopMove()
        {

            MoveInfo.EndMove();
            MoveAxis.SuddenStop();
            CompressAxis.SuddenStop();
            StartProg(false);
            CloseAllAxis();
            curcmd = "";
        }

        public override void StopRun()
        {
            SetConnectServerTimer(false);
            SetAllTimer(false);
            StopMove();
            SetBoxStatus(DeviceStatus.None, RunStatus.Wait);
        }

        public override string GetMoveStr()
        {
            string msg = "";
            int tLength = 15;

            msg += "runStatus:  " + runStatus + "\n";
            msg += "runStep:  " + MoveInfo.MoveStep + "\n";
            msg += "alarm:  " + alarmType + "\n";



            return msg;
        }

        #region 轴运动

        private DateTime checkAlarmTime = DateTime.Now;
        public void CheckAxisAlarm()
        {
            if (alarmType.Equals(AlarmType.AxisAlarm) || alarmType.Equals(AlarmType.AxisMoveError))
            {
                return;
            }
            TimeSpan span = DateTime.Now - checkAlarmTime;
            //在回原点,复位,出入库时,检测报警间隔减小
            if (MoveInfo.MoveType.Equals(MoveType.None))
            {
                if (span.TotalSeconds < 5)
                {
                    return;
                }
            }
            else if (span.TotalSeconds < 1)
            {
                return;
            }

            checkAlarmTime = DateTime.Now;
            foreach (AxisBean axisInfo in axisArray)
            {
                if (AxisIsAlarm(axisInfo))
                {
                    return;
                }
            }
        }


        public bool OpenAllAxis(bool isCheck = true)
        {
            foreach (AxisBean axis in axisArray)
            {
                string outMsg = "";
                if (!axis.Open(true, out outMsg))
                {
                    LogUtil.info(outMsg);
                    return false;
                }
            }
            return true;
        }

        public void CloseAllAxis()
        {
            foreach (AxisBean axis in axisArray)
            {
                axis.SuddenStop();
                axis.ServoOff();
            }
        }


        #endregion

        #region NG
        bool preNgSig = false;
        bool curNgSig = false;
        AlarmType preAlram = AlarmType.None;
        void NGBoxProcess()
        {
            preNgSig = curNgSig;
            curNgSig = IOValue(IO_Type.NGBox_TrayCheck).Equals(IO_VALUE.HIGH);
            if (curNgSig == true && preNgSig == false)
            {
                //BufferDataManager.AddTrayInNGBox(new InOutPosInfo());
            }
            if (NGBoxAlarm())
            {
                SetWarnMsg($"NG箱中的料盘需要处理,数量大于{validityCnt}或者料盘放置时间大于{validityPeriod}分钟");
                if (alarmType <= AlarmType.AxisAlarm)
                {
                    preAlram = alarmType;
                    Alarm(AlarmType.NGBoxWarn);
                }
            }
            else
            {
                ClearSpecifiedAlarm($"NG箱中的料盘需要处理,数量大于");
                if (isInSuddenDown || isNoAirpressure_Check)
                {
                    return;
                }
                if (alarmType.Equals(AlarmType.NGBoxWarn))
                {
                    alarmType = preAlram;
                }
            }
        }
        #endregion
        #region 门禁光栅
        protected void SecurityAccessProcess()
        {
            try
            {
                if(!MoveStop)
                {
                    if (IOValue(IO_Type.SafetyLightGrids).Equals(IO_VALUE.LOW))
                    {
                        Pause();
                        LogUtil.info("安全光栅触发,停止行走机构和机器人运动");
                    }
                }


            }
            catch (Exception ex)
            {
                LogUtil.error(Name + " 出错 :" + ex.ToString());
            }

        }
        public void Pause()
        {
            MoveStop = true;
            CompressAxis.SuddenStop();
            MoveAxis.SuddenStop();
            RobotPause();
            MoveInfo.CanWhileCount = 5;
        }
        public void Resume()
        {
            MoveStop = false;
            RobotResume();
        }
        #endregion


    }
}