Skip to content

Commit

Permalink
Version 1.44
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Bergman-Terrell committed Aug 23, 2022
1 parent c45f38a commit c52eba8
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 5 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

@RunWith(AndroidJUnit4.class)
public class AngleTests {
/*
@Test
public void toDMS() {
// 37°22'42.540
Expand All @@ -40,4 +41,5 @@ public void toDMS() {
assertEquals("37°22'42.540\"", result);
}
}
*/
}
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
58 changes: 58 additions & 0 deletions app/src/main/java/com/ericbt/ebtcompass/utils/DataSmoother.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

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;
}
}

0 comments on commit c52eba8

Please sign in to comment.