【问题标题】:Johnny-Five multiple Arduinos connecting, but not emitting "ready"Johnny-Five 多个 Arduino 连接,但未发出“就绪”
【发布时间】:2015-07-18 21:39:49
【问题描述】:

我在使用 johnny-5(多板)时遇到了一点困难;谁能帮我解释一下?

我连接了 2 个 Arduino,我可以使用“var board = new Five.Board()”单独访问它们。

我可以通过 Cylon.js 成功连接和使用它们。

但是,当我尝试使用“new Five.Boards()”时,它似乎永远不会发出“就绪”事件,因此我可以开始编写我的逻辑。

使用(稍作修改)johnny-5/eg/boards-multi.js

var five = require("../lib/johnny-five.js");
var boards = new five.Boards(["A", "B"]);

// Create 2 board instances with IDs "A" & "B"
boards.on("ready", function() {

  // Both "A" and "B" are initialized
  // (connected and available for communication)

  // |this| is an array-like object containing references
  // to each initialized board.
  this.each(function(board) {

    console.log("READY"); // NOTE: this never executes

    // Initialize an Led instance on pin 13 of
    // each initialized board and strobe it.
    var led = new five.Led({
      pin: 13,
      board: board
    });

    led.blink();
  });
});

我的控制台显示:

1437253899028 Device(s) /dev/ttyACM1,/dev/ttyACM0
1437253899104 Connected /dev/ttyACM1
1437253899121 Device(s) /dev/ttyACM0
1437253899126 Connected /dev/ttyACM0
1437253901860 Board ID:  A
1437253901862 Board ID:  B

...我永远等待它永远不会发出“就绪”...

注意1:我已经在他们两个上重新上传了最新的“StandardFirmata”几次;他们自己工作就好了。

注意 2:我在 3 个不同的系统(一个 ubuntu linux,一个在 Windows 和一个 Raspberry PI 2B)上尝试了完全相同的设置,所有系统都出现相同的问题......

我不确定我是否在这里遗漏了一些愚蠢的东西;但是,无论我尝试什么,johnny-5 都不允许我继续。正如我上面提到的,它似乎与 Cylon 完美配合 - 但是,我宁愿使用 j5,因为我已经有相当多的代码我不想移植到 Cylon 只是为了连接多个Arduino 到我的系统。

任何帮助将不胜感激!

更新 #1:

我离一点更近了,我现在可以处理每个 Arduino 板了。然而;我仍然对如何正确捕捉“就绪”事件感到困惑。

var five = require('johnny-five');
var ports = [
  { id: "A", port: "/dev/ttyACM0" },
  { id: "B", port: "/dev/ttyACM1" }
];

var boards = new five.Boards(ports).on('ready', function() {
  // does nothing?
  console.log("THIS SHOULD TRIGGER");
});

// Waiting 5 seconds for the boards to init, instead of "ready" event.
setTimeout( function() {
  console.log(boards[0].isReady);
  console.log(boards[1].isReady);
}, 5000);

这最终得到以下控制台输出:

1437268012413 Connected /dev/ttyACM0
1437268012427 Connected /dev/ttyACM1
1437268015161 Board ID:  A
1437268015163 Board ID:  B
true
true

....此时,我可以执行以下操作来处理板子(当然是在 setTimeout() 内):

  var led1 = new five.Led( {
    pin: 6,
    board: boards[0]
  });
  led1.on();
  var led2 = new five.Led( {
    pin: 4,
    board: boards[1]
  });
  led2.on();

仍在尝试确定为什么我无法捕捉到难以捉摸的“准备”。

更新 #2:

看来我想通了。它实际上已经准备好了,但是我没有正确使用 API。

工作代码:

new five.Boards(ports).on('ready', function( boards ) {
  console.log( boards );  // ready emits
});

更新 #3:

我想我在库中发现了一个错误。

好像是以下文件:

node_modules/johnny-5/lib/board.js

线路:1109

如果你改变:

if (this.repl) 
to
if (false && this.repl)

它似乎发出“就绪”事件。

【问题讨论】:

    标签: arduino emit johnny-five


    【解决方案1】:

    我在 repl 初始化中发现了一个错误,这将在下一个版本中修复。问题是在Repl.prototype.initialize 中对state.board.info(...) 的调用,其中info 方法由于最近重构板日志记录和日志记录函数定义期间的疏忽而未定义。这个错误出现在 v0.8.85 中,所以尝试npm install johnny-five@0.8.84 来解决这个问题,直到下一个版本。

    【讨论】:

      猜你喜欢
      • 2018-08-15
      • 2016-12-29
      • 2015-04-09
      • 2023-01-28
      • 2020-11-21
      • 2022-12-03
      • 2021-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多