FLYAD7.cs 31.3 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
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");
                }
            }
        }
34
        public int Version { get; } = 2;
潘栩锋's avatar
潘栩锋 committed
35 36


37 38 39 40 41
        public int ADForShow { get; set; }

        public int PosForShow { get; set; }

        public int Pos2ForShow { get; set; }
潘栩锋's avatar
潘栩锋 committed
42 43 44 45 46 47 48


        private int ADSum;
        private int ADCnt;

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

49 50
        public double TimeSpan1ms { get; set; }

潘栩锋's avatar
潘栩锋 committed
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

        /// <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接口

        /// <summary>
        /// systick 转换的时间
        /// </summary>
556
        public DateTime Now { get; set; } = DateTime.MinValue;
潘栩锋's avatar
潘栩锋 committed
557 558


559 560 561 562 563 564 565 566 567

        public int Position { get; set; }


        public int Speed { get; set; }


        public int AD { get; set; }

潘栩锋's avatar
潘栩锋 committed
568 569 570 571 572 573
        public int ADMax
        {
            get { return 0xffff; }
        }


574 575 576 577 578
        public UInt16 IStatus { get; set; }


        public UInt16 OStatus { get; set; } = 0xf;

潘栩锋's avatar
潘栩锋 committed
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

        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;
                }
            }
        }

625 626 627
        //1个Grid=?脉冲数(2B)
        public UInt16 PosOfGrid { get; set; }

潘栩锋's avatar
潘栩锋 committed
628 629 630 631 632 633



        /// <summary>
        /// //电机比例(2B)
        /// </summary>
634 635 636 637 638 639 640
        public UInt16 Ratio01 { get; set; } = 1;

        /// <summary>
        /// 脉冲比例(2B)
        /// </summary>
        public UInt16 Ratio02 { get; set; } = 1;

潘栩锋's avatar
潘栩锋 committed
641 642
        #endregion

643 644 645

        public Int16 PosOffset { get; set; }

潘栩锋's avatar
潘栩锋 committed
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

        UInt32 jogVelocity=500;
        public UInt32 JogVelocity
        {
            get
            {
                return jogVelocity;
            }
            set
            {
                if (jogVelocity != value) 
                {
                    jogVelocity = value;
                    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;
                    mOrderMan.order_runto.Velocity = (int)Velocity;
                }
            }
        }
682 683 684

        public uint SVelocity { get; set; }

潘栩锋's avatar
潘栩锋 committed
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
        UInt32 atime;
        public UInt32 ATime
        {
            get
            {
                return atime;
            }
            set
            {
                if (atime != value)
                {
                    atime = value;
                    mSimDrive.AccTime = atime;
                }
            }
        }
        UInt32 dtime;
        public UInt32 DTime
        {
            get
            {
                return dtime;
            }
            set
            {
                if (dtime != value)
                {
                    dtime = value;
                    mSimDrive.DecTime = dtime;
                }
            }
        }
        UInt32 hvelocity1;

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

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

                    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 密码

761
        public AREA_STATUS AreaStatus { get; set; }
潘栩锋's avatar
潘栩锋 committed
762

763
        public AREA_ERR AreaRet { get; set; }
潘栩锋's avatar
潘栩锋 committed
764 765 766 767




768 769 770 771 772
        public byte[] Code { get; } = new byte[7] { 0x74, 0xB7, 0x07, 0x4E, 0x52, 0x3D, 0x06 };

        public int Surplus { get; set; } = 8000;

        public byte[] Access { get; } = new byte[8] { 0x19, 0x4A, 0xA5, 0x21, 0x7B, 0x74, 0x1A, 0x00 };
潘栩锋's avatar
潘栩锋 committed
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

        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);
        }


835 836 837 838 839
        public DRIVE_MAN_ORDER DriveOrder { get; set; }


        public DRIVE_MAN_STATUS DriveStatus { get; set; }

潘栩锋's avatar
潘栩锋 committed
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857

        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 同步
858

潘栩锋's avatar
潘栩锋 committed
859 860 861
        /// <summary>
        /// 同步用标示
        /// </summary>
862 863
        public Int32 Marker { get; set; }

潘栩锋's avatar
潘栩锋 committed
864
        #region 横向脉冲
865

潘栩锋's avatar
潘栩锋 committed
866 867 868
        /// <summary>
        /// 逻辑横向脉冲偏移  Pos1 + Pos1LCShift = Pos1LC
        /// </summary>
869 870
        public int Pos1LCShift { get; set; } 

潘栩锋's avatar
潘栩锋 committed
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
        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;
                    
                    cal_position2();
                }
            }
        }
        private int position2;
        /// <summary>
        /// 纵向脉冲,也叫主轴脉冲
        /// </summary>
        public int Position2 {
            get { return position2; }
            set {
                if (position2 != value) 
                {
                    position2 = value;

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

        void cal_position2() 
        {
            position2 = (int)(Position2Org * Pos2Comp + Pos2Shift);
        }
910 911 912

        public int Speed2 { get; set; }

潘栩锋's avatar
潘栩锋 committed
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
        int pos2shift=0;
        /// <summary>
        /// 纵向偏移
        /// </summary>
        int Pos2Shift
        {
            get {
                return pos2shift;
            }

            set {
                if (pos2shift != value) 
                {
                    pos2shift = value;
                    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;
                    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();
        }
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
        public UInt16 GetSyncOrderCnt()
        {
            return (UInt16)mSyncOrderList.Count();
        }
        public SYNC_STATUS GetSyncStatus()
        {
            if (DriveOrder == DRIVE_MAN_ORDER.SYNC)
                return SYNC_STATUS.SYNCST_YES;
            else
                return SYNC_STATUS.SYNCST_NO;
        }
潘栩锋's avatar
潘栩锋 committed
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
        #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"
            };
        }
1112 1113 1114 1115 1116

        public void SetVelocity(uint velocity)
        {
            Velocity = velocity;
        }
潘栩锋's avatar
潘栩锋 committed
1117 1118 1119 1120
    }
    public static class FLYAD7_IODefinition
    {
        public const int IN_ORG = 2;
1121 1122 1123 1124 1125
        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;
潘栩锋's avatar
潘栩锋 committed
1126 1127
    }
}