FLYAD7.cs 38.2 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using FObjBase;
using System.IO;
using System.ComponentModel;
using System.Xml.Serialization;
using FlyADBase;

namespace FLY.Simulation.Flyad7
{
    public class FLYAD7 : IFlyAD, INotifyPropertyChanged, Misc.ISaveToXml
    {

        private IPEndPoint localep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 20008);
        public IPEndPoint LocalEP
        {
            get
            {
                return localep;
            }
            set
            {
                if (!IPEndPoint.Equals(localep, value))
                {
                    localep.Address = value.Address;
                    localep.Port = value.Port;
                    NotifyPropertyChanged("LocalEP");
                }
            }
        }

        private int adforshow;
        public int ADForShow 
        {
            get { return adforshow; }
            set
            {
                if (adforshow != value)
                {
                    adforshow = value;
                    NotifyPropertyChanged("ADForShow");
                }
            }
        }

        private int posforshow;
        public int PosForShow
        {
            get { return posforshow; }
            set
            {
                if (posforshow != value)
                {
                    posforshow = value;
                    NotifyPropertyChanged("PosForShow");
                }
            }
        }

        private int pos2forshow;
        public int Pos2ForShow
        {
            get { return pos2forshow; }
            set
            {
                if (pos2forshow != value)
                {
                    pos2forshow = value;
                    NotifyPropertyChanged("Pos2ForShow");
                }
            }
        }

        private int ADSum;
        private int ADCnt;

        public List<int> TimeGrid = new List<int>();

        private double timespan1ms;
        public double TimeSpan1ms
        {
            get { return timespan1ms; }
            set
            {
                if (timespan1ms != value)
                {
                    timespan1ms = value;
                    NotifyPropertyChanged("TimeSpan1ms");
                }
            }
        }

        /// <summary>
        /// 原始脉冲值
        /// </summary>
        public int PositionOrg=0;

        #region 模拟部分
        SimDrive mSimDrive = new SimDrive();
        public OrderMan mOrderMan = new OrderMan();

        public enum SIM_MODE 
        {
            /// <summary>
            /// 吹膜
            /// </summary>
            Blowing,
            /// <summary>
            /// 涂布
            /// </summary>
            Coating,
            /// <summary>
            /// 头尾部检测
            /// </summary>
            HeaderAndTailer,
            /// <summary>
            /// 流延
            /// </summary>
            Casting
        }
        private SIM_MODE simmode = SIM_MODE.Coating;
        /// <summary>
        /// 系统重启时,mSimModeCurr = SimMode ,之后怎么变化都不会影响 mSimModeCurr
        /// </summary>
        public SIM_MODE SimMode 
        {
            get {
                return simmode;
            }
            set {
                if (simmode != value)
                {
                    simmode = value;
                    NotifyPropertyChanged("SimMode");
                }
            }
        }

        /// <summary>
        /// 当前模拟类型
        /// </summary>
        SIM_MODE mSimMode = SIM_MODE.Blowing;
        public FLY.Simulation.ISimulationGageAD mSimGageAD;
        //public SimDrive_VF0 mSimDriveVF0 = new SimDrive_VF0();
        //public SimDrive_Servo mSimDriveServo = new SimDrive_Servo();
        #endregion

