reducing code duplication

This commit is contained in:
rusefi 2018-10-07 19:35:07 -04:00
parent c8c6e4b182
commit 01510bc8aa
1 changed files with 6 additions and 12 deletions

View File

@ -1,11 +1,12 @@
package com.rusefi.ui.widgets; package com.rusefi.ui.widgets;
import com.rusefi.CyclicBuffer;
import com.rusefi.DataBuffer;
import com.rusefi.core.Sensor; import com.rusefi.core.Sensor;
import com.rusefi.core.SensorCentral; import com.rusefi.core.SensorCentral;
import com.rusefi.core.SensorStats; import com.rusefi.core.SensorStats;
import com.rusefi.core.ValueSource; import com.rusefi.core.ValueSource;
import java.util.LinkedList;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -19,25 +20,24 @@ import java.util.concurrent.TimeUnit;
* @see SensorStats * @see SensorStats
*/ */
public class ClosedLoopControlQualityMetric { public class ClosedLoopControlQualityMetric {
private static final int BUFFER_SIZE = 100;
private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
private final ValueSource target; private final ValueSource target;
private final ValueSource result; private final ValueSource result;
private final Sensor destination; private final Sensor destination;
private int maxSize = 100;
/** /**
* Last maxSize number of error measurements * Buffer of recent error measurements
* GuardedBy(this) * GuardedBy(this)
*/ */
private LinkedList<Double> errors = new LinkedList<>(); private DataBuffer errors = new CyclicBuffer(BUFFER_SIZE);
/** /**
* @param target what value are we trying to achieve * @param target what value are we trying to achieve
* @param result what value do we actually have * @param result what value do we actually have
*/ */
public ClosedLoopControlQualityMetric(ValueSource target, ValueSource result, Sensor destination) { public ClosedLoopControlQualityMetric(ValueSource target, ValueSource result, Sensor destination) {
this.target = target; this.target = target;
this.result = result; this.result = result;
this.destination = destination; this.destination = destination;
@ -56,16 +56,10 @@ public class ClosedLoopControlQualityMetric {
} }
private synchronized double getStandardDeviation() { private synchronized double getStandardDeviation() {
float sum = 0; return DataBuffer.getStandardDeviation(errors.getValues());
for (Double error : errors) {
sum += error * error;
}
return Math.sqrt(sum / errors.size());
} }
private synchronized void rememberCurrentError(double error) { private synchronized void rememberCurrentError(double error) {
if (errors.size() == maxSize)
errors.removeFirst();
errors.add(error); errors.add(error);
} }
} }