GM_ScanMotion.cs 18.7 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using Misc;
using FlyADBase;
using FLY.Thick.Base.Common;
10
using System.Diagnostics;
潘栩锋's avatar
潘栩锋 committed
11 12 13 14 15 16 17 18 19 20 21 22

namespace FLY.Thick.Base.Server
{
    public delegate void EndEventHandler(object obj, MiniGridEventArgs e);
    /// <summary>
    /// 只是来回走的逻辑,不包含数据处理
    /// </summary>
    public abstract class GM_ScanMotion : GM_Base
    {
        #region 成员变量
        protected GSample gsample;

23 24 25 26 27 28 29 30 31
        /// <summary>
        /// 归0开始时间点,用于计算什么时候需要归0
        /// </summary>
        private TimeSpan orgStartTime;
       
        /// <summary>
        /// 使用timer.Elapsed 代替 DateTime.Now 使用
        /// </summary>
        private Stopwatch timer = new Stopwatch();
潘栩锋's avatar
潘栩锋 committed
32

33 34 35 36 37 38 39 40 41 42 43 44
        /// <summary>
        /// 归原点间隔
        /// </summary>
        public int OrgInterval { get; set; }


        /// <summary>
        /// 扫描范围
        /// </summary>
        public int ScanStart { get; set; }

        public int ScanEnd { get; set; }
潘栩锋's avatar
潘栩锋 committed
45 46 47 48 49 50 51 52 53 54


        public event EndEventHandler DataPosEvent;
        public event EndEventHandler EndEvent;

        public event EventHandler StartEvent;
        public event EventHandler FinishEvent;

        enum CTRLST
        {
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
            INIT = 0,//判断进入阶段

            #region 0  样品采集阶段
            ORIGINWAIT = 2,//正在归0
            FORWARD = 3,//前进到样品,或扫描结束位置
            FORWAIT_1 = 4,//前进等待
            FORWAIT_2 = 5,//前进结束,等待获取grid
            BACKWARD = 8,//后退到温修,或扫描开始位置
            BACKWAIT_1 = 9,//后退等待
            BACKWAIT_2 = 10,//后退结束,等待获取grid
            FORWAIT_SAMPLE = 11,//前进取样中
            FORW_SAMPLE = 12,//前进取样
            BACKWAIT_SAMPLE = 13,//后退取样中
            BACKW_SAMPLE = 14,//后退取样
            #endregion

            #region 正常扫描阶段
            SCAN_FORW = 20,//前进扫描
            SCAN_FORWAIT = 21,//前进等待
            SCAN_BACKW = 22,//后退扫描
            SCAN_BACKWAIT = 23,//后退等待
            #endregion


潘栩锋's avatar
潘栩锋 committed
79 80 81 82 83 84
        }
        CTRLST state;
        #endregion

        public UInt32 Velocity { get; set; }

85
        public GM_ScanMotion()
潘栩锋's avatar
潘栩锋 committed
86 87 88 89 90
        {
            GMState = CTRL_STATE.SCAN;

            ScanStart = -1;
            ScanEnd = -1;
91 92 93 94 95 96

        }
        public virtual void Init(FlyAD7 flyad, GSample gsample) 
        {
            base.Init(flyad);
            this.gsample = gsample;
潘栩锋's avatar
潘栩锋 committed
97 98
            flyad.MiniGridEvent += (s, e) =>
            {
99
                if ((state == CTRLST.FORWAIT_1)
100 101 102
                || (state == CTRLST.BACKWAIT_1)
                || (state == CTRLST.SCAN_FORWAIT)
                || (state == CTRLST.SCAN_BACKWAIT))
潘栩锋's avatar
潘栩锋 committed
103
                {
104
                    DataPosEvent?.Invoke(this, e);
潘栩锋's avatar
潘栩锋 committed
105 106 107 108 109 110 111 112 113
                }
            };
        }

        protected virtual bool Start2(int start, int end)
        {
            ScanStart = start;
            ScanEnd = end;

114 115 116
            timer.Restart();
            orgStartTime = TimeSpan.FromMinutes(-OrgInterval);
            state = CTRLST.INIT;
潘栩锋's avatar
潘栩锋 committed
117 118

            if (gsample != null)
潘栩锋's avatar
潘栩锋 committed
119
                gsample.Reset(ScanStart, ScanEnd);
潘栩锋's avatar
潘栩锋 committed
120

121
            StartEvent?.Invoke(this, null);
潘栩锋's avatar
潘栩锋 committed
122 123 124 125 126
            return true;
        }

