Coating.cs 16.5 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.ComponentModel;

namespace FLY.Simulation.Coating
{

    public class Coating : INotifyPropertyChanged, Misc.ISaveToXml
    {
        public CoatingCtrl hmi_cc;
        public CoatingCtrl inner_cc;
        public HMI hmi;

        private FLY.Simulation.Coating.CoatingCtrl.COATINGMODE coatingmode = CoatingCtrl.COATINGMODE.Extrusion;
        /// <summary>
        /// 涂布类型
        /// </summary>
        public FLY.Simulation.Coating.CoatingCtrl.COATINGMODE CoatingMode 
        {
            get {
                return coatingmode;
            }
            set {
                if (coatingmode != value) 
                {
                    coatingmode = value;
                    hmi_cc.CoatingMode = value;
                    inner_cc.CoatingMode = value;
                }
            }
        }

36 37 38
        public int Port { get; set; } = 502;


潘栩锋's avatar
潘栩锋 committed
39 40
        public class DataInfo
        {
41 42 43 44 45 46 47 48 49 50 51
            /// <summary>
            /// 上光纤是亮
            /// </summary>
            public bool IsLight;
            /// <summary>
            /// 下光纤是亮
            /// </summary>
            public bool IsLight2;
            /// <summary>
            /// 厚度值
            /// </summary>
52
            public double Thick;
潘栩锋's avatar
潘栩锋 committed
53 54 55 56 57
        }
        
        //当前数据
        public List<DataInfo> Datas_Vertical = new List<DataInfo>();//纵向数据
        public List<DataInfo> Datas_Horizontal = new List<DataInfo>();//横向数据
58
        double datas_horizontal_avg = 0;
潘栩锋's avatar
潘栩锋 committed
59
        int datas_vertical_position=0;
60 61 62 63
        /// <summary>
        /// 由于泵损坏带来的纵向波动
        /// </summary>
        double datas_vertical_pumpDamaged = 0;
潘栩锋's avatar
潘栩锋 committed
64 65 66 67 68 69 70

        //30m 后的数据
        public List<DataInfo> Datas_Vertical_30m = new List<DataInfo>();//纵向数据
        public List<DataInfo> Datas_Horizontal_30m = new List<DataInfo>();//横向数据
        int datas_vertical_30m_position=0;


71

潘栩锋's avatar
潘栩锋 committed
72 73 74
        /// <summary>
        /// 膜宽
        /// </summary>
75 76
        public int FilmWidth => Datas_Horizontal.Count();

潘栩锋's avatar
潘栩锋 committed
77 78 79 80

        /// <summary>
        /// 膜速度 m/min
        /// </summary>
81
        public double FilmVelocity { get; set; } = 12;
潘栩锋's avatar
潘栩锋 committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102


        /// <summary>
        /// 辊周长 mm
        /// </summary>
        public double MmOfR=120*3.14;

        /// <summary>
        /// 辊信号长度 mm
        /// </summary>
        public double MmOfRSignal = 10;

        /// <summary>
        /// 当前辊位置 mm
        /// </summary>
        double RPosition = 0;


        /// <summary>
        /// 膜已经跑了的长度。m
        /// </summary>
103 104
        public double FilmLength { get; set; }

潘栩锋's avatar
潘栩锋 committed
105 106 107 108 109 110 111 112 113
        /// <summary>
        /// 烘箱长度 30m
        /// </summary>
        public double OvenLength = 30;
        /// <summary>
        /// 辊信号 
        /// </summary>
        public bool RSignal;
        /// <summary>
114
        /// 上纵向信号
潘栩锋's avatar
潘栩锋 committed
115 116 117
        /// </summary>
        public bool VSignal;
        /// <summary>
118 119 120 121 122
        /// 下纵向信号
        /// </summary>
        public bool VSignal2;
        /// <summary>
        /// 上横向信号
潘栩锋's avatar
潘栩锋 committed
123 124
        /// </summary>
        public bool HSignal;
125 126 127 128
        /// <summary>
        /// 下横向信号
        /// </summary>
        public bool HSignal2;
潘栩锋's avatar
潘栩锋 committed
129

130 131 132
        /// <summary>
        /// 设备状态
        /// </summary>
133 134
        public bool DeviceState { get; set; }

135 136 137 138 139 140 141 142 143
        /// <summary>
        /// 泵的转子坏了, 会看到有周期的波动,周期为 泵速
        /// </summary>
        public bool IsPumpDamaged { get; set; }

        /// <summary>
        /// 由于 泵的转子坏了 导致 面密度 周期的波动 的变化公差,单位g/m²
        /// </summary>
        public double ToleranceOfPumpBeDamaged { get; set; } = 1;
144

145 146 147
        /// <summary>
        /// 均值
        /// </summary>
148
        public double Avg { get; set; }
潘栩锋's avatar
潘栩锋 committed
149 150 151 152

