BorderSearch.cs 32.8 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Misc;
using FLY.Thick.Base.Common;
using FLY.Thick.Base.IService;
9
using FObjBase;
10 11 12
using AutoMapper;
using System.IO;
using Newtonsoft.Json;
潘栩锋's avatar
潘栩锋 committed
13 14 15 16 17 18 19

namespace FLY.Thick.Base.Server
{

    /// <summary>
    /// 边界查找
    /// </summary>
20
    public class BorderSearch : IBorderSearchService, INotifyPropertyChanged
潘栩锋's avatar
潘栩锋 committed
21
    {
22 23 24
        /// <summary>
        /// 报警测试
        /// </summary>
25
        //public static FLY.OBJComponents.Common.ERRNO BORDER_ERRNO_NOFILM = new FLY.OBJComponents.Common.ERRNO() { Code = 50, Descrption = "边界异常,没有被测物" };
26

潘栩锋's avatar
潘栩锋 committed
27 28 29 30 31 32 33 34
        #region 延时执行,Markno
        public const int MARKNO_SAVE = 1;
        #endregion
        #region 属性,成员变量的代理

        /// <summary>
        /// 启动与否
        /// </summary>
潘栩锋's avatar
1  
潘栩锋 committed
35 36
        public bool Enable { get; set; }

37 38 39 40 41
        /// <summary>
        /// 单一材料,找空气与膜均值的 中间位置
        /// </summary>
        public bool IsOneMaterial { get; set; }

潘栩锋's avatar
潘栩锋 committed
42 43 44
        /// <summary>
        /// 边界拐点检测,找到的边界更加精确
        /// </summary>
潘栩锋's avatar
1  
潘栩锋 committed
45
        public bool IsBreakDetect { get; set; } = true;
潘栩锋's avatar
潘栩锋 committed
46

47

潘栩锋's avatar
潘栩锋 committed
48 49

        /// <summary>
50
        /// 有效范围
潘栩锋's avatar
潘栩锋 committed
51
        /// </summary>
52
        public RangeStruct Valid { get; set; } = RangeStruct.InvalidValue;
潘栩锋's avatar
潘栩锋 committed
53 54 55
        /// <summary>
        /// 边界
        /// </summary>
56
        public Range Border_Forw { get { return borders[0].value; } }
潘栩锋's avatar
1  
潘栩锋 committed
57 58 59
        /// <summary>
        /// 边界
        /// </summary>
60
        public Range Border_Backw { get { return borders[1].value; } }
潘栩锋's avatar
1  
潘栩锋 committed
61 62 63 64

        /// <summary>
        /// 膜范围
        /// </summary>
65
        public RangeStruct Border { get; set; } = RangeStruct.InvalidValue;
潘栩锋's avatar
1  
潘栩锋 committed
66

潘栩锋's avatar
潘栩锋 committed
67 68 69
        /// <summary>
        /// 当前膜宽,测量出来的,单位是 脉冲
        /// </summary>
潘栩锋's avatar
1  
潘栩锋 committed
70 71
        public int Width { get; protected set; }

潘栩锋's avatar
潘栩锋 committed
72 73 74
        /// <summary>
        /// 膜中间位置 单位 脉冲
        /// </summary>
潘栩锋's avatar
1  
潘栩锋 committed
75
        public int Mid { get; protected set; }
潘栩锋's avatar
潘栩锋 committed
76
        
潘栩锋's avatar
潘栩锋 committed
77 78 79
        /// <summary>
        /// 手动设置温修AD值
        /// </summary>
潘栩锋's avatar
1  
潘栩锋 committed
80
        public bool TempADBySet { get; set; }
潘栩锋's avatar
潘栩锋 committed
81 82 83 84

        /// <summary>
        /// 温修AD值
        /// </summary>
潘栩锋's avatar
1  
潘栩锋 committed
85 86
        public int CurrTempAD { get; set; } = 50000;

潘栩锋's avatar
潘栩锋 committed
87 88 89
        /// <summary>
        /// 温修AD值
        /// </summary>
潘栩锋's avatar
1  
潘栩锋 committed
90 91
        public int TempAD { get; set; } = 50000;

潘栩锋's avatar
潘栩锋 committed
92 93 94
        /// <summary>
        /// AD超过了范围, 就认为开始找到边界
        /// </summary>
95 96 97 98 99 100 101 102 103 104 105
        public int TempRange { get; set; } = 1000;

        /// <summary>
        /// 温修范围是温修的百分比
        /// </summary>
        public bool IsTempRangeByPercent { get; set; } = true;

        /// <summary>
        ///  温修范围百分比
        /// </summary>
        public double TempRangePercent { get; set; } = 0.02;
潘栩锋's avatar
1  
潘栩锋 committed
106 107


潘栩锋's avatar
潘栩锋 committed
108 109 110
        /// <summary>
        /// 有滤波器,只有非空的连续N个pos以上,才开始算边界开始
        /// </summary>
潘栩锋's avatar
1  
潘栩锋 committed
111
        public int N { get; set; }
潘栩锋's avatar
潘栩锋 committed
112 113 114 115

        /// <summary>
        /// 探头直径,单位脉冲, 膜宽 = 边界范围 - 探头直径
        /// </summary>
潘栩锋's avatar
1  
潘栩锋 committed
116 117
        public int SensorWidth { get; set; }

潘栩锋's avatar
潘栩锋 committed
118 119 120 121 122 123



        /// <summary>
        /// 找到边界后,再往内缩N2个脉冲,防止边界找得不准的问题。
        /// </summary>
潘栩锋's avatar
1  
潘栩锋 committed
124 125
        public int N2 { get; set; }

潘栩锋's avatar
潘栩锋 committed
126 127 128 129

        /// <summary>
        /// 记录两个边界以后扫描,以它们再外扩N3个脉冲,作为扫描范围 
        /// </summary>
潘栩锋's avatar
1  
潘栩锋 committed
130 131
        public int N3 { get; set; }

潘栩锋's avatar
潘栩锋 committed
132
        /// <summary>
133
        /// 数据更新时间
潘栩锋's avatar
潘栩锋 committed
134
        /// </summary>
135
        public DateTime UpdateTime { get; protected set; }
潘栩锋's avatar
潘栩锋 committed
136

137 138 139 140 141 142
        /// <summary>
        /// 产品宽度,设置值 单位:脉冲
        /// 当膜超过范围(没有经过阀值),就使用它,猜边界在哪
        /// </summary>
        public int ProductWidth { get; protected set; }

潘栩锋's avatar
潘栩锋 committed
143 144
        #endregion

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

        /// <summary>
        /// 边界
        /// </summary>
        protected class BorderWithOk
        {
            /// <summary>
            ///  边界值
            /// </summary>
            public Range value = new Range();
            /// <summary>
            /// 开始边界100%可靠
            /// </summary>
            public bool isBeginOk;
            /// <summary>
            /// 结束边界100%可靠
            /// </summary>
            public bool isEndOk;
            /// <summary>
            /// 真实可靠的边界值
            /// </summary>
            public Range real = new Range();

            public void UpdateReal(int sensorWidth, int expect, int mid, bool isBeginOk, bool isEndOk)
            {
                if (value.IsValid)
                {
                    real.Begin = value.Begin + sensorWidth / 2;
                    real.End = value.End - sensorWidth / 2;

                    //修正!!!
                    if (real.Width < expect)
                    {
                        if (isBeginOk && isEndOk)
                        {

                        }
                        else if (isBeginOk)
                        {
                            real.End = real.Begin + expect;
                        }
                        else if (isEndOk)
                        {
                            real.Begin = real.End - expect;
                        }
                        else //开始边界与结束边界都是假的
                        {
                            //居中处理
                            real.Begin = mid - expect / 2;
                            real.End = real.Begin + expect;
                        }
                    }
                }
            }

            public void UpdateReal(int sensorWidth, int expect, int mid)
            {
                UpdateReal(sensorWidth, expect, mid, isBeginOk, isEndOk);
            }
            public void UpdateReal(int sensorWidth)
            {
                if (value.IsValid)
                {
                    real.Begin = value.Begin + sensorWidth / 2;
                    real.End = value.End - sensorWidth / 2;
                }
            }
        }
        /// <summary>
        /// 正反向边界数据
        /// </summary>
        protected BorderWithOk[] borders = new BorderWithOk[2] { new BorderWithOk(), new BorderWithOk() };

218
        OBJComponents.Server.WarningSystem2 warningSystem;
219 220 221 222 223 224 225


        /// <summary>
        /// 用于调试, 进入到边界查找的数据
        /// </summary>
        protected BorderSearchGetViewReponse getViewReponse;

226
        private string param_path = "bordersearch.json";
潘栩锋's avatar
潘栩锋 committed
227 228 229 230
        public BorderSearch()
        {
            Default();
            this.PropertyChanged += new PropertyChangedEventHandler(BorderSearch_PropertyChanged);
潘栩锋's avatar
潘栩锋 committed
231 232


潘栩锋's avatar
潘栩锋 committed
233 234 235 236 237 238 239 240
        }

        public BorderSearch(string param_path) 
        {
            if (!string.IsNullOrEmpty(param_path))
                this.param_path = param_path;
            Default();
            this.PropertyChanged += new PropertyChangedEventHandler(BorderSearch_PropertyChanged);
潘栩锋's avatar
潘栩锋 committed
241 242

            this.Border_Forw.PropertyChanged += (s, e) => {
243
                NotifyPropertyChanged(nameof(Border_Forw));
潘栩锋's avatar
潘栩锋 committed
244 245
            };
            this.Border_Backw.PropertyChanged += (s, e) => {
246
                NotifyPropertyChanged(nameof(Border_Backw));
潘栩锋's avatar
潘栩锋 committed
247
            };
248 249 250 251

            if (!Load()) {
                Save();
            }
潘栩锋's avatar
潘栩锋 committed
252
        }
253

254
        public void Init(OBJComponents.Server.WarningSystem2 warningSystem) 
255 256 257
        {
            this.warningSystem = warningSystem;
        }
潘栩锋's avatar
潘栩锋 committed
258 259
        void Default() 
        {
260
            Enable = true;
261
            Valid = new RangeStruct(1200,7400);
潘栩锋's avatar
潘栩锋 committed
262 263 264 265 266 267
            TempAD = -1;
            N = 20;
            N2 = 400;
            N3 = 1000;
            SensorWidth = 250;
            TempRange = 500;
268 269
            TempRangePercent = 500.0 / 50000;
            IsTempRangeByPercent = true;
270 271

            Reset();
潘栩锋's avatar
潘栩锋 committed
272 273 274
        }


潘栩锋's avatar
潘栩锋 committed
275

潘栩锋's avatar
潘栩锋 committed
276 277 278

        void BorderSearch_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
279
            if (e.PropertyName == nameof(CurrTempAD))
潘栩锋's avatar
潘栩锋 committed
280 281 282 283
            {
                if (!TempADBySet)
                    TempAD = CurrTempAD;
            }
284
            else if (e.PropertyName == nameof(TempAD))
285 286 287 288 289 290
            {
                if (IsTempRangeByPercent)
                {
                    TempRange = (int)(TempAD * TempRangePercent);
                }
            }
291
            else if ((e.PropertyName == nameof(TempRangePercent)) || (e.PropertyName == nameof(IsTempRangeByPercent)))
292 293 294 295 296 297 298
            {
                if (IsTempRangeByPercent)
                {
                    TempRange = (int)(TempAD * TempRangePercent);
                }
            }

潘栩锋's avatar
潘栩锋 committed
299 300
        }

301
        public void Reset()
潘栩锋's avatar
潘栩锋 committed
302
        {
潘栩锋's avatar
潘栩锋 committed
303 304
            Border_Backw.Reset();
            Border_Forw.Reset();
潘栩锋's avatar
潘栩锋 committed
305
            Width = 0;
306
            Border = RangeStruct.InvalidValue;
潘栩锋's avatar
潘栩锋 committed
307 308
            Mid = Valid.Mid;
        }
309 310 311 312 313 314 315 316 317


        public void SetProductWidth(int productWidth) {
            ProductWidth = productWidth;
        }
        /// <summary>
        /// 获取扫描范围
        /// </summary>
        /// <returns></returns>
潘栩锋's avatar
潘栩锋 committed
318 319
        public Range GetScanRange() 
        {
320
            GetScanRange(out int b, out int e);
潘栩锋's avatar
潘栩锋 committed
321 322
            return new Range() { Begin = b, End = e };
        }
323 324 325 326 327
        /// <summary>
        /// 获取扫描范围
        /// </summary>
        /// <param name="posBegin"></param>
        /// <param name="posEnd"></param>
潘栩锋's avatar
潘栩锋 committed
328 329 330
        public void GetScanRange(out int posBegin, out int posEnd)
        {

331
            int min, max;
潘栩锋's avatar
潘栩锋 committed
332

333
            if (borders.All(b => b.value.IsValid))
潘栩锋's avatar
潘栩锋 committed
334
            {
335 336
                min = borders.Min(b => b.value.Begin) - N3;
                max = borders.Max(b => b.value.End) + N3;
潘栩锋's avatar
潘栩锋 committed
337
            }
338
            else if (borders[0].value.IsValid)
潘栩锋's avatar
潘栩锋 committed
339
            {
340 341
                min = borders[0].value.Begin - N3;
                max = borders[0].value.End + N3;
潘栩锋's avatar
潘栩锋 committed
342
            }
343
            else if (borders[1].value.IsValid)
潘栩锋's avatar
潘栩锋 committed
344
            {
345 346
                min = borders[1].value.Begin - N3;
                max = borders[1].value.End + N3;
潘栩锋's avatar
潘栩锋 committed
347
            }
348
            else 
潘栩锋's avatar
潘栩锋 committed
349
            {
350
                //都不能找到边界
潘栩锋's avatar
1  
潘栩锋 committed
351 352
                min = Valid.Begin;
                max = Valid.End;
潘栩锋's avatar
潘栩锋 committed
353
            }
354 355 356 357

            //范围限制
            posBegin = Math.Max(min, Valid.Begin);
            posEnd = Math.Min(max, Valid.End);
潘栩锋's avatar
潘栩锋 committed
358 359 360 361
        }



362

潘栩锋's avatar
潘栩锋 committed
363 364 365
        /// <summary>
        /// 初略操作,超过范围 就是边界
        /// </summary>
366 367 368 369 370 371 372
        /// <param name="dat">全部数据</param>
        /// <param name="validBegin_grid">有效范围开始</param>
        /// <param name="validEnd_grid">有效范围结束</param>
        /// <param name="borderBegin_grid">输出的开始边界</param>
        /// <param name="borderEnd_grid">输出的结束边界</param>
        /// <param name="isBorderBeginOk">开始边界经过阀值线</param>
        /// <param name="isBorderEndOk">结束边界经过阀值线</param>
潘栩锋's avatar
潘栩锋 committed
373
        /// <returns></returns>
374 375 376 377 378 379 380 381
        protected bool FindFilm_Threshold(
            int[] dat, 
            int validBegin_grid,
            int validEnd_grid, 
            out int borderBegin_grid,
            out int borderEnd_grid, 
            out bool isBorderBeginOk, 
            out bool isBorderEndOk) 
潘栩锋's avatar
潘栩锋 committed
382
        {
383 384 385 386 387
            borderBegin_grid = -1;
            borderEnd_grid = -1;
            isBorderBeginOk = false;
            isBorderEndOk = false;
            int mid_grid = (validBegin_grid + validEnd_grid) / 2;
潘栩锋's avatar
潘栩锋 committed
388 389 390

            //从中间开始向左边找
            //必须先找到膜
391
            for (int i = mid_grid; i >= validBegin_grid; i--)
潘栩锋's avatar
潘栩锋 committed
392 393 394 395 396 397 398 399
            {
                if (i >= 0 && i < dat.Length)
                {
                    if (Misc.MyBase.ISVALIDATA(dat[i]))
                    {
                        if (Math.Abs(TempAD - dat[i]) > TempRange)
                        {
                            //膜
400
                            borderBegin_grid = i;
潘栩锋's avatar
潘栩锋 committed
401 402 403 404
                        }
                        else
                        {
                            //空气
405
                            if (borderBegin_grid != -1)
潘栩锋's avatar
潘栩锋 committed
406
                            {
407
                                isBorderBeginOk = true;
潘栩锋's avatar
潘栩锋 committed
408 409 410 411 412 413 414 415 416 417 418 419
                                //之前有膜
                                break;
                            }
                        }
                    }
                }
                else if (i < 0)
                {
                    break;
                }
            }

420
            if (borderBegin_grid == -1)//没有膜
潘栩锋's avatar
潘栩锋 committed
421 422 423
            {
                //从左边开始找
                //找左边界 ,找阀值点
424
                for (int i = mid_grid; i <= validEnd_grid; i++)
潘栩锋's avatar
潘栩锋 committed
425 426 427 428 429 430 431 432
                {
                    if (i >= 0 && i < dat.Length)
                    {
                        if (Misc.MyBase.ISVALIDATA(dat[i]))
                        {
                            if (Math.Abs(TempAD - dat[i]) > TempRange)
                            {
                                //膜
433 434
                                borderBegin_grid = i;
                                isBorderBeginOk = true;
潘栩锋's avatar
潘栩锋 committed
435 436 437 438 439
                                break;
                            }
                        }
                    }
                }
440
                if (borderBegin_grid == -1)//没有膜
潘栩锋's avatar
潘栩锋 committed
441 442 443 444
                    return false;
            }
            
            //从左边界开始找
445
            for (int i = borderBegin_grid; i <= validEnd_grid; i++)
潘栩锋's avatar
潘栩锋 committed
446 447 448 449 450 451 452 453
            {
                if (i >= 0 && i < dat.Length)
                {
                    if (Misc.MyBase.ISVALIDATA(dat[i]))
                    {
                        if (Math.Abs(TempAD - dat[i]) > TempRange)
                        {
                            //膜
454
                            borderEnd_grid = i;
潘栩锋's avatar
潘栩锋 committed
455 456 457 458
                        }
                        else
                        {
                            //空气
459
                            if (borderEnd_grid != -1)
潘栩锋's avatar
潘栩锋 committed
460
                            {
461
                                isBorderEndOk = true;
潘栩锋's avatar
潘栩锋 committed
462 463 464 465 466 467 468 469
                                //之前有膜
                                break;
                            }
                        }
                    }
                }
            }

470
            if (borderEnd_grid == -1)//没有膜
潘栩锋's avatar
潘栩锋 committed
471 472 473 474 475 476 477 478
                return false;

            return true;
        }
        /// <summary>
        /// 精细操作,再向外找 折点
        /// </summary>
        /// <param name="dat"></param>
479 480
        /// <param name="sensor_grid">探头直径</param>
        /// <param name="filter_grid">滤波</param>
潘栩锋's avatar
潘栩锋 committed
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
        /// <param name="borderbegin_grid"></param>
        /// <param name="borderend_grid"></param>
        /// <returns></returns>
        bool FindFilm_Break(int[] dat, int sensor_grid, int filter_grid, ref int borderbegin_grid, ref int borderend_grid)
        {
            //当前肯定在斜波上
            //边界肯定在 粗边界以外 探头半径 内
            //int N4 = SensorWidth / posOfGrid;

            if (sensor_grid <= 2)
            {
                //不用找,传感器直径非常小,已经很准确了
                return true;
            }

            //int num = N / posOfGrid / 2;//滤波半径
            int num = filter_grid / 2;
            if (num < 1) num = 1;


            int[] filterdats = new int[dat.Length];//数据滤波
502
            for (int i = 0; i < dat.Length; i++)
潘栩锋's avatar
潘栩锋 committed
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
            {
                int b = i - num;
                int e = i + num;
                if (b < 0)
                {
                    b = 0;
                    e = b + num * 2;
                }
                else if (e >= (dat.Length - 1))
                {
                    e = dat.Length - 1;
                    b = e - num * 2;
                }
                filterdats[i] = Misc.MyMath.Avg(dat, b, e);
            }
518 519
            int bg = borderbegin_grid;
            int eg = borderend_grid;
潘栩锋's avatar
潘栩锋 committed
520

521 522 523 524
            int[] breakdats = new int[dat.Length];
            num = 1;

            for (int i = 0; i < dat.Length; i++)
潘栩锋's avatar
潘栩锋 committed
525
            {
526 527 528 529 530 531 532 533 534 535 536
                if ((i - num) < 0)
                    breakdats[i] = Misc.MyBase.NULL_VALUE;
                else if ((i + num) >= dat.Length)
                    breakdats[i] = Misc.MyBase.NULL_VALUE;
                else if (!Misc.MyBase.ISVALIDATA(filterdats[i - num]))
                    breakdats[i] = Misc.MyBase.NULL_VALUE;
                else if (!Misc.MyBase.ISVALIDATA(filterdats[i + num]))
                    breakdats[i] = Misc.MyBase.NULL_VALUE;
                else if (!Misc.MyBase.ISVALIDATA(filterdats[i]))
                    breakdats[i] = Misc.MyBase.NULL_VALUE;
                else
潘栩锋's avatar
潘栩锋 committed
537
                {
538
                    breakdats[i] = Math.Abs((filterdats[i] - filterdats[i - num]) * (filterdats[i + num] - filterdats[i]));
潘栩锋's avatar
潘栩锋 committed
539 540 541
                }
            }

542
            bool isdown = false;
潘栩锋's avatar
潘栩锋 committed
543 544 545 546 547 548 549
            //从第1次找到的边界开始,往外找,找折点的最小值
            for (int i = borderbegin_grid; i >= (borderbegin_grid - sensor_grid); i--)
            {
                if ((i - num) < 0)
                    break;
                if ((i + num) >= dat.Length)
                    break;
550
                if (!Misc.MyBase.ISVALIDATA(breakdats[i - num]))
潘栩锋's avatar
潘栩锋 committed
551
                    break;
552
                if (!Misc.MyBase.ISVALIDATA(breakdats[i + num]))
潘栩锋's avatar
潘栩锋 committed
553
                    break;
554
                if (!Misc.MyBase.ISVALIDATA(breakdats[i]))
潘栩锋's avatar
潘栩锋 committed
555
                    break;
556
                if (breakdats[i] <= breakdats[i + num])
557 558 559 560 561 562 563 564 565 566 567 568
                {
                    //在减少
                    bg = i;
                    isdown = true;
                }
                if (isdown)
                {
                    if (breakdats[i] < breakdats[i - num])
                    {
                        //之前那个就是最小值
                        break;
                    }
潘栩锋's avatar
潘栩锋 committed
569 570
                }
            }
571
            isdown = false;
潘栩锋's avatar
潘栩锋 committed
572 573 574 575 576 577 578 579 580 581 582 583 584
            for (int i = borderend_grid; i <= (borderend_grid + sensor_grid); i++)
            {
                if ((i - num) < 0)
                    break;
                if ((i + num) >= dat.Length)
                    break;
                if (!Misc.MyBase.ISVALIDATA(filterdats[i - num]))
                    break;
                if (!Misc.MyBase.ISVALIDATA(filterdats[i + num]))
                    break;
                if (!Misc.MyBase.ISVALIDATA(filterdats[i]))
                    break;

585
                if (breakdats[i] <= breakdats[i - num])
潘栩锋's avatar
潘栩锋 committed
586
                {
587 588 589 590 591 592 593 594 595 596 597
                    //在减少
                    eg = i;
                    isdown = true;
                }
                if (isdown)
                {
                    if (breakdats[i] < breakdats[i + num])
                    {
                        //之前那个就是最小值
                        break;
                    }
潘栩锋's avatar
潘栩锋 committed
598 599 600 601 602 603
                }
            }
            borderbegin_grid = bg;
            borderend_grid = eg;
            return true;
        }
604

潘栩锋's avatar
潘栩锋 committed
605 606 607 608 609 610 611 612
        /// <summary>
        /// 放入grid数据,分析边界
        /// </summary>
        /// <param name="direction"></param>
        /// <param name="posOfGrid"></param>
        /// <param name="gridBegin"></param>
        /// <param name="dat"></param>
        /// <returns></returns>
613
        bool FindFilm_Break(DIRECTION direction, int posOfGrid, int gridBegin, int[] dat)
潘栩锋's avatar
潘栩锋 committed
614 615
        {
            //把数据记录下来
616
            getViewReponse = new BorderSearchGetViewReponse()
潘栩锋's avatar
潘栩锋 committed
617
            {
618 619 620 621 622 623
                direction = direction,
                posOfGrid = posOfGrid,
                gridBegin = gridBegin,
                dat = dat
            };
            UpdateTime = DateTime.Now;
潘栩锋's avatar
潘栩锋 committed
624 625 626 627 628

            //转为 dat 的序号
            int validbegin_grid = Valid.Begin / posOfGrid - gridBegin;
            int validend_grid = Valid.End / posOfGrid - gridBegin;

629 630 631 632
            if (!FindFilm_Threshold(dat, 
                validbegin_grid, validend_grid, 
                out int borderbegin_grid, out int borderend_grid, 
                out bool isBorderBeginOk, out bool isBorderEndOk))
633
            {
634
                //warningSystem.Add(BORDER_ERRNO_NOFILM.Code, BORDER_ERRNO_NOFILM.Descrption, OBJComponents.Common.ERR_STATE.ON);
潘栩锋's avatar
潘栩锋 committed
635
                return false;
636
            }
637
            else {
638
                //warningSystem.Add(BORDER_ERRNO_NOFILM.Code, BORDER_ERRNO_NOFILM.Descrption, OBJComponents.Common.ERR_STATE.OFF);
639
            }
潘栩锋's avatar
潘栩锋 committed
640 641 642 643 644 645 646 647 648 649 650 651 652 653
            //N2 只是用来判断一下膜够不够宽而已,没用
            int N2_grid = (N2*3+SensorWidth) / posOfGrid;
            
            if ((borderend_grid - borderbegin_grid) < N2_grid)//膜太短
                return false;

            if (IsBreakDetect)
            {
                int sensor_grid = SensorWidth / posOfGrid;
                int filter_grid = N / posOfGrid;
                //精细查找
                FindFilm_Break(dat, sensor_grid, filter_grid, ref borderbegin_grid, ref borderend_grid);
            }

654
            int idx = (direction == DIRECTION.FORWARD) ? 0 : 1;
655
            var border = borders[idx];
656

潘栩锋's avatar
潘栩锋 committed
657
            //转为 脉冲
658 659 660 661 662
            border.value.Begin = (borderbegin_grid + gridBegin) * posOfGrid;
            border.value.End = (borderend_grid +gridBegin) * posOfGrid;
            border.isBeginOk = isBorderBeginOk;
            border.isEndOk = isBorderEndOk;

663
            
潘栩锋's avatar
潘栩锋 committed
664

665
            if (borders.All(b => b.value.IsValid))
潘栩锋's avatar
潘栩锋 committed
666
            {
667 668 669
                foreach(var bo in borders)
                    bo.UpdateReal(SensorWidth, ProductWidth, Valid.Mid, 
                        borders.All(b=>b.isBeginOk), borders.All(b => b.isEndOk));
670 671 672
                Border = new RangeStruct(
                    (int)borders.Average(b => b.real.Begin),
                    (int)borders.Average(b => b.real.End));
潘栩锋's avatar
潘栩锋 committed
673
            }
674
            else if (border.value.IsValid)
潘栩锋's avatar
潘栩锋 committed
675
            {
676 677
                foreach (var bo in borders)
                    bo.UpdateReal(SensorWidth, ProductWidth, Valid.Mid);
678
                Border = border.real.ToStruct();
潘栩锋's avatar
潘栩锋 committed
679
            }
680 681 682

            getViewReponse.border = border.real;

683 684
            Width = Border.Width;
            Mid = Border.Mid;
潘栩锋's avatar
潘栩锋 committed
685 686
            return true;
        }
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845

