Blowing.cs 33.9 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
7 8
using System.IO;
using Newtonsoft.Json;
9
using AutoMapper;
潘栩锋's avatar
潘栩锋 committed
10 11 12 13 14 15 16 17

namespace FLY.Simulation.Blowing
{
    
    public class Blowing:INotifyPropertyChanged
    {
        #region 参数
        #region 旋转架
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
        /// <summary>
        /// 360° 连续旋转
        /// </summary>
        public bool Is360 { get; set; }
        /// <summary>
        /// 正向旋转
        /// </summary>
        public bool IsForw { get; set; }

        /// <summary>
        /// 旋转架复位中
        /// </summary>
        public bool IsOrg { get; set; }

        /// <summary>
        /// 旋转架启动中
        /// </summary>
        public bool IsRotationOn { get; set; }

        /// <summary>
        /// 牵引启动中
        /// </summary>
        public bool IsTractionOn { get; set; }

        /// <summary>
        /// 1圈总脉冲
        /// </summary>
        public double PosOfR { get; set; }


潘栩锋's avatar
潘栩锋 committed
48 49 50
        /// <summary>
        /// 两个限位间的角度范围  != 旋转的总角度,限位值 °
        /// </summary>
51
        public double AngleRange { get; set; }
潘栩锋's avatar
潘栩锋 committed
52

53 54 55 56
        /// <summary>
        /// 复位信号角度范围°
        /// </summary>
        public double OrgAngleRange { get; set; }
潘栩锋's avatar
潘栩锋 committed
57 58 59
        /// <summary>
        /// 旋转的速度, min/R 1R = 360°
        /// </summary>
60 61
        public double AngleVelocity { get; set; }

潘栩锋's avatar
潘栩锋 committed
62 63 64 65

        /// <summary>
        /// 加速时间 单位 s
        /// </summary>
66 67
        public double AngleAccTime { get; set; }

潘栩锋's avatar
潘栩锋 committed
68 69 70
        /// <summary>
        /// 减速时间 单位 s
        /// </summary>
71 72
        public double AngleDecTime { get; set; }

潘栩锋's avatar
潘栩锋 committed
73 74 75
        /// <summary>
        /// 换方向冷却时间 单位s
        /// </summary>
76
        public double AngleCoolTime { get; set; }
潘栩锋's avatar
潘栩锋 committed
77 78 79 80
        #endregion


        #region 
81

潘栩锋's avatar
潘栩锋 committed
82
        /// <summary>
83
        /// 膜泡压扁后总宽度,单位 脉冲
潘栩锋's avatar
潘栩锋 committed
84
        /// </summary>
85 86
        public double FilmWidth { get; set; }

潘栩锋's avatar
潘栩锋 committed
87 88 89
        /// <summary>
        /// 膜走带速度, m/min
        /// </summary>
90
        public double FilmVelocity { get; set; }
潘栩锋's avatar
潘栩锋 committed
91 92 93 94

        /// <summary>
        /// 人字架到测厚仪的距离 23m
        /// </summary>
95
        public double FilmDistance { get; set; }
96 97 98 99 100
        /// <summary>
        /// FilmDistance+mFilmLength3D[CurrAngle]
        /// </summary>
        public double CurrFilmDistance { get; set; }
        public bool Is3D { get; set; }
潘栩锋's avatar
潘栩锋 committed
101 102 103 104

        /// <summary>
        /// 辊轴承,单位mm
        /// </summary>
105 106
        [JsonProperty]
        public double RollPerimeter { get; set; }
潘栩锋's avatar
潘栩锋 committed
107 108 109 110

        /// <summary>
        /// 测辊信号,信号长,单位mm
        /// </summary>
111
        public double RollSignLength { get; set; }
潘栩锋's avatar
潘栩锋 committed
112 113
        #endregion

114 115 116 117 118
        #region 风环
        /// <summary>
        /// 加热棒数量
        /// </summary>
        public int ChannelCnt { get; set; }
119

120 121 122 123
        /// <summary>
        /// 第1根加热棒对应角度
        /// </summary>
        public double Channel1stAngle { get; set; }
潘栩锋's avatar
潘栩锋 committed
124 125 126 127
        ///// <summary>
        ///// 温度影响厚度变化比例, 1厚度,对应多少加热
        ///// </summary>
        //public int Factor;//
128
        #endregion
129 130 131

        public bool IsShieldI9 { get; set; }

潘栩锋's avatar
潘栩锋 committed
132 133 134

