Skip to content

Instantly share code, notes, and snippets.

@Geesu
Created January 6, 2015 17:46
Show Gist options
  • Save Geesu/d1eae0b336766ddf8711 to your computer and use it in GitHub Desktop.
Save Geesu/d1eae0b336766ddf8711 to your computer and use it in GitHub Desktop.
Four wheel NodeBot control
var five = require("johnny-five");
var board = new five.Board();
var stdin = process.stdin;
stdin.setRawMode(true);
stdin.resume();
board.on("ready", function () {
var wheels = {
frontLeft: new five.Servo({ pin: 4, type: 'continuous' }),
frontRight: new five.Servo({ pin: 5, type: 'continuous' }),
backLeft: new five.Servo({ pin: 2, type: 'continuous' }),
backRight: new five.Servo({ pin: 3, type: 'continuous' }),
stop: function () {
wheels.frontLeft.center();
wheels.frontRight.center();
wheels.backLeft.center();
wheels.backRight.center();
},
forward: function () {
wheels.frontLeft.cw();
wheels.frontRight.cw();
wheels.backLeft.ccw();
wheels.backRight.cw();
console.log("goForward");
},
pivotLeft: function () {
wheels.frontLeft.ccw();
wheels.backLeft.cw();
wheels.frontRight.cw();
wheels.backRight.cw();
console.log("turnLeft");
},
pivotRight: function () {
wheels.frontLeft.cw();
wheels.backLeft.ccw();
wheels.frontRight.ccw();
wheels.backRight.ccw();
console.log("turnRight");
},
back: function () {
wheels.frontLeft.ccw();
wheels.frontRight.ccw();
wheels.backLeft.cw();
wheels.backRight.ccw();
}
};
wheels.stop();
console.log("Use the cursor keys or ASWD to move your bot. Hit escape or the spacebar to stop.");
stdin.on("keypress", function(chunk, key) {
if (!key) return;
switch (key.name) {
case 'up':
case 'w':
wheels.forward();
break;
case 'down':
case 's':
wheels.back();
break;
case 'left':
case 'a':
wheels.pivotLeft();
break;
case 'right':
case 'd':
wheels.pivotRight();
break;
case 'space':
case 'escape':
wheels.stop();
break;
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment