BlowingDetect.cs 83.9 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 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 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 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 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Misc;
using FLY.Thick.Base.IService;
using FLY.Thick.Blowing.IService;
using FObjBase;

namespace FLY.Thick.Blowing.Server
{
    /// <summary>
    /// <para>1.每次收到限位信号(有->没 and 没->有),都会触发 FilmInfoChangedEvent ;时间为上一次限位信号</para>
    /// <para>2.改变辊周长,膜长,都会触发 FilmInfoChangedEvent ;时间为前3次限位信号</para>
    /// <para>3.每次查找FilmInfo, 时间比 当前的限位信号+RenZiJiaPeriod 还要未来,修正 RenZiJiaPeriod,</para>
    /// <para>RenZiJiaPeriod预留10s的余量,避免不断修正</para>
    /// <para>并且触发FilmInfoChangedEvent 时间为上一次限位信号</para>
    /// </summary>
    public class BlowingDetect : IBlowingDetect, INotifyPropertyChanged, ISaveToXml
    {
        #region MARKNO
        const int MARKNO_DELAY0 = 1;
        const int MARKNO_DELAY1 = 2;
        #endregion
        
        #region 输入口定义
        /// <summary>
        /// 限位0 对应输入位
        /// </summary>
        const int Limit0_bit = 9;



        /// <summary>
        /// 限位1 对应输入位
        /// </summary>
        const int Limit1_bit = 10;



        /// <summary>
        /// 辊信号 对应输入位
        /// </summary>
        const int Roll_bit = 11;
        #endregion
        
        #region 自定义类
        public class RollCell
        {
            public RollCell Clone()
            {
                RollCell r = new RollCell();
                r.dt = dt;
                return r;
            }
            public DateTime dt;
            public override string ToString()
            {
                return dt.ToString();
            }
        }

        /// <summary>
        /// 撞向信号
        /// </summary>
        public class LimitCell
        {
            /// <summary>
            /// 创建副本
            /// </summary>
            /// <returns></returns>
            public LimitCell Clone()
            {
                LimitCell l = new LimitCell();

                l.dt_begin = dt_begin;
                l.dt_end = dt_end;
                l.dt_mid = dt_mid;
                l.isDouble = isDouble;
                l.isVirtual = isVirtual;
                l.no = no;
                return l;
            }
            /// <summary>
            /// 信号开始
            /// </summary>
            public DateTime dt_begin = DateTime.MinValue;
            /// <summary>
            /// 信号结束
            /// </summary>
            public DateTime dt_end = DateTime.MinValue;
            /// <summary>
            /// 信号序号 0 或 1
            /// </summary>
            public int no = 0;
            /// <summary>
            /// 两次撞转向 = 1次信号
            /// </summary>
            public bool isDouble = false;
            /// <summary>
            /// 当为两次撞转向, 第1次撞结束时间。用于当两次信号发生时间间隔很长,现在肯定就只有一次撞转向而已
            /// </summary>
            public DateTime dt_mid = DateTime.MinValue;

            /// <summary>
            /// 这是一个虚拟的信号
            /// </summary>
            public bool isVirtual = false;
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public override string ToString()
            {
                return no.ToString() + " " + ((isDouble) ? "D" : "S") + " (" + dt_begin.ToString() + "-" + dt_end.ToString() + ") " + (isVirtual ? "V" : "");
            }
        }
        #endregion

        #region 成员变量
        #region 参数
        private double rangle=350;
        /// <summary>
        /// 离开限位 到 撞下一个限位 的 旋转架转动总角度 单位°
        /// </summary>
        public double RAngle
        {
            get { return rangle; }
            set
            {
                if (rangle != value)
                {
                    rangle = value;
                    NotifyPropertyChanged("RAngle");
                }
            }
        }

        private TimeSpan defaultRPeriod;
        /// <summary>
        /// 人字架 旋转1周 设置 时间。 
        /// 刚开机时,RenZiJiaPeriod = DefaultRPeriod;
        /// 异常时,RenZiJiaPeriod = DefaultRPeriod;
        /// </summary>
        public TimeSpan DefaultRPeriod
        {
            get { return defaultRPeriod; }
            set
            {
                if (defaultRPeriod != value)
                {
                    defaultRPeriod = value;
                    NotifyPropertyChanged("DefaultRPeriod");
                }
            }
        }


        private TimeSpan renzijiaperiod;
        /// <summary>
        /// 离开限位 到 撞下一个限位  的 旋转架转动时间, 需要初始值,以后测量出来的
        /// </summary>
        public TimeSpan RenZiJiaPeriod
        {
            get { return renzijiaperiod; }
            protected set
            {
                if (renzijiaperiod != value)
                {
                    renzijiaperiod = value;
                    NotifyPropertyChanged("RenZiJiaPeriod");
                }
            }
        }
        private TimeSpan accdectime;
        /// <summary>
        /// 加速减速时间, 加速减速时间+正反转冷却时间 = 撞限位 到 离开限位 的时间
        /// </summary>
        public TimeSpan AccDecTime
        {
            get
            {
                return accdectime;
            }
            set
            {
                if (accdectime != value)
                {
                    accdectime = value;
                    NotifyPropertyChanged("AccDecTime");
                }
            }
        }

        private BlowingSignType signtype = BlowingSignType.Short;
        /// <summary>
        /// 信号模式, 短信号模式(只撞一下),长信号模式(只要是转着,信号就长亮,直到换向)
        /// </summary>
        public BlowingSignType SignType
        {
            get { return signtype; }
            set
            {
                if (signtype != value)
                {
                    signtype = value;
                    NotifyPropertyChanged("SignType");
                }
            }
        }

        private bool isSign0Double = false;
        /// <summary>
        /// 信号0 撞2次。 这个能自动设置。 两个撞同一信号 间隔30秒内,算一个换向信号。 
        /// 当为2次撞时:  StartTime = 第1次撞的StartTime, EndTime = 第2次撞的EndTime
        /// </summary>
        public bool IsSign0Double
        {
            get { return isSign0Double; }
            set
            {
                if (isSign0Double != value)
                {
                    isSign0Double = value;
                    NotifyPropertyChanged("IsSign0Double");
                }
            }
        }

        private bool isSign1Double = false;
        /// <summary>
        /// 信号1 撞2次。 这个能自动设置。 两个撞同一信号 间隔30秒内,算一个换向信号。 
        /// 当为2次撞时:  StartTime = 第1次撞的StartTime, EndTime = 第2次撞的EndTime
        /// </summary>
        public bool IsSign1Double
        {
            get { return isSign1Double; }
            set
            {
                if (isSign1Double != value)
                {
                    isSign1Double = value;
                    NotifyPropertyChanged("IsSign1Double");
                }
            }
        }

        private TimeSpan limitsigntime;
        /// <summary>
        /// 撞限位 到 离开限位 的时间, 需要初始值,以后测量出来的
        /// </summary>
        public TimeSpan LimitSignTime
        {
            get { return limitsigntime; }
            set
            {
                if (limitsigntime != value)
                {
                    limitsigntime = value;
                    NotifyPropertyChanged("LimitSignTime");
                }
            }
        }

        private double rollperimeter;
        /// <summary>
        /// 辊周长,单位mm
        /// </summary>
        public double RollPerimeter
        {
            get { return rollperimeter; }
            set
            {
                if (rollperimeter != value)
                {
                    rollperimeter = value;
                    NotifyPropertyChanged("RollPerimeter");
                }
            }
        }


        private double filmlength;
        /// <summary>
        /// 人字架到测厚仪膜长 单位m
        /// </summary>
        public double FilmLength
        {
            get { return filmlength; }
            set
            {
                if (filmlength != value)
                {
                    filmlength = value;
                    NotifyPropertyChanged("FilmLength");
                }
            }
        }


        #endregion

        #region 状态

        private TimeSpan swapcooltime;
        /// <summary>
        /// 正反转冷却时间, 默认1s, 一定要小于撞限位 到 离开限位 的时间
        /// </summary>
        public TimeSpan SwapCoolTime
        {
            get { return swapcooltime; }
            protected set
            {
                if (swapcooltime != value)
                {
                    swapcooltime = value;
                    NotifyPropertyChanged("SwapCoolTime");
                }
            }
        }

        private double angle;
        /// <summary>
        /// 当前旋转架旋转角度
        /// </summary>
        public double Angle
        {
            get { return angle; }
            set
            {
                if (angle != value)
                {
                    angle = value;
                    NotifyPropertyChanged("Angle");
                }
            }
        }

        private Misc.DIRECTION direction;
        /// <summary>
        /// 当前方向
        /// </summary>
        public Misc.DIRECTION Direction
        {
            get
            {
                return direction;
            }
            set
            {
                if (direction != value)
                {
                    direction = value;
                    NotifyPropertyChanged("Direction");
                }
            }
        }

        private double filmvelocity;
        /// <summary>
        /// 当前线速度,单位 m/min
        /// </summary>
        public double FilmVelocity
        {
            get { return filmvelocity; }
            protected set
            {
                if (filmvelocity != value)
                {
                    filmvelocity = value;
                    NotifyPropertyChanged("FilmVelocity");
                }
            }
        }

        //最新的限位信号序号
        private int lastlimitno;
        /// <summary>
        /// 最新的限位信号序号
        /// </summary>
        public int LastLimitNo
        {
            get { return lastlimitno; }
            protected set
            {
                if (lastlimitno != value)
                {
                    lastlimitno = value;
                    NotifyPropertyChanged("LastLimitNo");
                }
            }
        }
        private TimeSpan pasttime;
        /// <summary>
        /// 最后一个限位信号 到当前已经消耗的时间
        /// </summary>
        public TimeSpan PastTime
        {
            get { return pasttime; }
            protected set
            {
                if (pasttime != value)
                {
                    pasttime = value;
                    NotifyPropertyChanged("PastTime");
                }
            }
        }
        #endregion

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

        private Misc.RList<RollCell> mRollList;

        /// <summary>
        /// 转向信号列表
        /// </summary>
        public Misc.RList<LimitCell> mLimitList;


        private bool issignfilter = false;
        /// <summary>
        /// 转向信号100ms 滤波
        /// </summary>
        public bool IsSignFilter
        {
            get { return issignfilter; }
            set {
                if (issignfilter != value)
                {
                    issignfilter = value;
                    NotifyPropertyChanged("IsSignFilter");
                }
            }
        }

        private bool isLackSignMode = false;
        /// <summary>
        /// 缺少信号模式
        /// 正反信号线接得不好,有时候会丢失了。 
        /// 当前的旋转时间是上一次的1.1倍时,在以前的位置增加信号!!!!
        /// 当有连续两次相同的信号,且信号差刚好是旋转时间的1.9~2.1倍时,证明真的是缺少信号
        /// </summary>
        public bool IsLackSignMode
        {
            get { return isLackSignMode; }
            set {
                if (isLackSignMode != value)
                {
                    isLackSignMode = value;
                    NotifyPropertyChanged("IsLackSignMode");
                }
            }
        }

        /// <summary>
        /// 还没处理的信号0 列表
        /// </summary>
        List<SignData> mSign0ListNoJudge = new List<SignData>();
        /// <summary>
        /// 还没处理的信号1 列表
        /// </summary>
        List<SignData> mSign1ListNoJudge = new List<SignData>();
        /// <summary>
        /// 与mLimitList 一样!!
        /// 存在的目的用于看看信号是否正常
        /// </summary>
        public RList<SignData> mSignList;
        private int firstsignbm = -1;
        /// <summary>
        /// 信号列表第一个记录序号
        /// </summary>
        public int FirstSignBM
        {
            get { return firstsignbm; }
            protected set
            {
                if (firstsignbm != value)
                {
                    firstsignbm = value;
                    NotifyPropertyChanged("FirstSignBM");
                }
            }
        }
        private int lastsignbm = -1;
        /// <summary>
        /// 信号列表最后一个记录序号
        /// </summary>
        public int LastSignBM
        {
            get { return lastsignbm; }
            protected set
            {
                if (lastsignbm != value)
                {
                    lastsignbm = value;
                    NotifyPropertyChanged("LastSignBM");
                }
            }
        }



        //累计旋转次数
        private int rotation_cnt;
        /// <summary>
        /// 累计旋转次数
        /// </summary>
        public virtual int RotationCnt
        {
            get { return rotation_cnt; }
            protected set
            {
                if (rotation_cnt != value)
                {
                    rotation_cnt = value;
                    NotifyPropertyChanged("RotationCnt");
                }
            }
        }
        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>
        List<DateTime> mdtFilmInfoChangedEventList = new List<DateTime>();

        #endregion


        /// <summary>
        /// 
        /// </summary>
        public BlowingDetect()
        {
            RAngle = 337;//°
            DefaultRPeriod = TimeSpan.FromMinutes(8);
            RenZiJiaPeriod = DefaultRPeriod;
            AccDecTime = TimeSpan.FromSeconds(1);
            LimitSignTime = TimeSpan.FromSeconds(4);
            SignType = BlowingSignType.Short;
            IsSign0Double = false;
            IsSign1Double = false;
            IsLackSignMode = false;

            RollPerimeter = 314;//mm
            FilmLength = 25;//m

            SwapCoolTime = LimitSignTime - AccDecTime - AccDecTime;

            Angle = 0;//°
            Direction = Misc.DIRECTION.FORWARD;
            FilmVelocity = 0;//m/min

            mRollList = new Misc.RList<RollCell>(4000);//以50m/min 线速度,储存大于 30min数据
            mLimitList = new Misc.RList<LimitCell>(20);//人字架转10次的信号
            mSignList = new RList<SignData>(30);//人字架正反转信号
            LastLimitNo = -1;
            RotationCnt = 0;


            Clear();

            PropertyChanged += new PropertyChangedEventHandler(RenZiJiaAndFilmPositionDetect_PropertyChanged);

        }


        /// <summary>
        /// 使参数合法
        /// </summary>
        public void CheckParamErr()
        {
            if (RAngle < 100 || RAngle > 360)
            {
                RAngle = 350;
            }

            if ((DefaultRPeriod.TotalMinutes < 4) || (DefaultRPeriod.TotalMinutes > 18))
            {
                DefaultRPeriod = TimeSpan.FromMinutes(5.8);
            }

            if ((FilmLength < 0) || (FilmLength > 100))
            {
                FilmLength = 25;
            }

            if ((RollPerimeter < 100) || (RollPerimeter > 1000))
            {
                RollPerimeter = 314;
            }

            if (AccDecTime.TotalMilliseconds > 5000)
            {
                AccDecTime = TimeSpan.FromSeconds(1);
            }

            if (LimitSignTime.TotalSeconds > 30)
            {
                LimitSignTime = TimeSpan.FromSeconds(4);
            }

        }