        #endregion
        #region 动态数据
135 136 137



潘栩锋's avatar
潘栩锋 committed
138 139 140
        /// <summary>
        /// 当前人字架角度 °
        /// </summary>
141
        public double CurrAngle { get; private set; }
潘栩锋's avatar
潘栩锋 committed
142
        /// <summary>
143 144 145 146
        /// 全局角度 °
        /// </summary>
        public double GlobalAngle { get; private set; }
        /// <summary>
潘栩锋's avatar
潘栩锋 committed
147 148
        /// 当前速度,单位 °/s
        /// </summary>
149
        public double CurrAngleVelocity { get; private set; }
潘栩锋's avatar
潘栩锋 committed
150 151 152
        /// <summary>
        /// 当前加速度, 单位 °/s^2
        /// </summary>
153
        public double CurrAngleAccV { get; private set; }
潘栩锋's avatar
潘栩锋 committed
154 155 156 157 158


        /// <summary>
        /// 当前已经走到长度, 通过编码器获取 单位 m
        /// </summary>
159
        public double CurrFilmLength { get; private set; }
160 161 162 163
        /// <summary>
        /// 当前线速度 m/min
        /// </summary>
        public double CurrFilmVelocity { get; private set; }
潘栩锋's avatar
潘栩锋 committed
164 165
        #endregion
        public HMI mHMI;
166
        public int Avg { get; set; }
167 168 169

        public double[] mFilmLength3D;
        double lastFilmDistance;
潘栩锋's avatar
潘栩锋 committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
        /// <summary>
        /// 原始数据
        /// </summary>
        public ObservableCollection<int> BeforeDatas = new ObservableCollection<int>();
        /// <summary>
        /// 系统扰动后 数据
        /// </summary>
        public ObservableCollection<int> BeforeDatas2 = new ObservableCollection<int>();
        /// <summary>
        /// 系统扰动后 再 加热数据
        /// </summary>
        public ObservableCollection<int> AfterDatas = new ObservableCollection<int>();




        class PositionAngleInfo
        {
188 189 190
            /// <summary>
            /// 时间点
            /// </summary>
潘栩锋's avatar
潘栩锋 committed
191
            public DateTime dt;
192 193 194
            /// <summary>
            /// 测厚仪所在位置 已经跑了的膜长度位置
            /// </summary>
潘栩锋's avatar
潘栩锋 committed
195
            public double position;
196 197 198
            /// <summary>
            /// 这个时刻的角度
            /// </summary>
潘栩锋's avatar
潘栩锋 committed
199
            public double angle;
200 201 202 203 204 205 206 207 208
            /// <summary>
            /// 膜距离,应该找为N米前 PositionAngleInfo.angle 得出厚度信息
            /// </summary>
            public double filmDistance;

            /// <summary>
            /// position+filmDistance
            /// </summary>
            public double pos24m => position + filmDistance;
潘栩锋's avatar
潘栩锋 committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
        }



        /// <summary>
        /// 10ms 1个记录点
        /// </summary>
        List<PositionAngleInfo> mPositionAngleList = new List<PositionAngleInfo>();

        int ListCap = 30000;//300s

