GageAD.cs 4.75 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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.ComponentModel;

namespace FLY.Simulation.Casting
{
    public class GageAD : ISimulationGageAD, INotifyPropertyChanged
    {
        CurveCollection curve;
        //这是整个机架的数据
        List<int> datas_horizontal = new List<int>();//横向AD数据, 1mm 一个AD值

        /// <summary>
        /// 编码器2 1脉冲 = ? mm    辊直径120mm,编码器 转1圈800脉冲
        /// </summary>
        const double Mmpp2 = 0.1;//120 * 3.14 / 800;
        const double RLen = 120 * 3.14;
        /// <summary>
        /// 1脉冲 = ? mm
        /// </summary>
        double mmpp = 0.1133;
        public double Mmpp {
            get { return mmpp; }
            set {
                mmpp = value;
            }
        }


        double filmvelocity = 23;
        /// <summary>
        /// 膜速度 m/min
        /// </summary>
        public double FilmVelocity
        {
            get
            {
                return filmvelocity;
            }
            set
            {
                if (filmvelocity != value)
                {
                    filmvelocity = value;
                    NotifyPropertyChanged("FilmVelocity");
                }
            }
        }

        double filmlength = 0;
        /// <summary>
        /// 膜长 m
        /// </summary>
        public double FilmLength
        {
            get
            {
                return filmlength;
            }
            set
            {
                if (filmlength != value)
                {
                    filmlength = value;
                    NotifyPropertyChanged("FilmLength");
                }
            }
        }

        bool isRunning = true;
        /// <summary>
        /// 运转中
        /// </summary>
        public bool IsRunning
        {
            get
            {
                return isRunning;
            }
            set
            {
                if (isRunning != value)
                {
                    isRunning = value;
                    NotifyPropertyChanged("IsRunning");
                }
            }
        }
        public GageAD()
        {
            curve = new CurveCollection("curve.xml");
            Load();
        }

        public int GetAD(int position)
        {
            int idx = (int)(position * Mmpp);
            if (idx < 0)
                idx = 0;
            else if (idx >= datas_horizontal.Count())
                idx = datas_horizontal.Count() - 1;
            int ad = datas_horizontal[idx];


            //Random r = new Random();
            //int data = r.Next(50) - 25;

            //thick += data;

            return ad;// curve.Value2AD(thick, CurveCollection.AD2ValueFlag.NoRevised);
        }


        DateTime dtlast = DateTime.MinValue;
        public void OnPoll(DateTime now)
        {
            if (dtlast == DateTime.MinValue) 
            {
                dtlast = now;
                return;
            }


            if (IsRunning) 
            {
                double min = (now - dtlast).TotalMinutes;
                FilmLength += min * FilmVelocity;
            }

            
            dtlast = now;
        }

        public UInt16 GetInput()
        {
            UInt16 istatus = 0x0fff;

            double p = FilmLength * 1000 % RLen;
            if(p<RLen/10)
                Misc.MyBase.CLEARBIT(ref istatus, 10);

            return istatus;
        }

        void LoadHorizontal()
        {
            datas_horizontal.Clear();
            using (StreamReader sr = new StreamReader("datas_horizontal_casting.csv"))
            {
                string header = sr.ReadLine();//位置(mm),数据

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

                    int thick = (int)(double.Parse(ss[1]) * 100);
                    int ad = curve.ValueAD(thick);
                    datas_horizontal.Add(ad);
                }
                sr.Close();
            }
        }

        void Load()
        {

            LoadHorizontal();
        }


        public int GetPosition2()
        {
            return (int)(FilmLength * 1000 / Mmpp2);

        }
        public int GetSpeed2()
        {
            if (IsRunning)
                return (int)(FilmVelocity * 1000 / 60 / Mmpp2);
            else
                return 0;
        }

        protected void NotifyPropertyChanged(string propertyname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyname));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
}