BlowingDetectCore.cs 34.1 KB
Newer Older
1 2 3 4 5 6 7 8 9
using FLY.Thick.Blowing.IService;
using Misc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

10
namespace FLY.Thick.Blowing.UI
11 12 13 14 15 16 17 18
{

    public class BlowingDetectCore:INotifyPropertyChanged
    {
        /// <summary>
        /// 离开限位 到 撞下一个限位 的 旋转架转动总角度 单位°
        /// </summary>
        public double RAngle { get; set; }
潘栩锋's avatar
潘栩锋 committed
19

20 21 22 23
        /// <summary>
        /// 辊周长,单位mm
        /// </summary>
        public double RollPerimeter;
潘栩锋's avatar
潘栩锋 committed
24

25 26 27 28
        /// <summary>
        /// 辊周长 单位m
        /// </summary>
        double MofR => RollPerimeter / 1000.0;
潘栩锋's avatar
潘栩锋 committed
29

30 31 32 33
        /// <summary>
        /// 离开限位 到 撞下一个限位  的 旋转架转动时间, 需要初始值,以后测量出来的
        /// </summary>
        public TimeSpan RenZiJiaPeriod;
潘栩锋's avatar
潘栩锋 committed
34

35 36 37 38
        /// <summary>
        /// 人字架到测厚仪膜长 单位m
        /// </summary>
        public double FilmLength;
潘栩锋's avatar
潘栩锋 committed
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
        /// <summary>
        /// 累计旋转次数
        /// </summary>
        public int RotationCnt;

        /// <summary>
        /// 旋转架为立式,测厚仪安装在二牵引前面,膜距离 与 旋转角度 一起变化, 旋转角度越大 膜距离越大
        /// </summary>
        public bool Is3D;

        /// <summary>
        /// 使用编码器检测旋转架旋转位置模式
        /// </summary>
        public bool IsRPosMode;

        /// <summary>
        /// 人字架 离开限位 到 撞下一个限位 的 旋转架转动总脉冲 单位 脉冲
        /// </summary>
        public int RPosOfR;

        /// <summary>
        /// 原点旋转脉冲 上一次撞限位开关0时, 旋转架脉冲
        /// </summary>
        public int OrgRPos;

        /// <summary>
        /// 刚开机,信号列表一个信号都没有,创造一个时间点,作为信号点
        /// </summary>
        public DateTime DefaultTime;

        /// <summary>
        /// 旋转脉冲列表
        /// </summary>
        public List<RPosData> mRPosList;

        /// <summary>
        /// 辊信号列表
        /// </summary>
        public List<RollCell2> mRollList;
        /// <summary>
        /// 转向信号列表
        /// </summary>
        public List<LimitCell> mLimitList;

        /// <summary>
        /// 当为 立式旋转架时,膜距离与旋转角度有关系.
        /// 列表的长度为 360, 1°一个膜距离增量。
        /// 列表的头为-180°,尾为180°
        /// 
        /// FilmLength+mFilmLength3D[angle] 为真实膜距离
        /// </summary>
        [PropertyChanged.DoNotCheckEquality]
        public List<double> mFilmLength3D { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// 
        /// </summary>
        public BlowingDetectCore()
        {
            this.PropertyChanged += BlowingDetectCore_PropertyChanged;
        }

        public void Init(
            double rAngle,
            double rollPerimeter,
            TimeSpan renZiJiaPeriod,
            double filmLength,
            bool is3d,
            List<RollCell> rollList,
            List<LimitCell> limitList,
            List<double> filmLength3D,
            DateTime defaultTime,
            bool isRPosMode,
            int orgRPos,
            int rPosOfR,
            List<RPosData> rPosList
            )
        {
潘栩锋's avatar
潘栩锋 committed
120 121 122 123 124 125 126 127 128
            RAngle = rAngle;
            RollPerimeter = rollPerimeter;
            RenZiJiaPeriod = renZiJiaPeriod;
            FilmLength = filmLength;
            Is3D = is3d;
            IsRPosMode = isRPosMode;
            RPosOfR = rPosOfR;
            DefaultTime = defaultTime;
            OrgRPos = orgRPos;
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

            mRPosList = rPosList;
            mFilmLength3D = filmLength3D;

            mLimitList = limitList;

            RotationCnt = limitList.Count();

            //组装 rPosList 与 rollList
            mRollList = rollList.Select(r => new RollCell2() { Time = r.Time, SysTime = r.SysTime }).ToList();
        }

        private void BlowingDetectCore_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(RAngle))
            {
                RollList_ResetAngle();
            }
            else if (e.PropertyName == nameof(mFilmLength3D))
            {
                RollList_ResetAngle();
            }
        }



        int RotationCntToLimitIndex(int rotationcnt)
        {
            if (RotationCnt == 0)
                return -1;
            else
                return rotationcnt - 1 - (RotationCnt - mLimitList.Count);
        }
        /// <summary>
        /// 正反转信号列表中的序号,转旋转次数
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        int LimitIndexToRotationCnt(int index)
        {
            if (RotationCnt == 0)
                return 0;
            else
                return RotationCnt - mLimitList.Count + index + 1;
        }

        /// <summary>
        /// 查表 通过角度 获取 膜距离
        /// </summary>
        /// <param name="angle">-180~180</param>
        /// <returns></returns>
        double GetFilmLength3D(double angle)
        {
            if (mFilmLength3D == null || mFilmLength3D.Count() == 0)
                return FilmLength;


            angle += 180;

            int idx = (int)(mFilmLength3D.Count() * angle / 360);
            if (idx < 0) idx = 0;
            else if (idx >= mFilmLength3D.Count()) idx = mFilmLength3D.Count() - 1;
            return FilmLength + mFilmLength3D[idx];
        }

        /// <summary>
        /// 获取当前在膜上的测量点,对应于膜泡的角度信息, 当时间点,比LimitList.Last.dt+RenZiJiaPeriod还要后,更新RenZiJiaPeriod
        /// 返回 -1, 当前的时间点在列表以前;
        /// 返回 0, 当前的时间点在列表中;
        /// 返回 1, 当前的时间点在列表的未来;
        /// </summary>
        /// <param name="filmWidth">被压扁后的膜宽度</param>
        /// <param name="filmPosH">探头所在膜的横向位置</param>
        /// <param name="dt">测量的时间点</param>
        /// <returns></returns>
        public virtual int GetFilmInfo(out FilmInfo filminfo, DateTime dt, double filmWidth, double filmPosH)
        {

            filminfo = null;

            //与0°的偏移
            double a = filmPosH / filmWidth * 180;

            if ((a < 0) || (a > 180))
            {
                return -1;//不在膜的范围内
            }
            if (dt == DateTime.MinValue)
            {
                //TODO, BUG
                return -1;//异常!!!
            }


            int ret = GetAgo(dt, out DateTime ago_dt, out int ago_index);

225
            if (ret > 0)
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
                return ret;//发生在未来不处理!!!


            bool ret2 = GetAngle(a, ago_dt, out double angle0, out double angle1, out Misc.DIRECTION direction, out int rotationcnt, out bool inCV);
            if (!ret2)
                return -1;//异常!!!

            double velocity = RollList_GetVelocity(ago_index);


            filminfo = new FilmInfo()
            {
                angle1 = angle0,
                angle2 = angle1,
                filmVelocity = velocity,
                direction = direction,
                rotationCnt = rotationcnt,
                inCV = inCV
            };
            return ret;
        }

        #region 人字架时间点<-->24m后膜时间点
        void RollList_ResetAngle()
        {
            if (mRollList == null || mRollList.Count() == 0)
                return;

            for (int i = 0; i < mRollList.Count; i++)
            {
                mRollList[i].Angle = double.NaN;
            }

        }
        double RollList_GetAngle(int index) 
        {
            if (!double.IsNaN(mRollList[index].Angle))
            {
                return mRollList[index].Angle;
            }
            else
            {
                if (!GetAngle(mRollList[index].Time, out double angle))
                {
                    //不能找到角度
                    return double.NaN;
                }
                mRollList[index].Angle = angle;
                return mRollList[index].Angle;
            }
            
        }
        /// <summary>
        /// 3D旋转架,向前找,filmPosition  &gt;= filmPos0 + filmLength0 膜距离前的点
        /// </summary>
        /// <param name="index">开始查找的序号</param>
        /// <param name="filmPosition">纵向位置</param>
        /// <returns>返回查找到的序号</returns>
        int RollList_FindIndex3d24mPre(int index, double filmPosition) 
        {
            for (int i = index; i >= 0; i--)
            {
                double angle = RollList_GetAngle(i);
                if (double.IsNaN(angle))
                    return -1;//不能找到角度
                
                double filmPos0 = i * MofR;
                double filmLength0 = GetFilmLength3D(angle);
                //膜距离后的纵向位置
                double pos24m0 = filmPos0 + filmLength0;
                if (filmPosition >= pos24m0)
                {
                    return i;
                }
            }
            return -1;
        }


        /// <summary>
        /// 3D旋转架,向后找,filmPosition  &lt;= filmPos1 + filmLength1 膜距离前的点
        /// </summary>
        /// <param name="index">开始查找的序号</param>
        /// <param name="filmPosition">纵向位置</param>
        /// <returns>返回查找到的序号</returns>
        int RollList_FindIndex3d24mNext(int index, double filmPosition)
        {
            for (int i = index; i < mRollList.Count; i++)
            {
                double angle = RollList_GetAngle(i);
                if (double.IsNaN(angle))
                    return -1;//不能找到角度

                double filmPos1 = i * MofR;
                double filmLength1 = GetFilmLength3D(angle);
                //膜距离后的纵向位置
                double pos24m1 = filmPos1 + filmLength1;
                if (filmPosition <= pos24m1)
                {
                    return i;
                }
            }
            return 1;
        }

        /// <summary>
        /// 输入当前时间点,找到 刚出人字架时的时间点, 及那时候的线速度;
        /// 返回 -1, 当前的时间点在列表以前;
        /// 返回 0, 当前的时间点在列表中;
        /// 返回 1, 当前的时间点在列表的未来;
        /// </summary>
        /// <param name="dt">当前时间</param>
        /// <param name="ago_dt">24米真实长度</param>
        /// <returns></returns>
        int GetAgo(DateTime dt, out DateTime ago_dt, out int ago_index)
        {
            if (Is3D)
343 344 345 346 347 348
            {
                var ret = GetAgo3D(dt, out ago_dt, out ago_index);
                if (ret != -1)
                    return ret;
                return GetAgoFix(dt, out ago_dt, out ago_index);
            }
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 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 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
            else
                return GetAgoFix(dt, out ago_dt, out ago_index);
        }

        /// <summary>
        /// 膜距离固定 获取膜距离前的时间点
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="ago_dt"></param>
        /// <param name="ago_index"></param>
        /// <returns></returns>
        int GetAgoFix(DateTime dt, out DateTime ago_dt, out int ago_index)
        {
            ago_dt = DateTime.MinValue;
            ago_index = -1;
            int index;
            int ret = RollList_FindIndex(dt, out index);
            if (ret == 1)//发生在未来,什么都不干
                return ret;

            //纵向位置
            double filmPosition = RollList_GetPosition(index, dt);


            double filmPos0 = filmPosition - FilmLength;
            ret = RollList_GetDateTime(filmPos0, out index, out ago_dt);
            ago_index = index;
            return ret;
            

        }
        /// <summary>
        /// 3d旋转架的 获取膜距离前的时间点
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="ago_dt"></param>
        /// <param name="ago_index"></param>
        /// <returns></returns>
        int GetAgo3D(DateTime dt, out DateTime ago_dt, out int ago_index)
        {
            ago_dt = DateTime.MinValue;
            ago_index = -1;
            int index;
            int ret = RollList_FindIndex(dt, out index);
            if (ret == 1)//发生在未来,什么都不干
                return ret;

            //纵向位置
            double filmPosition = RollList_GetPosition(index, dt);

            //快速移动到 开始查找rollList 的序号
            int beginIndex = (int)((filmPosition - FilmLength) / MofR);
            if (beginIndex < 0)
                return -1;//发生在过去,什么都不干
            int index0;
            int index1;
            index = RollList_FindIndex3d24mPre(beginIndex, filmPosition);
            if (index == beginIndex)
            {
                //第0个就找到,还需要再向后找确定
                index = RollList_FindIndex3d24mNext(beginIndex + 1, filmPosition);
                index0 = index - 1;
                index1 = index;
            }
            else
            {
                if (index < 0)
                {
                    return -1;//找不到,发生在过去
                }

                index0 = index;
                index1 = index + 1;
            }
            if (index0 < 0)
                return -1;
            if (index1 >= mRollList.Count())
                return 1;

            DateTime dt0 = mRollList[index0].Time;
            double angle0 = RollList_GetAngle(index0);
            if (double.IsNaN(angle0))
                return -1;//不能找到角度

            double filmPos0 = index0 * MofR;
            double filmLength0 = GetFilmLength3D(angle0);
            //膜距离后的纵向位置
            double pos24m0 = filmPos0 + filmLength0;

            DateTime dt1 = mRollList[index1].Time;
            double angle1 = RollList_GetAngle(index1);
            if (double.IsNaN(angle1))
                return -1;//不能找到角度

            double filmPos1 = (index1) * MofR;
            double filmLength1 = GetFilmLength3D(angle1);
            //膜距离后的纵向位置
            double pos24m1 = filmPos1 + filmLength1;

            ago_dt = dt0.Add(new TimeSpan((long)((dt1 - dt0).Ticks * (filmPosition - pos24m0) / (pos24m1 - pos24m0))));
            ago_index = index0;
            return 0;
        }
        #endregion

        /// <summary>
        /// 计算出角度 -180~180 恒速阶段
        /// </summary>
        /// <param name="dtEnd">旋转结束时间点</param>
        /// <param name="dtStart">旋转开始时间点</param>
        /// <param name="noStart">开始时间点对应的限位 0 or 1</param>
        /// <param name="dt">用于转换为角度的时间</param>
        /// <param name="angle">输出: 角度</param>
        /// <param name="direction">输出: 方向</param>
        void LimitListCal_ConstantVelocity(DateTime dtEnd, DateTime dtStart, int noStart, DateTime dt, out double angle, out Misc.DIRECTION direction)
        {
            TimeSpan ts1 = dt - dtStart;
            TimeSpan ts2 = dtEnd - dtStart;

            angle = RAngle * ts1.Ticks / ts2.Ticks;

            if (noStart == 0)
            {
                angle = -RAngle / 2 + angle;
                direction = Misc.DIRECTION.FORWARD;
            }
            else
            {
                angle = RAngle / 2 - angle;
                direction = Misc.DIRECTION.BACKWARD;
            }
        }

        /// <summary>
        /// 时间点查找旋转角度 -180~180
        /// </summary>
        /// <param name="dt">时间</param>
        /// <param name="modifyparam">当dt 发生在限位列表的未来,是否修改参数,如RenZiJiaPeriod</param>
        /// <param name="angle">角度</param>
        /// <param name="direction">旋转的方向</param>
        /// <param name="rotationCnt">旋转的次数</param>
        /// <param name="inCV">在恒速区</param>
        public bool GetAngle(DateTime dt, bool modifyparam, out double angle, out Misc.DIRECTION direction, out int rotationCnt, out bool inCV)
        {
            if (IsRPosMode)
            {
                return GetAngleByRPos(dt, out angle, out direction, out rotationCnt, out inCV);
            }
            else
            {
                return GetAngleByTime(dt, modifyparam, out angle, out direction, out rotationCnt, out inCV);
            }
        }

        /// <summary>
        /// 时间点查找旋转角度 -180~180
        /// </summary>
        /// <param name="dt">时间点</param>
        /// <param name="angle">输出 旋转角度</param>
        bool GetAngle(DateTime dt, out double angle)
        {
            //不修改旋转速度!!!!!!!!!!!!
            bool ret = GetAngle(dt, false, out angle, out DIRECTION direction, out int rotationCnt, out bool inCV);
            if (ret == false)
                return false;

            return true;
        }

        /// <summary>
        /// 时间点查找旋转角度 -180~180 时间预测版
        /// </summary>
        /// <param name="dt">时间</param>
        /// <param name="modifyparam">当dt 发生在限位列表的未来,是否修改参数,如RenZiJiaPeriod</param>
        /// <param name="angle">角度</param>
        /// <param name="direction">旋转的方向</param>
        /// <param name="rotationCnt">旋转的次数</param>
        /// <param name="inCV">在恒速区</param>
        bool GetAngleByTime(DateTime dt, bool modifyparam, out double angle, out Misc.DIRECTION direction, out int rotationCnt, out bool inCV)
        {
            int index;
            ListFindIndexResult ret = LimitList_FindIndex(dt, out index);

            DateTime dt_start;
            DateTime dt_end;
            int no;

            switch (ret)
            {
                case ListFindIndexResult.Future://发生在未来 
                    {
                        if (dt < DefaultTime)
                            DefaultTime = dt - TimeSpan.FromSeconds(10);

                        //检测是否需要修正 RenZiJiaPeriod
                        if (mLimitList.Count == 0)
                        {
                            dt_start = DefaultTime;
                            no = 0;
                        }
                        else
                        {
                            dt_start = mLimitList[index].dt_end;
                            no = mLimitList[index].no;
                        }
                        rotationCnt = LimitIndexToRotationCnt(index);

                        if (dt > (dt_start + RenZiJiaPeriod))
                        {
                            if (modifyparam)
                            {
                                //修正!!!!
                                RenZiJiaPeriod = dt - dt_start + TimeSpan.FromSeconds(10);
                            }
                            else
                            {
                                dt = dt_start + RenZiJiaPeriod;//只是为了看,随便了。 时间停止下来
                            }
                        }

                        dt_end = dt_start + RenZiJiaPeriod;
                        inCV = true;
                        LimitListCal_ConstantVelocity(dt_end, dt_start, no, dt, out angle, out direction);
                    }
                    break;
                case ListFindIndexResult.BetweenCells:
                    {
                        dt_start = mLimitList[index].dt_end;
                        dt_end = mLimitList[index + 1].dt_begin;
                        no = mLimitList[index].no;
                        rotationCnt = LimitIndexToRotationCnt(index);
                        inCV = true;
                        LimitListCal_ConstantVelocity(dt_end, dt_start, no, dt, out angle, out direction);
                    }
                    break;
                case ListFindIndexResult.Past://过去 
                    {
                        if (DefaultTime >= mLimitList[0].dt_begin)
                        {
                            //异常?!!!!
                            DefaultTime = mLimitList[0].dt_begin - RenZiJiaPeriod;
                        }
                        dt_start = DefaultTime;

                        dt_end = mLimitList[0].dt_begin;
                        no = (mLimitList[0].no == 0) ? 1 : 0;
                        rotationCnt = LimitIndexToRotationCnt(-1);
                        inCV = true;
                        LimitListCal_ConstantVelocity(dt_end, dt_start, no, dt, out angle, out direction);
                    }
                    break;
                //case ListFindIndexResult.InCell:
                //case ListFindIndexResult.MaybeInCell:
                default:
                    {

                        //发生在 撞换向信号中间, 简单当它没有动就算了
                        inCV = false;
                        no = mLimitList[index].no;
                        rotationCnt = LimitIndexToRotationCnt(index);
                        if (no == 0)
                        {
                            angle = -RAngle / 2;
                            direction = DIRECTION.FORWARD;
                        }
                        else
                        {
                            angle = RAngle / 2;
                            direction = DIRECTION.BACKWARD;
                        }
                    }
                    break;
            }
            return true;
        }


        /// <summary>
        /// 时间点查找旋转角度 -180~180  脉冲版
        /// </summary>
        /// <param name="dt">时间</param>
        /// <param name="angle">角度</param>
        /// <param name="direction">旋转的方向</param>
        /// <param name="rotationCnt">旋转的次数</param>
        /// <param name="inCV">在恒速区</param>
        bool GetAngleByRPos(DateTime dt, out double angle, out Misc.DIRECTION direction, out int rotationCnt, out bool inCV)
        {
            angle = 0;
            rotationCnt = 0;
            direction = DIRECTION.FORWARD;
            inCV = true;

            int retPos = RPosList_FindIndex(dt, out int p_index);

            ListFindIndexResult ret = LimitList_FindIndex(dt, out int l_index);

            if (retPos == 1)
            {
                //发生在未来
                return false;
            }

            var posData = mRPosList[p_index + 1];
            TimeSpan ts = posData.Time - dt;
            int globalRPos = (int)(posData.GlobalRPos - ts.TotalSeconds * posData.Speed);

            int no;

            switch (ret)
            {
                case ListFindIndexResult.Future://发生在未来 
                    {
                        //检测是否需要修正 RenZiJiaPeriod
                        if (mLimitList.Count == 0)
                            no = 0;
                        else
                            no = mLimitList[l_index].no;
                        
                        angle = GetAngleByGlobalRPos(globalRPos);
                        rotationCnt = LimitIndexToRotationCnt(l_index);
                        direction = no == 0 ? DIRECTION.FORWARD : DIRECTION.BACKWARD;

                        inCV = true;
                    }
                    break;
                case ListFindIndexResult.BetweenCells:
                    {
                        no = mLimitList[l_index].no;
                        angle = GetAngleByGlobalRPos(globalRPos);

                        rotationCnt = LimitIndexToRotationCnt(l_index);
                        direction = no == 0 ? DIRECTION.FORWARD : DIRECTION.BACKWARD;

                        inCV = true;
                    }
                    break;
                case ListFindIndexResult.Past://过去 
                    {
                        no = mLimitList[0].no == 0 ? 1 : 0;
                        angle = GetAngleByGlobalRPos(globalRPos);

                        rotationCnt = LimitIndexToRotationCnt(-1);
                        direction = no == 0 ? DIRECTION.FORWARD : DIRECTION.BACKWARD;

                        inCV = true;
                    }
                    break;
                //case ListFindIndexResult.InCell:
                //case ListFindIndexResult.MaybeInCell:
                default:
                    {
                        //发生在 撞换向信号中间
                        no = mLimitList[l_index].no;
                        angle = GetAngleByGlobalRPos(globalRPos);

                        rotationCnt = LimitIndexToRotationCnt(l_index);
                        direction = no == 0 ? DIRECTION.FORWARD : DIRECTION.BACKWARD;

                    }
                    break;
            }

            return true;
        }


        public double GetAngleByGlobalRPos(int globalRPos)
        {
            //OrgRPos 为 -RAngle/2 

            int rPos = globalRPos - OrgRPos;

            //360°对应的旋转脉冲
            //int rPosOf360 = (int)(RPosOfR * 360 / RAngle);

            //while (rPos < 0)
            //    rPos += rPosOf360;
            //while (rPos > rPosOf360)
            //    rPos -= rPosOf360;

            double angle = RAngle * rPos / RPosOfR;

            //角度范围是 -360°~ +360°
            angle -=  RAngle / 2;
            return angle;
        }

        #region RollList 查找
        /// <summary>
        /// 获取 [idx,idx+1] 之间 的 位置, 单位m
        /// </summary>
        /// <param name="idx"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        double RollList_GetPosition(int idx, DateTime dt)
        {
            DateTime dt1 = mRollList[idx + 1].Time;
            double filmPos1 = (idx + 1) * MofR;
            DateTime dt0 = mRollList[idx].Time;
            double filmPos0 = (idx) * MofR;

            return (filmPos1 - filmPos0) * (dt - dt0).Ticks / (dt1 - dt0).Ticks + filmPos0;
        }
        /// <summary>
        /// 获取 [idx,idx+1] 之间 的 速度 m/min
        /// </summary>
        /// <param name="idx"></param>
        /// <returns></returns>
        double RollList_GetVelocity(int idx)
        {
            DateTime dt1 = mRollList[idx + 1].SysTime;
            DateTime dt2 = mRollList[idx].SysTime;

            return MofR / dt1.Subtract(dt2).TotalMinutes;
        }
        /// <summary>
        /// 获取 [idx,idx+1] 之间 的 时间
        /// </summary>
        /// <param name="idx"></param>
        /// <param name="filmPosition">纵向位置m</param>
        /// <returns></returns>
        DateTime RollList_GetDateTime(int idx, double filmPosition)
        {
            //TODO 经常出错,position 无限小,idx=0
            DateTime dt1 = mRollList[idx + 1].Time;
            double filmPos1 = (idx + 1) * MofR;
            DateTime dt2 = mRollList[idx].Time;
            double filmPos2 = (idx) * MofR;

            return dt2.Add(new TimeSpan((long)((dt1 - dt2).Ticks * (filmPosition - filmPos2) / (filmPos1 - filmPos2))));
        }

        /// <summary>
        /// 通过 filmPosition 查找时间
        /// 返回 -1,这个过去的时间;
        /// 返回 0,在列表中;
        /// 返回 1,发生在未来的时间;
        /// </summary>
        /// <param name="filmPosition">单位m</param>
        /// <returns></returns>
        int RollList_GetDateTime(double filmPosition, out int idx, out DateTime dt)
        {
            idx = 0;
            dt = DateTime.MinValue;
793 794 795
            if (mRollList.Count == 0)
                return 1;

796
            if (filmPosition < 0)
797 798 799
            {
                //发生在过去。 只能猜
                dt = RollList_GetDateTime(idx, filmPosition);
800
                return -1;
801
            }
802 803

            idx = (int)(filmPosition / MofR);
804 805
            if (idx >= mRollList.Count - 1)
            {
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 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 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 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
                return 1;
            }
            dt = RollList_GetDateTime(idx, filmPosition);
            return 0;
        }



        /// <summary>
        /// dt 这个时间,在是 index 与 index+1 中间发生的
        /// 返回 -1,这个过去的时间;
        /// 返回 0,在列表中;
        /// 返回 1,发生在未来的时间;
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        int RollList_FindIndex(DateTime dt, out int index)
        {
            index = 0;
            if (mRollList.Count <= 1)//没数据,所以是发生在未来的
                return 1;

            for (int i = mRollList.Count - 1; i >= 0; i--)
            {
                if (dt >= mRollList[i].Time)
                {
                    //找到了
                    if (i == (mRollList.Count - 1))//这个是未来的时间点,还没发生!!! 
                    {
                        index = i;
                        return 1;
                    }
                    else
                    {
                        index = i;
                        return 0;
                    }
                }
            }

            //这个在过去的时间
            return -1;
        }

        #endregion


        enum ListFindIndexResult
        {
            /// <summary>
            /// 发生在未来, index.dt_end ~ 未来, 可能 index都还没有发生
            /// </summary>
            Future,
            /// <summary>
            /// 发生在index.dt_begin ~ index.dt_end
            /// </summary>
            InCell,
            /// <summary>
            /// 应该发生在index.dt_begin ~ index.dt_end, 但 index.dt_end还没有数据
            /// </summary>
            MaybeInCell,
            /// <summary>
            /// 发生在 index.dt_end ~ (index+1).dt_begin
            /// </summary>
            BetweenCells,
            /// <summary>
            /// 发生在 过去
            /// </summary>
            Past
        }
        /// <summary>
        /// 找到 列表中 (开始时间,结束时间) 小于 dt 的信号点
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        ListFindIndexResult LimitList_FindIndex(DateTime dt, out int index)
        {
            index = 0;
            
            if (mLimitList.Count <= 0)//没数据,所以是发生在未来的
                return ListFindIndexResult.Future;


            for (int i = mLimitList.Count - 1; i >= 0; i--)
            {
                LimitCell lc = mLimitList[i];
                if (dt >= lc.dt_begin)
                {
                    //找到了
                    index = i;
                    if (lc.dt_end == DateTime.MinValue)
                    {
                        return ListFindIndexResult.MaybeInCell;
                    }
                    else if (dt <= mLimitList[i].dt_end)
                    {
                        return ListFindIndexResult.InCell;
                    }
                    else if (i == (mLimitList.Count - 1))//这个是未来的时间点,还没发生!!! 
                    {
                        return ListFindIndexResult.Future;
                    }
                    else
                    {
                        return ListFindIndexResult.BetweenCells;
                    }
                }
            }

            //这个在过去的时间
            return ListFindIndexResult.Past;
        }



        /// <summary>
        /// dt 这个时间,在是 index 与 index+1 中间发生的
        /// 返回 -1,这个过去的时间;
        /// 返回 0,在列表中;
        /// 返回 1,发生在未来的时间;
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        int RPosList_FindIndex(DateTime dt, out int index)
        {

            index = 0;
            if (mRPosList.Count <= 1)//没数据,所以是发生在未来的
                return 1;

            for (int i = mRPosList.Count - 1; i >= 0; i--)
            {
                if (dt >= mRPosList[i].Time)
                {
                    //找到了
                    if (i == (mRPosList.Count - 1))//这个是未来的时间点,还没发生!!! 
                    {
                        index = i;
                        return 1;
                    }
                    else
                    {
                        index = i;
                        return 0;
                    }
                }
            }

            //这个在过去的时间
            return -1;
        }

        /// <summary>
        /// 获取膜角度, TODO
        /// </summary>
        /// <param name="a">与0°的偏移, 0~180</param>
        /// <param name="dt">时间点</param>
        /// <param name="angle0">上面的膜对应的膜角度</param>
        /// <param name="angle1">下面的膜对应的膜角度</param>
        /// <param name="direction">人字架正转?反转? FIX, 停止!</param>
        /// <param name="rotationCnt">扫描次数</param>
        /// <param name="inCV">在恒速阶段</param>
        bool GetAngle(double a, DateTime dt, out double angle0, out double angle1, out Misc.DIRECTION direction, out int rotationCnt, out bool inCV)
        {
            angle0 = 0;
            angle1 = 0;
            direction = Misc.DIRECTION.FORWARD;
            rotationCnt = 0;
            inCV = true;
            if (a > 180 || a < 0)
                return false;

            //压扁对面的角度
            double a_other = 360 - a;

            //不修改旋转速度!!!!!!!!!!!!
            bool ret = GetAngle(dt, false, out double angle, out direction, out rotationCnt, out inCV);
            if (ret == false)
                return false;

            a = GetValidAngle(a + angle);
            a_other = GetValidAngle(a_other + angle);

            angle0 = a;
            angle1 = a_other;
            return true;
        }

        /// <summary>
        /// 限制角度在 [0~360)
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        double GetValidAngle(double a)
        {
            if (a >= 360)
                a -= 360;
            else if (a < 0)
                a += 360;
            return a;
        }
    }
}