SensorDataCycling: use Distance.

This commit is contained in:
Dennis Guse
2021-05-13 08:17:17 +02:00
parent 00489a121a
commit c00d769188
5 changed files with 13 additions and 7 deletions
@@ -5,6 +5,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import de.dennisguse.opentracks.content.data.Distance;
import de.dennisguse.opentracks.util.UintUtils; import de.dennisguse.opentracks.util.UintUtils;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@@ -100,7 +101,7 @@ public class SensorDataCyclingTest {
SensorDataCycling.DistanceSpeed current = new SensorDataCycling.DistanceSpeed("sensorAddress", "sensorName", 2, 8016); SensorDataCycling.DistanceSpeed current = new SensorDataCycling.DistanceSpeed("sensorAddress", "sensorName", 2, 8016);
// when // when
current.compute(previous, 2150); current.compute(previous, Distance.ofMM(2150));
// then // then
assertEquals(2.15, current.getValue().distance.toM(), 0.01); assertEquals(2.15, current.getValue().distance.toM(), 0.01);
@@ -114,7 +115,7 @@ public class SensorDataCyclingTest {
SensorDataCycling.DistanceSpeed current = new SensorDataCycling.DistanceSpeed("sensorAddress", "sensorName", 0, 2048); SensorDataCycling.DistanceSpeed current = new SensorDataCycling.DistanceSpeed("sensorAddress", "sensorName", 0, 2048);
// when // when
current.compute(previous, 2000); current.compute(previous, Distance.ofMM(2000));
// then // then
assertEquals(2, current.getValue().distance.toM(), 0.01); assertEquals(2, current.getValue().distance.toM(), 0.01);
@@ -24,6 +24,10 @@ public class Distance {
return of(distance_km * UnitConversions.KM_TO_M); return of(distance_km * UnitConversions.KM_TO_M);
} }
public static Distance ofMM(double distance_mm) {
return of(distance_mm * UnitConversions.MM_TO_M);
}
public static Distance one(boolean metricUnit) { public static Distance one(boolean metricUnit) {
if (metricUnit) { if (metricUnit) {
return Distance.ofKilometer(1); return Distance.ofKilometer(1);
@@ -123,7 +123,7 @@ public final class SensorDataCycling {
return wheelRevolutionsTime; return wheelRevolutionsTime;
} }
public void compute(DistanceSpeed previous, int wheel_circumference_mm) { public void compute(DistanceSpeed previous, Distance wheelCircumference) {
if (hasData() && previous != null && previous.hasData()) { if (hasData() && previous != null && previous.hasData()) {
float timeDiff_ms = UintUtils.diff(wheelRevolutionsTime, previous.wheelRevolutionsTime, UintUtils.UINT16_MAX) / 1024f * UnitConversions.S_TO_MS; float timeDiff_ms = UintUtils.diff(wheelRevolutionsTime, previous.wheelRevolutionsTime, UintUtils.UINT16_MAX) / 1024f * UnitConversions.S_TO_MS;
Duration timeDiff = Duration.ofMillis((long) timeDiff_ms); Duration timeDiff = Duration.ofMillis((long) timeDiff_ms);
@@ -134,7 +134,7 @@ public final class SensorDataCycling {
long wheelDiff = UintUtils.diff(wheelRevolutionsCount, previous.wheelRevolutionsCount, UintUtils.UINT16_MAX); long wheelDiff = UintUtils.diff(wheelRevolutionsCount, previous.wheelRevolutionsCount, UintUtils.UINT16_MAX);
wheelDiff = Math.abs(wheelDiff); //HACK for Garmin Speed 2 as some of those seem to count backwards wheelDiff = Math.abs(wheelDiff); //HACK for Garmin Speed 2 as some of those seem to count backwards
Distance distance = Distance.of(wheelDiff * wheel_circumference_mm * UnitConversions.MM_TO_M); Distance distance = wheelCircumference.multipliedBy(wheelDiff);
Distance distanceOverall = distance; Distance distanceOverall = distance;
if (previous.hasValue()) { if (previous.hasValue()) {
distanceOverall = distance.plus(previous.getValue().distanceOverall); distanceOverall = distance.plus(previous.getValue().distanceOverall);
@@ -25,6 +25,7 @@ import android.util.Log;
import java.time.Duration; import java.time.Duration;
import de.dennisguse.opentracks.R; import de.dennisguse.opentracks.R;
import de.dennisguse.opentracks.content.data.Distance;
import de.dennisguse.opentracks.content.sensor.SensorData; import de.dennisguse.opentracks.content.sensor.SensorData;
import de.dennisguse.opentracks.content.sensor.SensorDataCycling; import de.dennisguse.opentracks.content.sensor.SensorDataCycling;
import de.dennisguse.opentracks.content.sensor.SensorDataSet; import de.dennisguse.opentracks.content.sensor.SensorDataSet;
@@ -57,7 +58,7 @@ public class BluetoothRemoteSensorManager implements BluetoothConnectionManager.
private boolean started = false; private boolean started = false;
private final SharedPreferences sharedPreferences; private final SharedPreferences sharedPreferences;
private int preferenceWheelCircumference; private Distance preferenceWheelCircumference;
private final BluetoothConnectionManager.HeartRate heartRate = new BluetoothConnectionManager.HeartRate(this); private final BluetoothConnectionManager.HeartRate heartRate = new BluetoothConnectionManager.HeartRate(this);
private final BluetoothConnectionManager.CyclingCadence cyclingCadence = new BluetoothConnectionManager.CyclingCadence(this); private final BluetoothConnectionManager.CyclingCadence cyclingCadence = new BluetoothConnectionManager.CyclingCadence(this);
@@ -142,9 +142,9 @@ public class PreferencesUtils {
return getString(sharedPreferences, context, R.string.settings_sensor_bluetooth_cycling_speed_key, getBluetoothSensorAddressNone(context)); return getString(sharedPreferences, context, R.string.settings_sensor_bluetooth_cycling_speed_key, getBluetoothSensorAddressNone(context));
} }
public static int getWheelCircumference(SharedPreferences sharedPreferences, Context context) { public static Distance getWheelCircumference(SharedPreferences sharedPreferences, Context context) {
final int DEFAULT = Integer.parseInt(context.getResources().getString(R.string.settings_sensor_bluetooth_cycling_speed_wheel_circumference_default)); final int DEFAULT = Integer.parseInt(context.getResources().getString(R.string.settings_sensor_bluetooth_cycling_speed_wheel_circumference_default));
return getInt(sharedPreferences, context, R.string.settings_sensor_bluetooth_cycling_speed_wheel_circumference_key, DEFAULT); return Distance.ofMM(getInt(sharedPreferences, context, R.string.settings_sensor_bluetooth_cycling_speed_wheel_circumference_key, DEFAULT));
} }
public static String getBluetoothCyclingPowerSensorAddress(SharedPreferences sharedPreferences, Context context) { public static String getBluetoothCyclingPowerSensorAddress(SharedPreferences sharedPreferences, Context context) {