AutomaticBaiting.cs 18.7 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
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OnlineStore.DeviceLibrary
{
    public partial class AutomaticBaiting
    {
        /// <summary>
        /// 操作人员是否拿走料盘
        /// </summary>
        public static bool IsGetTrayGo = false;
        public static bool IsWaitTragGo = false;
        /// <summary>
        /// 0=未知。1=门打开,2=门关闭
        /// </summary>
        public static int DoorStatus = 2;
        private static int LastHeight = 0;
        private static int LastSize = 0;
        private static string LastCode = "";
        private static string LastPosId = "";
        public static int AxisChangeValue = ConfigAppSettings.GetIntValue(Setting_Init.AxisChangeValue);
        public static string WarnMsg = "";
        public static string CodeMsg = "";
        public static string Name = "批量上下料 ";
        public static StoreMoveInfo StoreMove = null;
        public static StoreRunStatus AutoBaitingStatus = StoreRunStatus.Wait;

        public static bool IsNeedStartInout = false;

        //记录没开门状态下,已经出库的数量
        public static int BatchOutStoreCount = 0;
        public static int BatchOutStoreHeight = 0;

        /// <summary>
        /// 初始化数据
        /// </summary>
        public static void Init()
        {
            StoreMove = new StoreMoveInfo(Name);
        }
        /// <summary>
        /// 定时处理
        /// </summary>
        public static void TimerProcess()
        {
            try
            {
                if (AutoBaitingStatus >= StoreRunStatus.Wait)
                {
                    if (StoreMove.MoveType.Equals(StoreMoveType.StoreReset))
                    {
                        ResetProcess();
                    }
                    else if (StoreMove.MoveType.Equals(StoreMoveType.InStore))
                    {
                        InStoreProcess();
                    }
                    else if (StoreMove.MoveType.Equals(StoreMoveType.OutStore))
                    {
                        //OutStoreProcess();
                    }
                }
            }catch(Exception ex)
            {
                LogUtil.error(Name+"出错:"+ex.StackTrace);
            }
        }

        /// <summary>
        /// 判断是否可以启动
        /// </summary>
        /// <returns></returns>
        public static string CanStart()
        {
            string msg = "";
            //需要判断门关闭
            if (!DoorIsClose())
            {
                msg = "上料机构门未关闭";
                return msg;
            }

            return msg;
        }

        private static void ClearInOutInfo()
        {
            BatchOutStoreCount = 0;
            BatchOutStoreHeight = 0;
            LastCode = "";
            LastPosId = "";
            LastHeight = 0;
            LastSize = 0;
        }


        public static void StopMove()
        {
            StoreMove.EndMove();
            ClearInOutInfo();
            KND.IOMove(IO_Type.SuckingDisc_Work, IO_VALUE.LOW);
            CylinderMove(IO_Type.SuckingDisc_Up, IO_Type.SuckingDisc_Down, false);
            BatchDoorClose(false);
        }
        public static bool Reset(bool isNeedInout)
        {
            AutomaticBaiting.IsNeedStartInout = isNeedInout;
            if (!StoreMove.MoveType.Equals(StoreMoveType.None))
            {
                StopMove();
            }
            string msg = CanStart();
            if (!String.IsNullOrEmpty(msg))
            {
                LogUtil.info(Name + "复位失败:" + msg);
                return false;
            }
            ClearInOutInfo();
            DoorStatus = 2;
            AutoBaitingStatus = StoreRunStatus.Reset;
            StoreMove.NewMove(StoreMoveType.StoreReset);
            StoreMove.NextMoveStep(StoreMoveStep.AUTO_R00_Start); 
            return true;
        }


        private static void ResetProcess()
        {

            if (StoreMove.IsInWait)
            {
                CheckWait();
            }
            if (StoreMove.IsInWait)
            {
                return;
            }
            switch (StoreMove.MoveStep)
            {
                case StoreMoveStep.AUTO_R00_Start:
                    StoreMove.NextMoveStep(StoreMoveStep.AUTO_R01_CloseDoor);
                    //关闭门,轴回原点,检测
                    BatchDoorClose(true);
                    KND.IOMove(IO_Type.SuckingDisc_Work, IO_VALUE.LOW);
                    CylinderMove(IO_Type.SuckingDisc_Up, IO_Type.SuckingDisc_Down, true);
                    LogUtil.info(Name + "复位中:关闭门锁");
                    break;
                case StoreMoveStep.AUTO_R01_CloseDoor:
                    StoreMove.NextMoveStep(StoreMoveStep.AUTO_R02_AutoAxisHome);
                    LogUtil.info(Name + "复位中:批量上下料轴原点返回");
                    StoreMove.TimeOutSeconds = 120;
                    ACAxisHomeMove(StoreManager.Config.Batch_Axis);
                    break;
                case StoreMoveStep.AUTO_R02_AutoAxisHome:
                    if (IsNeedStartInout)
                    {
                        StoreMove.NextMoveStep(StoreMoveStep.AUTO_R03_AutoAxisHome);
                        LogUtil.info(Name + "复位中:需要检测是否有料盘,批量轴匀速上升速度【" + StoreManager.Config.Batch_Axis.TargetSpeed + "】");
                        ACAxisSpeedMove(StoreManager.Config.Batch_Axis, StoreManager.Config.Batch_Axis.TargetSpeed);
                    }
                    else
                    {
                        LogUtil.info(Name + "复位完成");
                        StoreMove.EndMove();
                        AutoBaitingStatus = StoreRunStatus.Runing;
                        WarnMsg = "";
                        IsNeedStartInout = false;
                    }
                    break;
                case StoreMoveStep.AUTO_R03_AutoAxisHome:

                    if (KND.IOValue(IO_Type.TrayCheck_LoadMaterial).Equals(IO_VALUE.LOW))
                    //if (ACServerManager.GetLimitPositiveSingle(StoreManager.Config.Batch_Axis).Equals(1))
                    {
                        LogUtil.info(Name + "复位中: 没有料盘,不需要上料,上料轴回0点");
                        StoreMove.NextMoveStep(StoreMoveStep.AUTO_R04_AutoBack);
                        StoreMove.TimeOutSeconds = 120;
                        ACAxisHomeMove(StoreManager.Config.Batch_Axis);
                    }
                    else
                    {
                        LogUtil.info(Name + "复位完成,有料盘,需要开始自动入料");
                        StoreMove.EndMove();
                        AutoBaitingStatus = StoreRunStatus.Runing;
                        //TODO
                        StartInOut();
                    }
                    break;
                case StoreMoveStep.AUTO_R04_AutoBack:
                    LogUtil.info(Name + "复位完成");
                    StoreMove.EndMove();
                    AutoBaitingStatus = StoreRunStatus.Runing;
                    WarnMsg = "";
                    IsNeedStartInout = false;
                    break;
                default: break;
            }
        }

        public static bool StartInOut()
        {
            if ((!StoreManager.Store.storeRunStatus.Equals(StoreRunStatus.Runing)) ||
                (!StoreManager.Store.StoreMove.MoveType.Equals(StoreMoveType.None)))
            {
                LogUtil.error(Name + " 启动入料失败,料仓不在待机状态" + StoreManager.Store.storeRunStatus + ",MoveType" + StoreManager.Store.StoreMove.MoveType);
                return false;
            }
            if (AutoBaitingStatus == StoreRunStatus.Runing && StoreMove.MoveType.Equals(StoreMoveType.None))
            {
                AutoBaitingStatus = StoreRunStatus.Busy;
                StoreMove.NewMove(StoreMoveType.InStore);
                StoreMove.NextMoveStep(StoreMoveStep.AUTO_I01_Wait);
                LogUtil.info(Name + "启动入料");
                return true;
            }
            else
            {
                LogUtil.error(Name + " 启动入料失败, Status=" + AutoBaitingStatus + ",MoveType=" + StoreMove.MoveType);
                return false;
            }
        }
        private static int SuckingDisc_WorkCount = 0;
        private static void InStoreProcess()
        {

            if (StoreMove.IsInWait)
            {
                CheckWait();
            }
            if (StoreMove.IsInWait)
            {
                return;
            }
            if (StoreMove.MoveStep.Equals(StoreMoveStep.AUTO_I01_Wait))
            {
                StoreMove.NextMoveStep(StoreMoveStep.AUTO_I02_MoveToUp);
                if (KND.IOValue(IO_Type.TrayCheck_LoadMaterial).Equals(IO_VALUE.HIGH))
                {
                    LogUtil.info(Name + " 入料 ,已检测到料盘,轴不需要上升");
                }
                else
                {
                    LogUtil.info(Name + " 入料 ,轴上升到检测到料盘,速度【"+ StoreManager.Config.Batch_Axis.TargetSpeed + "】");
                    ACAxisSpeedMove(StoreManager.Config.Batch_Axis, StoreManager.Config.Batch_Axis.TargetSpeed);
                }
            }
            else if (StoreMove.MoveStep.Equals(StoreMoveStep.AUTO_I02_MoveToUp))
            {
                IsNeedScanCode();
            }
            else if (StoreMove.MoveStep.Equals(StoreMoveStep.AUTO_I03_ScanCode))
            {
                StoreMove.NextMoveStep(StoreMoveStep.AUTO_I04_SuckingDisc_Down);
                LogUtil.info(Name + "入料: 扫码结束,二维码【" + LastCode + "】 吸盘开始下降");
                CylinderMove(IO_Type.SuckingDisc_Down, IO_Type.SuckingDisc_Up, true);
            }
            else if (StoreMove.MoveStep.Equals(StoreMoveStep.AUTO_I04_SuckingDisc_Down))
            {
                SuckingDiscWork();
            }
            else if (StoreMove.MoveStep.Equals(StoreMoveStep.AUTO_I05_SuckingDisc_Work))
            {
                if (KND.IOValue(IO_Type.SuckingDisc_Air).Equals(IO_VALUE.HIGH))
                {
                    StoreMove.NextMoveStep(StoreMoveStep.AUTO_I06_SuckingDisc_Up);
                    LogUtil.info(Name + "入料: 吸盘上升,等待宽度到达" + StoreManager.Config.Default_TrayWidth);
                    CylinderMove(IO_Type.SuckingDisc_Up, IO_Type.SuckingDisc_Down, true);
                    if (StoreManager.Config.Default_TrayWidth.Equals(7))
                    {
                        StoreMove.WaitList.Add(WaitResultInfo.WaitIO(IO_Type.WidthCheck1, IO_VALUE.HIGH));
                        StoreMove.WaitList.Add(WaitResultInfo.WaitIO(IO_Type.WidthCheck2, IO_VALUE.LOW));
                    }
                    else if (StoreManager.Config.Default_TrayWidth.Equals(13))
                    {
                        StoreMove.WaitList.Add(WaitResultInfo.WaitIO(IO_Type.WidthCheck1, IO_VALUE.HIGH));
                        StoreMove.WaitList.Add(WaitResultInfo.WaitIO(IO_Type.WidthCheck2, IO_VALUE.HIGH));
                    }
                }
                else if (SuckingDisc_WorkCount < 3)
                {
                    SuckingDiscWork();
                }
                else
                {
                    //TODO 报警
                    WarnMsg = "吸盘已经尝试三次,仍然吸不到物料!";
                    StoreManager.Store.Alarm(StoreAlarmType.BatchAlarm, "吸盘吸料失败", WarnMsg, StoreMoveType.InStore);
                }
            }
            else if (StoreMove.MoveStep.Equals(StoreMoveStep.AUTO_I06_SuckingDisc_Up))
            {
                StoreMove.NextMoveStep(StoreMoveStep.AUTO_I07_BatchAxisUp);
                LogUtil.info(Name + "入料: 批量上下料轴上升速度【" + StoreManager.Config.Batch_Axis.TargetSpeed + "】");
                ACAxisSpeedMove(StoreManager.Config.Batch_Axis, StoreManager.Config.Batch_Axis.TargetSpeed);
            }
            else if (StoreMove.MoveStep.Equals(StoreMoveStep.AUTO_I07_BatchAxisUp))
            {
                if (StoreManager.Store.CanStarInOut())
                {
                    if (LastCode.Equals(""))
                    {
                        LogUtil.info(Name + "入料: 未扫到二维码,将料盘送出,等待料盘拿走");
                        SendTrayOut();
                    }
                    else
                    {
                        StoreMove.NextMoveStep(StoreMoveStep.AUTO_I08_GetPosId);
                        //计算高度
                        EndMovePosition = ACServerManager.GetActualtPosition(StoreManager.Config.Batch_Axis.DeviceName, StoreManager.Config.Batch_Axis.GetAxisValue());
                        LastHeight = Math.Abs(EndMovePosition - StartMovePosition) / AxisChangeValue;
                        LogUtil.info(Name + "入料: 计算高度:上升前位置【" + StartMovePosition + "】实时位置【" + EndMovePosition + "】,计算后高度【" + LastHeight + "】");
                        LastSize = StoreManager.Config.Default_TrayWidth;
                        LogUtil.info(Name + "入料: 从服务器获取入库PosId,尺寸:【" + LastSize + "*" + LastHeight + "】二维码【" + LastCode + "】");

                        GetInStorePosId(ProcessMsg());
                        StoreMove.WaitList.Add(WaitResultInfo.WaitTime(3000));
                    }
                }
            }
            else if (StoreMove.MoveStep.Equals(StoreMoveStep.AUTO_I08_GetPosId))
            {
                //判断是否开始入库
                if (StoreManager.Store.StoreMove.MoveType.Equals(StoreMoveType.InStore) || (!LastPosId.Equals("")))
                {
                    StoreMove.NextMoveStep(StoreMoveStep.AUTO_I09_WaitTrayLeave);
                    LogUtil.info(Name + "入料: 开始入料【" + LastPosId + "】,等待料盘拿走");
                    StoreMove.WaitList.Add(WaitResultInfo.WaitIO(IO_Type.SuckingDisc_Air, IO_VALUE.LOW));
                }
                else
                {
                    LogUtil.info(Name + "入料:  未获取到PosID或启动入库失败,将料盘送出,等待料盘拿走");
                    SendTrayOut();
                }
            }
            else if (StoreMove.MoveStep.Equals(StoreMoveStep.AUTO_I09_WaitTrayLeave))
            {
                if (StoreManager.Store.StoreMove.MoveType.Equals(StoreMoveType.None) ||
                   (StoreManager.Store.StoreMove.IsNeedInStore && StoreManager.Store.StoreMove.MoveStep >= StoreMoveStep.SI_09_MoveToBag))
                {
                    IsNeedScanCode();
                }
            }
            else if (StoreMove.MoveStep.Equals(StoreMoveStep.AUTO_I10_WaitTrayGo))
            {
                if (StoreManager.Store.CanStarInOut())
                {
                    IsNeedScanCode();
                }
            }
            else if (StoreMove.MoveStep.Equals(StoreMoveStep.AUTO_I11_BatchAxisHome))
            {
                //TODO 
                LogUtil.info(Name + "入料:已经没有料盘,批量上料结束");
                StoreMove.EndMove();
                AutoBaitingStatus = StoreRunStatus.Runing;
            }
            else
            {
                LogUtil.error(Name + "未找到步骤:" + StoreMove.MoveType);
            }
        }

        private static void SendTrayOut()
        {
            StoreMove.NextMoveStep(StoreMoveStep.AUTO_I10_WaitTrayGo);
            StoreMove.TimeOutSeconds = 120;
            string posId = StoreManager.Store.PositionNumList[0];
            StoreManager.Store.StartInStoreMove(new InOutStoreParam("", posId), true, false);
            StoreMove.WaitList.Add(WaitResultInfo.WaitStoreRuning());
        }

        private static void SuckingDiscWork()
        {
            KND.IOMove(IO_Type.SuckingDisc_Work, IO_VALUE.LOW);
            StoreMove.NextMoveStep(StoreMoveStep.AUTO_I05_SuckingDisc_Work);
            SuckingDisc_WorkCount++;
            LogUtil.info(Name + "入料: 吸盘开始第" + SuckingDisc_WorkCount + "次工作");
            KND.IOMove(IO_Type.SuckingDisc_Work, IO_VALUE.HIGH);
            StoreMove.WaitList.Add(WaitResultInfo.WaitTime(500));
            StoreMove.WaitList.Add(WaitResultInfo.WaitIO(IO_Type.SuckingDisc_Air, IO_VALUE.HIGH));
            StoreMove.OneWaitCanEndStep = true;
        }
        private static void IsNeedScanCode()
        {
            LastCode = "";
            LastHeight = 0;
            LastSize = 0;
            LastPosId = "";
            SuckingDisc_WorkCount = 0;
          //  if (ACServerManager.GetLimitPositiveSingle(StoreManager.Config.Batch_Axis).Equals(0))
            if (KND.IOValue(IO_Type.TrayCheck_LoadMaterial).Equals(IO_VALUE.HIGH))
            {
                StoreMove.NextMoveStep(StoreMoveStep.AUTO_I03_ScanCode);
                LogUtil.info(Name + "入料: 开始扫码,最多等待6000 ");
                StoreMove.OneWaitCanEndStep = true;
                StoreMove.WaitList.Add(WaitResultInfo.WaitCodeOK());
                StoreMove.WaitList.Add(WaitResultInfo.WaitTime(6000));
                GetCameraCode();
            }
            else
            {
                StoreMove.NextMoveStep(StoreMoveStep.AUTO_I11_BatchAxisHome);
                LogUtil.info(Name + "入料:已经没有料盘,批量上下轴回原点");
                StoreMove.TimeOutSeconds = 120;
                ACAxisHomeMove(StoreManager.Config.Batch_Axis); 
            }
        }
         

        public static bool DoorIsClose()
        {
            if (KND.IOValue(IO_Type.DoorClose_LoadMaterial).Equals(IO_VALUE.HIGH))
            {
                return true;
            }
            return false;
        }

        public static void BatchDoorOpen(bool isWait)
        {
            ClearInOutInfo();
            DoorStatus = 1;
            KND.IOMove(IO_Type.BatchDoor_Close, IO_VALUE.LOW);
            KND.IOMove(IO_Type.BatchDoor_Open, IO_VALUE.HIGH);
            if (isWait)
            {
                StoreMove.WaitList.Add(WaitResultInfo.WaitIO(IO_Type.BatchDoor_Close, IO_VALUE.LOW));
                StoreMove.WaitList.Add(WaitResultInfo.WaitIO(IO_Type.BatchDoor_Open, IO_VALUE.HIGH));
            }
        } 

        public static void BatchDoorClose(bool isWait)
        {
            ClearInOutInfo();
            DoorStatus = 2;
            if (isWait)
            {
                StoreMove.WaitList.Add(WaitResultInfo.WaitIO(IO_Type.BatchDoor_Open, IO_VALUE.LOW));
                StoreMove.WaitList.Add(WaitResultInfo.WaitIO(IO_Type.BatchDoor_Close, IO_VALUE.HIGH));
            }
            KND.IOMove(IO_Type.BatchDoor_Open, IO_VALUE.LOW);
            KND.IOMove(IO_Type.BatchDoor_Close, IO_VALUE.HIGH);
        }

    }
}