        public override void Stop()
        {
127 128
            timer.Stop();
            FinishEvent?.Invoke(this, null);
潘栩锋's avatar
潘栩锋 committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
            base.Stop();
        }
        bool WaitFinish(ref CTRLST src, CTRLST next)
        {
            switch (mFlyAD.DriveStatus)
            {
                case DRIVE_MAN_STATUS.STOP://完成任务
                case DRIVE_MAN_STATUS.LIMIT:
                    {
                        src = next;
                        return true;
                    } break;
                case DRIVE_MAN_STATUS.STOP_MANUAL://异常
                    {
                        NotifyError(ERRNO_Running_Manual);
                        Stop(); return false;
                    } break;
            }
            return false;
        }
        protected override void OnPoll()
        {
            switch (state)
            {
153
                case CTRLST.INIT:
潘栩锋's avatar
潘栩锋 committed
154
                    {
155
                        if (IsOrging())
潘栩锋's avatar
潘栩锋 committed
156
                        {
157 158
                            mFlyAD.Origin();
                            state = CTRLST.ORIGINWAIT;
潘栩锋's avatar
潘栩锋 committed
159
                        }
160 161 162 163 164 165
                        else {
                            state = CTRLST.SCAN_FORW;
                        }
                    }
                    break;
                case CTRLST.ORIGINWAIT:
潘栩锋's avatar
潘栩锋 committed
166
                    {
167
                        WaitFinish(ref state, CTRLST.FORWARD);
潘栩锋's avatar
潘栩锋 committed
168
                    } break;
169
                case CTRLST.FORWARD:
潘栩锋's avatar
潘栩锋 committed
170 171 172
                    {
                        int to = ScanEnd;

173 174
                        int b, e;
                        if (gsample.GetSampleRange(Misc.DIRECTION.FORWARD, out b, out e))
潘栩锋's avatar
潘栩锋 committed
175
                        {
176
                            FlyADBase.MOTORTYPE motorType = mFlyAD.MotorType;
潘栩锋's avatar
潘栩锋 committed
177

178 179 180 181 182 183 184 185
                            if (motorType == FlyADBase.MOTORTYPE.VF0)
                            {
                                //直接去到取样的位置
                                to = e;
                            }
                            else//要变速,分开两段
                            {
                                to = b;
潘栩锋's avatar
潘栩锋 committed
186 187
                            }
                        }
188 189
                        
                        mFlyAD.SetVelocity(Velocity);
潘栩锋's avatar
潘栩锋 committed
190
                        mFlyAD.Runto(to);
191
                        state = CTRLST.FORWAIT_1;
潘栩锋's avatar
潘栩锋 committed
192
                    } break;
193
                case CTRLST.FORWAIT_1:
潘栩锋's avatar
潘栩锋 committed
194
                    {
195
                        if (WaitFinish(ref state, CTRLST.FORWAIT_2))
潘栩锋's avatar
潘栩锋 committed
196 197 198
                            FlushAD(DIRECTION.FORWARD);

                    } break;
199
                case CTRLST.FORWAIT_2:
潘栩锋's avatar
潘栩锋 committed
200
                    {
201
                        state = CTRLST.BACKWARD;
潘栩锋's avatar
潘栩锋 committed
202

203 204
                        int b, e;
                        if (gsample.GetSampleRange(Misc.DIRECTION.FORWARD, out b, out e))
潘栩锋's avatar
潘栩锋 committed
205
                        {
206
                            FlyADBase.MOTORTYPE motorType = mFlyAD.MotorType;
潘栩锋's avatar
潘栩锋 committed
207

208
                            if (motorType == FlyADBase.MOTORTYPE.VF0)
潘栩锋's avatar
潘栩锋 committed
209
                            {
210 211 212 213 214 215 216 217
                                //直接去到取样的位置
                                state = CTRLST.FORW_SAMPLE;
                            }
                            else//要变速,分开两段
                            {
                                mFlyAD.SetVelocity(gsample.Velocity);
                                mFlyAD.Runto(e);
                                state = CTRLST.FORWAIT_SAMPLE;
潘栩锋's avatar
潘栩锋 committed
218 219 220
                            }
                        }
                    } break;
221
                case CTRLST.FORWAIT_SAMPLE:
潘栩锋's avatar
潘栩锋 committed
222
                    {
223
                        WaitFinish(ref state, CTRLST.FORW_SAMPLE);
潘栩锋's avatar
潘栩锋 committed
224
                    } break;
225
                case CTRLST.FORW_SAMPLE:
潘栩锋's avatar
潘栩锋 committed
226 227
                    {
                        gsample.Do(Misc.DIRECTION.FORWARD);
228
                        state = CTRLST.BACKWARD;
潘栩锋's avatar
潘栩锋 committed
229
                    } break;
230
                case CTRLST.BACKWARD:
潘栩锋's avatar
潘栩锋 committed
231 232
                    {
                        int to = ScanStart;
233 234 235

                        int b, e;
                        if (gsample.GetSampleRange(Misc.DIRECTION.BACKWARD, out b, out e))
潘栩锋's avatar
潘栩锋 committed
236
                        {
237 238 239
                            FlyADBase.MOTORTYPE motorType = mFlyAD.MotorType;

                            if (motorType == FlyADBase.MOTORTYPE.VF0)
潘栩锋's avatar
潘栩锋 committed
240
                            {
241 242 243 244 245 246
                                //直接去到取样的位置
                                to = b;
                            }
                            else//要变速,分开两段
                            {
                                to = e;
潘栩锋's avatar
潘栩锋 committed
247 248
                            }
                        }
249 250
                        
                        mFlyAD.SetVelocity(Velocity);
潘栩锋's avatar
潘栩锋 committed
251
                        mFlyAD.Runto(to);
252
                        state = CTRLST.BACKWAIT_1;
潘栩锋's avatar
潘栩锋 committed
253
                    } break;
254
                case CTRLST.BACKWAIT_1:
潘栩锋's avatar
潘栩锋 committed
255
                    {
256
                        if (WaitFinish(ref state, CTRLST.BACKWAIT_2))
潘栩锋's avatar
潘栩锋 committed
257 258
                            FlushAD(DIRECTION.BACKWARD);
                    } break;
259
                case CTRLST.BACKWAIT_2:
潘栩锋's avatar
潘栩锋 committed
260
                    {
261 262
                        int b, e;
                        if (gsample.GetSampleRange(Misc.DIRECTION.BACKWARD, out b, out e))
潘栩锋's avatar
潘栩锋 committed
263
                        {
264 265 266
                            FlyADBase.MOTORTYPE motorType = mFlyAD.MotorType;

                            if (motorType == FlyADBase.MOTORTYPE.VF0)
潘栩锋's avatar
潘栩锋 committed
267
                            {
268 269 270 271 272 273 274 275
                                //直接去到取样的位置
                                state = CTRLST.BACKW_SAMPLE;
                            }
                            else//要变速,分开两段
                            {
                                mFlyAD.SetVelocity(gsample.Velocity);
                                mFlyAD.Runto(b);
                                state = CTRLST.BACKWAIT_SAMPLE;
潘栩锋's avatar
潘栩锋 committed
276 277
                            }
                        }
278 279
                        else {
                            //完成一次周期
280
                            if (!gsample.IsFailure())
281 282 283
                                orgStartTime = timer.Elapsed;
                            state = CTRLST.INIT;
                        }
潘栩锋's avatar
潘栩锋 committed
284
                    } break;
285
                case CTRLST.BACKWAIT_SAMPLE:
潘栩锋's avatar
潘栩锋 committed
286
                    {
287
                        WaitFinish(ref state, CTRLST.BACKW_SAMPLE);
潘栩锋's avatar
潘栩锋 committed
288
                    } break;
289
                case CTRLST.BACKW_SAMPLE:
潘栩锋's avatar
潘栩锋 committed
290 291
                    {
                        gsample.Do(Misc.DIRECTION.BACKWARD);
292
                        //完成一次周期
293
                        if (!gsample.IsFailure())
294 295
                            orgStartTime = timer.Elapsed;
                        state = CTRLST.INIT;
潘栩锋's avatar
潘栩锋 committed
296
                    } break;
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


                case CTRLST.SCAN_FORW:
                    {
                        int to = ScanEnd;
                        mFlyAD.SetVelocity(Velocity);
                        mFlyAD.Runto(to);
                        state = CTRLST.SCAN_FORWAIT;
                    }
                    break;
                case CTRLST.SCAN_FORWAIT:
                    {
                        if (WaitFinish(ref state, CTRLST.SCAN_BACKW))
                            FlushAD(DIRECTION.FORWARD);
                    }
                    break;
                case CTRLST.SCAN_BACKW:
                    {
                        int to = ScanStart;
                        mFlyAD.SetVelocity(Velocity);
                        mFlyAD.Runto(to);
                        state = CTRLST.SCAN_BACKWAIT;
                    }
                    break;
                case CTRLST.SCAN_BACKWAIT:
                    {
                        if (WaitFinish(ref state, CTRLST.INIT))
                            FlushAD(DIRECTION.BACKWARD);
                    }
                    break;
潘栩锋's avatar
潘栩锋 committed
327 328 329 330 331
            }
        }