        public Blowing()
        {

            //撞了限位后,限位信号一直在。。。。
224 225
            PosOfR = 50000;
            OrgAngleRange = 5;//复位信号角度°
潘栩锋's avatar
潘栩锋 committed
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
            AngleRange = 350;//两个限位间的角度范围350°
            AngleVelocity = 6;//出限位 到 撞另一个限位的时间 8min/ 360°
            AngleAccTime = 1;//加速时间1s
            AngleDecTime = 5;//减速时间5s
            AngleCoolTime = 5;//正反转延迟时间5s 
            
            FilmWidth = 2.3;//膜宽2.3m
    
            FilmVelocity = 20;//线速度m/min
            FilmDistance = 26;//膜距离m


            RollPerimeter = 314;//辊周长mm
            RollSignLength = 30;//辊信号长度mm

241 242 243 244 245 246 247

            ChannelCnt = 88;//加热棒数量
            Channel1stAngle = 100;//第1根加热棒对应角度

            if (!Load())
                Save();

潘栩锋's avatar
潘栩锋 committed
248 249 250 251 252 253 254 255
            //整个膜片1000个数据
            int[] datas = OrgData.GetData(1000);
            for(int i=0;i<1000;i++)
            {
                BeforeDatas.Add(datas[i]);//原始数据
                BeforeDatas2.Add(datas[i]);//系统扰动后数据
                AfterDatas.Add(datas[i]);//系统扰动后 再 加热数据
            }
256 257 258
            //3d膜距离
            mFilmLength3D = FilmLength3D.GetData();

潘栩锋's avatar
潘栩锋 committed
259

260
            mAirRing = new AirRing(ChannelCnt);//88支加热棒
潘栩锋's avatar
潘栩锋 committed
261 262 263
            mAirRing.Init(BeforeDatas, BeforeDatas2, 0);


264 265
            mAirRing2 = new AirRing(ChannelCnt);//88支加热棒;
            mAirRing2.Init(BeforeDatas2, AfterDatas, (int)(1000*Channel1stAngle/360));//加热偏移了 
潘栩锋's avatar
潘栩锋 committed
266 267 268 269 270 271 272 273 274 275 276 277

            mAirRing2.AfterDatasUpdateEvent += new Action<ObservableCollection<int>>(mAirRing2_AfterDatasUpdateEvent);
            Avg = (int)(BeforeDatas.Average());//原始平均值

            TestPos = 500;
            UpdateTestHeat();

            //AfterDatas.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(AfterDatas_CollectionChanged);
            PropertyChanged += new PropertyChangedEventHandler(Blowing_PropertyChanged);

            mHMI = new HMI();
            mHMI.mAirRing = mAirRing2;
278 279 280 281 282 283 284 285 286



            Is360 = false;

            IsForw = true;
            IsOrg = false;
            IsRotationOn = true;
            IsTractionOn = true;
潘栩锋's avatar
潘栩锋 committed
287 288 289 290
        }

        void mAirRing2_AfterDatasUpdateEvent(ObservableCollection<int> obj)
        {
291
            NotifyPropertyChanged(nameof(AfterDatas));
潘栩锋's avatar
潘栩锋 committed
292 293 294
        }
        

295 296 297 298
        public int TestPos { get; set; }

        public int TestHeat { get; set; }

潘栩锋's avatar
潘栩锋 committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
        public void Test()
        {
            int idx = mAirRing.ChannelCnt * TestPos / BeforeDatas.Count();

            int offset = TestHeat - mAirRing.Heats[idx];

            mAirRing.Test(idx, offset);

            mAirRing2.HeatApply();
        }
        
        public class AirRing 
        {
            /// <summary>
            /// 加热通过
            /// </summary>
            public int ChannelCnt;
            /// <summary>
            /// 加热量, 数据量为 ChannelCnt
            /// </summary>
            public int[] Heats;
            /// <summary>
            /// 第1个加热棒,对应的 数据序号, 数据量为 1000, BeforeData.Count
            /// </summary>
            public int Channel1stIndex;
            /// <summary>
            /// 加热 对 厚度的影响
            /// </summary>
            public double Factor;

            //需要别人赋值
            /// <summary>
            /// 原始数据!! 数据量为1000
            /// </summary>
            public ObservableCollection<int> BeforeDatas;
            /// <summary>
            /// 加热后的数据!!!! 数据量为BeforeDatas.Count
            /// </summary>
            public ObservableCollection<int> AfterDatas;


            double[] p;// 一个凸起来的数组  0 1 4 9 16 9 4 1 0
            void p_init()
            {
                int size = ChannelCnt / 10;
                p = new double[size];
                for (int i = 0; i < size; i++)
                {
                    int index1 = i;
                    int index2 = size - 1 - i;
                    double d = i * i;
                    p[index1] = d;
                    p[index2] = d;
                    if (Math.Abs(index2-index1)<=1)
                        break;
                }
                double sum_p = p.Max();
                for (int i = 0; i < size; i++)
                {
                    p[i] = p[i] / sum_p;
                }
            }
            public AirRing(int channelcnt)
            {
                SetChannelCnt(channelcnt);
                p_init();
                Factor = 5;
            }
            public void Init(ObservableCollection<int> before_datas, ObservableCollection<int> after_datas, int channel1stIndex)
            {
                BeforeDatas = before_datas;
                AfterDatas = after_datas;
                Channel1stIndex = channel1stIndex;

            }
            public void SetChannelCnt(int channelcnt)
            {
                ChannelCnt = channelcnt;
                Heats = new int[ChannelCnt];
                Array.Clear(Heats, 0, ChannelCnt);
            }
            public event Action<ObservableCollection<int>> AfterDatasUpdateEvent;
            public void HeatApply() 
            {
                int boltcnt = BeforeDatas.Count();
                double b_c = (double)(boltcnt)/ChannelCnt;
                int b = Channel1stIndex;
                double beforeavg = BeforeDatas.Average();
                int[] datas = new int[boltcnt];
                for (int i = 0; i < ChannelCnt; i++) 
                {
                    int heat = Heats[i];
                    int e = (int)((i+1) * b_c)+ Channel1stIndex;

                    if (e >= boltcnt)
                        e -= boltcnt;
                    else if (e < 0)
                        e += boltcnt;
                        
                    int j=b;
                    while (j != e)
                    {
                        datas[j] = (int)(BeforeDatas[j] - beforeavg * heat / Factor/100);

                        j++;
                        if (j >= boltcnt)
                            j -= boltcnt;
                    }
                    b = e;
                }
                double afteravg = datas.Average();

                for (int j = 0; j < boltcnt; j++)
                {
                    AfterDatas[j] = (int)(beforeavg * datas[j] / afteravg);
                }
                if (AfterDatasUpdateEvent != null)
                {
                    AfterDatasUpdateEvent(AfterDatas);
                }
            }
            
            public void Test(int idx,int offset) 
            {
                idx -= p.Count() / 2;
                if (idx < 0)
                    idx += ChannelCnt;
                else if (idx >= ChannelCnt)
                    idx -= ChannelCnt;

                for (int i = 0; i < p.Count(); i++) 
                {
                    int index = idx+i;
                    
                    if (index < 0)
                        index += ChannelCnt;
                    else if (index>= ChannelCnt)
                        index -= ChannelCnt;


                    int heat = Heats[index] + (int)(offset * p[i]);
                    if (heat > 100)
                        heat = 100;
                    else if (heat < 0)
                        heat = 0;
                    Heats[index] = heat;
                }
                HeatApply();
            }
        }
        /// <summary>
        /// 1级加热, 模拟系统扰动
        /// </summary>
        private AirRing mAirRing;
        /// <summary>
        /// 2级加热, 通过HMI 控制的加热
        /// </summary>
        public AirRing mAirRing2;

        void Blowing_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
460
            if (e.PropertyName == nameof(Avg)) 
潘栩锋's avatar
潘栩锋 committed
461 462 463 464 465 466 467 468 469 470 471
            {
                int avg = (int)(BeforeDatas.Average());
                int offset = Avg - avg;
                if (offset != 0)
                {
                    for (int i = 0; i < BeforeDatas.Count(); i++)
                    {
                        BeforeDatas[i] += offset;
                    }
                }
            }
472
            else if (e.PropertyName == nameof(TestPos))
潘栩锋's avatar
潘栩锋 committed
473 474 475 476 477 478 479 480 481 482 483
            {
                UpdateTestHeat();
            }
        }
        void UpdateTestHeat() 
        {
            int idx = mAirRing.ChannelCnt * TestPos / BeforeDatas.Count();
                TestHeat = mAirRing.Heats[idx];
        }


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
        /// <summary>
        ///  找到一个时间点,它的位置+膜距离=输入的纵向位置, 返回对应的时间点的角度
        /// </summary>
        /// <param name="position"></param>
        /// <param name="angle"></param>
        /// <returns></returns>
        bool SearchAngleFromPositionList24m(double position, out double angle)
        {
            for (int i = 1; i < mPositionAngleList.Count; i++)
            {
                int idx1 = i;
                int idx0 = i - 1;
                double pos24m1 = mPositionAngleList[idx1].pos24m;
                double pos24m0 = mPositionAngleList[idx0].pos24m;
                double angle1 = mPositionAngleList[idx1].angle;
                double angle0 = mPositionAngleList[idx0].angle;

                if (pos24m1 >= position )
                {
                    //刚过了,就是它与上一个之间的值
                    

                    double percent = (position - pos24m0) / (pos24m1 - pos24m0);
                    if (percent < 0)
                    {
                        angle = 0;
                        return true;
                    }
                    //找到了
                    angle = (angle1 - angle0) * percent + angle0;
                    return true;
                }
            }
            angle = 0;
            return true;
        }

