我能够初始化两个板。(感谢@ladislas)。
对我来说,问题是 REPL 正在复制,而我的控制器似乎吓坏了(伺服系统混乱等)。
这些板似乎有冲突,尽管它们是完全不同的电路,所以不知道如何处理代码。
这是我的代码(抱歉其中有额外的噪音)。电机板(带电机罩的 Arduino)非常简单。我只是在使用 REPL 进行测试。Mega板是我的大多数传感器和伺服器作为仅供参考的地方。
// 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;