diff --git a/app/build.gradle b/app/build.gradle index ad9428f..3156c79 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -9,8 +9,8 @@ android { applicationId "com.ericbt.ebtcompass" minSdkVersion 21 targetSdkVersion 33 - versionCode 43 - versionName "1.43" + versionCode 44 + versionName "1.44" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" diff --git a/app/src/androidTest/java/com/ericbt/ebtcompass/AngleTests.java b/app/src/androidTest/java/com/ericbt/ebtcompass/AngleTests.java index d54bdfd..1379216 100644 --- a/app/src/androidTest/java/com/ericbt/ebtcompass/AngleTests.java +++ b/app/src/androidTest/java/com/ericbt/ebtcompass/AngleTests.java @@ -31,6 +31,7 @@ @RunWith(AndroidJUnit4.class) public class AngleTests { +/* @Test public void toDMS() { // 37°22'42.540 @@ -40,4 +41,5 @@ public void toDMS() { assertEquals("37°22'42.540\"", result); } -} \ No newline at end of file +*/ +} diff --git a/app/src/androidTest/java/com/ericbt/ebtcompass/DataSmootherTests.java b/app/src/androidTest/java/com/ericbt/ebtcompass/DataSmootherTests.java new file mode 100644 index 0000000..d317ef4 --- /dev/null +++ b/app/src/androidTest/java/com/ericbt/ebtcompass/DataSmootherTests.java @@ -0,0 +1,77 @@ +/* + EBT Compass + (C) Copyright 2022, Eric Bergman-Terrell + + This file is part of EBT Compass. + + EBT Compass is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + EBT Compass is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with EBT Compass. If not, see . +*/ + +package com.ericbt.ebtcompass; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import com.ericbt.ebtcompass.utils.DataSmoother; + +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) +public class DataSmootherTests { + @Test + public void singleValue() { + final DataSmoother dataSmoother = new DataSmoother(10); + + final Float value = 234.123f; + + Float average = dataSmoother.add(value); + + assertEquals(average, value); + } + + @Test + public void twoValues() { + final DataSmoother dataSmoother = new DataSmoother(2); + + final Float value1 = 1.0f; + final Float value2 = 2.0f; + + dataSmoother.add(value1); + Float average = dataSmoother.add(value2); + + assertTrue(average == (value1 + value2) / 2.0f); + } + + @Test + public void fourValues() { + final DataSmoother dataSmoother = new DataSmoother(2); + + final Float value1 = 1234.23452f; + final Float value2 = 221234.12f; + final Float value3 = 343.2342342f; + final Float value4 = 41234.567567f; + + dataSmoother.add(value1); + dataSmoother.add(value2); + dataSmoother.add(value3); + + final Float average = dataSmoother.add(value4); + + assertTrue(average == (value3 + value4) / 2.0f); + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/ericbt/ebtcompass/activities/CompassActivity.java b/app/src/main/java/com/ericbt/ebtcompass/activities/CompassActivity.java index 207855a..d452346 100644 --- a/app/src/main/java/com/ericbt/ebtcompass/activities/CompassActivity.java +++ b/app/src/main/java/com/ericbt/ebtcompass/activities/CompassActivity.java @@ -50,6 +50,7 @@ import com.ericbt.ebtcompass.services.CompassService; import com.ericbt.ebtcompass.services.GPSService; import com.ericbt.ebtcompass.ui.CompassRose; +import com.ericbt.ebtcompass.utils.DataSmoother; import com.ericbt.ebtcompass.utils.GoogleMapsUtils; import com.ericbt.ebtcompass.utils.LocaleUtils; import com.ericbt.ebtcompass.utils.MathUtils; @@ -81,6 +82,20 @@ public abstract class CompassActivity extends CustomActivity { private String accelerometerAccuracyText, magnetometerAccuracyText; + private static int DATA_SMOOTHER_VALUES = 25; + + private DataSmoother[] accelerometerReadingsDataSmoother = { + new DataSmoother(DATA_SMOOTHER_VALUES), + new DataSmoother(DATA_SMOOTHER_VALUES), + new DataSmoother(DATA_SMOOTHER_VALUES) + }; + + private DataSmoother[] magnetometerReadingsDataSmoother = { + new DataSmoother(DATA_SMOOTHER_VALUES), + new DataSmoother(DATA_SMOOTHER_VALUES), + new DataSmoother(DATA_SMOOTHER_VALUES) + }; + final protected String[] permissions = new String[] { Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION @@ -411,11 +426,19 @@ protected double updateOrientationAngles(float[] accelerometerReading, float[] m float correctedAzimuth = 0.0f; if (accelerometerReading != null) { - this.accelerometerReading = accelerometerReading; + this.accelerometerReading = new float[3]; + + for (int i = 0; i < this.accelerometerReading.length; i++) { + this.accelerometerReading[i] = accelerometerReadingsDataSmoother[i].add(accelerometerReading[i]); + } } if (magnetometerReading != null) { - this.magnetometerReading = magnetometerReading; + this.magnetometerReading = new float[3]; + + for (int i = 0; i < this.magnetometerReading.length; i++) { + this.magnetometerReading[i] = magnetometerReadingsDataSmoother[i].add(magnetometerReading[i]); + } } if (this.accelerometerReading != null && this.magnetometerReading != null) { diff --git a/app/src/main/java/com/ericbt/ebtcompass/utils/DataSmoother.java b/app/src/main/java/com/ericbt/ebtcompass/utils/DataSmoother.java new file mode 100644 index 0000000..704e0cc --- /dev/null +++ b/app/src/main/java/com/ericbt/ebtcompass/utils/DataSmoother.java @@ -0,0 +1,58 @@ +/* + EBT Compass + (C) Copyright 2022, Eric Bergman-Terrell + + This file is part of EBT Compass. + + EBT Compass is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + EBT Compass is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with EBT Compass. If not, see . +*/ + +package com.ericbt.ebtcompass.utils; + +public class DataSmoother { + private Float[] data; + private int index = 0; + + public DataSmoother(int maxValues) { + data = new Float[maxValues]; + + for (int i = 0; i < data.length; i++) { + data[i] = Float.NaN; + } + } + + public float add(float value) { + data[index] = value; + + index = (index + 1) % data.length; + + return average(); + } + + private float average() { + int n = 0; + float sum = 0.0f; + + for (int i = 0; i < data.length; i++) { + if (!data[i].isNaN()) { + n++; + sum += data[i]; + } else { + break; + } + } + + return sum / (float) n; + } +}