        /// <summary>
        /// 找空气与膜均值的 中间位置
        /// </summary>
        /// <returns></returns>
        void FindFilm_Mean(int[] dat, int sensor_grid, int n2_grid, ref int borderbegin_grid, ref int borderend_grid)
        {
            Range r1_grid = new Range();
            //算膜中间的平均值
            r1_grid.Begin = borderbegin_grid + sensor_grid + n2_grid;
            r1_grid.End = borderend_grid - (sensor_grid + n2_grid);
            int avg = MyMath.Avg(dat, r1_grid.Begin, r1_grid.End);

            //算空气的平均值
            Range r2_grid = new Range();
            r2_grid.Begin = borderbegin_grid - sensor_grid / 3;
            r2_grid.End = borderend_grid + sensor_grid / 3;
            if (r2_grid.Begin < 0)
                r2_grid.Begin = 0;
            if (r2_grid.End > dat.Length - 1)
                r2_grid.End = dat.Length - 1;

            int avg1 = MyMath.Avg(dat, 0, r2_grid.Begin);
            int avg2 = MyMath.Avg(dat, r2_grid.End, dat.Length - 1);
            int threshold;
            if ((Misc.MyBase.ISVALIDATA(avg1)) && (Misc.MyBase.ISVALIDATA(avg2)))
            {
                threshold = (avg + (avg1 + avg2) / 2) / 2;
            }
            else if (Misc.MyBase.ISVALIDATA(avg1))
            {
                threshold = (avg + avg1) / 2;
            }
            else if (Misc.MyBase.ISVALIDATA(avg2))
            {
                threshold = (avg + avg2) / 2;
            }
            else
            {
                threshold = (TempAD + avg) / 2;
            }

            //左边界
            for (int i = r1_grid.Begin; i >= r2_grid.Begin; i--)
            {
                if (Misc.MyBase.ISVALIDATA(dat[i]))
                {
                    if (dat[i] < threshold)
                    {
                        borderbegin_grid = i;
                    }
                }
            }
            //右边界
            for (int i = r1_grid.End; i < r2_grid.End; i++)
            {
                if (Misc.MyBase.ISVALIDATA(dat[i]))
                {
                    if (dat[i] < threshold)
                    {
                        borderend_grid = i;
                    }
                }
            }
        }

        /// <summary>
        /// 放入grid数据,分析边界
        /// </summary>
        /// <param name="direction"></param>
        /// <param name="posOfGrid"></param>
        /// <param name="gridBegin"></param>
        /// <param name="dat"></param>
        /// <returns></returns>
        public virtual bool FindFilm(DIRECTION direction, int posOfGrid, int gridBegin, int[] dat) 
        {
            if (IsOneMaterial)
            {
                return FindFilm_Mean(direction, posOfGrid, gridBegin, dat);
            }
            else {
                return FindFilm_Break(direction, posOfGrid, gridBegin, dat);
            }
        }


        /// <summary>
        /// 放入grid数据,分析边界
        /// </summary>
        /// <param name="direction"></param>
        /// <param name="posOfGrid"></param>
        /// <param name="gridBegin"></param>
        /// <param name="dat"></param>
        /// <returns></returns>
        bool FindFilm_Mean(DIRECTION direction, int posOfGrid, int gridBegin, int[] dat)
        {
            //把数据记录下来
            getViewReponse = new BorderSearchGetViewReponse()
            {
                direction = direction,
                posOfGrid = posOfGrid,
                gridBegin = gridBegin,
                dat = dat
            };
            UpdateTime = DateTime.Now;

            //这些是AD值
            //转为 dat 的序号
            int validbegin_grid = Valid.Begin / posOfGrid - gridBegin;
            int validend_grid = Valid.End / posOfGrid - gridBegin;


            if (!FindFilm_Threshold(dat,
                validbegin_grid, validend_grid,
                out int borderbegin_grid, out int borderend_grid,
                out bool isBorderBeginOk, out bool isBorderEndOk))
            {
                return false;
            }

            //N2 只是用来判断一下膜够不够宽而已,没用
            int N2_grid = (N2 * 3 + SensorWidth) / posOfGrid;

            if ((borderend_grid - borderbegin_grid) < N2_grid)//膜太短
                return false;

            FindFilm_Mean(dat, SensorWidth / posOfGrid, N2 / posOfGrid, ref borderbegin_grid, ref borderend_grid);

            int idx = (direction == DIRECTION.FORWARD) ? 0 : 1;
            var border = borders[idx];

            //borderbegin_grid, borderend_grid 是实际边界位置, 
            //border.value 是空气与膜的拐点
            //必须外扩

            //转为 脉冲
            border.value.Begin = (borderbegin_grid + gridBegin) * posOfGrid - SensorWidth / 2;
            border.value.End = (borderend_grid + gridBegin) * posOfGrid + SensorWidth / 2;
            border.isBeginOk = true;
            border.isEndOk = true;
            border.UpdateReal(SensorWidth);

            if (borders.All(b => b.value.IsValid))
            {
                Border = new RangeStruct((int)borders.Average(b => b.real.Begin), (int)borders.Average(b => b.real.End));
            }
            else if (border.value.IsValid)
            {
                Border = border.real.ToStruct();
            }

            getViewReponse.border = border.real;

            Width = Border.Width;
            Mid = Border.Mid;
            return true;
        }


潘栩锋's avatar
潘栩锋 committed
846 847 848 849 850 851 852 853 854
        /// <summary>
        /// 获取膜边界,当根本找不到膜时,返回找边界范围!!!
        /// 膜的边界,只是斜波的中间而已。
        /// </summary>
        /// <param name="direction"></param>
        /// <param name="posBegin"></param>
        /// <param name="posEnd"></param>
        public void GetBorder(DIRECTION direction, out int posBegin, out int posEnd) 
        {
855 856 857
            int idx = (direction == DIRECTION.FORWARD) ? 0 : 1;
            var border = borders[idx];
            if (border.value.IsValid)
潘栩锋's avatar
潘栩锋 committed
858
            {
859 860
                posBegin = border.real.Begin;
                posEnd = border.real.End;
潘栩锋's avatar
潘栩锋 committed
861
            }
862
            else
潘栩锋's avatar
潘栩锋 committed
863
            {
864 865 866 867 868
                idx += 1;
                if (idx > 1)
                    idx = 0;
                border = borders[idx];
                if (border.value.IsValid)
潘栩锋's avatar
潘栩锋 committed
869
                {
870 871 872 873 874 875
                    posBegin = border.real.Begin;
                    posEnd = border.real.End;
                }
                else {
                    posBegin = Misc.MyBase.NULL_VALUE;
                    posEnd = Misc.MyBase.NULL_VALUE;
潘栩锋's avatar
潘栩锋 committed
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
                }
            }
        }
        /// <summary>
        /// 获取膜边界,当根本找不到膜时,返回IsVaild=false 的Range!!!
        /// 膜的边界,只是斜波的中间而已。
        /// </summary>
        /// <param name="direction"></param>
        /// <returns></returns>
        public Range GetBorder(DIRECTION direction) 
        {
            int b, e;
            GetBorder(direction, out b, out e);
            return new Range() { Begin = b, End = e };
        }
        
        /// <summary>
        /// 获取 膜边界 向内缩 N2 + SensorWidth / 2
        /// </summary>
        /// <param name="direction"></param>
        /// <param name="posBegin"></param>
        /// <param name="posEnd"></param>
        public void GetBoltRange(DIRECTION direction, out int posBegin, out int posEnd) 
        {
            GetBorder(direction, out posBegin, out posEnd);
            if (Misc.MyBase.ISVALIDATA(posBegin)) 
            {
                posBegin += (SensorWidth / 2 + N2);
                posEnd -= (SensorWidth / 2 + N2);
            }
        }
        /// <summary>
        /// 膜边界 向内缩 N2 + SensorWidth / 2
        /// </summary>
        /// <param name="direction"></param>
        /// <returns></returns>
        public Range GetBoltRange(DIRECTION direction) 
        {
            int b, e;
            GetBoltRange(direction, out b, out e);
            return new Range() { Begin = b, End = e };
        }

919 920 921 922 923 924 925 926 927 928 929 930
        /// <summary>
        /// 获取混合后的横向数据;没有把横向留白剔除;只把纵向留白剔除。
        /// 数据单位 mm
        /// </summary>
        /// <param name="asyncDelegate"></param>
        /// <param name="asyncContext"></param>
        [AsyncCb(typeof(BorderSearchGetViewReponse))]
        public void GetView(AsyncCBHandler asyncDelegate, object asyncContext) 
        {
            asyncDelegate?.Invoke(asyncContext, getViewReponse);
        }

潘栩锋's avatar
潘栩锋 committed
931 932
        public bool Load() 
        {
933
            return BorderSearchJsonDb.Load(this, param_path);
潘栩锋's avatar
潘栩锋 committed
934 935 936
        }
        public bool Save() 
        {
937
            return BorderSearchJsonDb.Save(this, param_path);
潘栩锋's avatar
潘栩锋 committed
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
        }

        public void Apply() 
        {
            Save();
        }
        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string propertyname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyname));
            }
        }

        #endregion

957
    }
潘栩锋's avatar
潘栩锋 committed
958

959 960 961
    public class BorderSearchJsonDb
    {
        static Mapper Mapper { get; } = new AutoMapper.Mapper(new MapperConfiguration(c =>
潘栩锋's avatar
潘栩锋 committed
962
        {
963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
            c.CreateMap<BorderSearch, BorderSearchJsonDb>().ReverseMap();
        }));
        public static bool Load(BorderSearch src, string filePath)
        {
            try
            {
                if (File.Exists(filePath))
                {
                    string json = File.ReadAllText(filePath);
                    var p = JsonConvert.DeserializeObject<BorderSearchJsonDb>(json);
                    Mapper.Map(p, src);
                    return true;
                }
            }
            catch
            {
                //异常,没有json 解码失败
            }

            return false;
潘栩锋's avatar
潘栩锋 committed
983
        }
984 985 986 987 988 989 990 991 992 993 994
        public static bool Save(BorderSearch src, string filePath)
        {
            var p = Mapper.Map<BorderSearchJsonDb>(src);
            try
            {
                File.WriteAllText(filePath, JsonConvert.SerializeObject(p, Formatting.Indented));
                return true;
            }
            catch
            {
                //异常,没有json 编码失败
潘栩锋's avatar
潘栩锋 committed
995

996 997 998 999 1000 1001
            }
            return false;

        }

        public bool Enable;
1002
        public bool IsOneMaterial;
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
        public bool IsBreakDetect = true;
        public RangeStruct Valid = new RangeStruct(200,7000);
        public bool TempADBySet;
        public int TempAD = 50000;
        public int TempRange = 500;
        public bool IsTempRangeByPercent  = true;
        public double TempRangePercent  = 0.02;
        public int N=20;
        public int SensorWidth=250;
        public int N2=400;
        public int N3=1000;
潘栩锋's avatar
潘栩锋 committed
1014 1015
    }
}