Streaming quaternions with JavaScript

My goal is to stream quaternions from multiple boards. I previously had some trouble with getting BT dongles to work with the MetaHub, that was solved when I purchased new dongles.

After I got the dongles working, I tried to stream quaternions using a Python script despite the warning of @Laura that the BT controller for JavaScript was much more stable. She was right - I was able to get my python script to work, but it was hit or miss with finding the boards.

I'm not very comfortable with it, but it was clear that I needed to use JavaScript to do this. The multi_device.js example worked for me for streaming accelerations from 5 boards, but I have not been able to stream quaternions. I'm starting by trying to stream from just one board, using this code:

    var MetaWear = require('metawear');
    var fs = require('fs');
    var ref = require('ref');
    var Enum = require('enum');

    var SensorFusionData = new Enum({
      'CORRECTED_ACC': 0,
      'CORRECTED_GYRO': 1,
      'CORRECTED_MAG': 2,
      'QUATERNION': 3,
      'EULER_ANGLE': 4,
      'GRAVITY_VECTOR': 5,
      'LINEAR_ACC': 6
    }, ref.types.int);
    SensorFusionData.alignment = 4;

    var SensorFusionGyroRange = new Enum({
      '_2000DPS': 0,
      '_1000DPS': 1,
      '_500DPS': 2,
      '_250DPS': 3
    }, ref.types.int);
    SensorFusionGyroRange.alignment = 4;

    var SensorFusionAccRange = new Enum({
      '_2G': 0,
      '_4G': 1,
      '_8G': 2,
      '_16G': 3
    }, ref.types.int);
    SensorFusionAccRange.alignment = 4;

    var SensorFusionMode = new Enum({
      'SLEEP': 0,
      'NDOF': 1,
      'IMU_PLUS': 2,
      'COMPASS': 3,
      'M4G': 4
    }, ref.types.int);
    SensorFusionMode.alignment = 4;

    MetaWear.discoverByAddress('c6:ee:8b:e0:4b:24', function(device) {
        device.connectAndSetUp(function (error) {

                // start LED blinking
                var pattern = new MetaWear.LedPattern();
                MetaWear.mbl_mw_led_load_preset_pattern(pattern.ref(), MetaWear.LedPreset.BLINK);
                MetaWear.mbl_mw_led_write_pattern(device.board, pattern.ref(), MetaWear.LedColor.GREEN);
                MetaWear.mbl_mw_led_play(device.board);

                // configure board for quaternions
                MetaWear.mbl_mw_settings_set_connection_parameters(device.board, 7.5, 7.5, 0, 6000);
                MetaWear.mbl_mw_sensor_fusion_set_mode(device.board, SensorFusionMode.NDOF);
                MetaWear.mbl_mw_sensor_fusion_set_acc_range(device.board, SensorFusionAccRange._8G);
                MetaWear.mbl_mw_sensor_fusion_set_gyro_range(device.board, SensorFusionGyroRange._2000DPS);
                MetaWear.mbl_mw_sensor_fusion_write_config(device.board);

                // subscribe to quaternion signal
                var quatSignal = MetaWear.mbl_mw_sensor_fusion_get_data_signal(device.board, SensorFusionData.QUATERNION);
                MetaWear.mbl_mw_datasignal_subscribe(quatSignal, ref.NULL, MetaWear.FnVoid_VoidP_DataP.toPointer((context, dataPtr) => {
                    var data = dataPtr.deref();
                    var pt = data.parseValue();
                    console.log('qtn',pt.x, pt.y, pt.z, pt.w);
                }));

                // start stream
                MetaWear.mbl_mw_sensor_fusion_enable_data(device.board, SensorFusionData.QUATERNION);
                MetaWear.mbl_mw_sensor_fusion_start(device.board);

                // After 5 seconds we reset the board to clear the LED, when we receive
                // a disconnect notice we know the reset is complete, so exit the program
                setTimeout(function () {
                    device.on('disconnect', function () {
                            process.exit(0);
                    });
                    MetaWear.mbl_mw_sensor_fusion_stop(device.board);
                    // MetaWear.mbl_mw_sensor_fusion_clear_enabled_mask(device.board);
                    MetaWear.mbl_mw_datasignal_unsubscribe(quatSignal);
                    MetaWear.mbl_mw_debug_disconnect(device.board);
                    MetaWear.mbl_mw_debug_reset(device.board);
                }, 5000);
        });
    });

Running this makes the LED blink, but I don't see any quaternion values in the console. I don't know if it is a problem with how I am trying to configure the board or maybe it's just that I am not doing the right thing to display the data in the callback function.

Any help would be much appreciated.

Sign In or Register to comment.