FilmPositionDetect.cs 6.24 KB
Newer Older
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
using FLY.Thick.Base.Common;
using FLY.Thick.Base.IService;
using FlyADBase;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;

namespace FLY.Thick.Base.Server
{
    /// <summary>
    /// 膜位置检测
    /// 通过辊接近开关,或者编码器2
    /// </summary>
    public class FilmPositionDetect : IFilmPositionDetect
    {
        /// <summary>
        /// 线速度 m/min
        /// </summary>
        public double FilmVelocity { get; private set; }

        /// <summary>
        /// 膜位置, m
        /// </summary>
        public double FilmPosition { get; private set; }

        /// <summary>
        /// 线速度阀值,低于阀值,速度直接变为0
        /// </summary>
        public double FilmVThreshold { get; set; }

        /// <summary>
        /// 线速度来源
        /// </summary>
        public FilmVSRC FilmVSrc { get; set; }

        /// <summary>
        /// 线速度:编码器2 mm/pulse
        /// </summary>
        public double Encoder2_mmpp { get; set; }

        /// <summary>
        /// 编码器2 放大
        /// </summary>
        public float Encoder2_comp { get; set; }

        /// <summary>
        /// 线速度:1圈多少mm
        /// </summary>
        public double MmOfR { get; set; }
        

        public event PropertyChangedEventHandler PropertyChanged;

        private FlyAD7 mFlyAD;

        public void Init(FlyAD7 flyAD)
        {
            mFlyAD = flyAD;


            //------------------------------------------------------------------------------------------------------------------------------------
            //线速度------------------------------------------------------------------------------------------------------------------------------
            mFlyAD.PropertyChanged += (s, e) =>
            {
                if (FilmVSrc != FilmVSRC.EN2)
                    return;

                if (e.PropertyName == "Speed2")
                {
                    double speed = mFlyAD.Speed2 * Encoder2_mmpp;//mm/s
                    speed = speed * 60 / 1000;//m/min
                    FilmVelocity = speed;
                }
                else if (e.PropertyName == "Position2")
                {
                    if (FilmVSrc != FilmVSRC.EN2)
                        return;

                    double position2 = mFlyAD.Position2* Encoder2_mmpp;//mm
                    position2 /= 1000.0;
                    FilmPosition = position2 + BaseFilmPosition;
                }
            };

            //------------------------------------------------------------------------------------------------------------------------------------
            //辊信号生成线速度-----------------------------------------------------------------------------------------------------------------------------
            InitRoundFilmVelocity();
        }

        #region 辊信号生成 线速度
        DispatcherTimer round_t;
        DateTime dtRound;

        int RCnt = 0;
        double BaseFilmPosition = 0;
        void InitRoundFilmVelocity()
        {
            round_t = new DispatcherTimer();
            round_t.Interval = TimeSpan.FromSeconds(1);
            round_t.Tick += new EventHandler(round_t_Tick);
            mFlyAD.IStatusChangedEvent += new IStatusChangedEventHandler(mFlyAD_IStatusChangedEvent_round);
        }


        void mFlyAD_IStatusChangedEvent_round(object sender, IStatusChangedEventArgs e)
        {
            if (FilmVSrc != FilmVSRC.ROUND)
                return;

            if (!Misc.MyBase.CHECKBIT(e.IChanged, 11 - 1))
                return;
            
            if (Misc.MyBase.CHECKBIT(e.IStatus, 11 - 1))
                return;
            
            //1->0
            DateTime dt = e.Time;
            if (dt <= dtRound)
                return;
            if (dtRound != DateTime.MinValue)
            {
                double v = MmOfR / 1000.0 / (dt - dtRound).TotalMinutes;
                if (v < 2)//太慢了
                {
                    dt = DateTime.MinValue;
                    v = 0;
                }
                FilmVelocity = v;
                if (v > 0)
                {
                    RCnt++;
137
                    FilmPosition = RCnt * MmOfR / 1000.0 + BaseFilmPosition;
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
                }
            }

            dtRound = dt;

            if (dtRound != DateTime.MinValue)
            {
                if (!round_t.IsEnabled)
                    round_t.Start();
            }
        }

        void round_t_Tick(object sender, EventArgs e)
        {
            if (FilmVSrc != FilmVSRC.ROUND)
            {
                round_t.Stop();
                return;
            }

            if (dtRound == DateTime.MinValue)
            {
                round_t.Stop();
                return;
            }

            //1->0
            DateTime dt = DateTime.Now;
            if (dt <= dtRound)
                return;

            double v = MmOfR / 1000.0 / (dt - dtRound).TotalMinutes;

            if (v < (FilmVelocity - 1))
            {
                if (v < 2)
                {
                    dtRound = DateTime.MinValue;
                    v = 0;
                }
                FilmVelocity = v;
            }
            
            double p1 = RCnt * MmOfR / 1000.0;
            double p2 = (dt - dtRound).TotalMinutes * FilmVelocity;
183
            if (p2 > MmOfR / 1000.0)
184 185 186 187 188 189 190 191 192 193 194
                p2 = MmOfR / 1000.0;
            p1 += p2;
            if (FilmPosition < p1)
                FilmPosition = p1;
            
            if (dtRound == DateTime.MinValue)
            {
                round_t.Stop();
                return;
            }
        }
195
         
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
        #endregion


        /// <summary>
        /// 设置膜位置为某值
        /// </summary>
        /// <param name="filmPosition"></param>
        public void Reset(double filmPosition)
        {
            if (FilmVSrc == FilmVSRC.EN2)
            {
                mFlyAD.Position2 = 0;
            }
            else
            {
                RCnt = 0;
            }
            BaseFilmPosition = filmPosition;
214
            FilmPosition = BaseFilmPosition;
215 216 217 218 219 220
        }
        
        
    }

}