FlyAD7.cs 68.2 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1
using AutoMapper;
潘栩锋's avatar
潘栩锋 committed
2
using FObjBase;
潘栩锋's avatar
潘栩锋 committed
3 4
using Newtonsoft.Json;
using System;
潘栩锋's avatar
潘栩锋 committed
5
using System.Collections.ObjectModel;
潘栩锋's avatar
潘栩锋 committed
6
using System.ComponentModel;
7
using System.Diagnostics;
潘栩锋's avatar
潘栩锋 committed
8 9 10
using System.IO;
using System.Linq;
using System.Net;
潘栩锋's avatar
潘栩锋 committed
11 12 13 14 15 16 17 18 19

namespace FlyADBase
{
    /// <summary>
    /// 执行顺序
    /// 1. IFlyAD flyad = new FlyAD7();//加载速度参数,IP地址
    /// 2. flyad.Connect();//flyad.Connect("192.168.251.10:20006");
    /// 3. 当连接上,会设置推送,获取flyad7控制器里面所有数据,设置速度参数到控制器
    /// </summary>
20
    public partial class FlyAD7 : FObj, IFlyADClient
潘栩锋's avatar
潘栩锋 committed
21 22
    {
        #region MARKNO
23 24
        const int MARKNO_SET_ZEROPOS = 2;
        const int MARKNO_SET_SYSPARAM = 3;
25
        const int MARKNO_SET_SAVE = 4;
潘栩锋's avatar
潘栩锋 committed
26
        #endregion
27

28 29

        TCPCConn conn = null;
潘栩锋's avatar
潘栩锋 committed
30 31 32 33
        /// <summary>
        /// 不设置服务器
        /// </summary>
        private bool bShieldSetValueEx = false;
潘栩锋's avatar
潘栩锋 committed
34

潘栩锋's avatar
潘栩锋 committed
35 36
        class DtAndBool
        {
37 38
            bool bNeedGet;
            DateTime dt;
潘栩锋's avatar
潘栩锋 committed
39 40 41 42 43
            public void Clear()
            {
                bNeedGet = true;
                dt = DateTime.Now;
            }
44 45

            public bool NeedGet(DateTime now, TimeSpan delay)
潘栩锋's avatar
潘栩锋 committed
46
            {
47
                if (bNeedGet)
潘栩锋's avatar
潘栩锋 committed
48
                {
49 50 51 52 53
                    if ((now - dt) >= delay)
                    {
                        bNeedGet = false;
                        return true;
                    }
潘栩锋's avatar
潘栩锋 committed
54
                }
55
                return false;
潘栩锋's avatar
潘栩锋 committed
56 57 58
            }
        }

59 60
        DtAndBool driveman_wait = new DtAndBool();
        DtAndBool sysinfo_wait = new DtAndBool();
潘栩锋's avatar
潘栩锋 committed
61 62


63 64
        TimeSpan NoTGridTimeOut = TimeSpan.FromSeconds(60);
        Stopwatch stopwatch_noTGrid = new Stopwatch();
65

66 67

        int last_position = int.MinValue;
68 69 70 71
        bool isReadyGetState = false;
        bool isReadyGetPos1AD1 = false;
        bool isReadyGetPos2 = false;
        bool isReadyGetIO = false;
潘栩锋's avatar
潘栩锋 committed
72
        
73 74 75
        static FlyAD7() {
            propertyName_save = FlyAD7JsonDb.GetMemberNames();
        }
76 77 78
        /// <summary>
        /// 
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
79 80 81
        public FlyAD7()
        {
            Now = DateTime.Now;
82 83
            mSysTick.Reset();
            mSysTick.BeReseted += MSysTick_BeReseted;
潘栩锋's avatar
潘栩锋 committed
84 85 86
            this.PropertyChanged += new PropertyChangedEventHandler(FlyAD7_PropertyChanged);
            PollModule.Current.Poll_Config(PollModule.POLL_CONFIG.ADD, new PollModule.PollHandler(OnPoll));

87
            //每隔一个小时,改变剩下时间
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
            PollModule.Current.Poll_Config(PollModule.POLL_CONFIG.ADD, () =>
            {
                if (!IsConnected)
                    return;
                if (Surplus < 60000)
                {
                    if (Surplus > 0)
                    {
                        Surplus--;
                        CurrObjSys.CallFunctionEx(conn, FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.ID, ID,
                            FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.CALL_SURPLUS_SUB,
                            null);
                    }
                }
            }, TimeSpan.FromMinutes(60));
103

104
            //60秒内收不到1个timegrid ,就会重连
潘栩锋's avatar
潘栩锋 committed
105 106 107 108 109
            PollModule.Current.Poll_Config(PollModule.POLL_CONFIG.ADD,
                () =>
                {
                    if (!IsConnected)
                    {
110
                        stopwatch_noTGrid.Stop();
潘栩锋's avatar
潘栩锋 committed
111 112 113
                    }
                    else
                    {
114 115
                        if (!stopwatch_noTGrid.IsRunning)
                            stopwatch_noTGrid.Restart();
潘栩锋's avatar
潘栩锋 committed
116 117 118 119
                        else
                        {
                            if (stopwatch_noTGrid.Elapsed > NoTGridTimeOut)
                            {
120 121 122 123
                                logger.Error("60秒都无法接收到ad盒的timegrid,reconnect");
                                ReConnect();
                                stopwatch_noTGrid.Restart();
                            }
潘栩锋's avatar
潘栩锋 committed
124 125 126 127
                        }
                    }
                }, TimeSpan.FromSeconds(1));

128 129 130 131 132 133 134 135 136 137 138 139 140 141
            //每隔1秒发送一次 GetIO
            PollModule.Current.Poll_Config(PollModule.POLL_CONFIG.ADD, () =>
            {
                if (!IsConnected)
                    return;
                
                //FLYIO
                CurrObjSys.GetValueEx(
                    conn,
                    FLYAD7_OBJ_INTERFACE.FLYIO_OBJ_INTERFACE.ID,
                    ID,
                    FLYAD7_OBJ_INTERFACE.FLYIO_OBJ_INTERFACE.GET_IO);
            }, TimeSpan.FromSeconds(1));

142 143
            constructor2();
        }
潘栩锋's avatar
潘栩锋 committed
144

145 146 147 148 149
        private void MSysTick_BeReseted()
        {
            BeResetTime = DateTime.Now;
        }

150 151 152 153 154
        #region IFlyADClient
        /// <summary>
        /// 连接成功
        /// </summary>
        public bool IsConnected { get; private set; }
潘栩锋's avatar
潘栩锋 committed
155

156 157 158 159 160 161 162 163 164 165 166 167 168 169
        /// <summary>
        /// 重连次数
        /// </summary>
        public int ConnectCnt { get; private set; }

        /// <summary>
        /// CRC
        /// </summary>
        public bool HasCRC { get; set; }
        /// <summary>
        /// 连接
        /// </summary>
        public void Connect()
        {
170
            Connect(Addr);
171
        }
潘栩锋's avatar
潘栩锋 committed
172

173 174 175
        /// <summary>
        /// 连接 ep
        /// </summary>
176 177
        /// <param name="addr"></param>
        public void Connect(string addr)
178
        {
179
            Addr = addr;
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

            if (conn != null)
            {
                if (!conn.RemoteEP.Equals(LocalEP))
                {
                    //断开之前的连接
                    CurrObjSys.Disconnect_to_Another_OBJSys(conn.RemoteEP, ID);
                }
            }

            conn = CurrObjSys.Connect_to_Another_OBJSys(LocalEP, ID);
            conn.HasCRC = HasCRC;
        }

        /// <summary>
        /// 重连
        /// </summary>
        public void ReConnect()
        {
            if (conn != null)
            {
                //断开之前的连接
                CurrObjSys.Disconnect_to_Another_OBJSys(conn.RemoteEP, ID);
            }
潘栩锋's avatar
潘栩锋 committed
204

205 206
            conn = CurrObjSys.Connect_to_Another_OBJSys(LocalEP, ID);
            conn.HasCRC = HasCRC;
潘栩锋's avatar
潘栩锋 committed
207
        }
208 209 210 211
        /// <summary>
        /// 已经从AD盒获取全部当前数据
        /// </summary>
        public bool IsReady { get; protected set; }
潘栩锋's avatar
潘栩锋 committed
212

213 214 215 216 217
        /// <summary>
        ///  通过判断systick,判断AD盒是否发生重启;
        ///  BeReseted为上一次被复位时间
        /// </summary>
        public DateTime BeResetTime { get; private set; }
218 219 220 221 222 223 224 225 226 227 228 229
        /// <summary>
        /// 用于同步, 最后一次 纵向信号 0->1 时,主轴脉冲
        /// </summary>
        public bool IsSync { get; protected set; }
        /// <summary>
        /// 当前在同步状态
        /// </summary>
        public int LastPos2At01 { get; protected set; }
        /// <summary>
        /// 同步列表,完成后,会删除
        /// </summary>
        public ObservableCollection<SyncOrder> SyncOrders { get; } = new ObservableCollection<SyncOrder>();
潘栩锋's avatar
潘栩锋 committed
230

231
        #endregion
潘栩锋's avatar
潘栩锋 committed
232

潘栩锋's avatar
潘栩锋 committed
233 234
        void updateIsReady()
        {
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
            if (isReadyGetState == false)
            {
                IsReady = false;
                return;
            }
            if (isReadyGetPos1AD1 == false)
            {
                IsReady = false;
                return;
            }
            if (isReadyGetPos2 == false)
            {
                IsReady = false;
                return;
            }
            if (isReadyGetIO == false)
            {
                IsReady = false;
                return;
            }
            IsReady = true;
            return;
        }
潘栩锋's avatar
潘栩锋 committed
258

潘栩锋's avatar
潘栩锋 committed
259 260
        void FlyAD7_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
261
            if (e.PropertyName == nameof(HasCRC))
潘栩锋's avatar
潘栩锋 committed
262 263 264 265 266 267 268 269
            {
                if (conn != null)
                    conn.HasCRC = HasCRC;
            }

            if (bShieldSetValueEx)
                return;

270
            if ((e.PropertyName == nameof(PosOffset)) ||
271
                (e.PropertyName == nameof(JogVelocity)))
潘栩锋's avatar
潘栩锋 committed
272 273
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
274
                    delegate ()
潘栩锋's avatar
潘栩锋 committed
275 276 277 278 279 280 281 282 283 284
                    {
                        CurrObjSys.SetValueEx(conn, FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.ID,
                            ID, FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.SET_ZERO_POS,
                            new FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.Pack_ZeroPos()
                            {
                                zero_pos = PosOffset,
                                jog_velocity = JogVelocity
                            }.ToBytes());
                    }, this, MARKNO_SET_ZEROPOS);
            }