        /// <summary>
        /// 触发膜信息修改事件
        /// </summary>
        /// <param name="dt">时间点</param>
        void TouchFilmInfoChanged(DateTime dt)
        {
            if (FilmInfoChangedEvent == null)
                return;

            mdtFilmInfoChangedEventList.Add(dt);

            TouchFilmInfoChanged();
        }
        /// <summary>
        /// 触发膜信息修改事件列表的全部事件
        /// </summary>
        void TouchFilmInfoChanged()
        {
            if (FilmInfoChangedEvent == null)
                return;

            while (mdtFilmInfoChangedEventList.Count > 0)
            {
                DateTime dt = mdtFilmInfoChangedEventList.First();
                DateTime dt_future;
                int ret = GetFuture(dt, out dt_future);
                if (ret == 1)//发生在信号列表的未来,留着,以后再触发!! 
                {
                    break;
                }
                else if (ret == 0)//发生在信号列表的中间
                {
                    //只需要触发最老的那个就够了!!!!!
                    mdtFilmInfoChangedEventList.Clear();
                    FilmInfoChangedEvent(this, new FilmInfoChangedEventArgs(dt_future));
                }
                else//发生在信号列表的以前
                {
                    //以前的事件不触发,除非就只有这么一个
                    mdtFilmInfoChangedEventList.RemoveAt(0);
                    if (mdtFilmInfoChangedEventList.Count == 0)
                    {
                        FilmInfoChangedEvent(this, new FilmInfoChangedEventArgs(dt_future));
                    }

                }
            }
        }
        //TODO
        void RenZiJiaAndFilmPositionDetect_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if ((e.PropertyName == "FilmLength") ||
                (e.PropertyName == "RollPerimeter"))
            {
                TouchFilmInfoChanged_lastLimit(4);
            }
            else if (e.PropertyName == "RenZiJiaPeriod")
            {
                TouchFilmInfoChanged_lastLimit(1);
            }
            else if ((e.PropertyName == "LimitSignTime") || (e.PropertyName == "AccDecTime"))
            {
                SwapCoolTime = LimitSignTime - AccDecTime - AccDecTime;
            }
            else if (e.PropertyName == "DefaultRPeriod")
            {
                RenZiJiaPeriod = DefaultRPeriod;
            }
        }
        /// <summary>
        /// 触发膜信息修改事件
        /// </summary>
        /// <param name="last_cnt">最后旋转几次</param>
        void TouchFilmInfoChanged_lastLimit(int last_cnt)
        {
            if (mLimitList.Count < last_cnt)
            {
                last_cnt = mLimitList.Count;
            }

            int idx = mLimitList.Count - last_cnt - 1;
            
            if (idx <= 0)
            {
                TouchFilmInfoChanged(mDefaultTime);
            }
            else
            {
                TouchFilmInfoChanged(mLimitList[idx].dt_begin);
            }
        }

        /// <summary>
        /// 创建完对象后,必须赋值
        /// </summary>
        /// <param name="flyad"></param>
        public void Init(FlyADBase.IFlyAD flyad)
        {
            flyad.IStatusChangedEvent += new FlyADBase.IStatusChangedEventHandler(flyad_IStatusChangedEvent);
            FObjBase.PollModule.Current.Poll_Config(FObjBase.PollModule.POLL_CONFIG.ADD, new FObjBase.PollModule.PollHandler(OnPoll));
        }


        /// <summary>
        /// 用于获取 bit 从 0->1->0  这样的上升沿 -> 下降沿 中间的时间
        /// 只是给辊信号用
        /// </summary>
        class IBitChanged
        {
            DateTime dt_rising = DateTime.MinValue;//上升沿时间点
            DateTime dt_falling = DateTime.MinValue;//下降沿时间点
            public TimeSpan ts;
            public TimeSpan twice = new TimeSpan(0, 0, 0);
            enum STATE
            {
                WAIT_FOR_RISING_EDGE,//等待上升沿
                WAIT_FOR_FALLING_EDGE,//等待下降沿
                WAIT_FOR_TWICE_RISING_EDGE,//等待第二次上升沿
                WAIT_FOR_TWICE_FALLING_EDGE//等待第二次下升沿
            }
            STATE state = STATE.WAIT_FOR_RISING_EDGE;

            public void Clear()
            {
                state = STATE.WAIT_FOR_RISING_EDGE;
            }
            DateTime GetMid()
            {
                long ticks = (dt_falling - dt_rising).Ticks / 2;
                ts = TimeSpan.FromTicks(ticks);
                return dt_rising + ts;
            }
            /// <summary>
            /// 返回 0->1->0 中间的时间
            /// </summary>
            /// <param name="dt">当前时间点</param>
            /// <param name="status">当前状态</param>
            /// <param name="dtmid">中间时间点</param>
            /// <returns>成功返回 true </returns>
            public bool Update(DateTime dt, bool status, out DateTime dtmid)
            {
                dtmid = dt;
                switch (state)
                {
                    case STATE.WAIT_FOR_RISING_EDGE:
                        {
                            if (status == true)//上升沿
                            {
                                this.dt_rising = dt;
                                state = STATE.WAIT_FOR_FALLING_EDGE;
                            }
                        } break;
                    case STATE.WAIT_FOR_FALLING_EDGE:
                        {
                            if (status == false)//下降沿
                            {
                                this.dt_falling = dt;
                                if (twice.TotalSeconds == 0)
                                {
                                    //直接计算
                                    dtmid = GetMid();
                                    state = STATE.WAIT_FOR_RISING_EDGE;
                                    return true;
                                }
                                state = STATE.WAIT_FOR_TWICE_RISING_EDGE;
                            }
                        } break;
                    case STATE.WAIT_FOR_TWICE_RISING_EDGE:
                        {
                            TimeSpan ts = dt - this.dt_falling;
                            if (ts > twice)//超时,没有第2次发生了。
                            {
                                dtmid = GetMid();
                                state = STATE.WAIT_FOR_RISING_EDGE;
                                return true;
                            }

                            if (status == true) //第2次上升沿
                                state = STATE.WAIT_FOR_TWICE_FALLING_EDGE;

                        } break;
                    case STATE.WAIT_FOR_TWICE_FALLING_EDGE:
                        {
                            if (status == false)//下降沿
                            {
                                dtmid = GetMid();
                                state = STATE.WAIT_FOR_RISING_EDGE;
                                return true;
                            }
                        } break;
                }
                return false;
            }


        }


        /// <summary>
        /// 上一个辊信号变化
        /// </summary>
        IBitChanged mbcRoll = new IBitChanged();
        /// <summary>
        /// 正反信号滤波时间 150ms
        /// </summary>
        const int SIGN_DELAY_MS = 150;

