openbci-archive/OpenBCI_NodeJS_Ganglion

'accelerometer' events will be never fired

Closed this issue · 3 comments

Hi, I'm really enjoying using Ganglion and this Node.js SDK. really great works!

now i'm trying to fetch x-y-z axis accelerometer's data from the board, like ganglion.on('accelerometer',(data) => {...} but the Ganglion never fire the 'accelerometer' events and nothing I can get.

I tried to dig into the SDK's source code, and I found that the event is fired on the below code block.

// openBCIGanglion.js - line 842
Ganglion.prototype._processCompressedData = function (data) {
  // Save the packet counter
  this._packetCounter = parseInt(data[0]);
  // Decompress the buffer into array
  if (this._packetCounter <= k.OBCIGanglionByteId18Bit.max) {
    this._decompressSamples(ganglionSample.decompressDeltas18Bit(data.slice(k.OBCIGanglionPacket18Bit.dataStart, k.OBCIGanglionPacket18Bit.dataStop)));
    switch (this._packetCounter % 10) {
      case k.OBCIGanglionAccelAxisX:
        this._accelArray[0] = this.options.sendCounts ? data.readInt8(k.OBCIGanglionPacket18Bit.auxByte - 1) : data.readInt8(k.OBCIGanglionPacket18Bit.auxByte - 1) * k.OBCIGanglionAccelScaleFactor;
        break;
      case k.OBCIGanglionAccelAxisY:
        this._accelArray[1] = this.options.sendCounts ? data.readInt8(k.OBCIGanglionPacket18Bit.auxByte - 1) : data.readInt8(k.OBCIGanglionPacket18Bit.auxByte - 1) * k.OBCIGanglionAccelScaleFactor;
        break;
      case k.OBCIGanglionAccelAxisZ:
        this._accelArray[2] = this.options.sendCounts ? data.readInt8(k.OBCIGanglionPacket18Bit.auxByte - 1) : data.readInt8(k.OBCIGanglionPacket18Bit.auxByte - 1) * k.OBCIGanglionAccelScaleFactor;
        this.emit(k.OBCIEmitterAccelerometer, this._accelArray);
        break;
      default:
        break;
    }

and finally I found that the this._packetCounter value is always more than k.OBCIGanglionByteId18Bit.max, in other words, always data[0] > 100.

here is the result of debugging the this._packetCounter value on my console.
2017-02-25 0 48 05
the value is always fall between 101 - 200 (when it reaches 200, the next value is 101, not 0, and why is it never less than 100 ?

Another app, I mean, OpenBCI GUI (processing app) can take the accelerometer's value correctly, so I assume that the Ganglion board emits correct value.

is this problem already known one? or do I some mistakes?
and where can i find any references about openBCI's event emitting systems?

I'll really appreciate your help, thanks.

@denkigai seems like this may be due to the acceleration function not working properly.

I'm facing a similar issue (Also, commented on the OpenBCI forum)
This is my code:

var nano = require('nanomsg');

var addr = 'tcp://0.0.0.0:5100';
var pub = nano.socket('pub');
pub.bind(addr);

const Ganglion = require('openbci-ganglion').Ganglion;
const ganglion = new Ganglion({verbose: true},
	(error) => {
		if (error) 
		
			console.log("error", error);
		}
		else 
		{
			console.log("no error");
		}
	});

ganglion.on('ganglionFound', 
	(peripheral) => {
		ganglion.connect(peripheral)
			ganglion.once('ready', () => {
				tst = ganglion.accelStart();
				console.log(tst);
				ganglion.streamStart();
				ganglion.on('sample', 
					(sample) => {
						console.log(sample.accelData);
						console.log(sample.sampleNumber);
						strData = JSON.stringify(sample);
						pub.send(strData);
				});
			});
		});

ganglion.searchStart();

And this is my output:

sudo node accelTest.js 
no error
(node:3030) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Please turn blue tooth on.
(node:3030) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Bluetooth powered on
Scan started
Found ganglion!
Device is advertising 'Ganglion-8fcc' service.
Stopping scan
Scan stopped
Discovered 3 service characteristics
Found receiveCharacteristicUUID
Found sendCharacteristicUUID
Promise { <pending> }
Sent stream start to board.
undefined
0
undefined
1
undefined
2
undefined
3
undefined
4
undefined
5
undefined
6
undefined
7
undefined
8
undefined
9
undefined
10
undefined
11
undefined
12
undefined

I also tried the .on('accelerometer') function but the code inside is never executed.
Also, the .accelStart function returns a "pending" promise.

If there's anything else I could help with please let me know!

Hey @denkigai and @KoroNiko thanks for finding this issue! I just opened an issue on OpenBCI_Ganglion_Library and added an example with a temporary patch for this issue please see the working code here.

The problem is from the ganglion overwriting the first char sent. If we delay between sends the first message gets processed.