        void Load_Vertical()
        {
            Datas_Vertical.Clear();
153
            using (StreamReader sr = new StreamReader(@"battery\datas_vertical.csv"))
潘栩锋's avatar
潘栩锋 committed
154 155 156 157 158 159 160 161 162 163
            {
                string header = sr.ReadLine();//标题 , 位置(mm), 纵向信号,厚度

                while (!sr.EndOfStream)
                {
                    string s = sr.ReadLine();
                    string[] ss = s.Split(',');

                    int pos_mm = int.Parse(ss[0]);
                    bool isLight = bool.Parse(ss[1]);
164
                    bool isLight2 = bool.Parse(ss[2]);
165
                    double thick = double.Parse(ss[3]);//单位 g/m²
潘栩锋's avatar
潘栩锋 committed
166

167 168
                    Datas_Vertical.Add(new DataInfo() { IsLight = isLight, IsLight2 = isLight2, Thick = thick });
                    Datas_Vertical_30m.Add(new DataInfo() { IsLight = isLight, IsLight2 = isLight2, Thick = thick });
潘栩锋's avatar
潘栩锋 committed
169 170 171 172 173 174 175
                }
                sr.Close();
            }
        }
        void Load_Horizontal()
        {
            Datas_Horizontal.Clear();
176
            using (StreamReader sr = new StreamReader(@"battery\datas_horizontal.csv"))
潘栩锋's avatar
潘栩锋 committed
177 178 179 180 181 182 183 184 185 186
            {
                string header = sr.ReadLine();//标题 , 位置(mm), 厚度

                while (!sr.EndOfStream)
                {
                    string s = sr.ReadLine();
                    string[] ss = s.Split(',');

                    int pos_mm = int.Parse(ss[0]);
                    bool isLight = bool.Parse(ss[1]);
187
                    bool isLight2 = bool.Parse(ss[2]);
188
                    double thick = double.Parse(ss[3]);
潘栩锋's avatar
潘栩锋 committed
189

190 191
                    Datas_Horizontal.Add(new DataInfo() { IsLight = isLight, IsLight2 = isLight2, Thick = thick });
                    Datas_Horizontal_30m.Add(new DataInfo() { IsLight = isLight, IsLight2 = isLight2, Thick = thick });
潘栩锋's avatar
潘栩锋 committed
192 193 194 195 196 197 198 199 200 201 202
                }
                sr.Close();
            }
        }


        void Load() 
        {
            Load_Vertical();
            Load_Horizontal();

203
            datas_horizontal_avg = Datas_Horizontal.Average((d) => { return d.Thick; });
潘栩锋's avatar
潘栩锋 committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
            Misc.SaveToXmlHepler.Load("simulation_coating.xml", this);
        }
        public void Save() 
        {
            Misc.SaveToXmlHepler.Save("simulation_coating.xml", this);
        }
        public Coating() 
        {
            hmi_cc = new CoatingCtrl(this);
            inner_cc = new CoatingCtrl(this);
            DeviceState = true;
            Load();
            hmi = new HMI(this, hmi_cc, Port);

            this.PropertyChanged += new PropertyChangedEventHandler(Coating_PropertyChanged);
        }

        void Coating_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
223
            if (e.PropertyName == nameof(CoatingMode))
潘栩锋's avatar
潘栩锋 committed
224 225 226 227 228 229 230 231
                Save();
        }

        /// <summary>
        /// 获取30m后的数据
        /// </summary>
        /// <param name="mm"></param>
        /// <returns></returns>
232
        public double GetData30m(int mm) 
潘栩锋's avatar
潘栩锋 committed
233 234 235 236 237
        {
            if (mm < 0)
                return 0;
            if (mm >= FilmWidth)
                return 0;
238 239 240 241 242 243
            double d = Datas_Vertical_30m[datas_vertical_position].Thick * Datas_Horizontal_30m[mm].Thick / datas_horizontal_avg;
            if (IsPumpDamaged)
            {
                d += datas_vertical_pumpDamaged;
            }
            return d;
潘栩锋's avatar
潘栩锋 committed
244
        }
245
        public double GetData(int mm) 
潘栩锋's avatar
潘栩锋 committed
246 247 248 249 250 251
        {
            if (mm < 0)
                return 0;
            if (mm >= FilmWidth)
                return 0;

252 253 254 255 256 257 258
            double d = Datas_Vertical[datas_vertical_position].Thick * Datas_Horizontal[mm].Thick / datas_horizontal_avg;

            if (IsPumpDamaged)
            {
                d += datas_vertical_pumpDamaged;
            }
            return d;
潘栩锋's avatar
潘栩锋 committed
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
        }
        /// <summary>
        /// 上一个时间点
        /// </summary>
        DateTime dtLast = DateTime.MinValue;
        
