Skip to content

Instantly share code, notes, and snippets.

@danielfdsilva
Created October 4, 2013 17:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielfdsilva/6829870 to your computer and use it in GitHub Desktop.
Save danielfdsilva/6829870 to your computer and use it in GitHub Desktop.
Clone the dronestream repo (https://github.com/bkw/node-dronestream) and replace the code inside example/express/app.js
var express = require('express')
, routes = require('./routes')
, app = express()
, path = require('path')
, server = require("http").createServer(app)
;
app.configure(function () {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade', { pretty: true });
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function () {
app.use(express.errorHandler());
app.locals.pretty = true;
});
app.get('/', routes.index);
/*
* Important:
*
* pass in the server object to listen, not the express app
* call 'listen' on the server, not the express app
*/
// should be require("dronestream").listen(server);
require("../../index").listen(server);
var arDrone = require('ar-drone');
var client = arDrone.createClient();
var flying = false;
var stdin = process.stdin;
// without this, we would only get streams once enter is pressed
stdin.setRawMode( true );
// resume stdin in the parent process (node app won't quit all by itself
// unless an error or process.exit() happens)
stdin.resume();
// i don't want binary, do you?
stdin.setEncoding( 'utf8' );
setInterval(function() {
if (new Date().getTime() - last_press_time > 100 && !doing) {
pressing = false;
client.stop();
}
}, 50);
var doing = false;
var pressing = false;
var last_press_time = new Date().getTime();
// on any data into stdin
stdin.on( 'data', function( key ){
last_press_time = new Date().getTime();
pressing = true;
// ctrl-c ( end of text )
if ( key === '\u0003' ) {
k();
}
else if ( key === 't') {
if (!flying) {
console.log("**takeoff**");
client.takeoff();
flying = true;
}
}
else if ( key === 'w') {
client.front(0.5);
}
else if ( key === 's') {
client.back(0.5);
}
else if ( key === 'a') {
client.left(0.5);
}
else if ( key === 'd') {
client.right(0.5);
}
else if ( key === 'q') {
client.counterClockwise(0.5);
}
else if ( key === 'e') {
client.clockwise(0.5);
}
else if ( key === 'o') {
client.up(0.5);
}
else if ( key === 'p') {
client.down(0.5);
}
else if ( key === 'j') {
doing = true;
// mission parameters
var wide = 0.25;
var speed = .13;
var cycles = 1;
console.log ("Wide:", wide);
console.log ("Speed:", speed);
console.log ("Cycles:", cycles);
// flight control parameters
var igniTime = 450 / speed;
var rotationSpeed = speed / wide;
var flightTime = cycles * wide * 4600 / speed;
console.log ("igniTime:", igniTime);
console.log ("rotationSpeed:", rotationSpeed);
console.log ("flightTime:", flightTime);
client
.after(100, function() {
this.left(speed);
})
.after(igniTime, function() {
this.clockwise(rotationSpeed);
})
.after(flightTime, function() {
this.stop();
this.land(function () {
doing = false;
});
});
}
// write the key to stdout all normal like
//process.stdout.write( key );
});
// Land on ctrl-c
var exiting = false;
var k = function() {
if (exiting) {
process.exit(0);
} else {
console.log('Got SIGINT. Landing, press Control-C again to force exit.');
exiting = true;
client.stop();
client.land(function() {
exiting = false;
flying = false;
//process.exit(0);
});
}
}
server.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment