InletEquip_InStore.cs 18.0 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
using Asa;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

namespace OnlineStore.DeviceLibrary
{
    partial class InletEquip
    {
        /// <summary>
        /// 等待机器人在安全位置
        /// </summary>
        /// <param name="moveInfo"></param>
        /// <param name="wait"></param>
        /// <returns></returns>
        protected override bool CheckWaitResult(DeviceMoveInfo moveInfo, WaitResultInfo wait)
        {
            if (wait.WaitType.Equals(WaitEnum.W102_FeedScanCode))
            {
                if (LastCodeList.Count > 0)
                {
                    wait.IsEnd = true;
                }
            }
            else if (wait.WaitType.Equals(WaitEnum.W202_RobotInSafe))
            {
                if (StoreManager.Client.robotEquip.RobotInSafeAtInlet())
                {
                    wait.IsEnd = true;
                }
            }
            return wait.IsEnd;
        }
        private List<string> LastCodeList = new List<string>();
        #region 自动入料
        private DateTime startInTime = DateTime.Now;
        public override bool StartInstore(InOutParam param)
        {
            if (!NoAlarm())
            {
                LogInfo("报警中,无法开始自动入料:" + param.PosInfo.ToStr());
                return false;
            }

            if (MoveInfo.MoveType.Equals(MoveType.None))
            {
                runStatus = RunStatus.Busy;
                MoveInfo.NewMove(MoveType.InStore, param);
                startInTime = DateTime.Now;
                {
                    MoveInfo.NextMoveStep(StepEnum.Inlet_01_Wait);
                    MoveLog($"开始入料{MoveInfo.SLog}");
                }
                return true;
            }
            return false;
        }