        private bool IsOrging()
        {
332 333 334 335 336 337
            if (OrgInterval > 0) {

                if (timer.Elapsed >= (orgStartTime + TimeSpan.FromMinutes(OrgInterval)))
                    return true;
            }

潘栩锋's avatar
潘栩锋 committed
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
            return false;
        }

        private bool IsOrgEnable()
        {
            if (OrgInterval > 0)
                return true;
            else
                return false;
        }

        private void FlushAD(Misc.DIRECTION direction)
        {
            int pos1, pos2;

            pos1 = ScanStart;
            pos2 = ScanEnd;
            int start_grid = pos1 / mFlyAD.PosOfGrid;
            int end_grid = pos2 / mFlyAD.PosOfGrid;

            if (start_grid < 0)
                start_grid = 0;

            int len = end_grid - start_grid + 1;
            int[] dat;
            mFlyAD.GetGrid(direction, start_grid, len, out dat);

            if (EndEvent != null)
            {
                EndEvent(this, new MiniGridEventArgs()
                {
                    grid_start = start_grid,
                    direction = direction,
                    posOfGrid = mFlyAD.PosOfGrid,
                    buf = dat
                });
            }
        }
    }

    /// <summary>
    /// 只是来回走的逻辑,不包含数据处理 
    /// 0~posLen
    /// </summary>
    public abstract class GM_ScanMotion2 : GM_Base
    {
        #region 成员变量

       
        int scanCnt;//扫描次数, 当为-1时,无限次扫描,>0时,有限次扫描
        protected int ScanCnt
        {
            get
            {
                return scanCnt;
            }
            set
            {
                if (scanCnt != value)
                {
                    scanCnt = value;
                    NotifyPropertyChanged("ScanCnt");
                }
            }
        }