        private string param_path;
        public FLYAD7():this("simulation_flyad7.xml")
        {

        }
        public FLYAD7(string param_path) 
        {
            this.param_path = param_path;

            PosOfGrid = 10;
            Load();
            mSimMode = SimMode;

            switch (mSimMode)
            {
                case SIM_MODE.Blowing:
                    mSimGageAD = new FLY.Simulation.Blowing.GageAD();
                    break;
                case SIM_MODE.HeaderAndTailer:
                    mSimGageAD = new FLY.Simulation.HeaderAndTailer.GageAD();
                    break;
                case SIM_MODE.Casting:
                    mSimGageAD = new FLY.Simulation.Casting.GageAD();
                    break;
                case SIM_MODE.Coating:
                    mSimGageAD = new FLY.Simulation.Coating.GageAD();
                    break;
                default:
                    throw new Exception("不支持 SIM_MODE" + mSimGageAD.ToString());
            }
            
            PollModule.Current.Poll_Config(
                PollModule.POLL_CONFIG.ADD,
                new PollModule.PollHandler(OnPoll),
                new TimeSpan((long)(TimeSpan.TicksPerMillisecond * 1.28)));
            //一定只能用TimeSpan.FromTicks(12800)
            //TimeSpan.FromMilliseconds(1.28) 会被取整,最后是 1ms

            this.PropertyChanged += new PropertyChangedEventHandler(FLYAD7_PropertyChanged);

            mOrderMan.Init(this, mSimDrive);
            mSimGageAD.Mmpp = mSimDrive.Mmpp;

            HVelocity1 = (UInt32)((5 * 1000) / mSimDrive.Mmpp / 60);
            HVelocity2 = (UInt32)((1 * 1000) / mSimDrive.Mmpp / 60);
            Velocity = (UInt32)((12 * 1000) / mSimDrive.Mmpp / 60);
            SVelocity = (UInt32)((0 * 1000) / mSimDrive.Mmpp / 60);
            JogVelocity = (UInt32)((8 * 1000) / mSimDrive.Mmpp / 60);
            ATime = 500;
            DTime = 500;


            mOrderMan.order_org.Velocity1 = (int)HVelocity1;
            mOrderMan.order_org.Velocity2 = (int)HVelocity2;

            mOrderMan.order_runto.Velocity = (int)Velocity;
            mOrderMan.order_forw.Velocity = (int)JogVelocity;
            mOrderMan.order_backw.Velocity = (int)JogVelocity;

            mSimDrive.AccTime = ATime;
            mSimDrive.DecTime = DTime;


            mOrderMan.order_sync.mSyncOrderList = mSyncOrderList;
            mOrderMan.FinishEvent += new OrderMan.FinishEventHandler(mSimDriveMan_FinishEvent);
        }
        void mSimDriveMan_FinishEvent(bool isNormalStop)
        {
            if (isNormalStop)
                DriveStatus = DRIVE_MAN_STATUS.STOP;
            else
                DriveStatus = DRIVE_MAN_STATUS.LIMIT;

            DriveOrder = DRIVE_MAN_ORDER.IDLE;
        }

