Coating.cs 14.8 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 60 61 62 63 64 65 66 67 68 69 70
        int datas_vertical_position=0;


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


        /// <summary>
        /// 膜宽
        /// </summary>
71 72
        public int FilmWidth => Datas_Horizontal.Count();

潘栩锋's avatar
潘栩锋 committed
73 74 75 76

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


        /// <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>
99 100
        public double FilmLength { get; set; }

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

126 127 128 129
        public bool DeviceState { get; set; }


        public double Avg { get; set; }
潘栩锋's avatar
潘栩锋 committed
130 131 132 133

        void Load_Vertical()
        {
            Datas_Vertical.Clear();
134
            using (StreamReader sr = new StreamReader(@"battery\datas_vertical.csv"))
潘栩锋's avatar
潘栩锋 committed
135 136 137 138 139 140 141 142 143 144
            {
                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]);
145
                    bool isLight2 = bool.Parse(ss[2]);
146
                    double thick = double.Parse(ss[3]);
潘栩锋's avatar
潘栩锋 committed
147

148 149
                    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
150 151 152 153 154 155 156
                }
                sr.Close();
            }
        }
        void Load_Horizontal()
        {
            Datas_Horizontal.Clear();
157
            using (StreamReader sr = new StreamReader(@"battery\datas_horizontal.csv"))
潘栩锋's avatar
潘栩锋 committed
158 159 160 161 162 163 164 165 166 167
            {
                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]);
168
                    bool isLight2 = bool.Parse(ss[2]);
169
                    double thick = double.Parse(ss[3]);
潘栩锋's avatar
潘栩锋 committed
170

171 172
                    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
173 174 175 176 177 178 179 180 181 182 183
                }
                sr.Close();
            }
        }


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

184
            datas_horizontal_avg = Datas_Horizontal.Average((d) => { return d.Thick; });
潘栩锋's avatar
潘栩锋 committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
            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)
        {
204
            if (e.PropertyName == nameof(CoatingMode))
潘栩锋's avatar
潘栩锋 committed
205 206 207 208 209 210 211 212
                Save();
        }

        /// <summary>
        /// 获取30m后的数据
        /// </summary>
        /// <param name="mm"></param>
        /// <returns></returns>
213
        public double GetData30m(int mm) 
潘栩锋's avatar
潘栩锋 committed
214 215 216 217 218 219
        {
            if (mm < 0)
                return 0;
            if (mm >= FilmWidth)
                return 0;

220
            return Datas_Vertical_30m[datas_vertical_position].Thick * Datas_Horizontal_30m[mm].Thick / datas_horizontal_avg;
潘栩锋's avatar
潘栩锋 committed
221
        }
222
        public double GetData(int mm) 
潘栩锋's avatar
潘栩锋 committed
223 224 225 226 227 228
        {
            if (mm < 0)
                return 0;
            if (mm >= FilmWidth)
                return 0;

229
            return Datas_Vertical[datas_vertical_position].Thick * Datas_Horizontal[mm].Thick / datas_horizontal_avg;
潘栩锋's avatar
潘栩锋 committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
        }
        /// <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;//膜位置

            int p1 = (int)(FilmLength * 1000 % Datas_Vertical.Count());
            if (p1 != datas_vertical_position)
            {
                datas_vertical_position = p1;
                Avg = Datas_Vertical[datas_vertical_position].Thick / 100.0;
                if (AvgChanged != null) 
                {
                    AvgChanged(FilmLength, (int)(Avg*100));
                }
            }
            

            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;
271
            VSignal2 = Datas_Vertical[datas_vertical_position].IsLight2;
潘栩锋's avatar
潘栩锋 committed
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335



            //更新辊信号
            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>
        public event Action<double, int> AvgChanged;
        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[] { 
                "Port",
                "CoatingMode"
            };
        }
    }

    /// <summary>
    /// 涂布控制量
    /// </summary>
    public class CoatingCtrlElement:INotifyPropertyChanged
    {
        /// <summary>
        /// 控制的时间
        /// </summary>
        public DateTime Time = DateTime.Now;

336 337 338 339 340 341 342 343

        public double Pump { get; set; }

        public double LeftDis { get; set; }


        public double RightDis { get; set; }

潘栩锋's avatar
潘栩锋 committed
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463

        /// <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;
464
                double d = (d_right - d_left) / LeftRightFactor;
潘栩锋's avatar
潘栩锋 committed
465 466 467

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

471
                d = (d_left + d_right) / 2 / LeftRightFactor;
潘栩锋's avatar
潘栩锋 committed
472 473 474
                for (int i = 0; i < datas_vertical.Count(); i++)
                {
                    FLY.Simulation.Coating.Coating.DataInfo vi = datas_vertical[i];
475
                    vi.Thick += d;
潘栩锋's avatar
潘栩锋 committed
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
                    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;


    }
}