        public void OnPoll(DateTime now) 
        {
            if (dtLast == DateTime.MinValue)
            {
                dtLast = now;
                return;
            }
            TimeSpan ts= now - dtLast;
            dtLast = now;

            if (!DeviceState)//没有涂布
                return;

            double filmMove = ts.TotalMinutes * FilmVelocity;
            FilmLength += filmMove;//膜位置

281 282


潘栩锋's avatar
潘栩锋 committed
283 284 285 286
            int p1 = (int)(FilmLength * 1000 % Datas_Vertical.Count());
            if (p1 != datas_vertical_position)
            {
                datas_vertical_position = p1;
287 288 289 290

                double avg = Datas_Vertical[datas_vertical_position].Thick;
                
                if (hmi_cc.NowCtrl.Pump > 0 && IsPumpDamaged)
潘栩锋's avatar
潘栩锋 committed
291
                {
292 293 294 295 296 297 298 299
                    // 泵损坏 周期 m
                    double intervalOfPumpDamage = FilmVelocity / hmi_cc.NowCtrl.Pump;
                    double index = FilmLength % intervalOfPumpDamage;
                    double percent = index / intervalOfPumpDamage;
                    double v = ToleranceOfPumpBeDamaged * Math.Sin(Math.PI * 2 * percent);
                    
                    datas_vertical_pumpDamaged = v;
                    avg += datas_vertical_pumpDamaged;
潘栩锋's avatar
潘栩锋 committed
300
                }
301 302
                Avg = avg;
                AvgChanged?.Invoke(this, new AvgChangedEventArgs() { filmLength = FilmLength, avg = Avg });
潘栩锋's avatar
潘栩锋 committed
303 304 305
            }
            

306 307


潘栩锋's avatar
潘栩锋 committed
308 309 310 311 312 313 314
            datas_vertical_30m_position = (int)((FilmLength - OvenLength) * 1000 % Datas_Vertical.Count());
            if (datas_vertical_30m_position < 0)
                datas_vertical_30m_position += Datas_Vertical.Count();


            //更新纵向信号
            VSignal = Datas_Vertical[datas_vertical_position].IsLight;
315
            VSignal2 = Datas_Vertical[datas_vertical_position].IsLight2;
潘栩锋's avatar
潘栩锋 committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334



            //更新辊信号
            RPosition += filmMove * 1000;
            RPosition = RPosition % MmOfR;
            if (RPosition < MmOfRSignal)
                RSignal = true;
            else
                RSignal = false;


            hmi_cc.OnPoll(filmMove);
            inner_cc.OnPoll(filmMove);
        }

        /// <summary>
        /// 每1mm更新一次
        /// </summary>
335 336 337 338 339 340
        public event AvgChangedEventHandler AvgChanged;
        public delegate void AvgChangedEventHandler(object sender, AvgChangedEventArgs args);
        public class AvgChangedEventArgs : EventArgs {
            public double filmLength;
            public double avg;
        }
潘栩锋's avatar
潘栩锋 committed
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
        public event Action DatasChanged;
        public event Action Datas30mChanged;
        public void NotifyDatasChanged() 
        {
            if (DatasChanged != null) 
            {
                DatasChanged();
            }
        }
        public void NotifyDatas30mChanged()
        {
            if (Datas30mChanged != null)
            {
                Datas30mChanged();
            }
        }
        protected void NotifyPropertyChanged(string propertyname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyname));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;

        public string[] GetSavePropertyNames()
        {
            return new string[] { 
369 370
                nameof(Port),
                nameof(CoatingMode)
潘栩锋's avatar
潘栩锋 committed
371 372 373 374
            };
        }
    }

375