        void FLYAD7_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "DriveOrder")
            {
                switch (DriveOrder) 
                {

                    case DRIVE_MAN_ORDER.FORWORD:
                        DriveStatus = DRIVE_MAN_STATUS.RUNNING;
                        mOrderMan.Forw();
                        break;
                    case DRIVE_MAN_ORDER.BACKWORD:
                        DriveStatus = DRIVE_MAN_STATUS.RUNNING;
                        mOrderMan.Backw();
                        break;
                    case DRIVE_MAN_ORDER.ORIGIN:
                        DriveStatus = DRIVE_MAN_STATUS.RUNNING;
                        mOrderMan.Org();
                        break;
                    case DRIVE_MAN_ORDER.RUNTO:
                        DriveStatus = DRIVE_MAN_STATUS.RUNNING;
                        mOrderMan.Runto(DriveRuntoTarget);
                        break;
                    case DRIVE_MAN_ORDER.SYNC:
                        DriveStatus = DRIVE_MAN_STATUS.RUNNING;
                        mOrderMan.Sync();
                        break;
                    case DRIVE_MAN_ORDER.STOP:
                        DriveStatus = DRIVE_MAN_STATUS.RUNNING;
                        mOrderMan.Stop();
                        break;
                    case DRIVE_MAN_ORDER.IDLE:
                        DriveStatus = DRIVE_MAN_STATUS.STOP;
                        break;
                }
            }
        }
































        class SimulationDataCell
        {
            public UInt16 istatus;
            public UInt32 ad;
            public override string ToString()
            {
                return "ad=" + ad.ToString() + " istatus=" + istatus.ToString("X3");
            }
        }



        public void Save() 
        {
            Misc.SaveToXmlHepler.Save(param_path, this);
        }
        public void Load() 
        {
            Misc.SaveToXmlHepler.Load(param_path, this);
        }







        double systick;
        public Int32 GetSysTick()
        {
            //以读到的 mSimulationDatas_idx 作为时间, 一个为1.28ms
            return (Int32)systick;
        }
        public Int32 GetSysTick(DateTime dt) 
        {
            return (Int32)(systick + (dt - Now).TotalMilliseconds);
        }
        


        //1.28ms 触发一次
        void OnPoll() 
        {
            if (Now == DateTime.MinValue)
            {
                Now = DateTime.Now;
                systick = 0;
            }
            else
            {
                Now += TimeSpan.FromTicks((int)(TimeSpan.TicksPerMillisecond * 1.28));
                systick += 1.28;
            }

            UpdateAD();
            UpdatePos();
            UpdateIO();
            UpdateDrive();
        }

        DateTime dt = DateTime.MinValue;
        void UpdateTimeSpan1ms()
        {
            DateTime d = DateTime.Now;
            if (dt == DateTime.MinValue)
                dt = d;
            else
            {
                TimeSpan1ms = (d-dt).TotalMilliseconds;
                dt = d;
            }
        }
        void UpdateAD() 
        {
            mSimGageAD.OnPoll(Now);
            int ad = mSimGageAD.GetAD(Position);

            ADSum += ad;
            ADCnt++;
            AD = ad;
            TimeGrid.Add(ad);
            if (TimeGrid.Count >= 1000)
            {
                ADForShow = ADSum / ADCnt;
                ADSum = 0;
                ADCnt = 0;

                UpdateTimeSpan1ms();
                if (TimeGridEvent != null) 
                {
                    TimeGridEvent.Invoke(this,
                        new TimeGridEventArgs(
                            Now,
                            TimeSpan.FromMilliseconds(1.28),
                            TimeGrid.ToArray()));
                }
                TimeGrid.Clear();
            }
        }

        public class PosGrid 
        {
            
            public int[] Datas = new int[1000];
            List<int> gridsum = new List<int>();
            int grid_index_last = -1;//上一个grid_index
            bool isEnable;
            int grid_start = 0;
            public void Start() 
            {
                isEnable = true;
                grid_start = grid_index_last;
            }
            public void End() 
            {
                if (isEnable)
                {
                    if (gridsum.Count > 0)
                    {
                        Datas[grid_index_last] = (int)(gridsum.Average());
                        gridsum.Clear();
                    }
                    int grid_end = grid_index_last;
                    Misc.DIRECTION dir = Misc.DIRECTION.FORWARD;

                    if (grid_end < grid_start)
                    {
                        int t = grid_start;
                        grid_start = grid_end;
                        grid_end = t;
                        dir = Misc.DIRECTION.BACKWARD;
                    }
                    int len = grid_end - grid_start + 1;
                    int[] data = Datas.Skip(grid_start).Take(len).ToArray();
                    if (PushEvent != null)
                        PushEvent(false, dir, grid_start, data);
                } isEnable = false;
                
            }
            public delegate void PushEventHandler(bool isMini, Misc.DIRECTION dir, int grid_start, int[] data);
            public event PushEventHandler PushEvent;

            public void UpdateGrid(int grid_index, int ad) 
            {
                if (grid_index >= 0 && grid_index < Datas.Count())
                {
                    if (grid_index_last == grid_index)
                    {
                        gridsum.Add(ad);
                    }
                    else
                    {
                        Misc.DIRECTION dir = Misc.DIRECTION.FORWARD;
                        if (grid_index < grid_index_last)
                            dir = Misc.DIRECTION.BACKWARD;
                        if (gridsum.Count > 0)
                        {
                            Datas[grid_index_last] = (int)(gridsum.Average());
                            gridsum.Clear();
                            //TODO 推送MINIGRID
                            if (isEnable) 
                            {
                                int[] data = new int[1];
                                data[0] = Datas[grid_index_last];
                                if (PushEvent != null)
                                    PushEvent(true, dir, grid_index_last, data);
                            }
                        }
                        grid_index_last = grid_index;
                    }
                }
            }    
        }
        public PosGrid mPosGrid = new PosGrid();
        
        void UpdatePos() 
        {
            Position = mSimDrive.Pos;
            //Speed = mSimDrive.Speed;    
            mPosGrid.UpdateGrid(Position / PosOfGrid, AD);


            Position2Org = mSimGageAD.GetPosition2();
            int pos2;
            if(mSyncPos2List.Position2Changed(Position2, out pos2))
            {
                Position2 = pos2;
            }

            Speed2 = mSimGageAD.GetSpeed2();
        }


       
        void UpdateIO() 
        {
            UInt16 istatus = mSimGageAD.GetInput();

            if (mSimDrive.IsMaxLimit)
                Misc.MyBase.SIGNBIT(ref istatus, IODefinition.IN_MAXLIMIT - 1);
            else
                Misc.MyBase.CLEARBIT(ref istatus, IODefinition.IN_MAXLIMIT - 1);
            if (mSimDrive.IsMinLimit)
                Misc.MyBase.SIGNBIT(ref istatus, IODefinition.IN_MINLIMIT - 1);
            else
                Misc.MyBase.CLEARBIT(ref istatus, IODefinition.IN_MINLIMIT - 1);

            if (mSimDrive.IsOrg)
                Misc.MyBase.CLEARBIT(ref istatus, IODefinition.IN_ORG - 1);
            else
                Misc.MyBase.SIGNBIT(ref istatus, IODefinition.IN_ORG - 1);

            UInt16 inchange = (UInt16)(istatus ^ IStatus);
            if (Misc.MyBase.CHECKBIT(inchange, IODefinition.IN_VSENSOR - 1)) 
            {
                //处理position2
                int pos2;
                if(mSyncPos2List.VSensorChanged(Misc.MyBase.CHECKBIT(istatus, IODefinition.IN_VSENSOR - 1), Position2, out pos2))
                {
                    Position2 = pos2;
                }
            }
            IStatus = istatus;
        }
        class SyncPos2Collection
        {
            List<SyncPos2Item> list = new List<SyncPos2Item>();
            int curr_offset = int.MaxValue;
            public void Add(SyncPos2Item item) 
            {
                list.Add(item);
            }
            public void Clear() 
            {
                list.Clear();
            }
            /// <summary>
            /// 信号改变时调用
            /// </summary>
            /// <param name="vsensor"></param>
            /// <param name="position2"></param>
            public bool VSensorChanged(bool vsensor, int position2, out int correct_pos2) 
            {
                correct_pos2 = position2;
                if (list.Count() == 0)
                    return false;//列表是空的,什么都不干

                SyncPos2Item spitem = list.First();//当前在处理的项

                if (vsensor == spitem.At01) 
                {
                    int offset = position2 - spitem.pos2;
                    if (Math.Abs(curr_offset) > Math.Abs(offset))
                    {
                        curr_offset = offset;
                        return false;
                    }
                    else
                    {
                        //已经找到最近的一个
                        correct_pos2 = spitem.pos2 + curr_offset;
                        curr_offset = int.MaxValue;
                        list.RemoveAt(0);

                        if (list.Count() > 0)
                        {
                            spitem = list.First();//当前在处理的项
                            if (vsensor == spitem.At01)
                                curr_offset = position2 - spitem.pos2;
                        }
                        return true;
                    }
                }
                return false;
            }
            /// <summary>
            /// 脉冲改变时调用
            /// </summary>
            /// <param name="position2"></param>
            /// <param name="correct_pos2"></param>
            /// <returns></returns>
            public bool Position2Changed(int position2, out int correct_pos2) 
            {
                correct_pos2 = position2;
                if (list.Count() == 0)
                    return false;//列表是空的,什么都不干

                SyncPos2Item spitem = list.First();//当前在处理的项

                if(curr_offset!=int.MaxValue)//之前有一个信号
                {
                    int offset = position2 - spitem.pos2;
                    if (Math.Abs(curr_offset) > Math.Abs(offset))
                    {
                        return false;
                    }
                    else
                    {
                        //已经找到最近的一个
                        correct_pos2 = spitem.pos2 + curr_offset;
                        curr_offset = int.MaxValue;
                        list.RemoveAt(0);
                        return true;
                    }
                }
                return false;
            }

        }

        void UpdateDrive() 
        {
            mSimDrive.OnPoll(Now);
            mOrderMan.OnPoll();
        }

        

        #region INotifyPropertyChanged 成员
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion


        #region IFlyAD接口

        DateTime now = DateTime.MinValue;
        /// <summary>
        /// systick 转换的时间
        /// </summary>
        public DateTime Now
        {
            get
            {
                return now;
            }
            set
            {
                if (now != value)
                {
                    now = value;
                    NotifyPropertyChanged("Now");
                }
            }
        }


        
        private int position;
        public int Position
        {
            get { return position; }
            set
            {
                if (position != value)
                {
                    position = value;
                    NotifyPropertyChanged("Position");
                }
            }
        }
        private int speed;
        public int Speed
        {
            get { return speed; }
            set
            {
                if (speed != value)
                {
                    speed = value;
                    NotifyPropertyChanged("Speed");
                }
            }
        }
        private int ad;
        public int AD
        {
            get { return ad; }
            set
            {
                if (ad != value)
                {
                    ad = value;
                    NotifyPropertyChanged("AD");
                }
            }
        }
        public int ADMax
        {
            get { return 0xffff; }
        }

        private UInt16 istatus;
        public UInt16 IStatus
        {
            get { return istatus; }
            set
            {
                if (istatus != value)
                {
                    istatus = value;
                    NotifyPropertyChanged("IStatus");
                }
            }
        }

        private UInt16 ostatus = 0xf;
        public UInt16 OStatus
        {
            get { return ostatus; }
            set
            {
                if (ostatus != value)
                {
                    ostatus = value;
                    NotifyPropertyChanged("OStatus");
                }
            }
        }

        public void SetOutput(ushort mask, ushort enable)
        {
            UInt16 ostatus = OStatus;
            for (int i = 0; i < 16; i++)
            {
                if (Misc.MyBase.CHECKBIT(mask, i))
                {
                    if (Misc.MyBase.CHECKBIT(enable, i))
                        Misc.MyBase.SIGNBIT(ref ostatus, i);
                    else
                        Misc.MyBase.CLEARBIT(ref ostatus, i);
                }
            }
            OStatus = ostatus;
        }

        public void UpdateParam()
        {
            //throw new NotImplementedException();
        }
        #region 配置参数
        public void SetSysParam(ushort posOfGrid, FlyADBase.MOTORTYPE motortype, ushort ratio01, ushort ratio02)
        {
            MotorType = motortype;

        }

        private MOTORTYPE motorType = MOTORTYPE.VF0;
        public MOTORTYPE MotorType
        {
            get
            {
                return motorType;
            }
            set
            {
                if (motorType != value) 
                {
                    motorType = value;
                    //TODO
                    mSimDrive.MotorType = value;
                    NotifyPropertyChanged("MotorType");
                }
            }
        }

        UInt16 posOfGrid;//1个Grid=?脉冲数(2B)
        public UInt16 PosOfGrid
        {
            get
            {
                return posOfGrid;
            }
            set
            {
                if (posOfGrid != value) 
                {
                    posOfGrid = value;
                    NotifyPropertyChanged("PosOfGrid");
                }
            }
        }



        UInt16 ratio01=1;
        /// <summary>
        /// //电机比例(2B)
        /// </summary>
        public UInt16 Ratio01
        {
            get
            {
                return ratio01;
            }
            set
            {
                if (ratio01 != value)
                {
                    ratio01 = value;
                    NotifyPropertyChanged("Ratio01");
                }
            }
        }
        UInt16 ratio02=1;//脉冲比例(2B)
        public UInt16 Ratio02
        {
            get
            {
                return ratio02;
            }
            set
            {
                if (ratio02 != value) 
                {
                    ratio02 = value;
                    NotifyPropertyChanged("Ratio02");
                }
            }
        }
        #endregion

        Int16 posOffset;
        public Int16 PosOffset
        {
            get
            {
                return posOffset;
            }
            set
            {
                if (posOffset != value) 
                {
                    posOffset = value;
                    NotifyPropertyChanged("PosOffset");
                }
            }
        }

        UInt32 jogVelocity=500;
        public UInt32 JogVelocity
        {
            get
            {
                return jogVelocity;
            }
            set
            {
                if (jogVelocity != value) 
                {
                    jogVelocity = value;
                    NotifyPropertyChanged("JogVelocity");
                    mOrderMan.order_forw.Velocity = (int)JogVelocity;
                    mOrderMan.order_backw.Velocity = (int)JogVelocity;
                }
            }
        }

        #region 速度
        UInt32 velocity;
        public UInt32 Velocity
        {
            get
            {
                return velocity;
            }
            set
            {
                if (velocity != value) 
                {
                    velocity = value;
                    NotifyPropertyChanged("Velocity");
                    mOrderMan.order_runto.Velocity = (int)Velocity;
                }
            }
        }
        UInt32 svelocity;
        public uint SVelocity
        {
            get
            {
                return svelocity;
            }
            set
            {
                if (svelocity != value)
                {
                    svelocity = value;
                    NotifyPropertyChanged("SVelocity");
                }
            }
        }
        UInt32 atime;
        public UInt32 ATime
        {
            get
            {
                return atime;
            }
            set
            {
                if (atime != value)
                {
                    atime = value;
                    NotifyPropertyChanged("ATime");
                    mSimDrive.AccTime = atime;
                }
            }
        }
        UInt32 dtime;
        public UInt32 DTime
        {
            get
            {
                return dtime;
            }
            set
            {
                if (dtime != value)
                {
                    dtime = value;
                    NotifyPropertyChanged("DTime");
                    mSimDrive.DecTime = dtime;
                }
            }
        }
        UInt32 hvelocity1;

        public UInt32 HVelocity1
        {
            get
            {
                return hvelocity1;
            }
            set
            {
                if (hvelocity1 != value)
                {
                    hvelocity1 = value;

                    NotifyPropertyChanged("HVelocity1");

                    mOrderMan.order_org.Velocity1 = (int)HVelocity1;
                }
            }
        }
        UInt32 hvelocity2;
        public UInt32 HVelocity2
        {
            get
            {
                return hvelocity2;
            }
            set
            {
                if (hvelocity2 != value)
                {
                    hvelocity2 = value;
                    NotifyPropertyChanged("HVelocity2");

                    mOrderMan.order_org.Velocity2 = (int)HVelocity2;;
                }
            }
        }
        public void SetPosParam(uint velocity, uint sv, uint atime, uint dtime, uint hspeed1, uint hspeed2)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region 密码
        AREA_STATUS areaStatus;
        public AREA_STATUS AreaStatus
        {
            get {
                return areaStatus;
            }
            set {
                if (areaStatus != value) 
                {
                    areaStatus = value;
                    NotifyPropertyChanged("AreaStatus");
                }
            }
        }
        AREA_ERR areaRet;
        public AREA_ERR AreaRet
        {
            get
            {
                return areaRet;
            }
            set
            {
                if (areaRet != value)
                {
                    areaRet = value;
                    NotifyPropertyChanged("AreaRet");
                }
            }
        }

        byte[] code = new byte[7] { 0x74, 0xB7, 0x07, 0x4E, 0x52, 0x3D, 0x06 };
        int surplus = 8000;
        byte[] access = new byte[8] { 0x19, 0x4A, 0xA5, 0x21, 0x7B, 0x74, 0x1A, 0x00 };


        public byte[] Code
        {
            get { return code; }
        }


        public int Surplus
        {
            get { return surplus; }
            set {
                if (surplus != value) {
                    surplus = value;
                    NotifyPropertyChanged("Surplus");
                }
            }
        }

        public byte[] Access
        {
            get { return access; }
        }

        public void SetAccess(byte[] access)
        {
            if (access.Length != 8)
                return;
            Array.Copy(access, Access, 8);
            AreaStatus = AREA_STATUS.VALID; 
            AreaRet = AREA_ERR.NO_ERR;
            Surplus = 8000;
        }

        public void InitArea()
        {
            throw new NotImplementedException();
        }
        #endregion



        public void GetGrid(Misc.DIRECTION direction, int grid_start, int grid_len, out int[] dat) 
        {
            dat = null;
        }
        public void GetGrid(Misc.DIRECTION direction, out int[] dat) 
        {
            GetGrid(direction, 0, 1000,out  dat);
        }

        public event TimeGridEventHandler TimeGridEvent;

        public event MiniGridEventHandler MiniGridEvent;

        public event MiniGridEventHandler GridEvent;

        public event IStatusChangedEventHandler IStatusChangedEvent;

        public void Runto(int to)
        {
            Drive(DRIVE_MAN_ORDER.RUNTO, to);
        }

        public void Origin()
        {
            Drive(DRIVE_MAN_ORDER.ORIGIN);
        }

        public void Stop()
        {
            Drive(DRIVE_MAN_ORDER.STOP);
        }

        public void Backward()
        {
            Drive(DRIVE_MAN_ORDER.BACKWORD);
        }

        public void Forward()
        {
            Drive(DRIVE_MAN_ORDER.FORWORD);
        }


        DRIVE_MAN_ORDER driveorder;
        public DRIVE_MAN_ORDER DriveOrder
        {
            get
            {
                return driveorder;
            }
            set
            {
                if (driveorder != value)
                {
                    driveorder = value;
                    NotifyPropertyChanged("DriveOrder");
                }
            }
        }
        DRIVE_MAN_STATUS drivestatus;
        public DRIVE_MAN_STATUS DriveStatus
        {
            get { return drivestatus; }
            set
            {
                if (drivestatus != value)
                {
                    drivestatus = value;
                    NotifyPropertyChanged("DriveStatus");
                }
            }
        }

        int DriveRuntoTarget;

        void Drive(DRIVE_MAN_ORDER order, object param) 
        {
            if (order == DRIVE_MAN_ORDER.RUNTO)
            {
                DriveRuntoTarget = (int)param; 
            }
            DriveOrder = order;
        }
        void Drive(DRIVE_MAN_ORDER order) 
        {
            Drive(order, null);
        }
        #endregion

        #region 同步
        Int32 marker;
        /// <summary>
        /// 同步用标示
        /// </summary>
        public Int32 Marker {
            get {
                return marker;
            }
            set {
                if (marker != value)
                {
                    marker = value;
                    NotifyPropertyChanged("Marker");
                }
            }
        }
        #region 横向脉冲
        int pos1lcshift;
        /// <summary>
        /// 逻辑横向脉冲偏移  Pos1 + Pos1LCShift = Pos1LC
        /// </summary>
        public int Pos1LCShift 
        {
            get {
                return pos1lcshift;
            }
            set {
                if (pos1lcshift != value) 
                {
                    pos1lcshift = value;
                    NotifyPropertyChanged("Pos1LCShift");
                }
            }
        }
        public void SetPos1LCShift(int value)
        {
            Pos1LCShift = value;
        }
        #endregion
        #region 主轴脉冲
        private int position2_org;
        int Position2Org 
        {
            get { return position2_org; }
            set {
                if (position2_org != value) 
                {
                    position2_org = value;
                    
                    NotifyPropertyChanged("Position2Org");
                    cal_position2();
                }
            }
        }
        private int position2;
        /// <summary>
        /// 纵向脉冲,也叫主轴脉冲
        /// </summary>
        public int Position2 {
            get { return position2; }
            set {
                if (position2 != value) 
                {
                    position2 = value;

                    NotifyPropertyChanged("Position2");
                    Pos2Shift = position2 - (int)(Position2Org * Pos2Comp);
                }
            }
        }

        void cal_position2() 
        {
            position2 = (int)(Position2Org * Pos2Comp + Pos2Shift);
        }
        private int speed2;
        public int Speed2 {
            get { return speed2; }
            set {
                if (speed2 != value)
                {
                    speed2 = value;
                    NotifyPropertyChanged("Speed2");
                }
            }
        }
        int pos2shift=0;
        /// <summary>
        /// 纵向偏移
        /// </summary>
        int Pos2Shift
        {
            get {
                return pos2shift;
            }

            set {
                if (pos2shift != value) 
                {
                    pos2shift = value;
                    NotifyPropertyChanged("Pos2Shift");
                    cal_position2();
                }
            }
        }
        public void SetPos2Offset(int offest) 
        {
            Pos2Shift += offest;
        }

        private float pos2comp=1;
        /// <summary>
        /// 纵向值补偿系数,补偿时,先乘除后加减
        /// </summary>
        public float Pos2Comp 
        {
            get {
                return pos2comp;
            }
            set {
                if (pos2comp != value) 
                {
                    pos2comp = value;
                    NotifyPropertyChanged("Pos2Comp");
                    cal_position2();
                }
            }
        }

        struct SyncPos2Item
        {
            public bool At01;//? or At10
            public int pos2;
            public bool immediately;
        }
        SyncPos2Collection mSyncPos2List = new SyncPos2Collection();
        
        /// <summary>
        /// 纵向同步事件,0-1事件
        /// </summary>
        /// <param name="pos2"></param>
        public void SetPos2At01(int pos2,bool immediately) 
        {
            mSyncPos2List.Add(new SyncPos2Item() { At01 = true, pos2 = pos2, immediately = immediately });
        }

        #endregion
        #region 同步控制 同步状态转换规则
        /// <summary>
        /// 进入同步状态
        /// </summary>
        /// <param name="pos2"></param>
        public void SyncBegin(int pos2) 
        {
            Position2 = pos2;
            SyncBegin();
        }
        /// <summary>
        /// 进入同步状态
        /// </summary>
        public void SyncBegin() 
        {
            Drive(DRIVE_MAN_ORDER.SYNC);
        }
        /// <summary>
        /// 退出同步状态
        /// </summary>
        public void SyncEnd() 
        {
            Drive(DRIVE_MAN_ORDER.STOP);
        }

        /// <summary>
        /// 清空同步指令
        /// </summary>
        public void SyncClear()
        {
            mSyncOrderList.Clear();
        }
        /// <summary>
        /// 清空POS2同步列表
        /// </summary>
        public void SyncPos2Clear()
        {
            mSyncPos2List.Clear();
        }
        #endregion
        #region 同步扫描 脚本指令



        List<ASyncOrder> mSyncOrderList = new List<ASyncOrder>();




        //同步扫描至
        //D+0xE0+开始主轴位置+结束主轴位置+结束横向脉冲位置(逻辑位置)+脉冲开关(1B)+命令识标号(4B)
        public void SyncRunAtLC(int pos2_begin, int pos2_end, int pos1lc, bool hasDataGrid, Int32 marker) 
        {
            mSyncOrderList.Add(
                new SyncOrder_RunAtLC()
                {
                    pos2_begin = pos2_begin,
                    pos2_end = pos2_end,
                    pos1lc_end = pos1lc,
                    hasDataGrid = hasDataGrid,
                    marker = marker
                });
        }

        //7.2	位于队列头时运行,归零
        //D+0xE1+命令识标号(4B)
        public void SyncOrigin(Int32 marker) 
        {
            mSyncOrderList.Add(
                new SyncOrder_Origin()
                {
                    marker = marker
                });
        }


        //7.3	位于队列头时运行,以速度运行至物理位置
        //D+0xE2+横向脉冲位置(4B:int32,物理位置)+速度(4B:int32)+脉冲开关(1B)+命令识标号(4B)
        public void SyncRunTo(int pos1, UInt32 velocity, bool hasDataGrid, Int32 marker)
        {
            mSyncOrderList.Add(
                new SyncOrder_RunTo()
                {
                    pos1 = pos1,
                    velocity = velocity,
                    hasDataGrid = hasDataGrid,
                    marker = marker
                });
        }

        //7.3	位于队列头时运行,以速度运行至逻辑位置
        //D+0xE3+横向脉冲位置(4B:int32,逻辑位置)+速度(4B:int32)+脉冲开关(1B)+命令识标号(4B)
        public void SyncRunToLC(int pos1lc, UInt32 velocity, bool hasDataGrid, Int32 marker)
        {
            mSyncOrderList.Add(
                new SyncOrder_RunToLC()
                {
                    pos1lc = pos1lc,
                    velocity = velocity,
                    hasDataGrid = hasDataGrid,
                    marker = marker
                });
        }


        //7.4	等待,ms
        //D+0xE4+毫秒数(4B:int32)+命令识标号(4B)
        public void SyncWait(int ms, Int32 marker)
        {
            mSyncOrderList.Add(
                new SyncOrder_Wait()
                {
                    ms = ms,
                    marker = marker
                });
        }


        #endregion
        #endregion

        public string[] GetSavePropertyNames()
        {
            return new string[] { 
                "LocalEP",
                "SimMode",
                "MotorType"
            };
        }
    }
    public static class FLYAD7_IODefinition
    {
        public const int IN_ORG = 2;
                public const int IN_MINLIMIT = 3;
                public const int IN_MAXLIMIT = 4;
                public const int IN_FORW = 7;
                public const int IN_BACKW = 8;
                public const int IN_VSENSOR = 12;
    }
}