Created
September 13, 2013 15:13
-
-
Save zappan/6552009 to your computer and use it in GitHub Desktop.
This file contains 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
var express = require('express') | |
, ejs = require('ejs') | |
, arDrone = require("ar-drone") | |
, control = arDrone.createUdpControl() | |
// , pngStream = arDrone.createClient().getPngStream() | |
, TcpVideoStream = arDrone.Client.TcpVideoStream | |
, PngEncoder = arDrone.Client.PngEncoder | |
, start = Date.now() | |
, Drone | |
, drone | |
, ref = {} | |
, pcmd = {} | |
, app = express() | |
, port | |
, io | |
, __bind | |
; | |
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; | |
app.configure(function() { | |
app.use(express.bodyParser()); | |
app.set('dirname', __dirname); | |
app.use(app.router); | |
app.use(express["static"](__dirname + "/public/")); | |
app.use(express.errorHandler({ | |
dumpExceptions: true, | |
showStack: true | |
})); | |
return app.set('views', __dirname + "/views/"); | |
}); | |
app.get("/", function(req, res) { | |
return res.render('index.ejs'); | |
}); | |
port = process.env.PORT || 8080; | |
app.listen(port); | |
console.log("Listening on Port '" + port + "'"); | |
Drone = (function() { | |
var videoStream | |
, pngEncoder | |
, callback | |
; | |
function Drone(speed) { | |
this.decreaseSpeed = __bind(this.decreaseSpeed, this); | |
this.increaseSpeed = __bind(this.increaseSpeed, this); | |
this.commands = __bind(this.commands, this); | |
this.speed = speed; | |
this.accel = 0.01; | |
this.takingPhotos = false; | |
} | |
Drone.prototype.takeoff = function() { | |
console.log("Takeoff ..."); | |
ref.emergency = false; | |
return ref.fly = true; | |
if (!this.takingPhotos) { | |
this.takingPhotos = true; | |
setTimeout(function() { | |
drone.takePhoto() | |
}, 1 *1000); | |
} | |
}; | |
Drone.prototype.land = function() { | |
console.log("Landing ..."); | |
ref.fly = false; | |
return pcmd = {}; | |
}; | |
Drone.prototype.stop = function() { | |
return pcmd = {}; | |
}; | |
Drone.prototype.commands = function(names) { | |
var name, _i, _len; | |
pcmd = {}; | |
for (_i = 0, _len = names.length; _i < _len; _i++) { | |
name = names[_i]; | |
pcmd[name] = this.speed; | |
} | |
return console.log('PCMD: ', pcmd); | |
}; | |
Drone.prototype.increaseSpeed = function() { | |
this.speed += this.accel; | |
return console.log(this.speed); | |
}; | |
Drone.prototype.decreaseSpeed = function() { | |
this.speed -= this.accel; | |
return console.log(this.speed); | |
}; | |
Drone.prototype.disableEmergency = function() { | |
this.disablEemergency = false; | |
}; | |
Drone.prototype.takePhoto = function() { | |
if (!videoStream && !pngEncoder) { | |
console.log("initializing video stream and png encoder") | |
videoStream = videoStream || new TcpVideoStream({}); | |
pngEncoder = pngEncoder || new PngEncoder({}); | |
callback = function(err) { | |
if (err !== null) { | |
console.log('TcpVideoStream error: %s', err.message); | |
console.log(err); | |
setTimeout(function () { | |
console.log('Attempting to reconnect to TcpVideoStream...'); | |
videoStream.connect(callback); | |
}, 3 *1000); | |
} | |
}; | |
videoStream.connect(callback); | |
videoStream.on('error', callback); | |
videoStream.on('data', function(data) { | |
console.log("data received from video stream - fwd to png Encoder") | |
pngEncoder.write(data); | |
}); | |
} | |
}; | |
return Drone; | |
})(); | |
setInterval((function() { | |
control.ref(ref); | |
control.pcmd(pcmd); | |
return control.flush(); | |
}), 30); | |
drone = new Drone(0.5); | |
drone.speed = 0.2; | |
drone.disableEmergency(); | |
console.log(drone); | |
// setTimeout(function() { | |
// drone.takePhoto() | |
// }, 3 *1000); | |
// setTimeout(function() { | |
// setInterval(drone.takePhoto, 1.5 *1000); | |
// }, 3 *1000); | |
io = require("socket.io").listen(8081); | |
io.sockets.on("connection", function(socket) { | |
socket.on("takeoff", drone.takeoff); | |
socket.on("land", drone.land); | |
socket.on("stop", drone.stop); | |
socket.on("command", drone.commands); | |
socket.on("increaseSpeed", drone.increaseSpeed); | |
socket.on("disableEmergency", drone.disableEmergency); | |
// socket.on("takePhoto", drone.takePhoto); | |
// socket.on("takePhoto", function() { console.log("take photo") }); | |
return socket.on("decreaseSpeed", drone.decreaseSpeed); | |
}); |
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> | |
<title>Controls</title> | |
<style type="text/css" media="screen"> | |
#controls{ | |
position:absolute; | |
top:0; | |
right:0; | |
padding:10px; | |
} | |
</style> | |
</head> | |
<body class="deck-container"> | |
<div id='log'> | |
</div> | |
<div id='controls'> | |
</div> | |
<script src="jquery-1.7.2.min.js"></script> | |
<script src="keyboard.js"></script> | |
<script src="http://localhost:8081/socket.io/socket.io.js"></script> | |
<script> | |
var socket = io.connect('http://localhost:8081'); | |
socket.on('news', function (data) { | |
console.log(data); | |
socket.emit('my other event', { my: '2' }); | |
}); | |
var timer | |
var log | |
var mapping = { | |
'space': 'takeoff', | |
'x' : 'land', | |
'w' : 'front', | |
's' : 'back', | |
'a' : 'left', | |
'd' : 'right', | |
'j' : 'counterClockwise', | |
'l' : 'clockwise', | |
'i' : 'up', | |
'k' : 'down', | |
'u' : 'disableEmergency', | |
// 't' : 'takePhoto', | |
'dash': 'decreaseSpeed', | |
'equal': 'increaseSpeed', | |
} | |
$(function(){ | |
timer = setInterval(tick, 30); | |
log = $('#log') | |
for (var k in mapping) { | |
if (mapping.hasOwnProperty(k)) { | |
$('#controls').append(k + ' => ' + mapping[k] + '<br/>'); | |
} | |
} | |
}); | |
var takePhoto = function() { | |
socket.emit('takePhoto'); | |
}; | |
var tick = function(){ | |
keys = KeyboardJS.activeKeys() | |
log.text(keys) | |
if(keys.length > 0) { | |
commands = [] | |
for (var i = 0; i < keys.length; i++) { | |
key = keys[i] | |
command = mapping[key] | |
if(command != undefined){ | |
commands.push(command) | |
} | |
} | |
if(commands.length > 0){ | |
log.text(commands) | |
if(commands[0] === 'takeoff'){ | |
socket.emit('takeoff'); | |
} else if(commands[0] === 'land'){ | |
socket.emit('land'); | |
} else if(commands[0] === 'increaseSpeed'){ | |
socket.emit('increaseSpeed'); | |
} else if(commands[0] === 'decreaseSpeed'){ | |
socket.emit('decreaseSpeed'); | |
} else if(commands[0] === 'takePhoto'){ | |
socket.emit('takePhoto'); | |
} else { | |
socket.emit('command', commands); | |
} | |
} else{ | |
socket.emit('stop'); | |
log.text('') | |
} | |
} else{ | |
log.text('') | |
socket.emit('stop'); | |
} | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment