我能够将两块电路板初始化。 (感谢@ladislas)。
对我来说问题是REPL是重复的,我的控制器似乎吓坏了(嗡嗡作响的伺服器等)。
这些电路板似乎有冲突,虽然它们是完全不同的电路,因此不确定如何处理代码。
这是我的代码(抱歉它的额外噪音)。电机板(带电机屏蔽的Arduino)非常简单。我只是用REPL测试。巨型电路板是我的大多数传感器和伺服电机的所在地。
// Combo.js
// This program attempts to use multiple microcontrollers in the same app
// Includes
var five = require('johnny-five');
// Create an emitter object to receive commands from the server
//var emitter = new eventEmitter();
var events = require('events');
var emitter = new events.EventEmitter();
var boardMega = new five.Board({port: "/dev/ttyACM0"});
var boardMotor = new five.Board({port: "/dev/ttyUSB0"});
/* var ports = [
{ id: "mega", port: "/dev/ttyACM0" },
{ id: "motor", port: "/dev/ttyUSB0" }
];
var boards = new five.Boards(ports);
*/
boardMotor.on("ready", function() {
var configs = five.Motor.SHIELD_CONFIGS.ADAFRUIT_V1;
var motor1 = new five.Motor(configs.M1);
var motor2 = new five.Motor(configs.M2);
var motor3 = new five.Motor(configs.M3);
var motor4 = new five.Motor(configs.M4);
// Add devices to REPL (optional)
this.repl.inject({
motor4: motor4 // range: 30 - 100
});
});
// Board Ready
boardMega.on('ready', function(){
// Devices
// Track Road Crossing
var crossingLed1 = new five.Led(22); //crossing light 1
var crossingLed2 = new five.Led(23); //crossing light 2
var crossingServo = new five.Servo({
pin: 6,
rate: 0.05
}); // Crossing arm
var crossingSensor = new five.Sensor.Digital({ //crossing sensor
pin: 53,
freq: 150, // how often to read the sensor in milliseconds
});
// Add devices to REPL (optional)
this.repl.inject({
crossingServo: crossingServo,
crossingLed1: crossingLed1,
crossingLed2: crossingLed2,
});
// Receive the command for the server
emitter.on('command', function(command){
// Check command received and execute actions
if (command === 'crossing'){
crossing();
return;
}
});
// Robot Code
// Train Crossing
crossingSensor.on('change', function(){
crossing();
});
var crossingOn = false; // a state
var crossingDisable; // a timeout
var active = false;
function crossing(){
if(!active){
crossingOn = true;
crossingLed1.blink(500);
// timer for alternating lights
setTimeout(function(){
crossingLed2.blink(500);
});
crossingServo.to(70); // lower arm
// delay for crossing off
crossingDisable = setTimeout(function(){
crossingLed1.stop().off();
crossingLed2.stop().off();
crossingServo.to(150); // raise arm
crossingOn = false;
}, 3000); // milliseconds before disable
}else{
clearTimeout(crossingDisable);
crossingDisable = setTimeout(function(){
crossingLed1.stop().off();
crossingLed2.stop().off();
crossingServo.to(150); // raise arm
crossingOn = false;
}, 3000); // delay for crossing off
}
}
// Next Thing..
});
// API for use in server.js
module.exports = emitter;

答案 0 :(得分:2)
我现在有一个工作的例子!我从头开始,我的目标是使我的Arduino Mega上的LED闪烁,并在我的Arduino Uno w / Adafruit电机屏蔽v1上启动/停止电机。
我终于明白了如何构建多个板,更重要的是根据需要引用它们。这是我的基本应用程序。希望这有助于其他人。
// Combo.js
// This program uses multiple microcontrollers in the same app
// Includes
var five = require('johnny-five');
// Create an emitter object to receive commands from the server
var events = require('events');
var emitter = new events.EventEmitter();
// used for debugging purposes
var util = require('util');
var ports = [
{ id: "mega", port: "/dev/ttyACM0" }, // this[0]
{ id: "motor", port: "/dev/ttyUSB0" } // this[1]
];
new five.Boards(ports).on("ready", function(){
this.each(function(board) {
// Initialize an Led instance on pin 13 of
// each initialized board and strobe it.
// new five.Led({ pin: 13, board: board }).strobe();
});
var led = new five.Led({
pin: 13,
board: this[0]
});
led.blink();
var configs = five.Motor.SHIELD_CONFIGS.ADAFRUIT_V1;
// assign board to arduino with motor shield
configs.M4.board = this[1];
//console.log("configs");
//console.log(util.inspect(configs, false, null));
var motor4 = new five.Motor(configs.M4);
console.log("motor4 starting");
motor4.reverse(100);
setTimeout(function(){
console.log("motor4 braking");
motor4.brake();
}, 1000);
// Add devices to REPL (optional)
this.repl.inject({
motor4: motor4 // range: 30 - 100
});
});
// API for use in server.js
module.exports = emitter;
答案 1 :(得分:0)
five.Boards
类对此非常有用。
您也可以在原始问题中使用您的个人董事会代码,方法是将董事会命名为:
new five.Led({ pin: 13, board: boardMega });
或
configs.M4.board = boardMotor;
请记住,当您键入configs.M4.board = board
时,您正在修改全局配置对象。因此,如果您使用两块电路板,并且都使用其中一块电机护罩,这种方法将无法工作。相反,您希望深度克隆配置对象。
var configs = deepClone(five.Motor.SHIELD_CONFIGS.ADAFRUIT_V1);
configs.M4.board = this[1];
DeepClone是一项留给实施者的练习,但有很多选项(包括LoDash,或JSON.parse(JSON.stringify(obj))
)