        /// <summary>
        /// 通过纵向位置,查找角度
        /// </summary>
        /// <param name="position"></param>
        /// <param name="angle"></param>
        /// <returns></returns>
潘栩锋's avatar
潘栩锋 committed
527 528 529 530 531 532 533 534 535 536 537 538 539
        bool SearchAngleFromPositionList(double position, out double angle)
        {
            for (int i = 1; i < mPositionAngleList.Count; i++)
            {
                int idx1 = i;
                int idx2 = i - 1;
                if ((mPositionAngleList[idx1].position >= position))
                {

                    double percent = (position - mPositionAngleList[idx2].position) / (mPositionAngleList[idx1].position - mPositionAngleList[idx2].position);
                    if (percent < 0)
                    {
                        angle = 0;
540
                        return true;
潘栩锋's avatar
潘栩锋 committed
541 542 543 544 545 546 547
                    }
                    //找到了
                    angle = (mPositionAngleList[idx1].angle - mPositionAngleList[idx2].angle) * percent + mPositionAngleList[idx2].angle;
                    return true;
                }
            }
            angle = 0;
548
            return true;
潘栩锋's avatar
潘栩锋 committed
549
        }
550 551 552 553 554 555
        /// <summary>
        /// 给定相对于膜横向位置, 输出当前对应的厚度
        /// </summary>
        /// <param name="pos_m"></param>
        /// <returns></returns>
        public FilmData GetData(double pos_m)
潘栩锋's avatar
潘栩锋 committed
556
        {
557
            //double ago_filmlength = CurrFilmLength - CurrFilmDistance;
潘栩锋's avatar
潘栩锋 committed
558
            double angle;
559 560

            if (!SearchAngleFromPositionList24m(CurrFilmLength, out angle))
潘栩锋's avatar
潘栩锋 committed
561 562
                return null;

563 564 565 566 567
            //从膜位置列表找时间
            //if (!SearchAngleFromPositionList(ago_filmlength, out angle))
            //    return null;

            return GetData(pos_m, angle);
潘栩锋's avatar
潘栩锋 committed
568 569

        }
潘栩锋's avatar
潘栩锋 committed
570 571 572 573 574 575 576
        double GetValidAngle(double a) {
            while (a >= 360)
                a -= 360;
            while (a < 0)
                a += 360;
            return a;
        }
潘栩锋's avatar
潘栩锋 committed
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
        /// <summary>
        /// 获取膜泡数据
        /// </summary>
        /// <param name="position">相对于膜泡位置,横向</param>
        /// <param name="angle">人字架角度</param>
        /// <returns></returns>
        FilmData GetData(double position, double angle)
        {
            if ((position < 0) || (position > FilmWidth))
            {
                return null;
            }
            //与0°的偏移
            double a = position / FilmWidth * 180;
            //压扁对面的角度
            double a_other = 360 - a;

潘栩锋's avatar
潘栩锋 committed
594 595
            a = GetValidAngle( a + angle );
            a_other = GetValidAngle(a_other + angle);
潘栩锋's avatar
潘栩锋 committed
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614

            int dat_idx1 = (int)(a / 360 * AfterDatas.Count());
            int dat_idx2 = (int)(a_other / 360 * AfterDatas.Count());

            FilmData fd = new FilmData()
            {
                angle1 = a,
                angle2 = a_other,
                data1 = AfterDatas[dat_idx1],
                data2 = AfterDatas[dat_idx2]
            };
            return fd;
        }

        /// <summary>
        /// 1.28ms 1次
        /// </summary>
        public void OnPoll(DateTime now)
        {
615
           
潘栩锋's avatar
潘栩锋 committed
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
            UpdatePositionAndAngle(now);
        }


        enum STATE 
        {
            /// <summary>
            /// 加速
            /// </summary>
            Acc,
            /// <summary>
            /// 减速
            /// </summary>
            Dec,
            /// <summary>
            /// 恒速
            /// </summary>
            Constant,
            /// <summary>
            /// 方向交换,冷却期
            /// </summary>
            Wrap
        }
        STATE mState;
        DateTime mDTLast = DateTime.MinValue;
        /// <summary>
        /// 停下来的时间
        /// </summary>
        DateTime mDTWrap = DateTime.MinValue;

646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679

        /// <summary>
        /// 更新旋转
        /// </summary>
        /// <param name="now"></param>
        /// <param name="ts"></param>
        void UpdateAngleV(DateTime now, TimeSpan ts)
        {
            if (IsOrg) {
                IsRotationOn = false;

                UpdateAngleV_Org(now, ts);
                return;
            }

            if (IsRotationOn) {
                if (Is360)
                {
                    UpdateAngleV_360(now, ts);
                }
                else {
                    UpdateAngleV_ForwBackw(now, ts);
                }
            }
        }



        /// <summary>
        /// 正反向旋转
        /// </summary>
        /// <param name="now"></param>
        /// <param name="ts"></param>
        void UpdateAngleV_ForwBackw(DateTime now, TimeSpan ts) 
潘栩锋's avatar
潘栩锋 committed
680 681
        {
            double angle = CurrAngle + CurrAngleVelocity * ts.TotalSeconds;
682

潘栩锋's avatar
潘栩锋 committed
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
            switch (mState)
            {
                case STATE.Constant:
                    if (((CurrAngleVelocity > 0) && (angle >= AngleRange / 2)) ||
                        ((CurrAngleVelocity < 0) && (angle <= (-AngleRange / 2))))
                    {
                        mState = STATE.Dec;
                        double dv = 0 - CurrAngleVelocity;
                        CurrAngleAccV = dv / AngleDecTime;
                    } break;
                case STATE.Dec:
                    {
                        bool isForw = false;
                        if (CurrAngleVelocity > 0)
                            isForw = true;
                        CurrAngleVelocity += CurrAngleAccV * ts.TotalSeconds;
                        if (((isForw) && (CurrAngleVelocity < 0)) ||
                            ((!isForw) && (CurrAngleVelocity > 0)))
                        {
                            CurrAngleVelocity = 0;
                            mState = STATE.Wrap;
                            mDTWrap = now;
                        }
                    } break;
                case STATE.Wrap:
                    {
                        if ((now - mDTWrap).TotalSeconds >= AngleCoolTime)
                        {
                            //冷静完
                            mState = STATE.Acc;
                            double anglev = 360.0 / (AngleVelocity * 60);
                            if (angle >= AngleRange / 2)
                            {
                                //反转
                                anglev = -anglev;
718
                                IsForw = false;
潘栩锋's avatar
潘栩锋 committed
719
                            }
720 721 722 723
                            else {
                                IsForw = true;
                            }
                            CurrAngleAccV = anglev / AngleAccTime;
潘栩锋's avatar
潘栩锋 committed
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
                        }
                    } break;
                case STATE.Acc:
                    {
                        CurrAngleVelocity += CurrAngleAccV * ts.TotalSeconds;

                        double anglev = 360.0 / (AngleVelocity * 60);
                        if (CurrAngleAccV > 0)
                        {
                            if (CurrAngleVelocity >= anglev)
                            {
                                CurrAngleVelocity = anglev;
                                mState = STATE.Constant;
                            }
                        }
                        else
                        {
                            if (CurrAngleVelocity <= -anglev)
                            {
                                CurrAngleVelocity = -anglev;
                                mState = STATE.Constant;
                            }
                        }
                    } break;
            }
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
            double delta = angle - CurrAngle;
            GlobalAngle += delta;
            CurrAngle = angle;
        }


        /// <summary>
        /// 连续旋转
        /// </summary>
        /// <param name="now"></param>
        /// <param name="ts"></param>
        void UpdateAngleV_360(DateTime now, TimeSpan ts)
        {
            double anglev = 360.0 / (AngleVelocity * 60);
            if (IsForw)
                CurrAngleVelocity = anglev;
            else
                CurrAngleVelocity = -anglev;

            double angle = CurrAngle + CurrAngleVelocity * ts.TotalSeconds;
            double delta = angle - CurrAngle;
            GlobalAngle += delta;
潘栩锋's avatar
潘栩锋 committed
771 772 773 774 775 776

            while (angle >= 360)
                angle -= 360;
            while (angle < 0)
                angle +=360;

777
            CurrAngle = angle;
潘栩锋's avatar
潘栩锋 committed
778

779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
        }

        /// <summary>
        /// 复位
        /// </summary>
        /// <param name="now"></param>
        /// <param name="ts"></param>
        void UpdateAngleV_Org(DateTime now, TimeSpan ts)
        {
            double anglev = 360.0 / (AngleVelocity * 60);

            if (CurrAngle > 0){
                CurrAngleVelocity = -anglev;
            }
            else if (CurrAngle < 0) 
            { 
                CurrAngleVelocity = anglev;
            }
            else {
                CurrAngleVelocity = 0;
                IsOrg = false;
                return;
            }
潘栩锋's avatar
潘栩锋 committed
802

803 804 805 806 807 808 809 810 811 812 813 814 815
            double angle = CurrAngle + CurrAngleVelocity * ts.TotalSeconds;
            double delta = angle - CurrAngle;

            if (((CurrAngle > 0) && (angle <= 0))||
                ((CurrAngle < 0) && (angle >= 0)))
            {
                delta = 0 - CurrAngle;
                CurrAngle = 0;
                CurrAngleVelocity = 0;
                IsOrg = false;
                return;
            }
            GlobalAngle += delta;
潘栩锋's avatar
潘栩锋 committed
816 817
            CurrAngle = angle;
        }
818 819 820 821 822 823 824
        
        
        
        /// <summary>
        /// 驱动器,根据时间,更新angle,
        /// </summary>
        /// <param name="now"></param>
潘栩锋's avatar
潘栩锋 committed
825 826 827 828
        void UpdatePositionAndAngle(DateTime now)
        {
            if (mDTLast == DateTime.MinValue)
            {
829
                //提前把膜距离耗完,不用等
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
                double position = FilmDistance;
                DateTime time = now;
                while (position > 0)
                {
                    mPositionAngleList.Insert(0,
                        new PositionAngleInfo()
                        {
                            dt = time,
                            position = position,
                            angle = 0
                        });
                    TimeSpan ts1 = TimeSpan.FromMilliseconds(10);
                    time -= ts1;
                    position -= FilmVelocity * ts1.TotalMinutes;
                }

潘栩锋's avatar
潘栩锋 committed
846
                mDTLast = now;
847
                CurrFilmLength = FilmDistance;
848 849
                CurrFilmDistance = FilmDistance;
                lastFilmDistance = CurrFilmDistance;
潘栩锋's avatar
潘栩锋 committed
850 851 852 853 854 855
                CurrAngle = 0;//在中间

                mState = STATE.Constant;
                double anglev = 360.0 / (AngleVelocity * 60);
                CurrAngleVelocity = anglev;

856

潘栩锋's avatar
潘栩锋 committed
857 858 859 860 861 862 863 864 865 866 867
                return;
            }

            DateTime dt = now;

            TimeSpan ts = dt.Subtract(mDTLast);
            if (ts.TotalMilliseconds < 10)
                return;

            mDTLast = dt;
            //线速度。。。。
868

869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
            UpdateAngleV(dt, ts);
            if (Is3D && !Is360)//立体旋转架,肯定不是360°连续旋转的
            {
                double index1_5 = CurrAngle + 180;
                
                //提高精度。 线性插值
                int index1 = (int)Math.Floor(index1_5);
                int index2 = (int)Math.Ceiling(index1_5);
                while (index1 < 0)
                    index1 = 0;
                while (index1 >= 360)
                    index1 = 360 - 1;
                while (index2 < 0)
                    index2 = 0;
                while (index2 >= 360)
                    index2 = 360 - 1;
                double filmLength3d;
                if (index2 == index1)
                    filmLength3d = mFilmLength3D[index1];

                else if (index1 <= index1_5 && index1_5 <= index2)
                {
                    filmLength3d = (index1_5-index1)/(index2- index1) * (mFilmLength3D[index2]- mFilmLength3D[index1])+ mFilmLength3D[index1];
892

893 894 895 896 897 898 899
                }
                else 
                {
                    filmLength3d = mFilmLength3D[index1];
                }
                CurrFilmDistance = FilmDistance + filmLength3d;
            }
900

901 902 903 904 905 906 907 908 909 910 911
            if (IsTractionOn)//启动牵引
            {
                //膜距离增量 膜长了,2牵引速度要降
                double d = CurrFilmDistance - lastFilmDistance;
                lastFilmDistance = CurrFilmDistance;
                //2牵引速度
                double fv2 = FilmVelocity - d / ts.TotalMinutes;
                CurrFilmVelocity = fv2;
                CurrFilmLength += CurrFilmVelocity * ts.TotalMinutes;
            }
            
潘栩锋's avatar
潘栩锋 committed
912 913 914 915 916 917 918


            mPositionAngleList.Add(
                new PositionAngleInfo()
                {
                    dt = mDTLast,
                    angle = CurrAngle,
919 920
                    position = CurrFilmLength,
                    filmDistance = CurrFilmDistance
潘栩锋's avatar
潘栩锋 committed
921 922 923 924 925
                });

            if (mPositionAngleList.Count > ListCap)
                mPositionAngleList.RemoveAt(0);
        }
926
        public bool CheckOrg() 
潘栩锋's avatar
潘栩锋 committed
927
        {
928
            if (CurrAngle <= OrgAngleRange / 2 && CurrAngle >= -OrgAngleRange / 2)
潘栩锋's avatar
潘栩锋 committed
929 930 931 932
                return true;
            else
                return false;
        }
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
        public bool CheckLimit1() 
        {
            if (Is360)
            {
                if (CurrAngleVelocity < 0)
                    return true;
                else
                    return false;
            }
            else
            {
                if (CurrAngle >= AngleRange / 2)
                    return true;
                else
                    return false;
            }
        }
潘栩锋's avatar
潘栩锋 committed
950 951
        public bool CheckLimit0()
        {
952 953 954 955 956 957 958
            if (Is360)
            {
                if (CurrAngleVelocity > 0)
                    return true;
                else
                    return false;
            }
潘栩锋's avatar
潘栩锋 committed
959
            else
960 961 962 963 964 965
            {
                if (CurrAngle <= (-AngleRange / 2))
                    return true;
                else
                    return false;
            }
潘栩锋's avatar
潘栩锋 committed
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
        }
        public bool CheckRoll() 
        {
            int rollcnt = (int)(CurrFilmLength*1000 / RollPerimeter);
            double p = CurrFilmLength*1000 - rollcnt * RollPerimeter;
            if (p <= RollSignLength)
                return true;
            else
                return false;
        }
        protected void NotifyPropertyChanged(string propertyname) 
        {
            if (PropertyChanged != null) 
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyname));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
984 985

        private string param_path = "simulation_blowing.json";