285 286 287 288
            else if ((e.PropertyName == nameof(MotorType)) ||
                    (e.PropertyName == nameof(PosOfGrid)) ||
                    (e.PropertyName == nameof(Ratio01)) ||
                    (e.PropertyName == nameof(Ratio02)))
潘栩锋's avatar
潘栩锋 committed
289 290
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
291
                    delegate ()
潘栩锋's avatar
潘栩锋 committed
292 293 294 295
                    {
                        SetSysParam(PosOfGrid, MotorType, Ratio01, Ratio02);
                    }, this, MARKNO_SET_SYSPARAM);
            }
296
            else if (e.PropertyName == nameof(Pos1LCShift))
潘栩锋's avatar
潘栩锋 committed
297
            {
298

潘栩锋's avatar
潘栩锋 committed
299 300 301 302 303 304 305 306 307 308 309 310
                CurrObjSys.CallFunctionEx(
                   conn,
                   FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                   ID,
                   FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.CALL_PXX,
                   new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_Pos1LCShift()
                   {
                       pos1_LCShift = Pos1LCShift
                   }.ToBytes());
            }
        }

311
        void Init()
潘栩锋's avatar
潘栩锋 committed
312
        {
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
            //CurrObjSys.SenseConfigEx(
            //    conn,
            //    FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.ID,
            //    ID,
            //    Misc.MyBase.BIT(FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_POS1)
            //    | Misc.MyBase.BIT(FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_POS2)
            //    | Misc.MyBase.BIT(FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_AD1)
            //    | Misc.MyBase.BIT(FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_IO)
            //    | Misc.MyBase.BIT(FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_TIMEGRID)
            //    | Misc.MyBase.BIT(FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_GRID)
            //    | Misc.MyBase.BIT(FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_STATUS)
            //    | Misc.MyBase.BIT(FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_MINIGRID)
            //    ,
            //    SENSE_CONFIG.ADD);

            //注册全部推送事件
潘栩锋's avatar
潘栩锋 committed
329 330 331 332
            CurrObjSys.SenseConfigEx(
                conn,
                FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.ID,
                ID,
333
                0xffffffff,
潘栩锋's avatar
潘栩锋 committed
334 335 336
                SENSE_CONFIG.ADD);
            UpdateParam();

337 338 339 340 341
            isReadyGetState = false;
            isReadyGetPos1AD1 = false;
            isReadyGetPos2 = false;
            isReadyGetIO = false;
            updateIsReady();
潘栩锋's avatar
潘栩锋 committed
342 343 344 345 346 347 348 349

            //DRIVE_MAN
            CurrObjSys.GetValueEx(
                conn,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.GET_STATE);

350 351 352 353 354 355 356 357 358 359 360 361
            CurrObjSys.GetValueEx(
                conn,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.GET_SYNC_ORDER_CNT);

            CurrObjSys.GetValueEx(
                conn,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.GET_SYNC_STATUS);

潘栩锋's avatar
潘栩锋 committed
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
            //FLYADC
            CurrObjSys.GetValueEx(
                conn,
                FLYAD7_OBJ_INTERFACE.FLYADC_OBJ_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.FLYADC_OBJ_INTERFACE.GET_POS1AD1);

            CurrObjSys.GetValueEx(
                conn,
                FLYAD7_OBJ_INTERFACE.FLYADC_OBJ_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.FLYADC_OBJ_INTERFACE.GET_POS2);

            //FLYIO
            CurrObjSys.GetValueEx(
                conn,
                FLYAD7_OBJ_INTERFACE.FLYIO_OBJ_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.FLYIO_OBJ_INTERFACE.GET_IO);

382 383
            

384
            NotifyPropertyChanged(nameof(Pos1LCShift));//写入到AD盒
385

潘栩锋's avatar
潘栩锋 committed
386

潘栩锋's avatar
潘栩锋 committed
387
           
潘栩锋's avatar
潘栩锋 committed
388 389 390
            SyncClear();
            SyncEnd();

潘栩锋's avatar
潘栩锋 committed
391 392 393
            mSysTick.Reset();
            calSpeed.Reset();

394
            Init2();
395 396

            lastTimeGridTime = DateTime.MinValue;
潘栩锋's avatar
潘栩锋 committed
397 398 399 400 401 402 403 404 405
        }

        #region FObj
        public override void ConnectNotify(IFConn from)
        {
            IsConnected = from.IsConnected;
            if (IsConnected)
            {
                ConnectCnt++;
406

潘栩锋's avatar
潘栩锋 committed
407 408 409 410 411 412 413
                Init();
            }
            else
            {
                DriveStatus = DRIVE_MAN_STATUS.STOP;
            }
        }
414 415 416 417 418 419 420
        /// <summary>
        /// 
        /// </summary>
        /// <param name="from"></param>
        /// <param name="srcid"></param>
        /// <param name="memid"></param>
        /// <param name="infodata"></param>