潘栩锋's avatar
潘栩锋 committed
376 377 378 379 380 381 382 383 384 385
    /// <summary>
    /// 涂布控制量
    /// </summary>
    public class CoatingCtrlElement:INotifyPropertyChanged
    {
        /// <summary>
        /// 控制的时间
        /// </summary>
        public DateTime Time = DateTime.Now;

386 387 388 389 390 391 392 393

        public double Pump { get; set; }

        public double LeftDis { get; set; }


        public double RightDis { get; set; }

潘栩锋's avatar
潘栩锋 committed
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513

        /// <summary>
        /// 膜移动的距离
        /// </summary>
        public double FilmMove=0;

        public event PropertyChangedEventHandler PropertyChanged;
    }


    public class CoatingCtrl : INotifyPropertyChanged
    {
        Coating mCoating;

        #region 参数
        public enum COATINGMODE
        {
            /// <summary>
            /// 挤压涂布
            /// </summary>
            Extrusion,
            /// <summary>
            /// 转移涂布
            /// </summary>
            Transfer
        }

        /// <summary>
        /// 涂布类型
        /// </summary>
        public COATINGMODE CoatingMode = COATINGMODE.Extrusion;

        /// <summary>
        /// 泵速因子  1 个 面密度 = N个泵速, 只有挤压涂布有效
        /// </summary>
        public double PumpFactor = 0.5;

        /// <summary>
        /// 左右刀因子 1个面密度 = N个左右刀
        /// </summary>
        public double LeftRightFactor = 1;

        /// <summary>
        /// 当前涂布控制量
        /// </summary>
        public CoatingCtrlElement NowCtrl;
        CoatingCtrlElement NowCtrl_30m;
        #endregion



        /// <summary>
        /// 控制量
        /// </summary>
        List<CoatingCtrlElement> mCtrlElement = new List<CoatingCtrlElement>();

        public CoatingCtrl(Coating coating) 
        {
            mCoating = coating;
            NowCtrl = new CoatingCtrlElement() { FilmMove = 0, Pump = 90, LeftDis = 200, RightDis = 210 };
            NowCtrl_30m = new CoatingCtrlElement() { FilmMove = 0, Pump = 90, LeftDis = 200, RightDis = 210 };
        }


        public void OnPoll(double filmMove)
        {
            for (int i = 0; i < mCtrlElement.Count(); i++)
            {
                if (mCtrlElement[i].FilmMove<mCoating.OvenLength)
                {
                    mCtrlElement[i].FilmMove += filmMove;
                }
            }

            for (int i = 0; i < mCtrlElement.Count(); i++)
            {
                if (mCtrlElement[i].FilmMove>=mCoating.OvenLength)
                {
                    CoatingAction(NowCtrl_30m, mCtrlElement[i], mCoating.Datas_Vertical_30m, mCoating.Datas_Horizontal_30m, mCoating.NotifyDatas30mChanged);
                    mCtrlElement.RemoveAt(i);
                    i--;
                }
                else
                    break;
            }
        }

        /// <summary>
        /// 控制量生效
        /// </summary>
        /// <param name="element"></param>
        /// <param name="datas_vertical"></param>
        /// <param name="datas_horizontal"></param>
        void CoatingAction(CoatingCtrlElement now, CoatingCtrlElement element, List<Coating.DataInfo> datas_vertical, List<Coating.DataInfo> datas_horizontal, Action NotifyDatasChanged)
        {
            if (CoatingMode == COATINGMODE.Extrusion)
            {
                double d_left = element.LeftDis - now.LeftDis;
                double d_right = element.RightDis - now.RightDis;
                double d = (d_right - d_left) / LeftRightFactor * 100;

                for (int i = 0; i < datas_horizontal.Count(); i++)
                {
                    datas_horizontal[i].Thick += (int)(d * i / (datas_horizontal.Count() - 1) - d / 2);
                }


                double d_pump = element.Pump - now.Pump;
                d = (d_pump / PumpFactor) * 100;
                for (int i = 0; i < datas_vertical.Count(); i++)
                {
                    FLY.Simulation.Coating.Coating.DataInfo vi = datas_vertical[i];
                    vi.Thick += (int)d;
                    datas_vertical[i] = vi;
                }
            }
            else
            {
                double d_left = element.LeftDis - now.LeftDis;
                double d_right = element.RightDis - now.RightDis;
514
                double d = (d_right - d_left) / LeftRightFactor;
潘栩锋's avatar
潘栩锋 committed
515 516 517

                for (int i = 0; i < datas_horizontal.Count(); i++)
                {
518
                    datas_horizontal[i].Thick += d * i / (datas_horizontal.Count() - 1) - d / 2;
潘栩锋's avatar
潘栩锋 committed
519 520
                }

521
                d = (d_left + d_right) / 2 / LeftRightFactor;
潘栩锋's avatar
潘栩锋 committed
522 523 524
                for (int i = 0; i < datas_vertical.Count(); i++)
                {
                    FLY.Simulation.Coating.Coating.DataInfo vi = datas_vertical[i];
525
                    vi.Thick += d;
潘栩锋's avatar
潘栩锋 committed
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
                    datas_vertical[i] = vi;
                }
            }

            now.Pump = element.Pump;
            now.LeftDis = element.LeftDis;
            now.RightDis = element.RightDis;
            now.Time = element.Time;
            NotifyDatasChanged();
        }


        public void SetCoatingCtrl(double pump, double left, double right)
        {
            CoatingCtrlElement element = new CoatingCtrlElement() { Pump = pump, LeftDis = left, RightDis = right };
            mCtrlElement.Add(element);
            CoatingAction(NowCtrl, element, mCoating.Datas_Vertical, mCoating.Datas_Horizontal, mCoating.NotifyDatasChanged);
            
        }


        public event PropertyChangedEventHandler PropertyChanged;


    }
}