986
        public bool Save() 
987
        {
988
            return JsonDbHelper<Blowing, BlowingJsonDb>.Save(this, param_path);
989 990 991 992

        }
        bool Load() 
        {
993
            return JsonDbHelper<Blowing, BlowingJsonDb>.Load(this, param_path);
994
        }
潘栩锋's avatar
潘栩锋 committed
995 996 997 998 999 1000 1001 1002
    }
    public class FilmData
    {
        public double angle1;
        public double angle2;
        public int data1;
        public int data2;
    }
1003 1004


1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
    public class BlowingJsonDb 
    {
        #region 参数
        #region 旋转架
        /// <summary>
        /// 360° 连续旋转
        /// </summary>
        public bool Is360 { get; set; }

        /// <summary>
        /// 1圈总脉冲
        /// </summary>
        public double PosOfR { get; set; }


        /// <summary>
        /// 两个限位间的角度范围  != 旋转的总角度,限位值 °
        /// </summary>
        public double AngleRange { get; set; }

        /// <summary>
        /// 复位信号角度范围°
        /// </summary>
        public double OrgAngleRange { get; set; }
        /// <summary>
        /// 旋转的速度, min/R 1R = 360°
        /// </summary>
        public double AngleVelocity { get; set; }


        /// <summary>
        /// 加速时间 单位 s
        /// </summary>
        public double AngleAccTime { get; set; }

        /// <summary>
        /// 减速时间 单位 s
        /// </summary>
        public double AngleDecTime { get; set; }

        /// <summary>
        /// 换方向冷却时间 单位s
        /// </summary>
        public double AngleCoolTime { get; set; }
        #endregion


        #region 
        /// <summary>
        /// 膜走带速度, m/min
        /// </summary>
        public double FilmVelocity { get; set; }

        /// <summary>
        /// 人字架到测厚仪的距离 23m
        /// </summary>
        public double FilmDistance { get; set; }

        /// <summary>
        /// 立体收卷
        /// </summary>
        public bool Is3D { get; set; }

        /// <summary>
        /// 辊轴承,单位mm
        /// </summary>
        public double RollPerimeter { get; set; }

        /// <summary>
        /// 测辊信号,信号长,单位mm
        /// </summary>
        public double RollSignLength { get; set; }
        #endregion

        #region 风环
        /// <summary>
        /// 加热棒数量
        /// </summary>
        public int ChannelCnt { get; set; }

        /// <summary>
        /// 第1根加热棒对应角度
        /// </summary>
        public double Channel1stAngle { get; set; }
        #endregion


        #endregion
    }

    public static class JsonDbHelper<TSource,TDestination>
    {
        static Mapper Mapper { get; } = new AutoMapper.Mapper(new MapperConfiguration(c => {
            c.CreateMap<TSource, TDestination>().ReverseMap();
        }));

        public static bool Save(TSource src, string filePath)
        {
            try
            {
                var jsondb = Mapper.Map<TDestination>(src);
                string json = Newtonsoft.Json.JsonConvert.SerializeObject(jsondb, Formatting.Indented);
                File.WriteAllText(filePath, json);
                return true;
            }
            catch
            {

                return false;
            }
        }
        public static bool Load(TSource src, string filePath)
        {
            if (!File.Exists(filePath))
                return false;
            try
            {
                string json = File.ReadAllText(filePath);

                var jsondb = Newtonsoft.Json.JsonConvert.DeserializeObject<TDestination>(json);
                Mapper.Map(jsondb, src);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
潘栩锋's avatar
潘栩锋 committed
1134
}