潘栩锋's avatar
潘栩锋 committed
421 422 423 424 425 426 427 428 429 430 431
        public override void PushGetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            bShieldSetValueEx = true;
            switch (srcid)
            {
                case FLYAD7_OBJ_INTERFACE.FLYADC_OBJ_INTERFACE.ID:
                    {
                        switch (memid)
                        {
                            case FLYAD7_OBJ_INTERFACE.FLYADC_OBJ_INTERFACE.GET_POS1AD1:
                                {
432
                                    var pack = new FLYAD7_OBJ_INTERFACE.FLYADC_OBJ_INTERFACE.Pack_GetPos1AD1();
潘栩锋's avatar
潘栩锋 committed
433 434 435
                                    if (!pack.TryParse(infodata))
                                        break;
                                    Position = pack.pos;
潘栩锋's avatar
潘栩锋 committed
436 437
                                    //AD = pack.ad;
                                    advGetPos1AD1(pack.ad);
438 439 440

                                    isReadyGetPos1AD1 = true;
                                    updateIsReady();
潘栩锋's avatar
潘栩锋 committed
441 442
                                }
                                break;
潘栩锋's avatar
潘栩锋 committed
443 444 445 446 447 448 449
                            case FLYAD7_OBJ_INTERFACE.FLYADC_OBJ_INTERFACE.GET_POS2:
                                {
                                    FLYAD7_OBJ_INTERFACE.FLYADC_OBJ_INTERFACE.Pack_GetPos2 pack = new FLYAD7_OBJ_INTERFACE.FLYADC_OBJ_INTERFACE.Pack_GetPos2();
                                    if (!pack.TryParse(infodata))
                                        break;

                                    Position2 = pack.pos2;
450 451

                                    if (!IsCalSpeed)
452
                                        Speed2 = pack.speed2;
453

潘栩锋's avatar
潘栩锋 committed
454
                                    advGetPos2();
455

456 457
                                    isReadyGetPos2 = true;
                                    updateIsReady();
458 459
                                }
                                break;
潘栩锋's avatar
潘栩锋 committed
460
                        }
潘栩锋's avatar
潘栩锋 committed
461 462
                    }
                    break;
潘栩锋's avatar
潘栩锋 committed
463 464 465 466 467 468
                case FLYAD7_OBJ_INTERFACE.FLYIO_OBJ_INTERFACE.ID:
                    {
                        switch (memid)
                        {
                            case FLYAD7_OBJ_INTERFACE.FLYIO_OBJ_INTERFACE.GET_IO:
                                {
469
                                    var pack = new FLYAD7_OBJ_INTERFACE.FLYIO_OBJ_INTERFACE.Pack_GetIO();
潘栩锋's avatar
潘栩锋 committed
470 471 472 473 474
                                    if (!pack.TryParse(infodata))
                                        break;

                                    IStatus = pack.istatus;
                                    OStatus = pack.ostatus;
475

潘栩锋's avatar
潘栩锋 committed
476
                                    advGetIo();
477 478 479

                                    isReadyGetIO = true;
                                    updateIsReady();
潘栩锋's avatar
潘栩锋 committed
480 481
                                }
                                break;
潘栩锋's avatar
潘栩锋 committed
482
                        }
潘栩锋's avatar
潘栩锋 committed
483 484
                    }
                    break;
潘栩锋's avatar
潘栩锋 committed
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
                case FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID:
                    {
                        switch (memid)
                        {
                            case FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.GET_DRIVEPARAM:
                                {
                                    FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_DriveParam pack = new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_DriveParam();

                                    if (!pack.TryParse(infodata))
                                        break;
                                    Velocity = pack.Velocity;
                                    SVelocity = pack.SV;
                                    ATime = pack.ATime;
                                    DTime = pack.DTime;
                                    HVelocity1 = pack.hspeed1;
                                    HVelocity2 = pack.hspeed2;

潘栩锋's avatar
潘栩锋 committed
502 503
                                }
                                break;
潘栩锋's avatar
潘栩锋 committed
504 505 506 507 508
                            case FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.GET_STATE:
                                {
                                    FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_DriveState p = new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_DriveState();
                                    if (!p.TryParse(infodata))
                                        break;
509
                                    DriveOrder = p.order;
潘栩锋's avatar
潘栩锋 committed
510 511
                                    DriveStatus = p.status;
                                    Marker = p.marker;
512

潘栩锋's avatar
潘栩锋 committed
513
                                    advGetState();
514

515 516
                                    isReadyGetState = true;
                                    updateIsReady();
潘栩锋's avatar
潘栩锋 committed
517 518
                                }
                                break;
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
                            case FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.GET_SYNC_STATUS:
                                {
                                    FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncStatus p = new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncStatus();
                                    if (!p.TryParse(infodata))
                                        break;
                                    //TODO
                                }
                                break;
                            case FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.GET_SYNC_ORDER_CNT:
                                {
                                    FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncOrderCnt p = new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncOrderCnt();
                                    if (!p.TryParse(infodata))
                                        break;
                                    //TODO
                                }
                                break;
潘栩锋's avatar
潘栩锋 committed
535
                        }
潘栩锋's avatar
潘栩锋 committed
536 537
                    }
                    break;
潘栩锋's avatar
潘栩锋 committed
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
                case FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.ID:
                    {
                        switch (memid)
                        {
                            case FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.GET_IP:
                                break;
                            case FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.GET_SYSPARAM:
                                {
                                    FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.Pack_SysParam pack = new FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.Pack_SysParam();

                                    if (!pack.TryParse(infodata))
                                        break;
                                    PosOfGrid = pack.posOfGrid;//1个Grid=?脉冲数(2B)
                                    if ((pack.func_enable & 0x03) == 0x02)
                                        MotorType = MOTORTYPE.SERVO;
                                    else if ((pack.func_enable & 0x03) == 0x01)
                                        MotorType = MOTORTYPE.VF0;
                                    else
                                        MotorType = MOTORTYPE.NULL;

                                    Ratio01 = pack.ratio01;
                                    Ratio02 = pack.ratio02;
潘栩锋's avatar
潘栩锋 committed
560 561
                                }
                                break;
潘栩锋's avatar
潘栩锋 committed
562 563 564 565 566 567 568 569 570 571 572 573
                            case FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.GET_SYSINFO:
                                {
                                    FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.Pack_SysInfo pack = new FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.Pack_SysInfo();

                                    if (!pack.TryParse(infodata))
                                        break;

                                    AreaStatus = pack.status;
                                    AreaRet = pack.ret;

                                    Array.Copy(pack.code, Code, pack.code.Length);
                                    Code[6] = 0x06;
574
                                    NotifyPropertyChanged(nameof(Code));
潘栩锋's avatar
潘栩锋 committed
575 576 577 578

                                    Surplus = pack.surplus;

                                    Array.Copy(pack.access, Access, pack.access.Length);
579
                                    NotifyPropertyChanged(nameof(Access));
潘栩锋's avatar
潘栩锋 committed
580

潘栩锋's avatar
潘栩锋 committed
581 582
                                }
                                break;
潘栩锋's avatar
潘栩锋 committed
583 584 585 586 587 588 589 590 591
                            case FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.GET_ZERO_POS:
                                {
                                    FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.Pack_ZeroPos pack = new FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.Pack_ZeroPos();

                                    if (!pack.TryParse(infodata))
                                        break;

                                    PosOffset = pack.zero_pos;
                                    JogVelocity = pack.jog_velocity;
潘栩锋's avatar
潘栩锋 committed
592 593
                                }
                                break;
潘栩锋's avatar
潘栩锋 committed
594
                        }
潘栩锋's avatar
潘栩锋 committed
595 596
                    }
                    break;
潘栩锋's avatar
潘栩锋 committed
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
            }
            bShieldSetValueEx = false;
        }
        /// <summary>
        ///  systick 转 DateTime
        ///  不断把 从flyad7 获取的 systick 放入。  以每次systick 的差 加上 上一次 dt_1st, 得到时间。
        ///  如: 当前 获得 systick1, 之前是 systick0,   所以 dt = dt_1st+ (systick1-systick0);  dt_1st=dt;
        ///  
        /// 第1个 dt_1st, 是 从 DateTime.Now. 获取的。
        /// 
        /// 不会管 当前DateTime.Now 与 dt 差多少。 所以如果 flyad7 时间比较慢,可能会出现 3天后, DateTime.Now 与 dt 差了 1个小时。
        /// </summary>
        class TSysTick
        {
            Int32 systick_1st = 0;
            DateTime dt_1st = DateTime.MinValue;
613
            public event Action BeReseted;
潘栩锋's avatar
潘栩锋 committed
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
            public void Reset()
            {
                dt_1st = DateTime.MinValue;
            }
            /// <summary>
            /// 输入从 flyad7 获取的 timemark, 转换为 本地 DateTime
            /// </summary>
            /// <param name="systick"></param>
            /// <returns></returns>
            public DateTime ToDateTime(int systick)
            {
                if (dt_1st == DateTime.MinValue)
                {
                    dt_1st = DateTime.Now;
                    systick_1st = systick;
                    return dt_1st;
                }
潘栩锋's avatar
潘栩锋 committed
631

632
                int ms = systick - systick_1st;
潘栩锋's avatar
潘栩锋 committed
633
                if ((ms < -1000) ||//时间被提前
潘栩锋's avatar
潘栩锋 committed
634
                    (ms > 1000 * 60 * 3))//记录与新的timemark 相差3分钟,太大了,重新生成 timemark 与 dt 的关系
潘栩锋's avatar
潘栩锋 committed
635
                {
636 637
                    //被复位了
                    BeReseted?.Invoke();
潘栩锋's avatar
潘栩锋 committed
638 639 640 641 642
                    dt_1st = DateTime.Now;
                    systick_1st = systick;
                    return dt_1st;
                }

643 644 645 646 647 648 649 650 651 652 653
                DateTime dt = dt_1st.AddMilliseconds(ms);

                systick_1st = systick;
                dt_1st = dt;
                //if (Math.Abs((DateTime.Now-dt).TotalSeconds) > 5000)//滞后5s,异常
                //{
                //    //修正
                //    dt = DateTime.Now;
                //    dt_1st = dt;
                //}
                return dt;
潘栩锋's avatar
潘栩锋 committed
654 655 656
            }
        }
        TSysTick mSysTick = new TSysTick();
657 658 659 660 661

        private int pos1_version { get; set; } = 2;
        private int io_version { get; set; } = 2;
        private int timegrid_version { get; set; } = 2;
        private int grid_version { get; set; } = 2;
