deepthink
deepthink

Reputation: 1

node-sense-hat RTIMULib2 function getData() goes blank when scope changes

I am using the node-sense-hat package for Node.js to read sensor data from a sense hat on a Raspberry Pi 2 and save to a file.

I got it working for a single read or looping infinitely, but I found that I needed to add a delay between reads to give the sense hat time to make a reliable measurement.

When I tried to do that, I got a weird error that I wasn't able to find a solution for online.

My questions are:

  1. How do I read data from the Sense Hat at a regular interval? (probably around 20ms once the script is debugged)
  2. What is going on with RTIMULib2? I have never seen such behavior before. Is there some kind of live binding that goes stale when the IMU object is passed to a different scope? Is the process exiting somehow, such that IMU no longer exists?

Note that I am using nodejs v14.21.3, since node-sense-hat doesn't work with modern node (v19.8.1).

node --version
v14.21.3

My first attempt to add a delay was to use the sleep package, but it caused the entire node process to sleep, such that it never got around to executing the callback in the following code. This is part of a larger file, but the rest of the code processes the data and writes it and should be irrelevant to this post:

const libsensehat = require("node-sense-hat");
const libsleep    = require("sleep");
var sleep         = libsleep.sleep;
var IMU = new libsensehat.Imu.IMU;

function sensor_read_callback(err, data)
{
...process data...
}

while(true)
{
  IMU.getData(sensor_read_callback);
  sleep(1000);  //time in ms
}

This caused no data to be written to the file because Node was sleeping when it would have otherwise run the callback.

The next thing I tried was setInterval(). I changed my code to the following:

const libsensehat = require("node-sense-hat");
var IMU = new libsensehat.Imu.IMU;
var sensor_read_interval_handle;

function sensor_read_callback(err, data)
{
...process data...
}

sensor_read_interval_handle = setInterval(IMU.getData, 1000, sensor_read_callback);  //time in ms
}

This causes the following error in nodejs:

internal/validators.js:223
    throw new ERR_INVALID_CALLBACK(callback);
    ^

TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received undefined
    at setInterval (timers.js:210:3)
    at Object.<anonymous> (/root/uwrf-rocket-club-flight-recorder/sense-hat.js:92:31)
    at Module._compile (internal/modules/cjs/loader.js:1114:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1143:10)
    at Module.load (internal/modules/cjs/loader.js:979:32)
    at Function.Module._load (internal/modules/cjs/loader.js:819:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)
    at internal/main/run_main_module.js:17:47 {
  code: 'ERR_INVALID_CALLBACK'
}

I tried replacing IMU.getData with console.log, such that the code that ran was:

const libsensehat = require("node-sense-hat");
var IMU = new libsensehat.Imu.IMU;
var sensor_read_interval_handle;

function sensor_read_callback(err, data)
{
...process data...
}

sensor_read_interval_handle = setInterval(console.log, 1000, "stuff");  //time in ms
}

This worked as expected. It printed "stuff" to console once per second

This showed me that the issue was with RTIMULib2 in node-sense-hat. It seems to turn into a blank object when it is run in setInterval(). I don't understand why, and it looks like RTIMULib2 is a dead project now with no bug tracking available; so I am trying to make this work with what I have.

UPDATE:

I figured it out! The problem was that RTIMULib2's function getData() wants to be in the scope of its parent object so it can access it. The code that ended up working for me was:

const libsensehat = require("node-sense-hat");
var IMU = new libsensehat.Imu.IMU;
var sensor_read_interval_handle;

function sensor_read_callback(err, data)
{
...process data...
}

sensor_read_interval_handle = setInterval(getData.bind(IMU), 1000, sensor_read_callback);  //time in ms
}

Upvotes: 0

Views: 40

Answers (0)

Related Questions