rwaldron / tessel-io

A Tessel 2 IO Plugin for Johnny-Five JavaScript Robotics programs
http://johnny-five.io
50 stars 11 forks source link

Tessel freezing when reading sensor data #16

Open dtex opened 7 years ago

dtex commented 7 years ago

If there is more than one Sensor instance, the Tessel 2 console output will freeze after a few seconds.

Tessel environment versions:

INFO t2-cli: 0.1.4
INFO t2-firmware: 0.0.16
INFO Node.js: 4.5.0

One sensor seems to work fine:

const five = require("johnny-five");
const tessel = require("tessel-io");

var board = new five.Board({ io: new tessel() });

board.on("ready", () => {
  var x = new five.Sensor({pin: "b2"})
  x.on("change", function() {
    console.log(x.value);
  }); 
});

But two sensors will freeze up after a few seconds:

const five = require("johnny-five");
const tessel = require("tessel-io");

var board = new five.Board({ io: new tessel() });

board.on("ready", () => {
  var x = new five.Sensor({pin: "b2"})
  var y = new five.Sensor({pin: "b4"})
  x.on("change", function() {
    console.log(x.value);
  }); 
});

Note that I'm not doing anything with that second sensor. The fact that it exists seems to matter.

Notes:

This code seems to work fine which is what leads me to believe the problem is in tessel-io:

var tessel = require('tessel');

var x = tessel.port.B.pin[2];
var y = tessel.port.B.pin[4];

const foo = function() {
  x.analogRead((error, xVal) => {
    y.analogRead((error, yVal) => {
      console.log(xVal, yVal);
    })  
  });
  setTimeout( function() { foo(); }, 10);
}

foo();
dtex commented 7 years ago

Updated

rwaldron commented 7 years ago

I can reproduce with:

"use strict";

const Tessel = require("../lib/");
const board = new Tessel();

board.on("ready", () => {
  const pins = ["A4","A7","B2","B4","B7"];
  const values = pins.reduce((accum, pin) => (accum[pin] = 0, accum), {});

  pins.forEach(p => {
    board.pinMode(p, board.MODES.ANALOG);
    board.analogRead(p, value => values[p] = value);
  });

  // 50Hz
  setInterval(() => {
    process.stdout.write(`\r ${Date.now()}: ${pins.map(p => padStart(values[p], 4, 0)).join("")}`);
  }, 1000 / 50);
});

function padStart(v, l, c) {
  v = String(v);
  return Array(l - v.length + 1).join(c || " ") + v;
}
rwaldron commented 7 years ago

I will have a branch for you to try later today

rwaldron commented 7 years ago

Here's an updated test program that I've been running:

"use strict";
const Tessel = require("../lib/");
const board = new Tessel();

board.on("ready", () => {

  console.log(`Started At: ${Date.now()}`);
  const pins = ["A4","A7","B2","B3","B4","B5","B6","B7"];
  const values = pins.reduce((accum, pin) => (accum[pin] = 0, accum), {});

  pins.forEach(p => {
    board.pinMode(p, board.MODES.ANALOG);
    board.analogRead(p, value => values[p] = value);
  });

  // 50Hz
  setInterval(() => {
    process.stdout.write(`\r ${Date.now()}: ${pins.map(p => padStart(values[p], 4, 0)).join("")}`);
  }, 1000 / 50);
});

function padStart(v, l, c) {
  v = String(v);
  return Array(l - v.length + 1).join(c || " ") + v;
}

This has been going steadily for over 10 minutes. I did a screen recording for about a minute of that, which shows me adjusting a potentiometer on A4: https://www.youtube.com/watch?v=3f_xcWk3DPg

rwaldron commented 7 years ago

I just stopped the program after it passed 20 minutes. I'm going to add some digital writing into the process and see what happens.

rwaldron commented 7 years ago

@dtex if you would...

npm install rwaldron/tessel-io#16
rwaldron commented 7 years ago

Here's the version that writes...

"use strict";

const Tessel = require("../lib/");
const board = new Tessel();

board.on("ready", () => {
  console.log(`Started At: ${Date.now()}`);
  const pins = ["A4","A7","B2","B3","B4","B5","B6","B7"];
  const values = pins.reduce((accum, pin) => (accum[pin] = 0, accum), {});

  pins.forEach(p => {
    board.pinMode(p, board.MODES.ANALOG);
    board.analogRead(p, value => values[p] = value);
  });

  const leds = ["L0", "L1", "L2", "L3"];
  let state = 1;

  leds.forEach(led => board.digitalWrite(led, state));

  // 50Hz
  setInterval(() => {
    process.stdout.write(`\r ${Date.now()}: ${pins.map(p => padStart(values[p], 4, 0)).join("")}`);
    state ^= 1;
    leds.forEach(led => board.digitalWrite(led, state));
  }, 1000 / 50);
});

function padStart(v, l, c) {
  v = String(v);
  return Array(l - v.length + 1).join(c || " ") + v;
}
dtex commented 7 years ago

I missed your request. Will test tonight.

dtex commented 7 years ago

Same result. No more freezing.

rwaldron commented 7 years ago

That's great! I will get this merged and released asap