In general, there is a method that considers the average value in 2 seconds of time (But this is not so important)
private Map<Long, Double> mapAxisX = new HashMap<>(); private int countAxisXTimes; private double countAxisXSum; @Override public double calculateMiddleVarOfAxisX(double axisX) { long current = System.currentTimeMillis(); countAxisXTimes++; countAxisXSum += axisX; mapAxisX.put(current, axisX); if (mapAxisX.size() == 1) { return axisX; } Iterator iterator = mapAxisX.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<Long, Double> point = (Map.Entry) iterator.next(); long pointTime = point.getKey(); long difference = current - pointTime; if (difference > 2000) { double val = point.getValue(); countAxisXTimes--; countAxisXSum -= val; iterator.remove(); } } double result = countAxisXSum / countAxisXTimes; //Logger.logGeneral(String.valueOf(result) + " size : " + mapAxisX.size()); return result; } I call him like this
axisX = presenter.calculateMiddleVarOfAxisX(event.values[0]); And it works fine, but if I need to calculate the average value for another axis (say axisX ), then I need to duplicate this method completely, as if calling it simultaneously for another variable, the values will be considered with one method for two variables at once
axisX = presenter.calculateMiddleVarOfAxisX(event.values[0]); axisY = presenter.calculateMiddleVarOfAxisX(event.values[1]); and of course it turns out not what you need.
I thought at first that you can pass mapAxisX , countAxisXTimes and countAxisXSum as parameters, but then everything becomes more complicated and I need to return them, in general, it will be crooked ...
I think that you need to create a pool and use Callable return the values, but then it’s also not entirely clear how to make such an implementation, since these methods
axisX = presenter.calculateMiddleVarOfAxisX(event.values[0]); axisY = presenter.calculateMiddleVarOfAxisX(event.values[1]); are called in the callback -e from the sensor, with a frequency of up to 50 times per second ...
In general, tell me what to do, I do not want to duplicate the code ...