潘栩锋's avatar
潘栩锋 committed
662 663 664 665 666 667 668 669 670
        public override void PushInfo(IFConn from, uint srcid, ushort infoid, byte[] infodata)
        {
            bShieldSetValueEx = true;
            switch (infoid)
            {
                case FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_POS1:
                    {
                        FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushPos1_2 pack = new FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushPos1_2();
                        int version;
671
                        if (!pack.TryParse_version(infodata, out version))
潘栩锋's avatar
潘栩锋 committed
672
                            break;
673 674 675
                        pos1_version = version;

                        if (version == 1)
潘栩锋's avatar
潘栩锋 committed
676
                        {
潘栩锋's avatar
潘栩锋 committed
677 678 679 680
                            Position = pack.pos1;
                            if (!IsCalSpeed)
                                Speed = pack.speed1;
                            Now = DateTime.Now;
潘栩锋's avatar
潘栩锋 committed
681
                        }
682
                        else if (version == 2)
潘栩锋's avatar
潘栩锋 committed
683
                        {
潘栩锋's avatar
潘栩锋 committed
684 685 686 687
                            Position = pack.pos1;
                            if (!IsCalSpeed)
                                Speed = pack.speed1;

潘栩锋's avatar
潘栩锋 committed
688 689
                            DateTime dt = mSysTick.ToDateTime(pack.systick);
                            Now = dt;
潘栩锋's avatar
潘栩锋 committed
690
                            advPushPos1();
691
                            PositionChangedEvent?.Invoke(this, new PositionChangedEventArgs() { Time = dt, Position = Position });
潘栩锋's avatar
潘栩锋 committed
692
                        }
潘栩锋's avatar
潘栩锋 committed
693 694 695
                        calSpeed.SetPos1(Now, Position);
                    }
                    break;
潘栩锋's avatar
潘栩锋 committed
696 697 698 699
                case FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_POS2:
                    {
                        FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushPos2_2 pack = new FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushPos2_2();
                        int version;
700
                        if (!pack.TryParse_version(infodata, out version))
潘栩锋's avatar
潘栩锋 committed
701
                            break;
702 703 704

                        if (version == 1)
                        {
潘栩锋's avatar
潘栩锋 committed
705 706 707 708
                            Position2 = pack.pos2;
                            if (!IsCalSpeed)
                                Speed2 = pack.speed2;
                            Now = DateTime.Now;
709 710 711
                        }
                        else if (version == 2)
                        {
潘栩锋's avatar
潘栩锋 committed
712 713 714 715
                            Position2 = pack.pos2;
                            if (!IsCalSpeed)
                                Speed2 = pack.speed2;

716 717 718 719 720 721
                            DateTime dt = mSysTick.ToDateTime(pack.systick);
                            Now = dt;
                            advPushPos2();
                            Position2ChangedEvent?.Invoke(this, new PositionChangedEventArgs() { Time = dt, Position = Position });
                        }

潘栩锋's avatar
潘栩锋 committed
722 723 724
                        calSpeed.SetPos2(Now, Position2);
                    }
                    break;
潘栩锋's avatar
潘栩锋 committed
725 726 727 728 729 730
                case FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_AD1:
                    {
                        FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushAD1 pack = new FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushAD1();
                        if (!pack.TryParse(infodata))
                            break;

731 732
                        //AD = pack.AD;
                        advPushAd(pack.AD);
潘栩锋's avatar
潘栩锋 committed
733 734
                    }
                    break;
潘栩锋's avatar
潘栩锋 committed
735 736 737 738 739 740
                case FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_IO:
                    {
                        FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushIO_2 pack = new FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushIO_2();
                        int version;
                        if (!pack.TryParse_version(infodata, out version))
                            break;
741 742
                        io_version = version;
                        if (version == 1)
潘栩锋's avatar
潘栩锋 committed
743 744 745 746
                        {
                            UInt16 inchange = (UInt16)(IStatus ^ pack.istatus);
                            IStatus = pack.istatus;
                            OStatus = pack.ostatus;
潘栩锋's avatar
潘栩锋 committed
747 748 749 750 751 752 753 754 755
                            Now = DateTime.Now;
                            IStatusChangedEvent?.Invoke(
                                this,
                                new IStatusChangedEventArgs(
                                    Now,
                                    IStatus,
                                    inchange,
                                    Position,
                                    Position2));
潘栩锋's avatar
潘栩锋 committed
756
                        }
757
                        else if (version == 2)
潘栩锋's avatar
潘栩锋 committed
758
                        {
759 760 761 762 763
                            if (pack.inChange == 0)
                            {
                                //异常包,, 这个是AD盒的bug,  systick 是错的!!!!! 不能让它通过
                                return;
                            }
潘栩锋's avatar
潘栩锋 committed
764 765
                            IStatus = pack.istatus;
                            OStatus = pack.ostatus;
766 767
                            Position = pack.pos1;
                            Position2 = pack.pos2;
潘栩锋's avatar
潘栩锋 committed
768 769
                            DateTime dt = mSysTick.ToDateTime(pack.systick);
                            Now = dt;
770

潘栩锋's avatar
潘栩锋 committed
771
                            advPushIo();
772

773 774 775 776 777 778
                            IStatusChangedEvent?.Invoke(
                                this,
                                new IStatusChangedEventArgs(
                                    dt,
                                    pack.istatus,
                                    pack.inChange,
潘栩锋's avatar
潘栩锋 committed
779 780
                                    Position,
                                    Position2));
潘栩锋's avatar
潘栩锋 committed
781 782 783

                            calSpeed.SetPos1(Now, Position);
                            calSpeed.SetPos2(Now, Position2);
潘栩锋's avatar
潘栩锋 committed
784
                        }
潘栩锋's avatar
潘栩锋 committed
785 786
                    }
                    break;
潘栩锋's avatar
潘栩锋 committed
787 788
                case FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_TIMEGRID:
                    {
789
                        stopwatch_noTGrid.Restart();//收到timegrid!!!!!!!!!!!
潘栩锋's avatar
潘栩锋 committed
790 791 792 793 794 795 796
                        int version;
                        FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushTGrid_2 pack = new FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushTGrid_2();

                        if (!pack.TryParse_version(infodata, out version))
                        {
                            break;
                        }
797
                        timegrid_version = version;
潘栩锋's avatar
潘栩锋 committed
798 799 800

                        DateTime dt;
                        int[] data;
801 802 803 804 805 806
                        //怕数据发送出错
                        if (pack.data.Any(d => (d >> 16) != 0))
                        {
                            //异常
                            goto _end_push_info;
                        }
潘栩锋's avatar
潘栩锋 committed
807 808 809 810 811

                        if (version == 1)
                        {
                            //怕数据发送出错
                            dt = DateTime.Now;
潘栩锋's avatar
潘栩锋 committed
812
                            Now = dt;
潘栩锋's avatar
潘栩锋 committed
813
                        }
814
                        else// if (version == 2)
潘栩锋's avatar
潘栩锋 committed
815 816 817 818 819
                        {
                            dt = mSysTick.ToDateTime(pack.systick);
                            Now = dt;
                        }
                        data = pack.data;
820 821
                        advPushTimeGrid(dt, data);

822 823 824 825
                        TimeSpan ts = TimeSpan.FromTicks((long)(1.28 * TimeSpan.TicksPerMillisecond));
                        if (lastTimeGridTime != DateTime.MinValue)
                            ts = TimeSpan.FromTicks((dt - lastTimeGridTime).Ticks / data.Length);
                        lastTimeGridTime = dt;
826

827 828 829 830
                        TimeGridEvent?.Invoke(
                            this,
                            new TimeGridEventArgs(
                                dt,
831
                                ts,
832
                                data));
潘栩锋's avatar
潘栩锋 committed
833 834
                    }
                    break;
潘栩锋's avatar
潘栩锋 committed
835 836 837 838 839 840
                case FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_STATUS:
                    {
                        int version;
                        FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushStatus_2 pack = new FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushStatus_2();
                        if (!pack.TryParse_version(infodata, out version))
                            break;
841
                        if (version == 1)
潘栩锋's avatar
潘栩锋 committed
842 843 844 845
                        {
                            DriveOrder = pack.order;
                            DriveStatus = pack.status;
                        }
846
                        else if (version == 2)
潘栩锋's avatar
潘栩锋 committed
847 848 849
                        {
                            DriveOrder = pack.order;
                            DriveStatus = pack.status;
850 851 852
                            Marker = pack.marker;
                            DateTime dt = mSysTick.ToDateTime(pack.systick);
                            Now = dt;
853 854 855 856 857 858 859 860 861 862

                            advPushStatus();

                            DriveStatusChangedEvent?.Invoke(this, new DriveStatusChangedEventArgs()
                            {
                                Time = dt,
                                DriveOrder = DriveOrder,
                                DriveStatus = DriveStatus,
                                Marker = Marker,
                            });
潘栩锋's avatar
潘栩锋 committed
863
                        }
潘栩锋's avatar
潘栩锋 committed
864 865
                    }
                    break;
潘栩锋's avatar
潘栩锋 committed
866 867 868
                case FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_GRID:
                    {
                        int version;
869
                        var pack = new FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushGrid_2();
870
                        if (!pack.TryParse_version(infodata, out version))
潘栩锋's avatar
潘栩锋 committed
871
                            break;
872
                        grid_version = version;
潘栩锋's avatar
潘栩锋 committed
873 874
                        DateTime dt;
                        int marker;
875
                        if (version == 1)
潘栩锋's avatar
潘栩锋 committed
876 877 878 879
                        {
                            dt = DateTime.Now;
                            marker = Marker;
                        }
880
                        else //if (version == 2)
潘栩锋's avatar
潘栩锋 committed
881
                        {
882 883 884
                            dt = mSysTick.ToDateTime(pack.systick);
                            marker = pack.marker;
                        }
潘栩锋's avatar
潘栩锋 committed
885

886
                        advPushGrid(dt, marker, pack.direction, pack.grid_start, pack.data);
潘栩锋's avatar
潘栩锋 committed
887

888 889 890 891 892 893 894 895
                        GridEvent?.Invoke(this, new MiniGridEventArgs()
                        {
                            direction = pack.direction,
                            grid_start = pack.grid_start,
                            posOfGrid = PosOfGrid,
                            buf = pack.data,
                            marker = marker
                        });
潘栩锋's avatar
潘栩锋 committed
896

潘栩锋's avatar
潘栩锋 committed
897 898
                    }
                    break;
潘栩锋's avatar
潘栩锋 committed
899 900 901 902 903 904
                case FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_MINIGRID:
                    {
                        FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushGrid pack = new FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushGrid();
                        if (!pack.TryParse(infodata))
                            break;

905
                        advPushMiniGrid(pack.direction, pack.grid_start, pack.data);
潘栩锋's avatar
潘栩锋 committed
906

907
                        MiniGridEvent?.Invoke(this, new MiniGridEventArgs()
潘栩锋's avatar
潘栩锋 committed
908
                        {
909 910 911 912 913 914
                            direction = pack.direction,
                            grid_start = pack.grid_start,
                            posOfGrid = PosOfGrid,
                            buf = pack.data,
                            marker = Marker
                        });
潘栩锋's avatar
潘栩锋 committed
915

潘栩锋's avatar
潘栩锋 committed
916 917
                    }
                    break;
潘栩锋's avatar
潘栩锋 committed
918
            }
919
            _end_push_info:
潘栩锋's avatar
潘栩锋 committed
920 921 922
            bShieldSetValueEx = false;
        }