        private void NextMoveStep(StepEnum step, string msg)
        {
            MoveInfo.NextMoveStep(step);
            MoveLog($" {MoveInfo.SLog}:{msg}");
        }
        InOutPosInfo lastPosInfo = new InOutPosInfo();
        Task getTrayInfoTask = null;
        Task getTargetInfoTask = null;
        protected override void InstoreProcess()
        {
            if (MoveInfo.IsInWait)
            {
                CheckWait(MoveInfo);
            }
            if (MoveInfo.IsInWait)
            {
                return;
            }

            if (MoveInfo.IsStep(StepEnum.Inlet_01_Wait))
            {
                NextMoveStep(StepEnum.Inlet_02_CheckTray, "检查是否有料盘");
            }
            else if (MoveInfo.IsStep(StepEnum.Inlet_02_CheckTray))
            {
                if (HasTrayInPosition())
                {
                    NextMoveStep(StepEnum.Inlet_05_GetTrayInfo, "有料盘,扫码,准备获取料盘信息");
                    AllowFeedIn(false);
                    Inlet_05_GetTrayInfo();
                }
                else//无料盘
                {
                    NextMoveStep(StepEnum.Inlet_03_AllowFeedIn, "无料盘,允许料盘进入,链条运行");
                    AllowFeedIn(true);
                    InletRun(true);
                    AddWaitTrayInPosition(true);
                    AddWaitTime(5000);
                }
            }
            else if (MoveInfo.IsStep(StepEnum.Inlet_03_AllowFeedIn))
            {
                NextMoveStep(StepEnum.Inlet_04_TrayInPosition, "料盘到位,拒绝料盘进入,链条停止");
                AllowFeedIn(false);
                IOMove(IO_Type.Inlet_Run, IO_VALUE.HIGH,5000);

            }
            else if (MoveInfo.IsStep(StepEnum.Inlet_04_TrayInPosition))
            {
                NextMoveStep(StepEnum.Inlet_05_GetTrayInfo, "料盘已到位,扫码,准备获取料盘信息");
                Inlet_05_GetTrayInfo();
            }
            else if (MoveInfo.IsStep(StepEnum.Inlet_05_GetTrayInfo))
            {
                if (getTrayInfoTask != null && getTrayInfoTask.IsCompleted)
                {
                    NextMoveStep(StepEnum.Inlet_06_JackUp, "获取料盘信息完成,顶升抬起");
                    Inlet_06_JackUp();
                }
            }
            else if (MoveInfo.IsStep(StepEnum.Inlet_06_JackUp))
            {
                if (MoveInfo.MoveParam.PosInfo.IsNG)
                {
                    NextMoveStep(StepEnum.Inlet_08_WaitTrayLeave, "条码NG,等待机器人到安全位置");
                    AddWaitTrayInPosition(false);
                    BufferDataManager.TrayInInletInfo = MoveInfo.MoveParam.PosInfo.ToCopy();
                    AddWaitRobotInSafe();
                }
                else
                {
                    NextMoveStep(StepEnum.Inlet_07_GetTargetInfo, "从服务器获取入库库位");
                    GetTargetInfo();
                }

            }
            else if (MoveInfo.IsStep(StepEnum.Inlet_07_GetTargetInfo))
            {
                if (getTargetInfoTask != null && getTargetInfoTask.IsCompleted && lastPosInfo != null)
                {
                    getPosIdMsg = "";
                    ClearTimeoutAlarm("获取库位号超时");
                    //InOutPosInfo oldPos = MoveInfo.MoveParam.PosInfo;
                    //if ((!LastPosInfo.PlateH.Equals(oldPos.PlateH)) || (!LastPosInfo.PlateW.Equals(oldPos.PlateW)))
                    //{
                    //    MoveLog(" " + MoveInfo.SLog + " 原有料盘尺寸:【" + oldPos.PlateW + "X" + oldPos.PlateH + "】服务器返回尺寸【" + LastPosInfo.PlateW + "X" + LastPosInfo.PlateH + "】   ");
                    //}
                    MoveInfo.MoveParam.PosInfo = lastPosInfo;
                    NextMoveStep(StepEnum.Inlet_08_WaitTrayLeave, $"获取目标仓位成功【{MoveInfo.MoveParam.PosInfo.ToStr()}】,等待料盘离开");
                    BufferDataManager.TrayInInletInfo = MoveInfo.MoveParam.PosInfo.ToCopy();
                    AddWaitTrayInPosition(false);
                    AddWaitRobotInSafe();
                }
                else if (MoveInfo.IsTimeOut(15))
                {
                    MoveTimeOut(MoveInfo, "获取库位号超时 " + getPosIdMsg);
                }
            }
            else if (MoveInfo.IsStep(StepEnum.Inlet_08_WaitTrayLeave))
            {
                NextMoveStep(StepEnum.Inlet_09_WaitRobotInSafe, "料盘离开入料口,等待机器人到安全位置");
                AddWaitRobotInSafe();
                AddWaitTime(10000);
                MoveInfo.OneWaitCanEndStep = true;
            }
            else if (MoveInfo.IsStep(StepEnum.Inlet_09_WaitRobotInSafe))
            {
                NextMoveStep(StepEnum.Inlet_10_JackDown, "机器人到达安全位置,顶升下降");
                Inlet_10_JackDown();
            }
            else if (MoveInfo.IsStep(StepEnum.Inlet_10_JackDown))
            {
                //MoveInfo.EndMove();
                //runStatus = RunStatus.Runing;
                MoveInfo.NextMoveStep(StepEnum.Inlet_01_Wait);
                MoveInfo.MoveParam.PosInfo = new InOutPosInfo();
            }

        }
        #endregion
        private void AddWaitRobotInSafe()
        {
            MoveInfo.WaitList.Add(WaitResultInfo.WaitRobotInSafe());
        }
        private void Inlet_05_GetTrayInfo()
        {
            bool isScan = ConfigAppSettings.GetIntValue(Setting_Init.NeedScanCode).Equals(1);
            LastCodeList = new List<string>();
            getTrayInfoTask = Task.Factory.StartNew(() =>
            {
                if (isScan)
                {
                    MoveInfo.OneWaitCanEndStep = true;
                    MoveInfo.WaitList.Add(WaitResultInfo.WaitFeedScanCode());
                    MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(6000));
                    try
                    {
                        IOMove(IO_Type.Inlet_CamLed, IO_VALUE.HIGH);
                        Thread.Sleep(1000);
                        LastCodeList = CodeManager.CameraScan(Config.ScanCodeCamera, Name);
                        if (LastCodeList.Count <= 0)
                        {
                            LastCodeList = CodeManager.CameraScan(Config.ScanCodeCamera, Name);
                        }

                    }
                    catch (Exception ex)
                    {
                        LogUtil.error("扫码出错", ex);
                    }
                    finally
                    {
                        IOMove(IO_Type.Inlet_CamLed, IO_VALUE.LOW);
                    }
                }

                string code = CodeManager.ProcessCode(LastCodeList);
                MoveInfo.MoveParam.PosInfo.barcode = code;
                if (string.IsNullOrEmpty(code))
                {
                    MoveInfo.MoveParam.PosInfo.IsNG = true;
                    MoveInfo.MoveParam.PosInfo.NgMsg = "无有效条码NG";
                }
                //获取料盘宽度
                MoveInfo.MoveParam.PosInfo.PlateW = GetWidth();
                //获取厚度
                MoveInfo.MoveParam.PosInfo.PlateH = GetHeight();
                //扫码

            });
            AddWaitTime(3000);
        }
        private int GetWidth()
        {
            int width = 7;
            int idx_ad_7cun_1 = 0;
            double coef_ad_7cun_1 = Config.Coeff_Height_Ad1;
            int base_ad_7cun_1 = Config.BaseVal_Height_Ad1;

            int idx_ad_7cun_2 = 1;
            double coef_ad_7cun_2 = Config.Coeff_Height_Ad2;
            int base_ad_7cun_2 = Config.BaseVal_Height_Ad2;

            int idx_ad_13cun_1 = 2;
            double coef_ad_13cun_1 = Config.Coeff_Height_Ad3;
            int base_ad_13cun_1 = Config.BaseVal_Height_Ad3;

            int idx_ad_13cun_2 = 3;
            double coef_ad_13cun_2 = Config.Coeff_Height_Ad4;
            int base_ad_13cun_2 = Config.BaseVal_Height_Ad4;

            double val1 = GetCalValue(idx_ad_7cun_1, coef_ad_7cun_1, base_ad_7cun_1);
            double val2 = GetCalValue(idx_ad_7cun_2, coef_ad_7cun_2, base_ad_7cun_2);
            double val3 = GetCalValue(idx_ad_13cun_1, coef_ad_13cun_1, base_ad_13cun_1);
            double val4 = GetCalValue(idx_ad_13cun_2, coef_ad_13cun_2, base_ad_13cun_2);
            width = GetWidth(val3, val4);
            LogUtil.info($"获取宽度【{width}】:7寸宽度计算值1:{val1.ToString("f3")};7寸宽度计算值2:{val2.ToString("f3")};13寸宽度计算值1:{val3.ToString("f3")};13寸宽度计算值2:{val4.ToString("f3")};");
            return width;

        }
        /// <summary>
        /// 获取高度
        /// </summary>
        /// <returns></returns>
        private int GetHeight()
        {
            int height = 8;
            int idx_ad_7cun_1 = 0;
            double coef_ad_7cun_1 = Config.Coeff_Height_Ad1;
            int base_ad_7cun_1 = Config.BaseVal_Height_Ad1;

            int idx_ad_7cun_2 = 1;
            double coef_ad_7cun_2 = Config.Coeff_Height_Ad2;
            int base_ad_7cun_2 = Config.BaseVal_Height_Ad2;

            int idx_ad_13cun_1 = 2;
            double coef_ad_13cun_1 = Config.Coeff_Height_Ad3;
            int base_ad_13cun_1 = Config.BaseVal_Height_Ad3;

            int idx_ad_13cun_2 = 3;
            double coef_ad_13cun_2 = Config.Coeff_Height_Ad4;
            int base_ad_13cun_2 = Config.BaseVal_Height_Ad4;

            double val1 = GetCalValue(idx_ad_7cun_1, coef_ad_7cun_1, base_ad_7cun_1);
            double val2 = GetCalValue(idx_ad_7cun_2, coef_ad_7cun_2, base_ad_7cun_2);
            double val3 = GetCalValue(idx_ad_13cun_1, coef_ad_13cun_1, base_ad_13cun_1);
            double val4 = GetCalValue(idx_ad_13cun_2, coef_ad_13cun_2, base_ad_13cun_2);
            double max = getMax(val1, val2, val3, val4);
            height = getHeight(max);
            LogUtil.info($"获取高度【{height}】:最大计算值为{max.ToString("f3")}");
            return height;
        }
        double getMax(params double[] vals)
        {
            return vals.Max();
        }
        string heightRange = ConfigAppSettings.GetValue(Setting_Init.ReelHeightRange);
        private int getHeight(double max)
        {
            string[] range = heightRange.Split('#');
            string[] h5 = range[0].Split(',');
            string[] h8 = range[1].Split(',');
            string[] h12 = range[2].Split(',');
            int.TryParse(h5[0], out int low_5);
            int.TryParse(h5[1], out int high_5);
            int.TryParse(h8[0], out int low_8);
            int.TryParse(h8[1], out int high_8);
            int.TryParse(h12[0], out int low_12);
            int.TryParse(h12[1], out int high_12);
            int height = 5;
            if (max >= low_5 && max <= high_5)
                height = 5;
            else if (max >= low_8 && max <= high_8)
                height = 8;
            else if (max > low_12 && max <= high_12)
            {
                height = 12;
            }
            else
                height = 12;
            return height;
        }
        public int GetHeight(double[] coefs, int[] baseVals)
        {
            int height = 0;
            double val1 = GetCalValue(0, coefs[0], baseVals[0]);
            double val2 = GetCalValue(1, coefs[1], baseVals[1]);
            double val3 = GetCalValue(2, coefs[2], baseVals[2]);
            double val4 = GetCalValue(3, coefs[3], baseVals[3]);
            double max = getMax(val1, val2, val3, val4);
            //高度有5,8,12
            //各加4毫米
            height = getHeight(max);
            return height;
        }
        public int GetWidth(double calVal3, double calVal4)
        {
            if (calVal3 > 5 || calVal4 > 5)
                return 13;
            else
                return 7;
        }

        /// <summary>
        /// 计算模拟量的值
        /// </summary>
        /// <param name="adIdx"></param>
        /// <param name="coeff"></param>
        /// <param name="baseVal"></param>
        /// <returns></returns>
        public double GetCalValue(int adIdx, double coeff, int baseVal)
        {
            double val = IOManager.GetADVal(adIdx);
            return (val - baseVal) / coeff;
        }
        private int LastResult = 0;
        private string getPosIdMsg = "";
        private void GetTargetInfo()
        {
            InOutPosInfo pos = MoveInfo.MoveParam.PosInfo;
            lastPosInfo = null;
            List<string> codeList = Regex.Split(pos.barcode, "##", RegexOptions.IgnoreCase).ToList();
            getTargetInfoTask = Task.Factory.StartNew(() =>
            {
                //更新托盘条码信息 
                try
                {
                    int count = 1;
                    while (MoveInfo.MoveType.Equals(MoveType.InStore))
                    {
                        int ms = 5000;


                        string lastPosId = "";

                        //从服务器获取库位号
                        GetPosResult result = SServerManager.GetPosId(Name, codeList, pos.PlateH, pos.PlateW, pos.rfid, lastPosId);
                        LastResult = result.Result;
                        if (result.IsTimeOut)
                        {
                            if (count < 5)
                            {
                                ms = 2000;
                            }
                            Thread.Sleep(ms);
                            LogUtil.error(Name + "【" + pos.barcode + "】第[" + count + "]次 " + MoveInfo.SLog + " 超时,等待" + ms + "后重新获取");
                        }
                        else if (result.Result.Equals(99) || result.Result.Equals(100))
                        {
                            if (count < 5)
                            {
                                ms = 3000;
                            }
                            getPosIdMsg = result.Msg;
                            Thread.Sleep(ms);
                            LogUtil.error(Name + "【" + pos.barcode + "】第[" + count + "]次  " + MoveInfo.SLog + "  结果【" + result.Result + "】,等待" + ms + "后重新获取");
                        }
                        else if (!result.Result.Equals(0))
                        {
                            lastPosInfo = result.Param;
                            LogUtil.error(Name + "【" + pos.barcode + "】第[" + count + "]次  " + MoveInfo.SLog + "  入库NG:" + result.Msg);
                            break;
                        }
                        else
                        {
                            lastPosInfo = result.Param;
                            break;
                        }

                        if (count >= 3)
                        {
                            //自动NG
                            lastPosInfo = new InOutPosInfo(pos.barcode, "", MoveInfo.MoveParam.PosInfo.PlateW, MoveInfo.MoveParam.PosInfo.PlateH);
                            lastPosInfo.IsNG = true;
                            lastPosInfo.NgMsg = "获取库位超过三次,直接NG";
                            break;
                        }
                        count++;
                    }
                }
                catch (Exception ex)
                {
                    LogUtil.error(Name + "【" + pos.barcode + "】 " + MoveInfo.SLog + " 获取库位号报错:" + ex.ToString());
                }
            });
        }
        /// <summary>
        /// 入料线体是否有料盘
        /// </summary>
        /// <returns></returns>
        public bool HasTrayInPosition()
        {
            return IOValue(IO_Type.Inlet_TrayCheck).Equals(IO_VALUE.HIGH);
        }
        private void Inlet_06_JackUp()
        {
            if (MoveInfo.MoveParam.PosInfo != null && MoveInfo.MoveParam.PosInfo.PlateW.Equals(7))
            {
                Inch7JackUp(true);
            }
            else if (MoveInfo.MoveParam.PosInfo != null && MoveInfo.MoveParam.PosInfo.PlateW.Equals(13))
            {
                Inch13JackUp(true);
            }
        }
        private void Inlet_10_JackDown()
        {
            Inch7JackUp(false);
            Inch13JackUp(false);
        }
    }
}