AutoDie.cs 4.33 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 137 138 139 140 141 142 143 144 145 146 147 148 149
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FLY.Simulation.Casting
{
    public class AutoDie
    {
        /// <summary>
        /// 加热通过
        /// </summary>
        public int ChannelCnt;
        /// <summary>
        /// 加热量, 数据量为 ChannelCnt
        /// </summary>
        public int[] Heats;
        /// <summary>
        /// 第1个加热棒,对应的 数据序号, 数据量为 1000, BeforeData.Count
        /// </summary>
        public int Channel1stIndex;
        /// <summary>
        /// 加热 对 厚度的影响
        /// </summary>
        public double Factor;

        //需要别人赋值
        /// <summary>
        /// 原始数据!! 数据量为1000
        /// </summary>
        public ObservableCollection<int> BeforeDatas;
        /// <summary>
        /// 加热后的数据!!!! 数据量为BeforeDatas.Count
        /// </summary>
        public ObservableCollection<int> AfterDatas;


        double[] p;// 一个凸起来的数组  0 1 4 9 16 9 4 1 0
        void p_init()
        {
            int size = ChannelCnt / 10;
            p = new double[size];
            for (int i = 0; i < size; i++)
            {
                int index1 = i;
                int index2 = size - 1 - i;
                double d = i * i;
                p[index1] = d;
                p[index2] = d;
                if (Math.Abs(index2 - index1) <= 1)
                    break;
            }
            double sum_p = p.Max();
            for (int i = 0; i < size; i++)
            {
                p[i] = p[i] / sum_p;
            }
        }
        public AutoDie(int channelcnt)
        {
            SetChannelCnt(channelcnt);
            p_init();
            Factor = 5;
        }
        public void Init(ObservableCollection<int> before_datas, ObservableCollection<int> after_datas, int channel1stIndex)
        {
            BeforeDatas = before_datas;
            AfterDatas = after_datas;
            Channel1stIndex = channel1stIndex;

        }
        public void SetChannelCnt(int channelcnt)
        {
            ChannelCnt = channelcnt;
            Heats = new int[ChannelCnt];
            Array.Clear(Heats, 0, ChannelCnt);
        }
        public event Action<ObservableCollection<int>> AfterDatasUpdateEvent;
        public void HeatApply()
        {
            int boltcnt = BeforeDatas.Count();
            double b_c = (double)(boltcnt) / ChannelCnt;
            int b = Channel1stIndex;
            double beforeavg = BeforeDatas.Average();
            int[] datas = new int[boltcnt];
            for (int i = 0; i < ChannelCnt; i++)
            {
                int heat = Heats[i];
                int e = (int)((i + 1) * b_c) + Channel1stIndex;

                if (e >= boltcnt)
                    e -= boltcnt;
                else if (e < 0)
                    e += boltcnt;

                int j = b;
                while (j != e)
                {
                    datas[j] = (int)(BeforeDatas[j] - beforeavg * heat / Factor / 100);

                    j++;
                    if (j >= boltcnt)
                        j -= boltcnt;
                }
                b = e;
            }
            double afteravg = datas.Average();

            for (int j = 0; j < boltcnt; j++)
            {
                AfterDatas[j] = (int)(beforeavg * datas[j] / afteravg);
            }
            if (AfterDatasUpdateEvent != null)
            {
                AfterDatasUpdateEvent(AfterDatas);
            }
        }

        public void Test(int idx, int offset)
        {
            idx -= p.Count() / 2;
            if (idx < 0)
                idx += ChannelCnt;
            else if (idx >= ChannelCnt)
                idx -= ChannelCnt;

            for (int i = 0; i < p.Count(); i++)
            {
                int index = idx + i;

                if (index < 0)
                    index += ChannelCnt;
                else if (index >= ChannelCnt)
                    index -= ChannelCnt;


                int heat = Heats[index] + (int)(offset * p[i]);
                if (heat > 100)
                    heat = 100;
                else if (heat < 0)
                    heat = 0;
                Heats[index] = heat;
            }
            HeatApply();
        }
    }
}