Fuse Accelerometer, Gyroscope and magnetometer

Hi, I'm trying to fuse Accelerometer, Gyroscope and magnetometer.

I could do it between acc and gyro as explained in the documentation, but the app is crashing when trying to do it between three of them.

String[] buffers = new String[]{"gyro-buffer", "magn-buffer"};

gyroProducer.addRouteAsync(source ->
        source.buffer().name("gyro-buffer")
).onSuccessTask(gyroTaskContinuation -> magnProducer.addRouteAsync(source ->
        source.buffer().name("magn-buffer")
).onSuccessTask(accTaskContinuation -> accProducer.addRouteAsync(source ->
        source.fuse(buffers).stream((Subscriber) (data, objects) -> {
            Data[] values1 = data.value(Data[].class);
            System.out.printf("acc = %s, gyro = %s, magn = %s%n", values1[0].value(Acceleration.class),
                values1[1].value(AngularVelocity.class), values1[2].value(MagneticField.class));
        })
))).continueWith(task -> {
    streamRoute = task.getResult();

    gyro.angularVelocity().start();
    gyro.start();

    accProducer.start();
    accelerometer.start();

    magnetometer.magneticField().start();
    magnetometer.start();

    return null;
});

Is it possible to fuse more than two sensor's data?

I appreciate any help you can give me!

Comments

  • edited September 2019
    • Add error handling to your async tasks
    • Post stack trace of the crash
  • Hi Eric, this is the line where the lib crashes when I execute that code, and this is the stacktrace:

    java.lang.NullPointerException: Attempt to read from field 'com.mbientlab.metawear.impl.DataTypeBase com.mbientlab.metawear.impl.DataProcessorImpl$Processor.state' on a null object reference
        at com.mbientlab.metawear.impl.DataTypeBase.dataProcessorTransform(DataTypeBase.java:351)
        at com.mbientlab.metawear.impl.FloatVectorData.dataProcessorTransform(FloatVectorData.java:57)
        at com.mbientlab.metawear.impl.RouteComponentImpl.fuse(RouteComponentImpl.java:877)
        at com.mbientlab.metawear.app.AccelerometerFragment.lambda$-com_mbientlab_metawear_app_AccelerometerFragment_lambda$5(AccelerometerFragment.java:247)
        at com.mbientlab.metawear.app.-$Lambda$66.$m$0(Unknown Source:4)
        at com.mbientlab.metawear.app.-$Lambda$66.configure(Unknown Source:0)
        at com.mbientlab.metawear.impl.JseMetaWearBoard.createRoute(JseMetaWearBoard.java:1404)
        at com.mbientlab.metawear.impl.JseMetaWearBoard.access$1100(JseMetaWearBoard.java:93)
        at com.mbientlab.metawear.impl.JseMetaWearBoard$1.queueRouteBuilder(JseMetaWearBoard.java:312)
        at com.mbientlab.metawear.impl.AccelerometerBoschImpl$1.addRouteAsync(AccelerometerBoschImpl.java:660)
        at com.mbientlab.metawear.app.AccelerometerFragment.lambda$-com_mbientlab_metawear_app_AccelerometerFragment_lambda$4(AccelerometerFragment.java:246)
        at com.mbientlab.metawear.app.-$Lambda$88.$m$0(Unknown Source:8)
        at com.mbientlab.metawear.app.-$Lambda$88.then(Unknown Source:0)
        at bolts.Task$15.run(Task.java:917)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
        at java.lang.Thread.run(Thread.java:764)
    
Sign In or Register to comment.