Odd values returned by map(ADD, String)

miqmiq
edited May 2017 in Android
Hi,

I have a problem with results of map(Function2.ADD) while using it with named data route on Metawear C. Basically I would like to add absolute values of x and y retrieved from the acceloremeter. Retrieving, splitting and calculating absolute values work OK, but when I feed these values to the ADD function using feedback mechanism I am getting odd values (see logcat below):

I/MainActivity: x=0.096191406
I/MainActivity: y=0.026367188
I/MainActivity: x+y=5648.1226 [class java.lang.Float]

I/MainActivity: x=0.119628906
I/MainActivity: y=0.036132812
I/MainActivity: x+y=5648.156 [class java.lang.Float]

It looks like there is something wrong happening with type casting at some point. At the same time when I change ADD to SUBTRACT the returned values (x-y) are correct. Please advise what am I doing wrong (the code below)? Thanks!

source.split().index(0).map(Function1.ABS_VALUE).name("xAxisAbs").stream(new Subscriber() {
@Override
public void apply(Data data, Object... env) {
Log.i("MainActivity", "x=" + data.value(Float.class).toString());
}
});
source.split().index(1).map(Function1.ABS_VALUE).name("yAxisAbs").stream(new Subscriber() {
@Override
public void apply(Data data, Object... env) {
Log.i("MainActivity", "y=" + data.value(Float.class).toString());
}
});
source.split().index(0).map(Function1.ABS_VALUE).map(Function2.ADD, "yAxisAbs").stream(new Subscriber() {
@Override
public void apply(Data data, Object... env) {
Log.i("MainActivity", "x+y=" + data.value(Float.class).toString() + " " + Arrays.toString(data.types()));
}
});

Comments

  • edited May 2017
    Yeah, this is a bug with how the API sets up the feedback loops.  For now, you can work around this by having your Subscriber only scale the first 2 bytes of the Data object.

    public void apply(Data data, Object... env) {
        ByteBuffer buffer = ByteBuffer.wrap(data.bytes());
        float value = buffer.getShort(0) / data.scale();
    }
    By the way, the source variable only needs to be used once in the configure function.  You can express your route as follows:

    source.split().index(0).map(Function1.ABS_VALUE).name("x-abs")
            .index(1).map(Function1.ABS_VALUE).map(Function2.ADD, "x-abs");
  • I can see that the bug was fixed in the 3.0.32 release. After upgrade I removed the workaround and all works perfectly now! Thanks for help! 
This discussion has been closed.