From 8a220d397d632d06abe549106faf88dba36ab4f5 Mon Sep 17 00:00:00 2001 From: "Samuele E. Locatelli" Date: Tue, 25 Jul 2017 16:54:15 +0200 Subject: [PATCH] modifiche x calcolo esatto mediana e fix reinit contatori --- MTC_Adapter/MTC/baseUtils.cs | 18 +++++- MTC_Adapter/MTC_Adapter/AdapterGeneric.cs | 62 +++++++++++-------- .../MTC_Adapter/DATA/DAT/PersistData.dat | 32 +--------- 3 files changed, 53 insertions(+), 59 deletions(-) diff --git a/MTC_Adapter/MTC/baseUtils.cs b/MTC_Adapter/MTC/baseUtils.cs index 9c81cca..f541c61 100644 --- a/MTC_Adapter/MTC/baseUtils.cs +++ b/MTC_Adapter/MTC/baseUtils.cs @@ -460,12 +460,25 @@ namespace MTC { double answ = 0; // restituisce la mediana SE valida, altrimenti null... - if (numElem > 1 && flWindSize > windSize) + if (numElem > 2 && flWindSize > windSize) { try { // calcolo mediana! - answ = Statistics.Median(lVal.ToArray()); + //answ = Statistics.Median(lVal.ToArray()); + + // rif: https://blogs.msmvps.com/deborahk/linq-mean-median-and-mode/ + var sortedNumbers = lVal.OrderBy(n => n); + int numCount = lVal.Count; + int indice50 = lVal.Count / 2; + if ((numCount % 2) == 0) + { + answ = ((sortedNumbers.ElementAt(indice50) + sortedNumbers.ElementAt(indice50 - 1)) / 2); + } + else + { + answ = sortedNumbers.ElementAt(indice50); + } } catch { } @@ -483,6 +496,7 @@ namespace MTC return (flWindSize > windSize && numElem > 1); } } + } public static class TimingData diff --git a/MTC_Adapter/MTC_Adapter/AdapterGeneric.cs b/MTC_Adapter/MTC_Adapter/AdapterGeneric.cs index 8044892..c9dd641 100644 --- a/MTC_Adapter/MTC_Adapter/AdapterGeneric.cs +++ b/MTC_Adapter/MTC_Adapter/AdapterGeneric.cs @@ -2551,7 +2551,7 @@ namespace MTC_Adapter { lastWatchDog = setFlag; } - retACK_DW = utils.setBitOnStFlag(retACK_DW, setFlag, 0); // imposto primo bit!!! + retACK_DW = utils.setBitOnStFlag(retACK_DW, setFlag, 0); // imposto primo bit!!! // scrivo su area PLC SE variato if (ACK_DW2 != (StFlag32)BitConverter.ToUInt32(retACK_DW, 0)) { @@ -4561,25 +4561,30 @@ namespace MTC_Adapter public bool procLubro(bool needSave) { uint delta = 0; + uint valore = 0; string outString = string.Format("Lubro_[1-{0}]_Count: ", currAdpConf.nLubro); for (int i = 0; i < currAdpConf.nLubro; i++) { - delta = Convert.ToUInt32(istLubroCount[i].vcMedian) - currLubroCount[i]; - // controllo delta < 50% max... - if (delta < uint.MaxValue / 2) + valore = Convert.ToUInt32(istLubroCount[i].vcMedian); + if (valore > currLubroCount[i]) { - //processo comunque sempre... - uint contTot = updateValUIntByIncr(i, delta, "Lubro_{0:00}_Count"); - // passo valore totale all'adapter - vettLubro[i].mLubroNum.Value = contTot; - // controllo valore riferimento... - if (delta > 0) + delta = valore - currLubroCount[i]; + // controllo delta < 50% max... + if (delta < uint.MaxValue / 2) { - // segnalo necessità salvataggio! - needSave = true; + //processo comunque sempre... + uint contTot = updateValUIntByIncr(i, delta, "Lubro_{0:00}_Count"); + // passo valore totale all'adapter + vettLubro[i].mLubroNum.Value = contTot; + // controllo valore riferimento... + if (delta > 0) + { + // segnalo necessità salvataggio! + needSave = true; + } } // ...aggiorno valore riferimento... - currLubroCount[i] = Convert.ToUInt32(istLubroCount[i].vcMedian); + currLubroCount[i] = valore; } outString += string.Format("{0} | ", vettLubro[i].mLubroNum.Value); } @@ -4595,29 +4600,34 @@ namespace MTC_Adapter public bool procCounters(bool needSave) { uint delta = 0; + uint valore = 0; StringBuilder sb = new StringBuilder(); for (int i = 0; i < mCounters.Length; i++) { // procedo solo SE HO FINESTRA VALIDA... if (istCounters[i].vcValid) { - delta = Convert.ToUInt32(istCounters[i].vcMedian) - currCounters[i]; - // controllo delta < 50% max... - if (delta < uint.MaxValue / 2) + valore = Convert.ToUInt32(istCounters[i].vcMedian); + if (valore >= currCounters[i]) { - //processo comunque sempre... - uint contTot = updateValUIntByIncr(i, delta, "Counter_{0:000}"); - // passo valore totale all'adapter - mCounters[i].Value = contTot; - // controllo valore riferimento... - if (delta > 0) + delta = valore - currCounters[i]; + // controllo delta < 50% max... + if (delta < uint.MaxValue / 2) { - // segnalo necessità salvataggio! - needSave = true; + //processo comunque sempre... + uint contTot = updateValUIntByIncr(i, delta, "Counter_{0:000}"); + // passo valore totale all'adapter + mCounters[i].Value = contTot; + // controllo valore riferimento... + if (delta > 0) + { + // segnalo necessità salvataggio! + needSave = true; + } } - // ...aggiorno valore riferimento... - currCounters[i] = Convert.ToUInt32(istCounters[i].vcMedian); } + // ...aggiorno valore riferimento... + currCounters[i] = valore; sb.AppendLine(mCounters[i].ToString().Replace("|", " | ")); } } diff --git a/MTC_Adapter/MTC_Adapter/DATA/DAT/PersistData.dat b/MTC_Adapter/MTC_Adapter/DATA/DAT/PersistData.dat index d72ce69..5f28270 100644 --- a/MTC_Adapter/MTC_Adapter/DATA/DAT/PersistData.dat +++ b/MTC_Adapter/MTC_Adapter/DATA/DAT/PersistData.dat @@ -1,31 +1 @@ -ACC_TIME:0 -ACC_TIME_WORK:0 -Axis_01_AccTime:0 -Axis_01_DistDone:0 -Axis_01_InvDDone:0 -Axis_02_AccTime:0 -Axis_02_DistDone:0 -Axis_02_InvDDone:0 -Axis_03_AccTime:0 -Axis_03_DistDone:0 -Axis_03_InvDDone:0 -Axis_04_AccTime:0 -Axis_04_DistDone:0 -Axis_04_InvDDone:0 -Axis_05_AccTime:0 -Axis_05_DistDone:0 -Axis_05_InvDDone:0 -Axis_06_AccTime:0 -Axis_06_DistDone:0 -Axis_06_InvDDone:0 -Lubro_01_Count:0 -Lubro_02_Count:0 -ProtMagazzino_01_Count:0 -ProtMagazzino_02_Count:0 -SlittaTastatore_Count:0 -UnOp_01_AccTime:0 -UnOp_01_NumCU:0 -VacAct_01_Count:0 -VacAct_02_Count:0 -VacPump_01_WrkTime:0 -VacPump_02_WrkTime:0 + \ No newline at end of file