        public event EndEventHandler DataPosEvent;
        public event EndEventHandler EndEvent;

        public event EventHandler StartEvent;
        public event EventHandler FinishEvent;

        enum CTRLST
        {
            /// <summary>
            /// 准备动作:归0
            /// </summary>
            READY_ORIGIN = 1,
            /// <summary>
            /// 准备动作:正在归0
            /// </summary>
            READY_ORIGINWAIT = 2,
            /// <summary>
            /// 准备动作:后退到 0 脉冲
            /// </summary>
            READY_BACKW = 3,
            /// <summary>
            /// 准备动作:正在后退
            /// </summary>
            READY_BACKWAIT = 4,
            /// <summary>
            /// 前进到 机架总长
            /// </summary>
            SFORWARD = 5,
            /// <summary>
            /// 前进等待
            /// </summary>
            SFORWAIT_1 = 6,
            /// <summary>
            /// 前进结束,等待获取grid
            /// </summary>
            SFORWAIT_2 = 7,
            /// <summary>
            /// 后退到 0 脉冲
            /// </summary>
            SBACKWARD = 8,
            /// <summary>
            /// 后退等待
            /// </summary>
            SBACKWAIT_1 = 9,
            /// <summary>
            /// 后退结束,等待获取grid
            /// </summary>
            SBACKWAIT_2 = 10,
        }
        CTRLST state;
        #endregion

