Loghorn / ant-plus

A node module for ANT+
MIT License
138 stars 67 forks source link

Attaching multiple HR sensors #13

Open dburongarcia opened 6 years ago

dburongarcia commented 6 years ago

Hello, I wonder what is the proper way to use multiple sensors at the same time. I'm trying to attach 8 hr sensors and right now I'm getting: " MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 read listeners added. Use emitter.setMaxListeners() to increase limit" even with just two of them, so I think I'm not attaching them well. I have that warning until I increase the limit to 1000, and that doesn't seem right.

stick0.on("startup", function() { console.log("startup stick"); console.log("Max channels:", stick0.maxChannels); // will output 8 hrSensor0.attach(0, 0); hrSensor1.attach(1, 0); ... I'm just attaching the sensors on the stick startup using DeviceID=0 for all of them because I want to be able to attach to any hr sensors, not only the ones I know. I wonder if I could scan the DeviceID before attaching it.

I know I should follow your instructions. So I'd like to know which is the best way to do it:

never attach a sensor before receiveing the startup event never attach a new sensor before receiving the attached or detached event of the previous sensor never detach a sensor before receiving the attached or detached event of the previous sensor

Do I have to attach the sensors recursively then? (I did and that seems to make the limit warning disappear) Should I detach every sensor just after it's attached to reattach it again with its real DeviceID?

The lib gives also an exit error if I ever try to attach o detach a sensor that is already attached o detached. Any way to prevent that beyond making sure of not doing that?

This is the best nodejs ANT+ lib I've found. Thank you for your great work.

ikovac commented 5 years ago

Yeah i have the same problem. It works with only one sensor, but when i attach 2 or more sensors it doesn't work.

Please anyone help. when i attach another sensor after receiving 'attached' event from the first sensor i got ERROR: "already attached".

cryptocyclist commented 4 years ago

A single Ant.HeartRateScanner can handle several heart rate monitors (multiple straps).

For example

const Ant = require('ant-plus');
const stick = new Ant.GarminStick2();

const beatCounters = {};

const sensors = {
    51347 : 'Garmin HRM-RUN',
    16594 : 'Garmin HRM-Dual',
};

const hrScanner = new Ant.HeartRateScanner(stick);
hrScanner.on('hbData', data => {

    // Ignore it if it's another transmission of the same heart beat information (use the counter)
    const counter = beatCounters[data.DeviceID];
    if (typeof(counter) !== 'undefined' && (counter === data.BeatCount)) return;
    beatCounters[data.DeviceID] = data.BeatCount;

    // This is now a different measurement, so process it.  In our case, we're just going to log to console.

    console.log(sensors[data.DeviceID] + ': HR = ' + data.ComputedHeartRate);
});

stick.on('startup', function() {
    console.log('startup');
    hrScanner.scan();
});

if (!stick.open()) {
    console.log('Stick not found!');
}

gives me

$ node multi-hr.js
startup
Garmin HRM-Dual: HR = 61
Garmin HRM-RUN: HR = 60
Garmin HRM-RUN: HR = 61
Garmin HRM-Dual: HR = 62
Garmin HRM-RUN: HR = 63
Garmin HRM-Dual: HR = 63
Garmin HRM-Dual: HR = 64
Garmin HRM-RUN: HR = 65
Garmin HRM-RUN: HR = 68
Garmin HRM-Dual: HR = 66

If you don't know the DeviceIDs in advance, just keep track of when you see new ones. You'll need some way for the user to name a new one, as otherwise it is just too confusing.