923

924 925 926



潘栩锋's avatar
潘栩锋 committed
927 928 929 930 931 932 933 934 935 936 937 938
        #endregion

        /// <summary>
        /// 100ms 周期
        /// </summary>
        private void OnPoll()
        {
            if (!IsConnected)
                return;

            DateTime now = DateTime.Now;

939
            if (sysinfo_wait.NeedGet(now, TimeSpan.FromSeconds(1)))
潘栩锋's avatar
潘栩锋 committed
940
            {
941 942 943 944 945
                CurrObjSys.GetValueEx(
                    conn,
                    FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.ID,
                    ID,
                    FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.GET_SYSINFO);
潘栩锋's avatar
潘栩锋 committed
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
            }

            if (DriveStatus == DRIVE_MAN_STATUS.RUNNING)
            {
                //if (Position == TargetPos) //已经到达位置,问AD卡是不是?
                //{
                //    if ((now - driveman_wait.dt).TotalSeconds >= 1)//每次发送必须间隔1s
                //    {
                //        driveman_wait.Clear();
                //        CurrObjSys.GetValueEx(
                //            conn,
                //            FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                //            ID,
                //            FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.GET_STATE);
                //    }
                //}
                //else 
963
                if (driveman_wait.NeedGet(now, TimeSpan.FromSeconds(3)))//每3秒检查一次
潘栩锋's avatar
潘栩锋 committed
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
                {
                    driveman_wait.Clear();
                    if (Position == last_position)//当前正在running,但脉冲在3秒内都没变化,问AD卡,现在什么情况。
                    {
                        CurrObjSys.GetValueEx(
                            conn,
                            FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                            ID,
                            FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.GET_STATE);
                    }
                    last_position = Position;
                }
            }

        }
979

潘栩锋's avatar
潘栩锋 committed
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995

        #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 成员
996

997 998 999 1000

        /// <summary>
        /// AD盒地址
        /// </summary>
1001
        public string Addr { get; set; } = "192.168.251.10:20006";
潘栩锋's avatar
潘栩锋 committed
1002 1003 1004 1005
        IPEndPoint LocalEP
        {
            get
            {
1006
                return Misc.StringConverter.ToIPEndPoint(Addr);
潘栩锋's avatar
潘栩锋 committed
1007 1008 1009
            }
        }

1010 1011 1012 1013
        /// <summary>
        /// 从flyad7 获取 的systick 转换的时间
        /// </summary>
        public DateTime Now { get; private set; }
潘栩锋's avatar
潘栩锋 committed
1014

1015 1016 1017
        /// <summary>
        /// AD盒版本号 版本1 没有systick;版本2 有systick;
        /// </summary>
1018
        public int Version
1019
        {
潘栩锋's avatar
潘栩锋 committed
1020 1021
            get
            {
1022 1023 1024 1025 1026 1027 1028 1029
                if (pos1_version == 2
                    && timegrid_version == 2
                    && grid_version == 2)
                    return 2;
                else
                    return 1;
            }
        }
潘栩锋's avatar
潘栩锋 committed
1030

1031 1032 1033 1034
        /// <summary>
        /// 硬件版本,设置的!!!
        /// </summary>
        public int HardwareVersion { get; set; } = 2;
1035 1036 1037 1038
        /// <summary>
        /// 脉冲
        /// </summary>
        public int Position { get; private set; }
潘栩锋's avatar
潘栩锋 committed
1039

1040 1041 1042 1043 1044 1045 1046 1047 1048
        /// <summary>
        /// 速度 pps
        /// </summary>
        public int Speed { get; private set; }

        /// <summary>
        /// 1000ms从AD卡获取(或推送)1个AD值。此AD值只用于调试,定点
        /// </summary>
        public int AD { get; private set; }
1049 1050


1051 1052 1053
        /// <summary>
        /// AD值最大值 ,恒定返回65535
        /// </summary>
1054 1055
        public int ADMax { get; } = 65535;

1056
        /// <summary>
1057
        /// 输入口状态,只有12位有效, 大AD盒 16位
1058 1059
        /// </summary>
        public UInt16 IStatus { get; private set; } = 0xffff;
1060

1061
        /// <summary>
1062
        /// 输出口状态,只有4位有效, 大AD盒 8位
1063 1064
        /// </summary>
        public UInt16 OStatus { get; private set; } = 0xffff;
潘栩锋's avatar
潘栩锋 committed
1065

1066 1067 1068 1069 1070 1071
        /// <summary>
        /// Out0 = 0, SetOutput(0x0001,0x0000);
        /// Out0 = 1, Out1 = 0, SetOutput(0x0003,0x0001);
        /// </summary>
        /// <param name="mask"></param>
        /// <param name="enable"></param>
潘栩锋's avatar
潘栩锋 committed
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
        public void SetOutput(ushort mask, ushort enable)
        {
            CurrObjSys.CallFunctionEx(
                conn,
                FLYAD7_OBJ_INTERFACE.FLYIO_OBJ_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.FLYIO_OBJ_INTERFACE.CALL_OUTPORT,
                new FLYAD7_OBJ_INTERFACE.FLYIO_OBJ_INTERFACE.Pack_CallOutPort()
                {
                    mask = mask,
                    port = enable
                }.ToBytes());
1084 1085 1086 1087 1088 1089 1090 1091

            //回读
            //FLYIO
            CurrObjSys.GetValueEx(
                conn,
                FLYAD7_OBJ_INTERFACE.FLYIO_OBJ_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.FLYIO_OBJ_INTERFACE.GET_IO);
潘栩锋's avatar
潘栩锋 committed
1092 1093 1094
        }

        /// <summary>
1095
        /// 读取参数, 全部!!!!
潘栩锋's avatar
潘栩锋 committed
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
        /// </summary>
        public void UpdateParam()
        {
            //OBJ_SYS_DATA
            CurrObjSys.GetValueEx(
                conn,
                FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.GET_SYSPARAM);

            CurrObjSys.GetValueEx(
                conn,
                FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.GET_ZERO_POS);

            CurrObjSys.GetValueEx(
                conn,
                FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.GET_SYSINFO);

            //DRIVE_MAN
            CurrObjSys.GetValueEx(
                conn,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.GET_DRIVEPARAM);
        }

1126
        #region 运动参数
潘栩锋's avatar
潘栩锋 committed
1127
        /// <summary>
1128
        /// 设置 运动参数
潘栩锋's avatar
潘栩锋 committed
1129
        /// </summary>
1130 1131 1132 1133
        /// <param name="posOfGrid">脉冲/grid</param>
        /// <param name="motortype">电机类型</param>
        /// <param name="ratio01">脉冲比例 分母(电机脉冲)</param>
        /// <param name="ratio02">脉冲比例 分子(编码器脉冲)</param>
潘栩锋's avatar
潘栩锋 committed
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
        public void SetSysParam(UInt16 posOfGrid, MOTORTYPE motortype, UInt16 ratio01, UInt16 ratio02)
        {
            byte func_enable = 0x0e;
            if (motortype == MOTORTYPE.NULL)
                func_enable = 0x0e;
            else if (motortype == MOTORTYPE.SERVO)
                func_enable = 0x0e;
            else// if (motortype == MOTORTYPE.VF0)
                func_enable = 0x0d;

            CurrObjSys.SetValueEx(
                conn,
                FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.SET_SYSPARAM,
                new FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.Pack_SysParam()
                {
                    posOfGrid = posOfGrid,
                    ad1_interval = 1000,
                    ad2_interval = 1000,
                    push_interval = 100,
                    func_setting = 0,
                    func_enable = func_enable,
                    ratio01 = ratio01,
                    ratio02 = ratio02
                }.ToBytes());
            //CurrObjSys.GetValueEx(
            //    conn,
            //    FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.ID,
            //    ID,
            //    0x1234,
            //    FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.GET_SYSPARAM);
        }
1167

1168 1169 1170
        /// <summary>
        /// 电机类型
        /// </summary>
1171 1172
        public MOTORTYPE MotorType { get; set; } = MOTORTYPE.SERVO;

1173
        private UInt16 posofgrid = 10;
潘栩锋's avatar
潘栩锋 committed
1174
        /// <summary>
1175
        /// 脉冲/grid, 最大1000个grid
潘栩锋's avatar
潘栩锋 committed
1176
        /// </summary>
1177
        public UInt16 PosOfGrid
潘栩锋's avatar
潘栩锋 committed
1178 1179 1180 1181
        {
            get { return posofgrid; }
            set
            {
1182 1183
                if (value < 1)
                    value = 1;
1184

潘栩锋's avatar
潘栩锋 committed
1185 1186
                if (posofgrid != value)
                    posofgrid = value;
1187

潘栩锋's avatar
潘栩锋 committed
1188 1189
            }
        }
1190 1191


1192 1193 1194 1195
        /// <summary>
        /// 脉冲比例 分母(电机脉冲)
        /// </summary>
        public UInt16 Ratio01 { get; set; } = 4;
1196

潘栩锋's avatar
潘栩锋 committed
1197
        /// <summary>
1198
        /// 脉冲比例 分子(编码器脉冲)
潘栩锋's avatar
潘栩锋 committed
1199
        /// </summary>
1200 1201
        public UInt16 Ratio02 { get; set; } = 1;

潘栩锋's avatar
潘栩锋 committed
1202 1203
        #endregion

1204 1205

        /// <summary>
1206
        /// 脉冲平移(0位处脉冲值)
1207 1208 1209
        /// </summary>
        public Int16 PosOffset { get; set; }

1210 1211 1212 1213
        /// <summary>
        /// 手动速度 pps
        /// </summary>
        public UInt32 JogVelocity { get; set; } = 5000;
1214

1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
        #region 速度参数


        /// <summary>
        /// 设置速度参数
        /// </summary>
        /// <param name="velocity">runto 速度 pps</param>
        /// <param name="sv">开始速度 pps</param>
        /// <param name="atime">加速时间 ms</param>
        /// <param name="dtime">减速时间 ms</param>
        /// <param name="hspeed1">归0 第1段速 pps</param>
        /// <param name="hspeed2">归0 第2段速 pps</par
潘栩锋's avatar
潘栩锋 committed
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
        public void SetPosParam(UInt32 velocity, UInt32 sv, UInt32 atime, UInt32 dtime, UInt32 hspeed1, UInt32 hspeed2)
        {
            bShieldSetValueEx = true;
            if (velocity != 0xffffffff)
                Velocity = velocity;
            if (sv != 0xffffffff)
                SVelocity = sv;
            if (atime != 0xffffffff)
                ATime = atime;
            if (dtime != 0xffffffff)
                DTime = dtime;
            if (hspeed1 != 0xffffffff)
                HVelocity1 = hspeed1;
            if (hspeed2 != 0xffffffff)
                HVelocity2 = hspeed2;
            bShieldSetValueEx = false;
            CurrObjSys.SetValueEx(
                conn,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.SET_DRIVEPARAM,
                new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_DriveParam()
                {
                    Velocity = velocity,
                    SV = sv,
                    ATime = atime,
                    DTime = dtime,
                    hspeed1 = hspeed1,
                    hspeed2 = hspeed2
                }.ToBytes());
            //CurrObjSys.GetValueEx(
            //    conn,
            //    FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
            //    ID,
            //    0x1234,
            //    FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.GET_DRIVEPARAM);
        }
1264

1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
        /// <summary>
        /// 设置速度
        /// </summary>
        /// <param name="velocity"></param>
        public void SetVelocity(UInt32 velocity)
        {
            SetPosParam(velocity, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff);
        }
        /// <summary>
        /// runto 速度 pps
        /// </summary>
1276
        public UInt32 Velocity { get; private set; } = 8000;
1277

1278 1279 1280
        /// <summary>
        /// 开始速度 pps
        /// </summary>
1281
        public UInt32 SVelocity { get; private set; } = 200;
1282

1283 1284 1285
        /// <summary>
        /// 加速时间 ms
        /// </summary>
1286
        public UInt32 ATime { get; private set; } = 300;
1287

1288 1289 1290
        /// <summary>
        /// 减速时间 ms
        /// </summary>
1291
        public UInt32 DTime { get; private set; } = 200;
1292

1293 1294 1295
        /// <summary>
        /// 归0 第1段速 pps
        /// </summary>
1296
        public UInt32 HVelocity1 { get; private set; } = 4000;
1297

1298 1299 1300
        /// <summary>
        /// 归0 第2段速 pps
        /// </summary>
1301
        public UInt32 HVelocity2 { get; private set; } = 500;
1302

潘栩锋's avatar
潘栩锋 committed
1303 1304 1305
        #endregion

        #region 密码
1306 1307 1308
        /// <summary>
        /// 使用码区状态
        /// </summary>
1309 1310
        public AREA_STATUS AreaStatus { get; set; }

1311 1312 1313
        /// <summary>
        /// 使用码区出错码
        /// </summary>
1314
        public AREA_ERR AreaRet { get; set; }
潘栩锋's avatar
潘栩锋 committed
1315

1316 1317 1318
        /// <summary>
        /// 序列码byte[6]
        /// </summary>
1319
        public byte[] Code { get; } = new byte[7];
潘栩锋's avatar
潘栩锋 committed
1320 1321


1322 1323 1324 1325 1326 1327 1328 1329
        /// <summary>
        /// //系统剩余时间
        /// </summary>
        public int Surplus { get; set; } = 0xffff;
        /// <summary>
        /// 系统授权码
        /// </summary>
        public byte[] Access { get; } = new byte[8];
潘栩锋's avatar
潘栩锋 committed
1330 1331 1332 1333

        /// <summary>
        /// 输入系统授权码
        /// </summary>
1334
        /// <param name="access">系统授权码access[8]</param>
潘栩锋's avatar
潘栩锋 committed
1335 1336 1337 1338 1339 1340 1341 1342
        public void SetAccess(byte[] access)
        {
            CurrObjSys.CallFunctionEx(
                conn,
                FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.CALL_ACCESS,
                access);
1343
            sysinfo_wait.Clear();
潘栩锋's avatar
潘栩锋 committed
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
        }
        /// <summary>
        /// 初始化系统信息区
        /// </summary>
        public void InitArea()
        {
            CurrObjSys.CallFunctionEx(
                conn,
                FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.CALL_INIT,
                new FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.Pack_Init().ToBytes());
1356
            sysinfo_wait.Clear();
潘栩锋's avatar
潘栩锋 committed
1357 1358 1359
        }
        #endregion

1360 1361
        /// <summary>
        /// timegrid 事件; 版本1: 1.28s 一次; 版本2: 0.2s一次, 
1362
        /// ad值不会被机架修正
1363
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
1364
        public event TimeGridEventHandler TimeGridEvent;
1365
        DateTime lastTimeGridTime;
潘栩锋's avatar
潘栩锋 committed
1366

1367

1368 1369 1370
        /// <summary>
        /// runto 中,每经过1个grid 推送数据
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
1371 1372
        public event MiniGridEventHandler MiniGridEvent;

1373 1374 1375
        /// <summary>
        /// runto 的 grid事件
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
1376 1377
        public event MiniGridEventHandler GridEvent;

1378 1379 1380
        /// <summary>
        /// 输入状态改变事件
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
1381 1382
        public event IStatusChangedEventHandler IStatusChangedEvent;

1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
        /// <summary>
        /// 指令状态改变事件
        /// </summary>
        public event DriveStatusChangedEventHandler DriveStatusChangedEvent;

        /// <summary>
        /// 脉冲1改变事件
        /// </summary>
        public event PositionChangedEventHandler PositionChangedEvent;

        /// <summary>
        /// 脉冲2改变事件
        /// </summary>
        public event PositionChangedEventHandler Position2ChangedEvent;

1398 1399 1400 1401 1402
        #region 运行动作
        /// <summary>
        /// 运行至指定位置
        /// </summary>
        /// <param name="to">脉冲</param>
潘栩锋's avatar
潘栩锋 committed
1403 1404 1405 1406
        public void Runto(int to)
        {
            Drive(DRIVE_MAN_ORDER.RUNTO, to);
        }
1407 1408 1409
        /// <summary>
        /// 归原点
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
1410 1411 1412 1413
        public void Origin()
        {
            Drive(DRIVE_MAN_ORDER.ORIGIN);
        }
1414 1415 1416
        /// <summary>
        /// 停止
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
1417 1418 1419 1420
        public void Stop()
        {
            Drive(DRIVE_MAN_ORDER.STOP);
        }
1421 1422 1423
        /// <summary>
        /// 反向,直到撞了限位
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
1424 1425 1426 1427
        public void Backward()
        {
            Drive(DRIVE_MAN_ORDER.BACKWORD);
        }
1428 1429 1430
        /// <summary>
        /// 正行,直到撞了限位
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
1431 1432 1433 1434
        public void Forward()
        {
            Drive(DRIVE_MAN_ORDER.FORWORD);
        }
1435 1436 1437
        /// <summary>
        /// 当前运行指令
        /// </summary>
1438
        public DRIVE_MAN_ORDER DriveOrder { get; set; } = DRIVE_MAN_ORDER.IDLE;
潘栩锋's avatar
潘栩锋 committed
1439

1440 1441 1442
        /// <summary>
        /// 之前运行动作的状态
        /// </summary>
1443
        public DRIVE_MAN_STATUS DriveStatus { get; set; } = DRIVE_MAN_STATUS.STOP;
潘栩锋's avatar
潘栩锋 committed
1444

1445 1446 1447
        #endregion

        #endregion
潘栩锋's avatar
潘栩锋 committed
1448 1449 1450

        void Drive(DRIVE_MAN_ORDER order, object param)
        {
1451

潘栩锋's avatar
潘栩锋 committed
1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
            switch (order)
            {
                case DRIVE_MAN_ORDER.RUNTO:
                    {
                        int TargetPos = (int)param;
                        if (Position == TargetPos)
                        {
                            //已经到了位置!!!!!, FLYAD7 是没有动作,没有推送的!!!
                            DriveStatus = DRIVE_MAN_STATUS.STOP;
                        }
                        else
                        {
                            DriveStatus = DRIVE_MAN_STATUS.RUNNING;
                            CurrObjSys.CallFunctionEx(
                                conn,
                                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                                ID,
                                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.CALL_PXX,
                                new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_CallRunto() { to = TargetPos }.ToBytes());

                        }
潘栩锋's avatar
潘栩锋 committed
1473 1474
                    }
                    break;
潘栩锋's avatar
潘栩锋 committed
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
                case DRIVE_MAN_ORDER.ORIGIN:
                case DRIVE_MAN_ORDER.BACKWORD:
                case DRIVE_MAN_ORDER.FORWORD:
                case DRIVE_MAN_ORDER.STOP:
                    {
                        DriveStatus = DRIVE_MAN_STATUS.RUNNING;
                        CurrObjSys.CallFunctionEx(
                            conn,
                            FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                            ID,
                            FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.CALL_PXX,
                            new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_CallPXX() { order = order }.ToBytes());
潘栩锋's avatar
潘栩锋 committed
1487 1488
                    }
                    break;
潘栩锋's avatar
潘栩锋 committed
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
                case DRIVE_MAN_ORDER.SYNC:
                    {
                        if (param != null)
                        {
                            CurrObjSys.CallFunctionEx(
                                conn,
                                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                                ID,
                                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.CALL_PXX,
                                new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncBeginWithPos2()
                                {
                                    pos2 = (int)param
                                }.ToBytes());
                        }
                        else
                        {

                            CurrObjSys.CallFunctionEx(
                                conn,
                                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                                ID,
                                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.CALL_PXX,
                                new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncBegin().ToBytes());
                        }
潘栩锋's avatar
潘栩锋 committed
1513 1514
                    }
                    break;
潘栩锋's avatar
潘栩锋 committed
1515 1516 1517 1518 1519 1520 1521 1522 1523 1524
            }

            driveman_wait.Clear();
        }
        void Drive(DRIVE_MAN_ORDER order)
        {
            Drive(order, null);
        }

        #region 同步
1525

潘栩锋's avatar
潘栩锋 committed
1526 1527 1528
        /// <summary>
        /// 同步用标示
        /// </summary>
1529 1530
        public Int32 Marker { get; protected set; }

潘栩锋's avatar
潘栩锋 committed
1531 1532 1533 1534
        #region 横向脉冲
        /// <summary>
        /// 逻辑横向脉冲偏移  Pos1 + Pos1LCShift = Pos1LC
        /// </summary>
1535 1536
        public int Pos1LCShift { get; set; }

潘栩锋's avatar
潘栩锋 committed
1537 1538
        #endregion
        #region 主轴脉冲
1539

潘栩锋's avatar
潘栩锋 committed
1540 1541 1542
        /// <summary>
        /// 纵向脉冲速度, pps, 1秒脉冲数
        /// </summary>
1543 1544 1545
        public int Speed2 { get; set; }


潘栩锋's avatar
潘栩锋 committed
1546 1547 1548
        /// <summary>
        /// 纵向脉冲,也叫主轴脉冲
        /// </summary>
1549
        public int Position2 { get; private set; }
1550

1551 1552 1553 1554
        /// <summary>
        /// 设置纵向位置
        /// </summary>
        /// <param name="postion2"></param>
潘栩锋's avatar
潘栩锋 committed
1555
        [Obsolete("pos2 不应该被修改")]
潘栩锋's avatar
潘栩锋 committed
1556 1557 1558
        public void SetPos2(int postion2)
        {

1559 1560 1561 1562 1563 1564 1565 1566 1567
            CurrObjSys.CallFunctionEx(
                conn,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.CALL_PXX,
                new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_Pos2()
                {
                    pos2 = postion2
                }.ToBytes());
潘栩锋's avatar
潘栩锋 committed
1568

1569
        }
潘栩锋's avatar
潘栩锋 committed
1570 1571 1572
        /// <summary>
        /// 纵向偏移
        /// </summary>
1573
        public void SetPos2Offset(int offset)
潘栩锋's avatar
潘栩锋 committed
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
        {
            CurrObjSys.CallFunctionEx(
                conn,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.CALL_PXX,
                new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_Pos2Shift()
                {
                    pos2_shift = offset
                }.ToBytes());
        }
1585 1586


潘栩锋's avatar
潘栩锋 committed
1587 1588 1589 1590
        /// <summary>
        /// 纵向同步事件,0-1事件
        /// </summary>
        /// <param name="pos2"></param>
1591
        /// <param name="immediately">只要是 01,就立刻修正</param>
潘栩锋's avatar
潘栩锋 committed
1592
        [Obsolete("pos2 不应该被修改")]
1593
        public void SetPos2At01(int pos2, bool immediately)
潘栩锋's avatar
潘栩锋 committed
1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
        {
            CurrObjSys.CallFunctionEx(
                conn,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.CALL_PXX,
                new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_Pos2_01()
                {
                    pos2 = pos2,
                    immediately = immediately
                }.ToBytes());
            LastPos2At01 = pos2;
        }

        #endregion
        #region 同步控制 同步状态转换规则
        /// <summary>
        /// 进入同步状态
        /// </summary>
        /// <param name="pos2"></param>
潘栩锋's avatar
潘栩锋 committed
1614
        [Obsolete("pos2 不应该被修改")]
潘栩锋's avatar
潘栩锋 committed
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651
        public void SyncBegin(int pos2)
        {
            Drive(DRIVE_MAN_ORDER.SYNC, pos2);
            IsSync = true;
        }
        /// <summary>
        /// 进入同步状态
        /// </summary>
        public void SyncBegin()
        {
            Drive(DRIVE_MAN_ORDER.SYNC, null);
            IsSync = true;
        }
        /// <summary>
        /// 退出同步状态
        /// </summary>
        public void SyncEnd()
        {
            CurrObjSys.CallFunctionEx(
                conn,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.CALL_PXX,
                new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncEnd().ToBytes());
            IsSync = false;
        }
        /// <summary>
        /// 清空同步指令
        /// </summary>
        public void SyncClear()
        {
            CurrObjSys.CallFunctionEx(
                conn,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.CALL_PXX,
                new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncClear().ToBytes());
1652

潘栩锋's avatar
潘栩锋 committed
1653 1654 1655
            SyncOrders.Clear();

        }
1656 1657 1658
        /// <summary>
        /// 清空POS2同步列表
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
1659
        [Obsolete("pos2 不应该被修改")]
潘栩锋's avatar
潘栩锋 committed
1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
        public void SyncPos2Clear()
        {
            CurrObjSys.CallFunctionEx(
              conn,
              FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
              ID,
              FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.CALL_PXX,
              new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncPos2Clear().ToBytes());
        }
        #endregion
        #region 同步扫描 脚本指令

        /// <summary>
1673 1674
        /// 同步扫描至;
        /// D+0xE0+开始主轴位置+结束主轴位置+结束横向脉冲位置(逻辑位置)+脉冲开关(1B)+命令识标号(4B)
潘栩锋's avatar
潘栩锋 committed
1675
        /// </summary>
1676 1677 1678 1679 1680
        /// <param name="pos2_begin"></param>
        /// <param name="pos2_end"></param>
        /// <param name="pos1lc"></param>
        /// <param name="hasDataGrid"></param>
        /// <param name="marker"></param>public void SyncRunAtLC(int pos2_begin, int pos2_end, int pos1lc, bool hasDataGrid, Int32 marker)
