Bugfix: cycling sensors report wheel count as UINT32.

Garmin Speed Sensor 2 actually counts forward: it was an UINT16 overflow problem (removed workaround).

Fixes #810.
This commit is contained in:
Dennis Guse
2021-06-10 21:22:40 +02:00
parent 2f0449c420
commit 40188774b1
5 changed files with 32 additions and 14 deletions
-2
View File
@@ -23,8 +23,6 @@ However, often only one value is provided.
* Has updatable firmware that requires an account for garmin.com
* This sensor reports speed data as cadence.
A workaround is in place.
* This sensor sometimes counts backwards (unknown software version).
A workaround is in place.
* Wahoo Speed (Model: WFRPMSPD)
@@ -0,0 +1,23 @@
package de.dennisguse.opentracks.util;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class UintUtilsTest {
@Test
public void diff() {
assertEquals(0, UintUtils.diff(1, 1, UintUtils.UINT16_MAX));
assertEquals(1, UintUtils.diff(2, 1, UintUtils.UINT16_MAX));
assertEquals(3, UintUtils.diff(5, 2, UintUtils.UINT16_MAX));
assertEquals(65534, UintUtils.diff(1, 2, UintUtils.UINT16_MAX));
assertEquals(65530, UintUtils.diff(UintUtils.UINT16_MAX, 5, UintUtils.UINT16_MAX));
}
@Test
public void realData() {
assertEquals(3, UintUtils.diff(381616, 381615, UintUtils.UINT32_MAX));
}
}
@@ -15,8 +15,6 @@ import de.dennisguse.opentracks.util.UnitConversions;
/**
* Provides cadence in rpm and speed in milliseconds from Bluetooth LE Cycling Cadence and Speed sensors.
* <p>
* https://www.bluetooth.org/docman/handlers/downloaddoc.ashx?doc_id=261449
*/
public final class SensorDataCycling {
@@ -96,7 +94,7 @@ public final class SensorDataCycling {
public static class DistanceSpeed extends SensorData<DistanceSpeed.Data> {
private final Integer wheelRevolutionsCount; // UINT16
private final Integer wheelRevolutionsCount; // UINT32
private final Integer wheelRevolutionsTime; // UINT16; 1/1024s
public DistanceSpeed(String sensorAddress) {
@@ -133,8 +131,7 @@ public final class SensorDataCycling {
return;
}
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
long wheelDiff = UintUtils.diff(wheelRevolutionsCount, previous.wheelRevolutionsCount, UintUtils.UINT32_MAX);
Distance distance = wheelCircumference.multipliedBy(wheelDiff);
Distance distanceOverall = distance;
@@ -164,11 +161,11 @@ public final class SensorDataCycling {
if (!(obj instanceof DistanceSpeed)) return false;
DistanceSpeed comp = (DistanceSpeed) obj;
if (hasData() && comp.hasData() == hasData()) {
return getWheelRevolutionsCount() == comp.getWheelRevolutionsCount() && getWheelRevolutionsTime() == comp.getWheelRevolutionsTime();
} else {
if (!(hasData() && comp.hasData())) {
return false;
}
return getWheelRevolutionsCount() == comp.getWheelRevolutionsCount() && getWheelRevolutionsTime() == comp.getWheelRevolutionsTime();
}
public static class Data {
@@ -215,7 +215,7 @@ public class StringUtils {
}
StringBuilder builder = new StringBuilder();
builder.append("[").append(category).append("]");
builder.append(getCategory(category));
if (description != null && description.length() != 0) {
builder.append(" ").append(description);
}
@@ -11,14 +11,14 @@ public class UintUtils {
/**
* Computes a - b for UINT with overflow (b < a).
*
* @return diff or -1 (invalid)
* @return diff
*/
public static long diff(long a, long b, final long UINT_MAX) {
if (a < 0 || b < 0) {
return -1;
throw new RuntimeException("a or b cannot be less than zero.");
}
if (a > UINT_MAX || b > UINT_MAX) {
return -1;
throw new RuntimeException("a or b are outside of the allowed range." + a + " " + b);
}
if (a >= b) {