        void update_limit(FlyADBase.IStatusChangedEventArgs e)
        {
            
            bool bchanged = false;//信号更新?
            DateTime dt = e.Time;//信号更新发生的时间
            int no = -1;//信号序号
            bool status = false;//信号的状态 true开始, false结束


            if (Misc.MyBase.CHECKBIT(e.IChanged, Limit0_bit - 1))//信号0 发生变化
            {
                bchanged = true;
                no = 0;
                if (!Misc.MyBase.CHECKBIT(e.IStatus, Limit0_bit - 1))
                {
                    status = true;
                }
            }
            else if (Misc.MyBase.CHECKBIT(e.IChanged, Limit1_bit - 1))//信号1 发生变化
            {
                bchanged = true;
                no = 1;
                if (!Misc.MyBase.CHECKBIT(e.IStatus, Limit1_bit - 1))
                {
                    status = true;
                }
            }

            if (!bchanged)//没有信号更新
                return;
            SignData sd = new SignData() { No = no, Time = dt, On = status };
            if (mSignList.Count() > 0)
            {
                SignData sd_last = mSignList.Last();
                sd.Interval = dt - sd_last.Time;
            }
            mSignList.RAdd(sd);
            
            FirstSignBM = mSignList.Index2No(0);
            LastSignBM = mSignList.GetLastNo();



            if (IsSignFilter)
            {
                //信号滤波
                if (no == 0)
                {
                    mSign0ListNoJudge.Add(mSignList.Last());
                    //等100ms再触发
                    FObjBase.PollModule.Current.Poll_Config(FObjBase.PollModule.POLL_CONFIG.ADD,
                        () =>
                        {
                            PopSign(0);
                        },
                        TimeSpan.FromMilliseconds(SIGN_DELAY_MS), true, false, this, MARKNO_DELAY0, true);
                }
                else
                {
                    mSign1ListNoJudge.Add(mSignList.Last());
                    //等100ms再触发
                    FObjBase.PollModule.Current.Poll_Config(FObjBase.PollModule.POLL_CONFIG.ADD,
                        () =>
                        {
                            
                            PopSign(1);
                        },
                        TimeSpan.FromMilliseconds(SIGN_DELAY_MS), true, false, this, MARKNO_DELAY1, true);
                }
            }
            else
            {
                update_limit(dt, no, status);
            }
        }
        /// <summary>
        /// 判断信号是否毛刺,最后会调用update_limit
        /// </summary>
        /// <param name="no"></param>
        void PopSign(int no)
        {
            List<SignData> noJudgeList;
            if (no == 0)
                noJudgeList = mSign0ListNoJudge;
            else
                noJudgeList = mSign1ListNoJudge;

            if (noJudgeList.Count() == 0)
            {
                //没有信号,异常
                noJudgeList.Clear();
                return;
            }

            if (noJudgeList.Count() == 1)
            {
                //只有一个信号,它持续了100ms,是信号不是毛刺
                update_limit(noJudgeList.First());
            }
            else
            {
                SignData sd0 = noJudgeList.First();
                SignData sd1 = noJudgeList.Last();
                if (sd0.On == sd1.On)
                {
                    //状态一样, 不应该会发生的情况,AD卡坏了?
                    //异常处理:两次信号间的都只是毛刺,触发第1个信号
                    update_limit(sd0);
                }
                else
                {
                    if (sd1.Time - sd0.Time >= TimeSpan.FromMilliseconds(SIGN_DELAY_MS))
                    {
                        //两个信号时间差大于 100ms 
                        //触发它们
                        update_limit(sd0);
                        update_limit(sd1);
                    }
                    else
                    {
                        //时间差在100ms内,只是毛刺而已
                    }
                }
            }
            //判断完,清空列表!!!
            noJudgeList.Clear();
        }


        /// <summary>
        /// 添加正反转信号到 列表
        /// </summary>
        /// <param name="s"></param>
        void update_limit(SignData s)
        {
            update_limit(s.Time, s.No, s.On);
        }


        /// <summary>
        /// 添加正反转信号到 列表
        /// </summary>
        /// <param name="dt">信号更新发生的时间</param>
        /// <param name="no">信号序号</param>
        /// <param name="status">信号的状态 true开始, false结束</param>
        void update_limit(DateTime dt, int no, bool status)
        {
            //上一个是虚拟信号
            //1.当接收的信号与虚拟信号相同时,把虚拟信号删除!!!!!
            //2.当接收的信号与虚拟信号不同时,更新旋转时间!!!!!
            if (!update_limit_virtual(dt, no, status))
            {
                LastLimitNo = no;

                switch (SignType)
                {
                    case BlowingSignType.Short:
                        update_limit_short(dt, no, status);
                        break;
                    default:
                        update_limit_long(dt, no, status);
                        break;
                }
            }


            //更新其它参数
            update_param_for_limit(dt);
        }

        /// <summary>
        /// 更新因为旋转信号的添加导致的变化
        /// </summary>
        /// <param name="dt">信号变化的时间</param>
        void update_param_for_limit(DateTime dt)
        {
            if (mLimitList.Count() > 0)
            {
                LimitCell lc = mLimitList.Last();

                if (dt == lc.dt_begin)//刚撞转向
                {
                    //RotationCnt++;
                    //更新人字架旋转周期
                    if (mLimitList.Count > 1)
                    {
                        DateTime dt1 = mLimitList[mLimitList.Count - 1].dt_begin;
                        DateTime dt2 = mLimitList[mLimitList.Count - 2].dt_end;
                        RenZiJiaPeriod = dt1 - dt2;//只有这个是正确的!!!
                                                    //触发修正事件!!!!!
                        TouchFilmInfoChanged(dt2);
                    }
                    else if (mLimitList.Count == 1)
                    {
                        DateTime dt1 = mLimitList[mLimitList.Count - 1].dt_begin;
                        mDefaultTime = dt1 - RenZiJiaPeriod;
                        //触发修正事件!!!!!
                        TouchFilmInfoChanged(mDefaultTime);
                    }
                }
                else if (lc.dt_end != DateTime.MinValue)
                    LimitSignTime = lc.dt_end - lc.dt_begin;//更新撞信号的耗时
            }
        }

        /// <summary>
        /// 处理上一个是虚拟信号的正反转信号,它可能是多余的,返回true,新来的真实信号已经被处理
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="no"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        bool update_limit_virtual(DateTime dt, int no, bool status)
        {
            if (mLimitList.Count() == 0)
                return false;
            
            LimitCell lc_v = mLimitList.Last();
            if (lc_v.isVirtual == false)//不是虚拟信号,不用再判断了
                return false;

            if (lc_v.no == no)//有真实的信号,与虚拟信号一样
            {
                if (status != true)
                    return false;//异常, 不可能在 真实的信号中间插了个虚拟信号

                //上一个信号是多余的
                lc_v.isVirtual = false;
                lc_v.dt_begin = dt;
                lc_v.dt_end = DateTime.MinValue;
                return true;
            }
            else
            {
                //更新旋转时间!!!!!!
                if (mLimitList.Count() > 1)
                {
                    DateTime dt2 = mLimitList[mLimitList.Count - 2].dt_end;
                   
                    RenZiJiaPeriod = TimeSpan.FromTicks((dt - dt2 - LimitSignTime).Ticks/2);//只有这个是正确的!!!

                    lc_v.dt_begin = dt2 + RenZiJiaPeriod;
                    lc_v.dt_end = lc_v.dt_begin + LimitSignTime;
                }
            }
            return false;
        }
        /// <summary>
        /// 处理短信号
        /// </summary>
        /// <param name="dt">信号发生的时间</param>
        /// <param name="no">信号序号</param>
        /// <param name="status">信号的状态 true开始, false结束)</param>
        void update_limit_short(DateTime dt, int no, bool status)
        {
            //把信号加入到列表

            if (status == true)//添加,写入开始时间
            {
                if (mLimitList.Count > 0)
                {
                    LimitCell lc = mLimitList.Last();
                    if (lc.no == no)//连续撞了两次相同限位
                    {
                        if (lc.isDouble)
                        {
                            lc.dt_end = dt;//信号结束了
                            LimitSignTime = lc.dt_end - lc.dt_begin;//更新撞限位的耗时
                        }
                        else
                        {
                            if (lc.dt_end == DateTime.MinValue)//上一个限位还没离开
                            {
                                //异常,AD卡出错了,或者断开连接了,清空!!!
                                mLimitList.Clear();

                                mLimitList.RAdd(
                                    new LimitCell()
                                    {
                                        dt_begin = dt,
                                        no = no,
                                        isDouble = (no == 0) ? IsSign0Double : IsSign1Double
                                    });
                                RotationCnt++;
                            }
                            else
                            {
                                //判断两次的时间差
                                if ((dt - lc.dt_end) < TimeSpan.FromSeconds(30))
                                {
                                    //这是同一个转向信号
                                    lc.isDouble = true;
                                    if (lc.no == 0)
                                        IsSign0Double = lc.isDouble;
                                    else
                                        IsSign1Double = lc.isDouble;

                                    lc.dt_mid = lc.dt_end;
                                    lc.dt_end = dt;
                                }
                                else
                                {
                                    //时间差很大, 异常,旋转架重新转启
                                    //异常,AD卡出错了,或者断开连接了,清空!!!
                                    mLimitList.Clear();

                                    mLimitList.RAdd(
                                        new LimitCell()
                                        {
                                            dt_begin = dt,
                                            no = no,
                                            isDouble = (no == 0) ? IsSign0Double : isSign1Double
                                        });
                                    RotationCnt++;
                                }
                            }
                        }
                    }
                    else
                    {
                        //一个新的 信号

                        if (lc.dt_end == DateTime.MinValue)//上一个转向还没离开
                        {
                            if (lc.isDouble)
                            {
                                if (lc.dt_mid != DateTime.MinValue)
                                {
                                    //没事, 只是从需要撞两次,变成只撞一次就可以了

                                    lc.isDouble = false;
                                    if (lc.no == 0)
                                        IsSign0Double = lc.isDouble;
                                    else
                                        IsSign1Double = lc.isDouble;

                                    lc.dt_end = lc.dt_mid;
                                    lc.dt_mid = DateTime.MinValue;
                                }
                                else
                                {
                                    //异常 输入口没有结束
                                    //AD卡出错了,或者断开连接了,清空!!!
                                    mLimitList.Clear();

                                    mLimitList.RAdd(
                                        new LimitCell()
                                        {
                                            dt_begin = dt,
                                            no = no,
                                            isDouble = (no == 0) ? IsSign0Double : isSign1Double
                                        });
                                    RotationCnt++;
                                }
                            }
                            else
                            {
                                //异常 输入口没有结束
                                //AD卡出错了,或者断开连接了,清空!!!
                                mLimitList.Clear();

                                mLimitList.RAdd(
                                    new LimitCell()
                                    {
                                        dt_begin = dt,
                                        no = no,
                                        isDouble = (no == 0) ? IsSign0Double : isSign1Double
                                    });
                                RotationCnt++;
                            }
                        }
                        else
                        {
                            //一个新的信号
                            mLimitList.RAdd(
                            new LimitCell()
                            {
                                dt_begin = dt,
                                no = no,
                                isDouble = (no == 0) ? IsSign0Double : isSign1Double
                            });
                            RotationCnt++;
                        }
                    }
                }
                else
                {
                    //一个新的信号
                    mLimitList.RAdd(
                    new LimitCell()
                    {
                        dt_begin = dt,
                        no = no,
                        isDouble = (no == 0) ? IsSign0Double : isSign1Double
                    });
                    RotationCnt++;
                }
            }
            else //写入 结束时间
            {
                if (mLimitList.Count > 0)
                {
                    LimitCell lc = mLimitList.Last();
                    if (lc.no == no)
                    {
                        if (lc.isDouble)
                        {
                            //需要撞2次
                            if (lc.dt_mid == DateTime.MinValue)
                            {
                                lc.dt_mid = dt;//完成撞第1次
                            }
                            else if (lc.dt_end != DateTime.MinValue)
                            {
                                //完成撞第2次
                            }
                            else
                            {
                                //异常, 撞第2次 还没开始,就结束了
                                //AD卡出错了,或者断开连接了,清空!!!
                                mLimitList.Clear();
                            }
                        }
                        else
                        {
                            if (lc.dt_end == DateTime.MinValue)
                            {
                                lc.dt_end = dt;//完成一次信号
                            }
                            else
                            {
                                //异常,连续2次结束
                                mLimitList.Clear();
                            }
                        }
                    }
                    else
                    {
                        //异常,还没开始,就结束了
                        mLimitList.Clear();
                    }
                }
                else
                {
                    //异常,还没开始,就结束了
                }
            }
        }

        /// <summary>
        /// 处理长信号, 只关心 status=true
        /// </summary>
        /// <param name="dt">信号发生的时间</param>
        /// <param name="no">信号序号</param>
        /// <param name="status">信号的状态 true开始, false结束)</param>
        void update_limit_long(DateTime dt, int no, bool status)
        {
            //把信号加入到列表

            if (status == true)//添加,写入开始时间
            {
                if (mLimitList.Count > 0)
                {
                    LimitCell lc = mLimitList.Last();
                    if (lc.no == no)//连续撞了两次相同限位, 可能重新在转
                    {
                        mLimitList.Clear();
                    }
                }
                mLimitList.RAdd(
                    new LimitCell()
                    {
                        no = no,
                        dt_begin = dt,
                        dt_end = dt + LimitSignTime
                    });
                RotationCnt++;
            }
        }
        void update_roll(FlyADBase.IStatusChangedEventArgs e)
        {
            bool bchanged = false;//信号更新?
            DateTime dt = e.Time;//信号更新发生的时间
            bool status = false;//信号的状态

            //----------------------------------------------------------------------
            //更新辊信号
            if (Misc.MyBase.CHECKBIT(e.IChanged, Roll_bit - 1))
            {
                bchanged = true;
                if (!Misc.MyBase.CHECKBIT(e.IStatus, Roll_bit - 1))
                {
                    status = true;
                }
            }
            if (!bchanged)//没有信号更新
                return;

            DateTime dtmid;
            if (mbcRoll.Update(dt, status, out dtmid))
            {
                mRollList.RAdd(
                    new RollCell()
                    {
                        dt = dtmid
                    });

                //更新当前线速度
                if (mRollList.Count > 1)
                {
                    DateTime dt1 = mRollList[mRollList.Count - 1].dt;
                    DateTime dt2 = mRollList[mRollList.Count - 2].dt;
                    TimeSpan ts = dt1 - dt2;
                    FilmVelocity = RollPerimeter / 1000.0 / ts.TotalMinutes;

                    if (mRollList.Count > 100)
                    {
                        DateTime dtn = mRollList[mRollList.Count - 101].dt;
                        TimeSpan tsn = dt1 - dtn;
                        double avg_filmvelocity = RollPerimeter / 10.0 / tsn.TotalMinutes;

                        DateTime dt3 = mRollList[mRollList.Count - 3].dt;
                        TimeSpan ts3 = dt2 - dt3;
                        double last_filmvelocity = RollPerimeter / 1000.0 / ts3.TotalMinutes;


                        //异常检测
                        if (FilmVelocity > 10)
                        {
                            if (Math.Abs(avg_filmvelocity - FilmVelocity) / FilmVelocity > 0.1)
                            {


                                //异常
                                //可能少了信号!!!
                                if (Math.Abs(last_filmvelocity - FilmVelocity * 2) < 0.3)
                                {
                                    TimeSpan ts12 = TimeSpan.FromTicks((dt1 - dt2).Ticks / 2);
                                    //肯定少了一个
                                    mRollList.Insert(
                                        mRollList.Count - 1,
                                        new RollCell()
                                        {
                                            dt = dt2 + ts12
                                        });
                                }
                                else if (Math.Abs(last_filmvelocity - FilmVelocity * 3) < 0.3)
                                {
                                    TimeSpan ts12 = TimeSpan.FromTicks((dt1 - dt2).Ticks / 3);
                                    //肯定少了两个
                                    mRollList.Insert(
                                        mRollList.Count - 1,
                                        new RollCell()
                                        {
                                            dt = dt2 + ts12
                                        });
                                    mRollList.Insert(
                                        mRollList.Count - 1,
                                        new RollCell()
                                        {
                                            dt = dt2 + ts12 + ts12
                                        });
                                }
                                else
                                {

                                }
                            }
                        }
                    }
                }
                //触发修正事件!!!!!
                TouchFilmInfoChanged();
            }
        }
        void flyad_IStatusChangedEvent(object sender, FlyADBase.IStatusChangedEventArgs e)
        {
            update_limit(e);
            update_roll(e);

        }