潘栩锋's avatar
潘栩锋 committed
1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
        public void SyncRunAtLC(int pos2_begin, int pos2_end, int pos1lc, bool hasDataGrid, Int32 marker)
        {
            CurrObjSys.CallFunctionEx(
                conn,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.CALL_PXX,
                new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncRunAtLC()
                {
                    pos2_begin = pos2_begin,
                    pos2_end = pos2_end,
                    pos1lc = pos1lc,
                    hasDataGrid = hasDataGrid,
                    marker = marker
                }.ToBytes());

            SyncOrders.Add(new SyncOrder_SyncRunAtLC()
            {
                Pos2Begin = pos2_begin,
                Pos2End = pos2_end,
                Pos1LC = pos1lc,
                HasDataGrid = hasDataGrid,
                Marker = marker
            });
        }

1707 1708 1709 1710 1711
        /// <summary>
        /// 位于队列头时运行,归零;
        /// D+0xE1+命令识标号(4B)
        /// </summary>
        /// <param name="marker"></param>
潘栩锋's avatar
潘栩锋 committed
1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730
        public void SyncOrigin(Int32 marker)
        {
            CurrObjSys.CallFunctionEx(
                conn,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.CALL_PXX,
                new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncOrigin()
                {
                    marker = marker
                }.ToBytes());

            SyncOrders.Add(new SyncOrder_SyncOrigin()
            {
                Marker = marker
            });
        }


1731 1732 1733 1734 1735 1736 1737 1738
        /// <summary>
        /// 位于队列头时运行,以速度运行至物理位置;
        /// D+0xE2+横向脉冲位置(4B:int32,物理位置)+速度(4B:int32)+脉冲开关(1B)+命令识标号(4B)
        /// </summary>
        /// <param name="pos1"></param>
        /// <param name="velocity"></param>
        /// <param name="hasDataGrid"></param>
        /// <param name="marker"></param>
潘栩锋's avatar
潘栩锋 committed
1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762
        public void SyncRunTo(int pos1, UInt32 velocity, bool hasDataGrid, Int32 marker)
        {
            CurrObjSys.CallFunctionEx(
                conn,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.CALL_PXX,
                new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncRunto()
                {
                    pos1 = pos1,
                    velocity = velocity,
                    hasDataGrid = hasDataGrid,
                    marker = marker
                }.ToBytes());

            SyncOrders.Add(new SyncOrder_SyncRunTo()
            {
                Pos1 = pos1,
                Velocity = velocity,
                HasDataGrid = hasDataGrid,
                Marker = marker
            });
        }

1763
        /// <summary>
1764 1765 1766 1767 1768 1769 1770
        /// 位于队列头时运行,以速度运行至逻辑位置;
        /// D+0xE3+横向脉冲位置(4B:int32,逻辑位置)+速度(4B:int32)+脉冲开关(1B)+命令识标号(4B)
        /// </summary>
        /// <param name="pos1lc"></param>
        /// <param name="velocity"></param>
        /// <param name="hasDataGrid"></param>
        /// <param name="marker"></param>
潘栩锋's avatar
潘栩锋 committed
1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794
        public void SyncRunToLC(int pos1lc, UInt32 velocity, bool hasDataGrid, Int32 marker)
        {
            CurrObjSys.CallFunctionEx(
                conn,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.CALL_PXX,
                new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncRuntoLC()
                {
                    pos1 = pos1lc,
                    velocity = velocity,
                    hasDataGrid = hasDataGrid,
                    marker = marker
                }.ToBytes());
            SyncOrders.Add(new SyncOrder_SyncRunToLC()
            {
                Pos1 = pos1lc,
                Velocity = velocity,
                HasDataGrid = hasDataGrid,
                Marker = marker
            });
        }


1795 1796 1797 1798 1799 1800
        /// <summary>
        /// 等待,ms;
        /// D+0xE4+毫秒数(4B:int32)+命令识标号(4B)
        /// </summary>
        /// <param name="ms"></param>
        /// <param name="marker"></param>
潘栩锋's avatar
潘栩锋 committed
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821
        public void SyncWait(int ms, Int32 marker)
        {
            CurrObjSys.CallFunctionEx(
                conn,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.CALL_PXX,
                new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncWait()
                {
                    ms = ms,
                    marker = marker
                }.ToBytes());
            SyncOrders.Add(new SyncOrder_SyncWait()
            {
                MS = ms,
                Marker = marker
            });
        }
        #endregion
        #endregion

1822 1823
        private static string[] propertyName_save;

1824 1825
        string defaultPath = "flyad.json";
        private string jsonDbPath;
潘栩锋's avatar
潘栩锋 committed
1826 1827
        public bool Load()
        {
1828
            return Load(defaultPath);
1829 1830
        }

潘栩锋's avatar
潘栩锋 committed
1831 1832
        public bool Load(string jsonDbPath)
        {
1833 1834 1835
            this.jsonDbPath = jsonDbPath;

            this.PropertyChanged -= FlyAD7_PropertyChanged2;
1836 1837 1838

            //不需要设置到 AD盒
            bShieldSetValueEx = true;
1839
            bool ret = FlyAD7JsonDb.Load(jsonDbPath, this);
1840

1841
            //还没连到 AD盒前, 有比较正常的 默认值,不需要设置到AD盒
1842 1843 1844 1845 1846 1847
            PosOfGrid = Backup_PosOfGrid;
            MotorType = Backup_MotorType;
            Ratio01 = Backup_Ratio01;
            Ratio02 = Backup_Ratio02;
            PosOffset = Backup_PosOffset;
            JogVelocity = Backup_JogVelocity;
1848 1849 1850 1851

            bShieldSetValueEx = false;

            
1852
            this.PropertyChanged += FlyAD7_PropertyChanged2;
1853
            
1854 1855 1856
            return ret;

        }
1857 1858 1859 1860 1861 1862

        /// <summary>
        /// 属性改变,自动保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
1863 1864
        private void FlyAD7_PropertyChanged2(object sender, PropertyChangedEventArgs e)
        {
潘栩锋's avatar
潘栩锋 committed
1865 1866
            if (propertyName_save.Contains(e.PropertyName))
            {
1867 1868 1869 1870 1871 1872
                PollModule.Current.Poll_JustOnce(() =>
                {
                    Save();
                }, this, MARKNO_SET_SAVE);
            }
        }
潘栩锋's avatar
潘栩锋 committed
1873

潘栩锋's avatar
潘栩锋 committed
1874 1875
        public void Save()
        {
1876
            FlyAD7JsonDb.Save(jsonDbPath, this);
1877
        }
1878 1879 1880

    }

1881 1882
    public class FlyAD7JsonDb
    {
1883 1884 1885 1886
        static Mapper Mapper { get; } = new AutoMapper.Mapper(new MapperConfiguration(c =>
        {
            c.CreateMap<FlyAD7, FlyAD7JsonDb>().ReverseMap();
        }));
潘栩锋's avatar
潘栩锋 committed
1887 1888 1889
        public static void Save(string jsonDbPath, FlyAD7 src)
        {

1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905
            if (string.IsNullOrEmpty(jsonDbPath))
                return;
            var p = FlyAD7JsonDb.Mapper.Map<FlyAD7JsonDb>(src);
            try
            {
                File.WriteAllText(jsonDbPath, JsonConvert.SerializeObject(p, Formatting.Indented));
            }
            catch
            {
                //异常,没有json 编码失败
            }
        }
        public static bool Load(string jsonDbPath, FlyAD7 src)
        {
            if (string.IsNullOrEmpty(jsonDbPath))
                return false;
1906

1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923
            try
            {
                if (File.Exists(jsonDbPath))
                {
                    string json = File.ReadAllText(jsonDbPath);
                    var p = JsonConvert.DeserializeObject<FlyAD7JsonDb>(json);
                    Mapper.Map(p, src);
                    return true;
                }
            }
            catch
            {
                //异常,没有json 解码失败
            }
            return false;
        }
        public string Addr = "192.168.251.10:20006";
1924 1925 1926
        public bool HasCRC = false;
        public int GridSmooth = 0;
        public bool IsCalSpeed = true;
1927
        public int ADLag = 0;
1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939
        public int PosMin = 0;
        public int PosMax = 8900;
        public bool HasPosMaxMin = false;

        //备份参数
        public MOTORTYPE Backup_MotorType  = MOTORTYPE.SERVO;
        public UInt16 Backup_PosOfGrid = 10;
        public UInt16 Backup_Ratio01 = 4;
        public UInt16 Backup_Ratio02 = 1;
        public Int16 Backup_PosOffset = 0;
        public UInt32 Backup_JogVelocity  = 5000;

1940 1941 1942 1943 1944 1945 1946 1947 1948
        /// <summary>
        /// 硬件版本
        /// version = 1,不支持 systick
        /// version = 2,支持 timeGridAdv
        /// version = 3,伟的大AD盒, io口定义不一样
        /// </summary>
        public int HardwareVersion = 2;


1949 1950 1951 1952 1953
        public static string[] GetMemberNames() {
            var  type = typeof(FlyAD7JsonDb);
            return type.GetMembers().Select(mi => mi.Name).ToArray();
        }

1954
    }
潘栩锋's avatar
潘栩锋 committed
1955 1956 1957


}