        public UInt32 Velocity { get; set; }

458
        public GM_ScanMotion2()
潘栩锋's avatar
潘栩锋 committed
459 460 461 462 463 464 465
        {
            GMState = CTRL_STATE.SCAN;

            ScanCnt = -1;//无限扫描

            state = CTRLST.READY_ORIGIN;

466 467 468 469 470
        }
        public override void Init(FlyAD7 flyad)
        {
            base.Init(flyad);

潘栩锋's avatar
潘栩锋 committed
471 472 473 474 475 476 477 478 479
            flyad.MiniGridEvent += (s, e) =>
            {
                if ((state == CTRLST.SFORWAIT_1) || (state == CTRLST.SBACKWAIT_1))
                {
                    if (DataPosEvent != null)
                        DataPosEvent(this, e);
                }
            };
        }
480
        protected virtual void Start2()
潘栩锋's avatar
潘栩锋 committed
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
        {

            state = CTRLST.READY_ORIGIN;

            if (StartEvent != null)
                StartEvent(this, null);
        }

        public override void Stop()
        {
            if (FinishEvent != null)
                FinishEvent(this, null);
            base.Stop();
        }
        bool WaitFinish(ref CTRLST src, CTRLST next)
        {
            switch (mFlyAD.DriveStatus)
            {
                case DRIVE_MAN_STATUS.STOP://完成任务
                case DRIVE_MAN_STATUS.LIMIT:
                    {
                        src = next;
                        return true;
                    } break;
                case DRIVE_MAN_STATUS.STOP_MANUAL://异常
                    {
                        NotifyError(ERRNO_Running_Manual);
                        Stop(); return false;
                    } break;
            }
            return false;
        }
        protected override void OnPoll()
        {
            switch (state)
            {
                case CTRLST.READY_ORIGIN:
                    {
                        mFlyAD.Origin();
                        state = CTRLST.READY_ORIGINWAIT;
                    } break;
                case CTRLST.READY_ORIGINWAIT:
                    {
                        WaitFinish(ref state, CTRLST.READY_BACKW);
                    }break;
                case CTRLST.READY_BACKW:
                    {
528 529 530

                        mFlyAD.SetVelocity(Velocity);
                        mFlyAD.RuntoMin();
潘栩锋's avatar
潘栩锋 committed
531 532 533 534 535 536 537 538
                        state = CTRLST.READY_BACKWAIT;
                    } break;
                case CTRLST.READY_BACKWAIT:
                    {
                        WaitFinish(ref state, CTRLST.SFORWARD);
                    } break;
                case CTRLST.SFORWARD:
                    {
539 540
                        mFlyAD.SetVelocity(Velocity);
                        mFlyAD.RuntoMax();
潘栩锋's avatar
潘栩锋 committed
541 542 543 544 545 546 547 548 549 550 551 552 553
                        state = CTRLST.SFORWAIT_1;
                    } break;
                case CTRLST.SFORWAIT_1:
                    {
                        if (WaitFinish(ref state, CTRLST.SFORWAIT_2))
                            FlushAD(DIRECTION.FORWARD);
                    } break;
                case CTRLST.SFORWAIT_2:
                    {
                        state = CTRLST.SBACKWARD;
                    } break;
                case CTRLST.SBACKWARD:
                    {
554 555
                        mFlyAD.SetVelocity(Velocity);
                        mFlyAD.RuntoMin();
潘栩锋's avatar
潘栩锋 committed
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
                        state = CTRLST.SBACKWAIT_1;
                    } break;
                case CTRLST.SBACKWAIT_1:
                    {
                        if (WaitFinish(ref state, CTRLST.SBACKWAIT_2))
                            FlushAD(DIRECTION.BACKWARD);
                    } break;
                case CTRLST.SBACKWAIT_2:
                    {
                        state = CTRLST.SFORWARD;

                        //扫描次数
                        if (ScanCnt > 0)
                            ScanCnt--;

                        if (ScanCnt == 0)
                        {
                            //扫描完成
                            Stop(); return;
                        }
                    } break;
            }
        }
        private void FlushAD(Misc.DIRECTION direction)
        {
            int start_grid = 0;
            int end_grid = mFlyAD.PosLen / mFlyAD.PosOfGrid;

            int len = end_grid - start_grid + 1;
            int[] dat;
            mFlyAD.GetGrid(direction, start_grid, len, out dat);

            if (EndEvent != null)
            {
                EndEvent(this, new MiniGridEventArgs()
                {
                    grid_start = start_grid,
                    direction = direction,
                    posOfGrid = mFlyAD.PosOfGrid,
                    buf = dat
                });
            }
        }
    }
}