        /// <summary>
        /// 1秒触发一次
        /// </summary>
        void OnPoll()
        {
            //--------------------------------------------------------------------
            //添加虚拟信号
            if(IsLackSignMode)
                OnPoll_Virtual();

            //--------------------------------------------------------------------
            //正反向信号撞两次的超时判断
            OnPoll_checkDouble();
            //--------------------------------------------------------------------
            //旋转架速度安全检测
            OnPoll_checkRVelocity();
            //--------------------------------------------------------------------
            //当前旋转架角度 
            {
                int rotationCnt;
                double angle;
                bool inCV;
                LimitListSearch(DateTime.Now, false, out angle, out direction, out rotationCnt, out inCV);
                Angle = angle;
                Direction = direction;
            }
            //--------------------------------------------------------------------
            //线速度安全检测
            if (mRollList.Count > 0)
            {
                DateTime dt = mRollList[mRollList.Count - 1].dt;
                TimeSpan ts = DateTime.Now - dt;
                if (ts.TotalMinutes > 1)//1min 
                {
                    //大于1分钟,也没有一个信号,复位
                    Clear();
                }
            }

            //--------------------------------------------------------------------
            //更新当前线速度
            if (mRollList.Count > 1)
            {
                DateTime dt1 = mRollList[mRollList.Count - 1].dt;
                DateTime dt2 = mRollList[mRollList.Count - 2].dt;
                TimeSpan ts_last = dt1 - dt2;
                TimeSpan ts_now = DateTime.Now - dt1;
                if (ts_now > (ts_last + ts_last))
                {
                    FilmVelocity = RollPerimeter / 1000.0 / ts_now.TotalMinutes;
                }
            }
        }
        /// <summary>
        /// 虚拟信号添加
        /// </summary>
        void OnPoll_Virtual()
        {
            DateTime now = DateTime.Now;

            //--------------------------------------------------------------------
            //正反向信号撞两次的超时判断
            if (mLimitList.Count > 0)
            {
                LimitCell lc = mLimitList.Last();
                if (!lc.isVirtual)//上一个不能是虚拟信号
                {
                    if (lc.dt_end != DateTime.MinValue)//上一个信号完成了
                    {
                        if (now - lc.dt_end > TimeSpan.FromSeconds(RenZiJiaPeriod.TotalSeconds + 20))
                        {
                            //已经大于上一次旋转信号+20s,都没有信号了
                            //添加虚拟信号
                            int no = (lc.no == 0) ? 1 : 0;
                            DateTime dt = lc.dt_end + RenZiJiaPeriod;
                            mLimitList.RAdd(new LimitCell()
                            {
                                no = no,
                                dt_begin = dt,
                                dt_end = dt + LimitSignTime,
                                isVirtual = true
                            });
                            RotationCnt++;
                            LastLimitNo = no;
                            update_param_for_limit(dt);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 正反向信号撞两次的超时判断
        /// </summary>
        void OnPoll_checkDouble()
        {
            if (mLimitList.Count > 0)
            {
                LimitCell lc = mLimitList.Last();
                //需要撞两次的超时判断!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                if (lc.isDouble)
                {
                    if (lc.dt_end == DateTime.MinValue)
                    {
                        //第2次还没撞
                        if (DateTime.Now - lc.dt_begin > TimeSpan.FromSeconds(30))
                        {
                            //长时间第2次都没撞, 看来只需要撞一次而已
                            lc.isDouble = false;
                            if (lc.no == 0)
                                IsSign0Double = lc.isDouble;
                            else
                                IsSign1Double = lc.isDouble;
                            lc.dt_end = lc.dt_mid;
                            lc.dt_mid = DateTime.MinValue;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 旋转架速度安全检测
        /// </summary>
        void OnPoll_checkRVelocity()
        {
            DateTime dt = mDefaultTime;
            if (mLimitList.Count > 0)
            {
                LimitCell lc = mLimitList.Last();
                if (lc.dt_end != DateTime.MinValue)
                    dt = lc.dt_end;
                else
                    dt = lc.dt_begin;
            }

            PastTime = DateTime.Now - dt;
            if (PastTime > TimeSpan.FromMinutes(16))//16min 
            {
                //大于15分钟,也没有一个信号,复位
                Clear();
            }
        }



        public void Clear()
        {
            mbcRoll.Clear();

            mLimitList.Clear();
            mRollList.Clear();

            mDefaultTime = DateTime.Now;
            FilmVelocity = 0;
            RenZiJiaPeriod = DefaultRPeriod;
            LimitSignTime = TimeSpan.FromSeconds(4);
            if (ClearEvent != null)
            {
                ClearEvent(this);
            }
        }

        public class FilmInfo
        {
            /// <summary>
            /// 上层 位于膜泡的角度 0~360°
            /// </summary>
            public double angle1;
            /// <summary>
            /// 下层 位于膜泡的角度 0~360°
            /// </summary>
            public double angle2;
            /// <summary>
            /// 膜泡旋转方向
            /// </summary>
            public Misc.DIRECTION direction;
            /// <summary>
            /// 线速度
            /// </summary>
            public double filmVelocity;
            /// <summary>
            /// 累计旋转次数
            /// </summary>
            public int rotationCnt;
            /// <summary>
            /// 在匀速阶段; 在限位信号 开始~结束 期间 inCV = false
            /// </summary>
            public bool inCV;
        }




        public enum GetLimitTimeResult
        {
            /// <summary>
            /// 获取成功
            /// </summary>
            OK,
            /// <summary>
            /// 不存在该点
            /// </summary>
            NOEXIST,
            /// <summary>
            /// 限位已经撞了,但膜还没走完,等吧
            /// </summary>
            ISFURTURE
        }
        /// <summary>
        /// 计算膜长用;
        /// 输入膜长,获取对应的 限位点时间
        /// </summary>
        /// <param name="rotationcnt">旋转次数</param>
        /// <param name="minFilmLen">膜长</param>
        /// <param name="timeOfMinFilmLen">膜长对应限位时间</param>
        /// <returns></returns>
        public GetLimitTimeResult GetLimitTime(int rotationcnt, double filmlength, out DateTime timeOfFilmLength)
        {
            timeOfFilmLength = DateTime.MinValue;
            int index = RotationCntToLimitIndex(rotationcnt);
            if (index == -1)
                return GetLimitTimeResult.NOEXIST;
            if (index >= mLimitList.Count)
                return GetLimitTimeResult.ISFURTURE;

            DateTime dt = mLimitList[index].dt_begin;
            int ret = GetFuture(dt, filmlength, out timeOfFilmLength);
            if (ret == 1)
                return GetLimitTimeResult.ISFURTURE;

            return GetLimitTimeResult.OK;
        }

        ///// <summary>
        ///// 计算膜长用;
        ///// 输入膜长,获取对应的 限位点时间
        ///// </summary>
        ///// <param name="rotationcnt">旋转次数</param>
        ///// <param name="timeOfFilmLength">膜长对应限位时间</param>
        ///// <returns></returns>
        //public GetLimitTimeResult GetLimitTime(int rotationcnt, out DateTime timeOfFilmLength)
        //{
        //    return GetLimitTime(rotationcnt, FilmLength, out timeOfFilmLength);
        //}




        /// <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 int GetFilmInfo(out FilmInfo filminfo, DateTime dt, double filmWidth, double filmPosH)
        {
            return GetFilmInfo(out filminfo, dt, filmWidth, filmPosH, FilmLength);
        }
        public int GetFilmInfo(out FilmInfo filminfo, DateTime dt, double filmWidth, double filmPosH, double filmLen)
        {
            filminfo = null;

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

            if ((a < 0) || (a > 180))
            {
                return -1;//不在膜的范围内
            }

            DateTime ago_dt;
            double velocity;
            double angle0;
            double angle1;
            Misc.DIRECTION direction;
            int rotationcnt;
            bool inCV;
            if (dt == DateTime.MinValue)
            {
                //TODO, BUG
            }
            int ret = GetAgo(dt, filmLen, out ago_dt, out velocity);

            if (ret == 1)
                return ret;//发生在未来不处理!!!
            GetAngle(a, ago_dt, out angle0, out angle1, out direction, out rotationcnt, out inCV);

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


        #region 人字架时间点<-->24m后膜时间点
        /// <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 double ago_velocity)
        {
            return GetAgo(dt, FilmLength, out ago_dt, out ago_velocity);
        }
        /// <summary>
        /// 输入当前时间点,找到 刚出人字架时的时间点, 及那时候的线速度;
        /// 返回 -1, 当前的时间点在列表以前;
        /// 返回 0, 当前的时间点在列表中;
        /// 返回 1, 当前的时间点在列表的未来;
        /// </summary>
        /// <param name="dt">当前时间</param>
        /// <param name="ago_dt">24米真实长度</param>
        /// <returns></returns>
        int GetAgo(DateTime dt, double filmlength, out DateTime ago_dt, out double ago_velocity)
        {

            ago_dt = DateTime.MinValue;
            ago_velocity = 0;
            int i;
            int ret = RollList_FindIndex(dt, out i);
            if (ret == 1)
            {
                return ret;
            }

            double position = RollList_GetPosition(i, dt);
            position -= filmlength * 1000;
            ret = RollList_GetDateTime(out i, position, out ago_dt);
            ago_velocity = RollList_GetVelocity(i);
            return ret;
        }
        /// <summary>
        /// 输入过去时间,让它再加上膜长时间,得到未来时间;
        /// 返回 -1, 未来的时间在列表以前;
        /// 返回 0, 未来的时间在列表中;
        /// 返回 1, 未来的时间在列表的未来;
        /// </summary>
        /// <param name="dt">过去时间</param>
        /// <param name="dt_future">未来时间</param>
        /// <returns></returns>
        int GetFuture(DateTime dt, out DateTime dt_future)
        {
            return GetFuture(dt, FilmLength, out dt_future);
        }
        /// <summary>
        /// 输入过去时间,让它再加上膜长时间,得到未来时间;
        /// 返回 -1, 未来的时间在列表以前;
        /// 返回 0, 未来的时间在列表中;
        /// 返回 1, 未来的时间在列表的未来;
        /// </summary>
        /// <param name="dt">过去时间</param>
        /// <param name="filmlength">膜长</param>
        /// <param name="dt_future">未来时间</param>
        /// <returns></returns>
        int GetFuture(DateTime dt, double filmlength, out DateTime dt_future)
        {
            dt_future = DateTime.MinValue;
            double position;
            int i;
            int ret = RollList_FindIndex(dt, out i);
            if (ret == -1)
                position = RollList_GetPosition(0, dt);
            else if (ret == 0)
                position = RollList_GetPosition(i, dt);
            else
                return ret;

            position += filmlength * 1000;

            return RollList_GetDateTime(out i, position, out dt_future);

        }

        #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="dtEnd"></param>
        /// <param name="dtStart"></param>
        /// <param name="noStart">dtStart的信号</param>
        /// <param name="dt">dt 在 [dtStart , dtEnd]</param>
        ///  <param name="renZiJiaPeriod">上一次两个限位的间距时间</param>
        /// <param name="angle">角度</param>
        /// <param name="direction">方向, 只有正向 与  反向</param>
        void LimitListCal_SpeedChange(DateTime dtEnd, DateTime dtStart, int noStart, DateTime dt,  TimeSpan renZiJiaPeriod, out double angle, out Misc.DIRECTION direction) 
        {
            //默认 dt 在 dtStart <= dt <= dtEnd, 出错不处理!!!

            if((dt<dtStart)|| (dt>dtEnd))
            {
                throw new Exception("(dt<dtStart)|| (dt>dtEnd) dt="+dt.ToString()+" dtStart="+dtStart.ToString()+" dtEnd="+dtEnd.ToString());
            }

            TimeSpan accDecTime = AccDecTime;
            if ((dtEnd - dtStart) < (accDecTime + accDecTime))
                accDecTime = TimeSpan.FromTicks((dtEnd - dtStart).Ticks / 2);

            DateTime dtMid = dtStart + TimeSpan.FromTicks((dtEnd - dtStart).Ticks / 2);

            if (noStart == 0)
            {
                if (dt < dtMid)
                    direction = Misc.DIRECTION.BACKWARD;
                else
                    direction = Misc.DIRECTION.FORWARD;
            }
            else 
            {
                if (dt < dtMid)
                    direction = Misc.DIRECTION.FORWARD;
                else
                    direction = Misc.DIRECTION.BACKWARD;
            }



            double dAngle;
            if (dt < (dtStart + accDecTime))//减速
            {
                TimeSpan ts = dt - dtStart;//已经过去时间
                double vStart = RAngle / renZiJiaPeriod.TotalSeconds;//开始速度 °/s
                double vEnd = 0;//结束速度 °/s
                double a = (vEnd - vStart) / accDecTime.TotalSeconds;//加速度 °/s^2
                double vCurr = vStart + a * ts.TotalSeconds;//当前速度 //开始速度 °/s
                dAngle = (vStart + vCurr) / 2 * ts.TotalSeconds;//总角度 °

            }
            else if (dt < (dtEnd - accDecTime))//停止
            {
                double vStart = RAngle / renZiJiaPeriod.TotalSeconds;//开始速度 °/s
                double vEnd = 0;//结束速度 °/s
                double a = (vEnd - vStart) / accDecTime.TotalSeconds;//加速度 °/s^2
                dAngle = (vStart + vEnd) / 2 * accDecTime.TotalSeconds;//总角度 °
            }
            else //加速
            {
                TimeSpan ts = dtEnd - dt;
                double vStart = 0; //开始速度 °/s
                double vEnd = RAngle / renZiJiaPeriod.TotalSeconds;//结束速度 °/s
                double a = (vEnd - vStart) / accDecTime.TotalSeconds;//加速度 °/s^2
                double vCurr = vEnd - a * ts.TotalSeconds;//当前速度 //开始速度 °/s
                dAngle = (vEnd + vCurr) / 2 * ts.TotalSeconds;//总角度 °
            }

            if (noStart == 0)
                angle = -RAngle / 2 - dAngle;
            else
                angle = RAngle / 2 + dAngle;
        }

        /// <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>
        void LimitListSearch(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;
            //TODO,rotationCnt 还没处理!!!!!

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

                        //检测是否需要修正 RenZiJiaPeriod
                        if (mLimitList.Count == 0)
                        {
                            dt_start = mDefaultTime;
                            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 (mLimitList[0].no == 0)
                            no = 1;
                        else
                            no = 0;

                        dt_start = mDefaultTime;
                        dt_end = mLimitList[0].dt_begin;

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

                        DateTime dtMid = dt_start + TimeSpan.FromTicks((dt_end - dt_start).Ticks / 2);

                        if (dt < dtMid)//以限位的中间时间为分界
                        {
                            rotationCnt--;
                        }

                        TimeSpan renZiJiaPeriod;
                        if (index >= 1)
                        {
                            renZiJiaPeriod = mLimitList[index].dt_begin - mLimitList[index - 1].dt_end;
                        }
                        else
                        {
                             renZiJiaPeriod = mLimitList[index].dt_begin - mDefaultTime;
                        }
                        inCV = false;
                        LimitListCal_SpeedChange(dt_end, dt_start, no, dt, renzijiaperiod, out angle, out direction);
                    }
                    break;
                default:
                //case ListFindIndexResult.MaybeInCell:
                    {
                        
                        dt_start = mLimitList[index].dt_begin;
                        dt_end = dt_start + LimitSignTime;

                        if (dt > dt_end)
                        {
                            if (modifyparam) //基本不会发生!!!
                            {
                                LimitSignTime = dt - dt_start + TimeSpan.FromSeconds(5);
                                dt_end = dt_start + LimitSignTime;
                            }
                            else 
                            {
                                dt_end = dt;
                            }
                        }
                        no = mLimitList[index].no;
                        rotationCnt = LimitIndexToRotationCnt(index);

                        DateTime dtMid = dt_start + TimeSpan.FromTicks((dt_end - dt_start).Ticks / 2);

                        if (dt < dtMid)//以限位的中间时间为分界
                        {
                            rotationCnt--;
                        }

                        TimeSpan renZiJiaPeriod;
                        if (index >= 1)
                        {
                            renZiJiaPeriod = mLimitList[index].dt_begin - mLimitList[index - 1].dt_end;
                        }
                        else
                        {
                            renZiJiaPeriod = mLimitList[index].dt_begin - mDefaultTime;
                        }



                        inCV = false;
                        LimitListCal_SpeedChange(dt_end, dt_start, no, dt, renzijiaperiod, out angle, out direction);
                    }
                    break;
            }




        }

        #region RollList 查找
        /// <summary>
        /// 获取 [idx,idx+1] 之间 的 位置
        /// </summary>
        /// <param name="idx"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        double RollList_GetPosition(int idx, DateTime dt)
        {
            DateTime dt1 = mRollList[idx + 1].dt;
            double position1 = (idx + 1) * RollPerimeter;
            DateTime dt2 = mRollList[idx].dt;
            double position2 = (idx) * RollPerimeter;

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

            return RollPerimeter / 1000 / dt1.Subtract(dt2).TotalMinutes;
        }
        /// <summary>
        /// 获取 [idx,idx+1] 之间 的 时间
        /// </summary>
        /// <param name="idx"></param>
        /// <param name="position"></param>
        /// <returns></returns>
        DateTime RollList_GetDateTime(int idx, double position)
        {
            //TODO 经常出错,position 无限小,idx=0
            DateTime dt1 = mRollList[idx + 1].dt;
            double position1 = (idx + 1) * RollPerimeter;
            DateTime dt2 = mRollList[idx].dt;
            double position2 = (idx) * RollPerimeter;

            return dt2.Add(new TimeSpan((long)((dt1 - dt2).Ticks * (position - position2) / (position1 - position2))));
        }

        /// <summary>
        /// 通过 position 查找时间
        /// 返回 -1,这个过去的时间;
        /// 返回 0,在列表中;
        /// 返回 1,发生在未来的时间;
        /// </summary>
        /// <param name="position"></param>
        /// <returns></returns>
        int RollList_GetDateTime(out int idx, double position, out DateTime dt)
        {
            idx = 0;
            dt = DateTime.MinValue;

            if (mRollList.Count == 0)
                return 1;

            for (int i = mRollList.Count - 1; i > 0; i--)
            {
                if (position >= ((i) * RollPerimeter))
                {
                    //找到了
                    idx = i;
                    if (i == (mRollList.Count - 1))
                        return 1;
                    else
                    {
                        dt = RollList_GetDateTime(i, position);
                        return 0;
                    }

                }
            }
            //TODO, 这里会出错,position无限小
            idx = 0;
            dt = RollList_GetDateTime(0, position);
            //发生在过去
            return -1;
        }

        /// <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].dt)
                {
                    //找到了
                    if (i == (mRollList.Count - 1))//这个是未来的时间点,还没发生!!! 
                    {
                        index = i;
                        return 1;
                    }
                    else
                    {
                        index = i;
                        return 0;
                    }
                }
            }

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

        #endregion

        /// <summary>
        /// 时间对应旋转状态
        /// </summary>
        enum DTSTATUS
        {
            /// <summary>
            /// 发生在未来的均速阶段
            /// </summary>
            Future,
            /// <summary>
            /// 发生在过去的均速阶段
            /// </summary>
            Past,
            /// <summary>
            /// 发生在均速阶段
            /// </summary>
            Normal,
            /// <summary>
            /// 发生在方向切换阶段
            /// </summary>
            Swap,

        }
        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>
        /// 获取膜角度, 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;
            double angle;


            //LimitListSearch(dt, true, out angle, out direction, out rotationCnt, out inCV);
            //不修改旋转速度!!!!!!!!!!!!
            LimitListSearch(dt, false, out angle, out direction, out rotationCnt, out inCV);


            a += angle;
            if (a >= 360)
                a -= 360;
            else if (a < 0)
                a += 360;

            a_other += angle;
            if (a_other >= 360)
                a_other -= 360;
            else if (a_other < 0)
                a_other += 360;
            angle0 = a;
            angle1 = a_other;
            return true;
        }

        /// <summary>
        /// 获取 信号列表
        /// </summary>
        /// <param name="AsyncDelegate">retdata = GetBufListReponse</param>
        /// <param name="AsyncState"></param>
        public void GetSignList(AsyncCBHandler AsyncDelegate, object AsyncState)
        {
            GetSignListReponse p = new GetSignListReponse();

            p.lastbm = LastSignBM;
            p.firstbm = FirstSignBM;
            p.datas = mSignList;

            AsyncDelegate(AsyncState, p);
        }

        public void Apply()
        {
            Save();
        }
        public class FilmInfoChangedEventArgs : EventArgs
        {
            public FilmInfoChangedEventArgs(DateTime time)
            {
                Time = time;
            }
            /// <summary>
            /// 开始时间点
            /// </summary>
            public DateTime Time
            {
                get;
                set;
            }
        }

        public delegate void FilmInfoChangedEventHandler(object sender, FilmInfoChangedEventArgs e);
        public event FilmInfoChangedEventHandler FilmInfoChangedEvent;

        public delegate void ClearEventHandler(object sender);
        public event ClearEventHandler ClearEvent;

        public BlowingDetect Clone()
        {
            BlowingDetect rdetect = new BlowingDetect();
            rdetect.mDefaultTime = mDefaultTime;
            foreach (RollCell r in mRollList)
            {
                rdetect.mRollList.Add(r.Clone());
            }
            foreach (LimitCell l in mLimitList)
            {
                rdetect.mLimitList.Add(l.Clone());
            }

            rdetect.RAngle = RAngle;
            rdetect.RollPerimeter = RollPerimeter;
            rdetect.RenZiJiaPeriod = RenZiJiaPeriod;
            rdetect.FilmLength = FilmLength;
            rdetect.RotationCnt = RotationCnt;

            //多余
            rdetect.DefaultRPeriod = DefaultRPeriod;
            rdetect.IsSign0Double = IsSign0Double;
            rdetect.IsSign1Double = IsSign1Double;
            rdetect.SignType = SignType;

            return rdetect;
        }
        public void Dispose()
        {
            Clear();
        }

        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;


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

        #endregion



        public bool Load()
        {
            return Misc.SaveToXmlHepler.Load("blowingdetect.xml", this);
        }
        public void Save()
        {
            Misc.SaveToXmlHepler.Save("blowingdetect.xml", this);
        }

        public string[] GetSavePropertyNames()
        {
            return new string[]{
                "RAngle",
                "AccDecTime",
                "LimitSignTime",
                "FilmLength",
                "RollPerimeter",
                "DefaultRPeriod",
                "IsSign0Double",
                "IsSign1Double",
                "SignType",
                "IsSignFilter",
                "IsLackSignMode"
            };
        }
    }


}