Created
September 16, 2013 20:16
-
-
Save darach/6585991 to your computer and use it in GitHub Desktop.
NodeConf EU NodeCopter - Use EEP (Embedded Event Processing) and Beam (compute streams) to fly an AR Drone.
Uses a makey makey (www.makeymakey.com) or keyboard as input
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright © 2013 Darach Ennis. All rights reserved. | |
// | |
// Sample EEP/Beam AR drone integration. | |
// Works with keyboard | |
// Works with a makey makey - http://www.makeymakey.com/ | |
// Coded during NodeConf EU but not demonstrated or tested ... | |
// | |
// Enjoy! | |
// | |
var arDrone = require('ar-drone'); | |
var keypress = require('keypress'); | |
var beam = require('beam'); | |
var exec = require('child_process').exec; | |
var _ = require('lodash'); | |
var drone = arDrone.createClient(); | |
var flow = beam.Source(); | |
// signals - key events are compatible with makey makey [optional] | |
var keyBindings = [ 'up', 'down', 'left', 'right', 'w', 'a', 's', 'd', 'space' ]; | |
// filter out noisy fat fingers | |
var filterKeys = beam.Operator.filter(function(x) { | |
return _.contains(keyBindings, x.name); | |
}); | |
// selectors | |
var up = beam.Operator.filter(function(x) { return x.name === "up" }); | |
var down = beam.Operator.filter(function(x) { return x.name === "down" }); | |
var rotatePort = beam.Operator.filter(function(x) { return x.name === "left" }); | |
var rotateStar = beam.Operator.filter(function(x) { return x.name === "right" }); | |
var forward = beam.Operator.filter(function(x) { return x.name === "w" }); | |
var backward = beam.Operator.filter(function(x) { return x.name === "s" }); | |
var port = beam.Operator.filter(function(x) { return x.name === "a" }); | |
var starboard = beam.Operator.filter(function(x) { return x.name === "d" }); | |
var land = beam.Operator.filter(function(x) { return x.name === "space" }); | |
// actuators | |
var flyUp = beam.Sink(); | |
var flyDown = beam.Sink(); | |
var flyRotateClockwise = beam.Sink(); | |
var flyRotateCounterClockwise = beam.Sink(); | |
var flyForward = beam.Sink(); | |
var flyBackward = beam.Sink(); | |
var flyToPort = beam.Sink(); | |
var flyToStarboard = beam.Sink(); | |
var flyToGround = beam.Sink(); | |
// On Mac OS X will invoke 'say' command - unless overridden this will convert text to speach and play | |
var speak = function(what) { | |
console.log(what); | |
exec('say ' + what, function callback(error, stdout, stderr){ /* meh! */ }); | |
}; | |
var say = function(what) { | |
return beam.Operator.transform(function(x) { speak(what); }); | |
}; | |
// algorithm | |
flow.pipe(filterKeys).pipe(up).pipe(flyUp).pipe(say("flying up")); | |
flow.pipe(filterKeys).pipe(down).pipe(flyDown).pipe(say("flying down")); | |
flow.pipe(filterKeys).pipe(rotatePort).pipe(flyRotateClockwise).pipe(say("rotate to port")); | |
flow.pipe(filterKeys).pipe(rotateStar).pipe(flyRotateCounterClockwise).pipe(say("rotate to starboard")); | |
flow.pipe(filterKeys).pipe(forward).pipe(flyForward).pipe(say("flying forwards")); | |
flow.pipe(filterKeys).pipe(backward).pipe(flyBackward).pipe(say("flying backwards")); | |
flow.pipe(filterKeys).pipe(port).pipe(flyToPort).pipe(say("flying to port")); | |
flow.pipe(filterKeys).pipe(starboard).pipe(flyToStarboard).pipe(say("flying to starboard")); | |
flow.pipe(filterKeys).pipe(land).pipe(flyToGround).pipe(say("landing")); | |
// actions | |
flyUp.on('data', function(data) { drone.up(0.3); }); | |
flyDown.on('data', function(data) { drone.down(0.3); }); | |
flyRotateClockwise.on('data', function(data) { drone.clockwise(0.3); }); | |
flyRotateCounterClockwise.on('data', function(data) { drone.counterClockwise(0.3); }); | |
flyForward.on('data', function(data) { drone.front(0.3); }); | |
flyBackward.on('data', function(data) { drone.back(0.3); }); | |
flyToPort.on('data', function(data) { drone.left(0.3); }); | |
flyToStarboard.on('data', function(data) { drone.right(0.3); }); | |
flyToGround.on('data', function(data) { drone.land(); }); | |
// Bind key events to standard input | |
keypress(process.openStdin()); | |
process.stdin.setRawMode(true); | |
process.stdin.resume(); | |
// Pump key events into flow | |
process.stdin.on('keypress', function (ch, key) { | |
flow.push(key); | |
}); | |
// chocks away! | |
drone.takeoff(); | |
speak("Chocks away"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment