GageAD.cs 3.89 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.IO;
using System.ComponentModel;
7
using FLY.Thick.Base.Server;
潘栩锋's avatar
潘栩锋 committed
8 9 10 11 12 13 14 15 16 17 18

namespace FLY.Simulation.HeaderAndTailer
{
    public class GageAD : ISimulationGageAD, INotifyPropertyChanged
    {
        CurveCollection curve;
        List<int> datas_vertical = new List<int>();//纵向AD数据,1.28ms 一个数据

        /// <summary>
        /// 编码器2 1脉冲 = ? mm    辊直径120mm,编码器 转1圈800脉冲
        /// </summary>
19
        const double Mmpp2 = 0.25;
潘栩锋's avatar
潘栩锋 committed
20 21 22 23 24
        const double RLen = 120 * 3.14;

        /// <summary>
        /// 1脉冲 = ? mm
        /// </summary>
25 26
        public double Mmpp { get; set; } = 0.2;

潘栩锋's avatar
潘栩锋 committed
27 28 29 30

        /// <summary>
        /// 膜速度 m/min
        /// </summary>
31 32
        public double FilmVelocity { get; set; } = 18;

潘栩锋's avatar
潘栩锋 committed
33 34 35 36

        /// <summary>
        /// 膜长 m
        /// </summary>
37 38
        public double FilmLength { get; set; }

潘栩锋's avatar
潘栩锋 committed
39 40 41 42

        /// <summary>
        /// 运转中
        /// </summary>
43 44 45 46 47
        public bool IsRunning { get; set; } = true;


        public int TotalLength { get; set; } = 940;

潘栩锋's avatar
潘栩锋 committed
48 49
        public GageAD()
        {
50
            curve = new CurveCollection();
潘栩锋's avatar
潘栩锋 committed
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
            Load();
        }

        public int GetAD(int position)
        {
            int idx = (int)datas_vertical_index;
            if (idx < 0)
                idx = 0;
            else if (idx >= datas_vertical.Count())
                idx = datas_vertical.Count() - 1;
            int ad = datas_vertical[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;
        double datas_vertical_index = 0;
        public void OnPoll(DateTime now)
        {
            if (dtlast == DateTime.MinValue) 
            {
                dtlast = now;
                return;
            }


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

                double ms = (double)((now - dtlast).Ticks) / TimeSpan.TicksPerMillisecond;
                datas_vertical_index += ms/1.28;

                if (datas_vertical_index >= datas_vertical.Count()) 
                {
                    datas_vertical_index -= datas_vertical.Count();
                }
            }

            
            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 LoadVertical()
        {
            datas_vertical.Clear();
116
            using (StreamReader sr = new StreamReader(@"headerAndTailer\datas_fix_1.28.csv"))
潘栩锋's avatar
潘栩锋 committed
117 118 119 120 121 122 123 124
            {
                string header = sr.ReadLine();//时间(ms),数据

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

125 126
                    double thick = double.Parse(ss[1]);
                    int ad = curve.Value2Ad(thick, Thick.Base.Common.AD2ValueFlag.NoRevised);
潘栩锋's avatar
潘栩锋 committed
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
                    datas_vertical.Add(ad);
                }
                sr.Close();
            }
        }

        void Load()
        {

            LoadVertical();
        }


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

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

153
        public void SetOutput(ushort output)
潘栩锋's avatar
潘栩锋 committed
154 155
        {
        }
156

潘栩锋's avatar
潘栩锋 committed
157 158 159
        public event PropertyChangedEventHandler PropertyChanged;
    }
}