0

我能够初始化两个板。(感谢@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;

4

2 回答 2

2

我现在有一个工作示例!我从头开始,我的目标是让我的 Arduino Mega 上的 LED 闪烁,并在我的 Arduino Uno 上启动/停止一个电机,带有 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;
于 2015-09-08T18:49:27.450 回答
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))

于 2015-09-09T20:22:24.933 回答